Hi, am a noob at webpack so please bear with me. I am doing the best I can.
What i am trying to do is to cherrypick a subset of lodash, specifically debounce to load in my application via app.js
The debounce function is available via _.debounce
as _ is a global. e.g. _.debounce()
But the debounce
function is not available on its own e.g. debounce()
Also any kind feedback on my webpack.config.js will be very much appreciated.
Thank you.
app.js
import _ from "../node_modules/lodash";
import debounce from "../node_modules/lodash/debounce";
webpack.config.js
const path = require('path');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { SourceMapDevToolPlugin } = require("webpack");
const PurgecssPlugin = require('purgecss-webpack-plugin');
const globAll = require('glob-all');
// Custom PurgeCSS extractor for Tailwind that allows special characters in
// class names.
// Regex explanation: https://tailwindcss.com/docs/controlling-file-size/#understanding-the-regex
const TailwindExtractor = content => {
return content.match(/[\w-/:]+(?<!:)/g) || [];
};
module.exports = (env, options) => ({
optimization: {
minimizer: [
new TerserPlugin({ cache: true, parallel: true, sourceMap: false }),
new OptimizeCSSAssetsPlugin({}),
new PurgecssPlugin({
paths: globAll.sync([
'../lib/**/templates/**/*.html.eex',
'../lib/**/views/**/*.ex',
'../assets/js/**/*.js',
]),
extractors: [
{
extractor: TailwindExtractor,
extensions: ['html', 'js', 'eex', 'ex'],
},
],
}),
]
},
entry: {
'./app.js': glob.sync('./vendor/**/*.js').concat(['./js/app.js']),
//'./froala_tui_integration.js': './js/froala_tui_integration.js',
// './test.js': './js/test.js',
},
output: {
filename: '[name]',
path: path.resolve(__dirname, '../priv/static/js'),
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.js$/,
enforce: 'pre',
use: ['source-map-loader'],
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader']
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: '../fonts'
}
}]
}
]
},
plugins: [
new MiniCssExtractPlugin({ filename: '../css/app.css' }),
new CopyWebpackPlugin([{ from: 'static/', to: '../' }]),
new SourceMapDevToolPlugin({ filename: "[file].map" }),
]
});