Skip to content

Commit

Permalink
fix: minify cjs assets (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Oct 7, 2020
1 parent f49e786 commit b9c694d
Show file tree
Hide file tree
Showing 16 changed files with 902 additions and 896 deletions.
1,202 changes: 613 additions & 589 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
"dependencies": {
"cacache": "^15.0.5",
"find-cache-dir": "^3.3.1",
"jest-worker": "^26.3.0",
"jest-worker": "^26.5.0",
"p-limit": "^3.0.2",
"schema-utils": "^2.7.1",
"schema-utils": "^3.0.0",
"serialize-javascript": "^5.0.1",
"source-map": "^0.6.1",
"terser": "^5.3.2",
"terser": "^5.3.4",
"webpack-sources": "^1.4.3"
},
"devDependencies": {
Expand All @@ -59,25 +59,25 @@
"@commitlint/config-conventional": "^11.0.0",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.3.0",
"copy-webpack-plugin": "^6.1.1",
"babel-jest": "^26.5.2",
"copy-webpack-plugin": "^6.2.0",
"cross-env": "^7.0.2",
"del": "^5.1.0",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"eslint": "^7.9.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.21.2",
"eslint": "^7.10.0",
"eslint-config-prettier": "^6.12.0",
"eslint-plugin-import": "^2.22.1",
"file-loader": "^6.1.0",
"husky": "^4.3.0",
"jest": "^26.4.2",
"jest": "^26.5.2",
"lint-staged": "^10.4.0",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"standard-version": "^9.0.0",
"uglify-js": "^3.10.4",
"uglify-js": "^3.11.1",
"webpack": "^4.44.2",
"worker-loader": "^3.0.2"
"worker-loader": "^3.0.3"
},
"keywords": [
"uglify",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import webpack, {
} from 'webpack';
import RequestShortener from 'webpack/lib/RequestShortener';

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

class TerserPlugin {
constructor(options = {}) {
validateOptions(schema, options, {
validate(schema, options, {
name: 'Terser Plugin',
baseDataPath: 'options',
});

const {
minify,
terserOptions = {},
test = /\.m?js(\?.*)?$/i,
test = /\.[cm]?js(\?.*)?$/i,
extractComments = true,
sourceMap,
cache = true,
Expand Down
6 changes: 6 additions & 0 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,12 @@ describe('TerserPlugin', () => {
{
from: path.resolve(__dirname, './fixtures/copy.js'),
},
{
from: path.resolve(__dirname, './fixtures/copy.cjs'),
},
{
from: path.resolve(__dirname, './fixtures/copy.mjs'),
},
],
}).apply(compiler);
new TerserPlugin().apply(compiler);
Expand Down
36 changes: 19 additions & 17 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack4

Large diffs are not rendered by default.

60 changes: 31 additions & 29 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack5

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions test/__snapshots__/cache-option.test.js.snap.webpack5

Large diffs are not rendered by default.

72 changes: 36 additions & 36 deletions test/__snapshots__/extractComments-option.test.js.snap.webpack4

Large diffs are not rendered by default.

198 changes: 99 additions & 99 deletions test/__snapshots__/extractComments-option.test.js.snap.webpack5

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions test/__snapshots__/include-option.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Object {
// foo
/* @preserve*/
// bar
const a = 2 + 2;
const a = (/* unused pure expression or super */ null && (2 + 2));

module.exports = function Foo() {
const b = 2 + 2;
Expand Down Expand Up @@ -118,7 +118,7 @@ Object {
// foo
/* @preserve*/
// bar
const a = 2 + 2;
const a = (/* unused pure expression or super */ null && (2 + 2));

module.exports = function Foo() {
const b = 2 + 2;
Expand Down Expand Up @@ -225,7 +225,7 @@ Object {
// foo
/* @preserve*/
// bar
const a = 2 + 2;
const a = (/* unused pure expression or super */ null && (2 + 2));

module.exports = function Foo() {
const b = 2 + 2;
Expand Down Expand Up @@ -287,7 +287,7 @@ Object {
// foo
/* @preserve*/
// bar
const a = 2 + 2;
const a = (/* unused pure expression or super */ null && (2 + 2));

module.exports = function Foo() {
const b = 2 + 2;
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/sourceMap-option.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ exports[`sourceMap should match snapshot for a "true" value (the "devtool" optio

exports[`sourceMap should match snapshot for a "true" value (the "devtool" option has the "eval" value): assets 1`] = `
Object {
"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)})();",
"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)})();",
}
`;

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

exports[`sourceMap should match snapshot for multi compiler mode with source maps: assets 1`] = `
Object {
"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)})();",
"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)})();",
}
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports[`terserOptions option should match snapshot for the "compress" option wi
exports[`terserOptions option should match snapshot for the "compress" option with the "false" value: assets 1`] = `
Object {
"main.js": "/*! For license information please see main.js.LICENSE.txt */
(()=>{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)})();",
(()=>{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)})();",
"main.js.LICENSE.txt": "/* @preserve*/
",
}
Expand Down
Loading

0 comments on commit b9c694d

Please sign in to comment.