Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit 24fe22b

Browse files
fix: all nodejs globals avaliable inside minify function (#332)
1 parent 0174605 commit 24fe22b

File tree

5 files changed

+139
-92
lines changed

5 files changed

+139
-92
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ If you use your own `minify` function please read the `minify` section for handl
196196
197197
### `minify`
198198

199+
> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**
200+
199201
**webpack.config.js**
200202
```js
201203
[
@@ -204,11 +206,12 @@ If you use your own `minify` function please read the `minify` section for handl
204206
const extractedComments = [];
205207

206208
// Custom logic for extract comments
207-
208-
const { error, map, code, warnings } = minify(
209-
file,
210-
{ /* Your options for minification */ },
211-
);
209+
210+
const { error, map, code, warnings } = require('uglify-module') // Or require('./path/to/uglify-module')
211+
.minify(
212+
file,
213+
{ /* Your options for minification */ },
214+
);
212215

213216
return { error, map, code, warnings, extractedComments };
214217
}

package-lock.json

Lines changed: 68 additions & 78 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uglify/worker.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ module.exports = (options, callback) => {
55
// 'use strict' => this === undefined (Clean Scope)
66
// Safer for possible security issues, albeit not critical at all here
77
// eslint-disable-next-line no-new-func, no-param-reassign
8-
options = new Function(`'use strict'\nreturn ${options}`)();
8+
options = new Function(
9+
'exports',
10+
'require',
11+
'module',
12+
'__filename',
13+
'__dirname',
14+
`'use strict'\nreturn ${options}`,
15+
)(exports, require, module, __filename, __dirname);
916

1017
callback(null, minify(options));
1118
} catch (errors) {

test/__snapshots__/minify.test.js.snap

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`when applied with minify option matches snapshot for \`terser\` minifier and \`parallel: true\`: errors 1`] = `Array []`;
4+
5+
exports[`when applied with minify option matches snapshot for \`terser\` minifier and \`parallel: true\`: main.js 1`] = `"webpackJsonp([0],[function(t,e,s){\\"use strict\\";Object.defineProperty(e,\\"__esModule\\",{value:!0});e.default=class{constructor(t,e){this.x=t,this.y=e}static distance(t,e){const s=t.x-e.x,c=t.y-e.y;return Math.hypot(s,c)}}}],[0]);"`;
6+
7+
exports[`when applied with minify option matches snapshot for \`terser\` minifier and \`parallel: true\`: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a<e.length;a++)i=e[a],o[i]&&l.push(o[i][0]),o[i]=0;for(f in u)Object.prototype.hasOwnProperty.call(u,f)&&(r[f]=u[f]);for(n&&n(e,u,c);l.length;)l.shift()();if(c)for(a=0;a<c.length;a++)p=t(t.s=c[a]);return p};var e={},o={1:0};function t(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return r[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}t.m=r,t.c=e,t.d=function(r,n,e){t.o(r,n)||Object.defineProperty(r,n,{configurable:!1,enumerable:!0,get:e})},t.n=function(r){var n=r&&r.__esModule?function(){return r.default}:function(){return r};return t.d(n,\\"a\\",n),n},t.o=function(r,n){return Object.prototype.hasOwnProperty.call(r,n)},t.p=\\"\\",t.oe=function(r){throw console.error(r),r}}([]);"`;
8+
9+
exports[`when applied with minify option matches snapshot for \`terser\` minifier and \`parallel: true\`: warnings 1`] = `Array []`;
10+
311
exports[`when applied with minify option matches snapshot for \`terser\` minifier and \`sourceMap: true\`: errors 1`] = `Array []`;
412

513
exports[`when applied with minify option matches snapshot for \`terser\` minifier and \`sourceMap: true\`: main.js 1`] = `
614
Object {
715
"map": Object {
816
"file": "x",
9-
"mappings": "+CAAAA,OAAAC,eAAAC,EAAA,cAAAC,OAAA,IAcAD,EAAA,cAbAE,YAAAC,EAAAC,GACAC,KAAAF,IACAE,KAAAD,IAGAF,gBAAAI,EAAAC,GACA,MAAAC,EAAAF,EAAAH,EAAAI,EAAAJ,EACAM,EAAAH,EAAAF,EAAAG,EAAAH,EAEA,OAAAM,KAAAC,MAAAH,EAAAC",
17+
"mappings": "+CAAAA,OAAAC,eAAAC,EAAA,cAAAC,OAAA,IAcAD,EAAA,QAdA,MACAE,YAAAC,EAAAC,GACAC,KAAAF,IACAE,KAAAD,IAGAF,gBAAAI,EAAAC,GACA,MAAAC,EAAAF,EAAAH,EAAAI,EAAAJ,EACAM,EAAAH,EAAAF,EAAAG,EAAAH,EAEA,OAAAM,KAAAC,MAAAH,EAAAC",
1018
"names": Array [
1119
"Object",
1220
"defineProperty",

0 commit comments

Comments
 (0)