Skip to content

Commit b9c694d

Browse files
fix: minify cjs assets (#328)
1 parent f49e786 commit b9c694d

16 files changed

+902
-896
lines changed

package-lock.json

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

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@
4343
"dependencies": {
4444
"cacache": "^15.0.5",
4545
"find-cache-dir": "^3.3.1",
46-
"jest-worker": "^26.3.0",
46+
"jest-worker": "^26.5.0",
4747
"p-limit": "^3.0.2",
48-
"schema-utils": "^2.7.1",
48+
"schema-utils": "^3.0.0",
4949
"serialize-javascript": "^5.0.1",
5050
"source-map": "^0.6.1",
51-
"terser": "^5.3.2",
51+
"terser": "^5.3.4",
5252
"webpack-sources": "^1.4.3"
5353
},
5454
"devDependencies": {
@@ -59,25 +59,25 @@
5959
"@commitlint/config-conventional": "^11.0.0",
6060
"@webpack-contrib/defaults": "^6.3.0",
6161
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
62-
"babel-jest": "^26.3.0",
63-
"copy-webpack-plugin": "^6.1.1",
62+
"babel-jest": "^26.5.2",
63+
"copy-webpack-plugin": "^6.2.0",
6464
"cross-env": "^7.0.2",
65-
"del": "^5.1.0",
65+
"del": "^6.0.0",
6666
"del-cli": "^3.0.1",
67-
"eslint": "^7.9.0",
68-
"eslint-config-prettier": "^6.11.0",
69-
"eslint-plugin-import": "^2.21.2",
67+
"eslint": "^7.10.0",
68+
"eslint-config-prettier": "^6.12.0",
69+
"eslint-plugin-import": "^2.22.1",
7070
"file-loader": "^6.1.0",
7171
"husky": "^4.3.0",
72-
"jest": "^26.4.2",
72+
"jest": "^26.5.2",
7373
"lint-staged": "^10.4.0",
7474
"memfs": "^3.2.0",
7575
"npm-run-all": "^4.1.5",
7676
"prettier": "^2.1.2",
7777
"standard-version": "^9.0.0",
78-
"uglify-js": "^3.10.4",
78+
"uglify-js": "^3.11.1",
7979
"webpack": "^4.44.2",
80-
"worker-loader": "^3.0.2"
80+
"worker-loader": "^3.0.3"
8181
},
8282
"keywords": [
8383
"uglify",

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import webpack, {
1111
} from 'webpack';
1212
import RequestShortener from 'webpack/lib/RequestShortener';
1313

14-
import validateOptions from 'schema-utils';
14+
import { validate } from 'schema-utils';
1515
import serialize from 'serialize-javascript';
1616
import terserPackageJson from 'terser/package.json';
1717
import pLimit from 'p-limit';
@@ -28,15 +28,15 @@ const { SourceMapSource, RawSource, ConcatSource } =
2828

2929
class TerserPlugin {
3030
constructor(options = {}) {
31-
validateOptions(schema, options, {
31+
validate(schema, options, {
3232
name: 'Terser Plugin',
3333
baseDataPath: 'options',
3434
});
3535

3636
const {
3737
minify,
3838
terserOptions = {},
39-
test = /\.m?js(\?.*)?$/i,
39+
test = /\.[cm]?js(\?.*)?$/i,
4040
extractComments = true,
4141
sourceMap,
4242
cache = true,

test/TerserPlugin.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,12 @@ describe('TerserPlugin', () => {
787787
{
788788
from: path.resolve(__dirname, './fixtures/copy.js'),
789789
},
790+
{
791+
from: path.resolve(__dirname, './fixtures/copy.cjs'),
792+
},
793+
{
794+
from: path.resolve(__dirname, './fixtures/copy.mjs'),
795+
},
790796
],
791797
}).apply(compiler);
792798
new TerserPlugin().apply(compiler);

test/__snapshots__/TerserPlugin.test.js.snap.webpack4

Lines changed: 19 additions & 17 deletions
Large diffs are not rendered by default.

test/__snapshots__/TerserPlugin.test.js.snap.webpack5

Lines changed: 31 additions & 29 deletions
Large diffs are not rendered by default.

test/__snapshots__/cache-option.test.js.snap.webpack5

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

test/__snapshots__/extractComments-option.test.js.snap.webpack4

Lines changed: 36 additions & 36 deletions
Large diffs are not rendered by default.

test/__snapshots__/extractComments-option.test.js.snap.webpack5

Lines changed: 99 additions & 99 deletions
Large diffs are not rendered by default.

test/__snapshots__/include-option.test.js.snap.webpack5

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Object {
1111
// foo
1212
/* @preserve*/
1313
// bar
14-
const a = 2 + 2;
14+
const a = (/* unused pure expression or super */ null && (2 + 2));
1515

1616
module.exports = function Foo() {
1717
const b = 2 + 2;
@@ -118,7 +118,7 @@ Object {
118118
// foo
119119
/* @preserve*/
120120
// bar
121-
const a = 2 + 2;
121+
const a = (/* unused pure expression or super */ null && (2 + 2));
122122

123123
module.exports = function Foo() {
124124
const b = 2 + 2;
@@ -225,7 +225,7 @@ Object {
225225
// foo
226226
/* @preserve*/
227227
// bar
228-
const a = 2 + 2;
228+
const a = (/* unused pure expression or super */ null && (2 + 2));
229229

230230
module.exports = function Foo() {
231231
const b = 2 + 2;
@@ -287,7 +287,7 @@ Object {
287287
// foo
288288
/* @preserve*/
289289
// bar
290-
const a = 2 + 2;
290+
const a = (/* unused pure expression or super */ null && (2 + 2));
291291

292292
module.exports = function Foo() {
293293
const b = 2 + 2;

test/__snapshots__/sourceMap-option.test.js.snap.webpack5

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ exports[`sourceMap should match snapshot for a "true" value (the "devtool" optio
4545

4646
exports[`sourceMap should match snapshot for a "true" value (the "devtool" option has the "eval" value): assets 1`] = `
4747
Object {
48-
"main.js": "(()=>{var __webpack_modules__={791:module=>{eval(\\"// foo\\\\n/* @preserve*/\\\\n// bar\\\\nconst a = 2 + 2;\\\\n\\\\nmodule.exports = function Foo() {\\\\n const b = 2 + 2;\\\\n console.log(b + 1 + 2);\\\\n};\\\\n\\\\n\\\\n//# sourceURL=webpack://terser-webpack-plugin/./test/fixtures/entry.js?\\")}},__webpack_module_cache__={};function __webpack_require__(_){if(__webpack_module_cache__[_])return __webpack_module_cache__[_].exports;var e=__webpack_module_cache__[_]={exports:{}};return __webpack_modules__[_](e,e.exports,__webpack_require__),e.exports}__webpack_require__(791)})();",
48+
"main.js": "(()=>{var __webpack_modules__={791:module=>{eval(\\"// foo\\\\n/* @preserve*/\\\\n// bar\\\\nconst a = (/* unused pure expression or super */ null && (2 + 2));\\\\n\\\\nmodule.exports = function Foo() {\\\\n const b = 2 + 2;\\\\n console.log(b + 1 + 2);\\\\n};\\\\n\\\\n\\\\n//# sourceURL=webpack://terser-webpack-plugin/./test/fixtures/entry.js?\\")}},__webpack_module_cache__={};function __webpack_require__(e){if(__webpack_module_cache__[e])return __webpack_module_cache__[e].exports;var _=__webpack_module_cache__[e]={exports:{}};return __webpack_modules__[e](_,_.exports,__webpack_require__),_.exports}__webpack_require__(791)})();",
4949
}
5050
`;
5151

@@ -184,7 +184,7 @@ exports[`sourceMap should match snapshot for a "true" value (the "devtool" optio
184184

185185
exports[`sourceMap should match snapshot for multi compiler mode with source maps: assets 1`] = `
186186
Object {
187-
"main-1.js": "(()=>{var __webpack_modules__={791:module=>{eval(\\"// foo\\\\n/* @preserve*/\\\\n// bar\\\\nconst a = 2 + 2;\\\\n\\\\nmodule.exports = function Foo() {\\\\n const b = 2 + 2;\\\\n console.log(b + 1 + 2);\\\\n};\\\\n\\\\n\\\\n//# sourceURL=webpack://terser-webpack-plugin/./test/fixtures/entry.js?\\")}},__webpack_module_cache__={};function __webpack_require__(_){if(__webpack_module_cache__[_])return __webpack_module_cache__[_].exports;var e=__webpack_module_cache__[_]={exports:{}};return __webpack_modules__[_](e,e.exports,__webpack_require__),e.exports}__webpack_require__(791)})();",
187+
"main-1.js": "(()=>{var __webpack_modules__={791:module=>{eval(\\"// foo\\\\n/* @preserve*/\\\\n// bar\\\\nconst a = (/* unused pure expression or super */ null && (2 + 2));\\\\n\\\\nmodule.exports = function Foo() {\\\\n const b = 2 + 2;\\\\n console.log(b + 1 + 2);\\\\n};\\\\n\\\\n\\\\n//# sourceURL=webpack://terser-webpack-plugin/./test/fixtures/entry.js?\\")}},__webpack_module_cache__={};function __webpack_require__(e){if(__webpack_module_cache__[e])return __webpack_module_cache__[e].exports;var _=__webpack_module_cache__[e]={exports:{}};return __webpack_modules__[e](_,_.exports,__webpack_require__),_.exports}__webpack_require__(791)})();",
188188
}
189189
`;
190190

test/__snapshots__/terserOptions-option.test.js.snap.webpack5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exports[`terserOptions option should match snapshot for the "compress" option wi
1313
exports[`terserOptions option should match snapshot for the "compress" option with the "false" value: assets 1`] = `
1414
Object {
1515
"main.js": "/*! For license information please see main.js.LICENSE.txt */
16-
(()=>{var o={791:o=>{const r=2+2;o.exports=function o(){const r=2+2;console.log(r+1+2)}}};var r={};function t(n){if(r[n]){return r[n].exports}var e=r[n]={exports:{}};o[n](e,e.exports,t);return e.exports}t(791)})();",
16+
(()=>{var o={791:o=>{const r=null&&2+2;o.exports=function o(){const r=2+2;console.log(r+1+2)}}};var r={};function t(n){if(r[n]){return r[n].exports}var e=r[n]={exports:{}};o[n](e,e.exports,t);return e.exports}t(791)})();",
1717
"main.js.LICENSE.txt": "/* @preserve*/
1818
",
1919
}

0 commit comments

Comments
 (0)