I'm building a vue app and using webpack to bundle it.
This line
var UglifyJS = require("uglify-js");
causes the following:
tools sync?6ded:2
Uncaught Error: Cannot find module '../lib/utils.js'
at Function.webpackEmptyContext [as resolve] (eval at ./node_modules/uglify-js/tools sync recursive (app.js:5165),
at eval (node.js?c35f:17)
at Array.map (
at eval (node.js?c35f:16)
at Object../node_modules/uglify-js/tools/node.js (app.js:5176)
at __webpack_require__ (app.js:724)
at fn (app.js:101)
at eval (util.js?c276:4)
at Module../src/libs/util.js (app.js:6383)
at __webpack_require__ (app.js:724)
and how can fix it
FWIW I think this may be to do with this code which loads modules dynamically, presumably webpack would be picking them up if it weren't for them being in map??
// node.js
exports.FILES = [
"../lib/utils.js",
"../lib/ast.js",
"../lib/parse.js",
"../lib/transform.js",
"../lib/scope.js",
"../lib/output.js",
"../lib/compress.js",
"../lib/sourcemap.js",
"../lib/mozilla-ast.js",
"../lib/propmangle.js",
"../lib/minify.js",
"./exports.js",
].map(function(file) {
return require.resolve(file);
});
Not an issue with this project, please contact & consult with webpack instead.
@homerjam Yeah, I'm guessing this would all work if it just exported an array of require statements...
I feel like this is actually an issue that should be solved here. There are many different packaging and transpilation tools that all struggle with dynamic imports/requires.
In some cases it's necessary to do a dynamic import, but in this case it is not, so it should be relatively easy to fix.
I'm willing to put in a PR, if that helps?
Any progress here? With this issue I can't webpack email-templates
Any news in this issue?
I solved it by adding all the packages that use uglify or any other package that can't parsed by webpack to the externals. See this issue.
@OBrown92 If someone does use next.js I solved it like the below:
const path = require('path');
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});
module.exports = withBundleAnalyzer({
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
config.module.rules.push({
test: path.resolve(__dirname, 'node_modules/uglify-js/tools/node.js'),
loader: 'null-loader',
});
// Important: return the modified config
return config;
},
...
}
Most helpful comment
@homerjam Yeah, I'm guessing this would all work if it just exported an array of require statements...