Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

How do I minify the worker chunk? #92

Closed
nkovacs opened this issue Sep 8, 2017 · 2 comments
Closed

How do I minify the worker chunk? #92

nkovacs opened this issue Sep 8, 2017 · 2 comments

Comments

@nkovacs
Copy link

nkovacs commented Sep 8, 2017

It seems I have to use LoaderOptionsPlugin to set options.worker.plugins (https://github.com/webpack-contrib/worker-loader/blob/v0.8.0/index.js#L42), but when I use that to add new webpack.optimize.UglifyJsPlugin({}) to options.worker.plugins, I get this error:

Module build failed: TypeError: Cannot read property 'replace' of undefined
    at new RequestShortener (node_modules/webpack/lib/RequestShortener.js:11:24)
    at UglifyJsPlugin.apply (node_modules/uglifyjs-webpack-plugin/dist/index.js:40:27)
    at Compiler.apply (node_modules/tapable/lib/Tapable.js:306:16)
    at options.plugins.forEach.plugin (node_modules/worker-loader/index.js:52:54)
    at Array.forEach (native)
    at Object.pitch (node_modules/worker-loader/index.js:52:21)

I'm using webpack 3.5.6

LoaderOptionsPlugin config:

new webpack.LoaderOptionsPlugin({
	options: {
		// context: path.resolve(__dirname, ".."),
		worker:  {
			plugins: [
				new webpack.optimize.UglifyJsPlugin({
					compress: {
						warnings: false,
					},
					sourceMap: false,
				}),
			],
		},
	},
}),

I tried with and without context in LoaderOptionsPlugin, it fails with the same error either way.

@nkovacs
Copy link
Author

nkovacs commented Sep 12, 2017

I was wrong. UglifyJsPlugin is applied to the child compilation, but uglifyjs throws an error, and that error isn't shown: https://github.com/webpack-contrib/uglifyjs-webpack-plugin/blob/v0.4.6/src/index.js#L211
Maybe the errors aren't propagated properly to the parent compilation?

The error is caused by a let. It looks like babel-loader did not transform the code. My loader config looks like this:

{
	test:    /\.js$/,
	loader:  "babel-loader",
	include: [resolve("src"), resolve("test")],
	rules:   [{
		test:    /\.worker\.js$/,
		loader:  "worker-loader",
		include: [resolve("src"), resolve("test")],
		options: {
			name: utils.assetsPath("js/[hash].worker.js"),
		},
	}],
},

@nkovacs
Copy link
Author

nkovacs commented Sep 12, 2017

The order was wrong. Both of these work:

{
	test:    /\.worker\.js$/,
	loader:  "worker-loader",
	include: [resolve("src"), resolve("test")],
	options: {
		name: utils.assetsPath("js/[hash].worker.js"),
	},
},
{
	test:    /\.js$/,
	loader:  "babel-loader",
	include: [resolve("src"), resolve("test")],
},
{
	test:    /\.js$/,
	loader:  "babel-loader",
	include: [resolve("src"), resolve("test")],
	rules:   [{
		test:    /\.worker\.js$/,
		loader:  "worker-loader",
		enforce: "post",
		include: [resolve("src"), resolve("test")],
		options: {
			name: utils.assetsPath("js/[hash].worker.js"),
		},
	}],
},

@nkovacs nkovacs closed this as completed Sep 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant