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

fix: all nodejs globals avaliable inside minify function #332

Merged
merged 1 commit into from Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Expand Up @@ -196,6 +196,8 @@ If you use your own `minify` function please read the `minify` section for handl

### `minify`

> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**

**webpack.config.js**
```js
[
Expand All @@ -204,11 +206,12 @@ If you use your own `minify` function please read the `minify` section for handl
const extractedComments = [];

// Custom logic for extract comments

const { error, map, code, warnings } = minify(
file,
{ /* Your options for minification */ },
);

const { error, map, code, warnings } = require('uglify-module') // Or require('./path/to/uglify-module')
.minify(
file,
{ /* Your options for minification */ },
);

return { error, map, code, warnings, extractedComments };
}
Expand Down
146 changes: 68 additions & 78 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/uglify/worker.js
Expand Up @@ -5,7 +5,14 @@ module.exports = (options, callback) => {
// 'use strict' => this === undefined (Clean Scope)
// Safer for possible security issues, albeit not critical at all here
// eslint-disable-next-line no-new-func, no-param-reassign
options = new Function(`'use strict'\nreturn ${options}`)();
options = new Function(
'exports',
'require',
'module',
'__filename',
'__dirname',
`'use strict'\nreturn ${options}`,
)(exports, require, module, __filename, __dirname);

callback(null, minify(options));
} catch (errors) {
Expand Down
10 changes: 9 additions & 1 deletion test/__snapshots__/minify.test.js.snap
@@ -1,12 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`when applied with minify option matches snapshot for \`terser\` minifier and \`parallel: true\`: errors 1`] = `Array []`;

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]);"`;

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}}([]);"`;

exports[`when applied with minify option matches snapshot for \`terser\` minifier and \`parallel: true\`: warnings 1`] = `Array []`;

exports[`when applied with minify option matches snapshot for \`terser\` minifier and \`sourceMap: true\`: errors 1`] = `Array []`;

exports[`when applied with minify option matches snapshot for \`terser\` minifier and \`sourceMap: true\`: main.js 1`] = `
Object {
"map": Object {
"file": "x",
"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",
"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",
"names": Array [
"Object",
"defineProperty",
Expand Down