From cee12af550ee671865e03a8b546a81f191873b8b Mon Sep 17 00:00:00 2001 From: yisraelx Date: Tue, 2 Jul 2019 22:10:47 +0300 Subject: [PATCH] feat: add support for filter by chunk files names (#37) --- README.md | 31 +++++++++++++++++++++++++++++++ index.js | 9 ++++++++- package.json | 1 + test/test.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 12 ++++++++++++ 5 files changed, 101 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4ed8df8..2d9fbe9 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,39 @@ Generates source maps and passes them to rollup. Defaults to `true`. Amount of workers to spawn. Defaults to the number of CPUs minus 1. + +`options.include: Array | string | RegExp` + +`options.exclude: Array | string | RegExp` + +Specifically include/exclude chunk files names (minimatch pattern, or array of minimatch patterns), By default all chunk files will be minify. + ## Examples +### include/exclude +If you'd like that only some of the files will be minify, then you can filter by `include` and `exclude` to do this like so: + +```js +// rollup.config.js +import { terser } from "rollup-plugin-terser"; + +export default { + input: "index.js", + output: [ + { file: 'lib.js', format: 'cjs' }, + { file: 'lib.min.js', format: 'cjs' }, + { file: 'lib.esm.js', format: 'es' }, + { dir: '.', entryFileNames: 'lib-[format].js', format: 'iife' } + ], + plugins: [ + terser({ + include: [/^.+\.min\.js$/, '*esm*'], + exclude: [ 'some*' ] + }) + ] +}; +``` + ### Comments If you'd like to preserve comments (for licensing for example), then you can specify a function to do this like so: diff --git a/index.js b/index.js index fd741ab..0c817a4 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,23 @@ const { codeFrameColumns } = require("@babel/code-frame"); const Worker = require("jest-worker").default; const serialize = require("serialize-javascript"); +const { createFilter } = require('rollup-pluginutils'); function terser(userOptions = {}) { if (userOptions.sourceMap != null) { throw Error("sourceMap option is removed, use sourcemap instead"); } + const filter = createFilter( userOptions.include, userOptions.exclude, { resolve: false } ); + return { name: "terser", renderChunk(code, chunk, outputOptions) { + if(!filter(chunk.fileName)){ + return null; + } + if (!this.worker) { this.worker = new Worker(require.resolve("./transform.js"), { numWorkers: userOptions.numWorkers @@ -26,7 +33,7 @@ function terser(userOptions = {}) { module: outputOptions.format === "es" || outputOptions.format === "esm" }); - for (let key of ["sourcemap", "numWorkers"]) { + for (let key of ["include", "exclude", "sourcemap", "numWorkers"]) { if (normalizedOptions.hasOwnProperty(key)) { delete normalizedOptions[key]; } diff --git a/package.json b/package.json index 512ff24..760078a 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "dependencies": { "@babel/code-frame": "^7.0.0", "jest-worker": "^24.6.0", + "rollup-pluginutils": "^2.8.1", "serialize-javascript": "^1.7.0", "terser": "^4.0.0" }, diff --git a/test/test.js b/test/test.js index c4d653a..5a2eaef 100644 --- a/test/test.js +++ b/test/test.js @@ -237,3 +237,52 @@ test("allow arrow function definitions passing to worker", async () => { '"use strict";window.a=5,window.a<3&&console.log(4);\n' ); }); + +test("allow to pass not string values to worker", async () => { + const bundle = await rollup({ + input: "test/fixtures/unminified.js", + plugins: [terser({ mangle: { properties: { regex: /^_/ } } })] + }); + const result = await bundle.generate({ format: "cjs" }); + expect(result.output[0].code).toEqual( + '"use strict";window.a=5,window.a<3&&console.log(4);\n' + ); +}); + +test("include chunk file by string name", async () => { + const bundle = await rollup({ + input: "test/fixtures/unminified.js", + plugins: [ terser({ include: 'some.js' }) ] + }); + + const result = await bundle.generate({ format: "es", file: 'some.js' }); + const { code, map } = result.output[0]; + expect(code).toBe(`window.a=5,window.a<3&&console.log(4);\n`); + expect(map).toBeFalsy(); +}); + +test("exclude chunk file pattern name by minimatch pattern", async () => { + const bundle = await rollup({ + input: "test/fixtures/unminified.js", + plugins: [ terser({ exclude: '*-cjs.js' }) ] + }); + const result = await bundle.generate({ format: "cjs", entryFileNames: '[name]-[format].js' }); + const { code, map } = result.output[0]; + + expect(code).toBe(`'use strict';\n\nwindow.a = 5;\n\nif (window.a < 3) {\n console.log(4);\n}\n`); + expect(map).toBeFalsy(); +}); + +test("include only one chunk file by regex", async () => { + const bundle = await rollup({ + input: [ "test/fixtures/chunk-1.js", "test/fixtures/chunk-2.js" ], + plugins: [ terser({ include: /.+-1\.\w+/ }) ] + }); + const result = await bundle.generate({ format: "es" }); + const { 0: chunk1, 1: chunk2 } = result.output; + + expect(chunk1.code).toBe(`console.log("chunk-1");\n`); + expect(chunk1.map).toBeFalsy(); + expect(chunk2.code).toBe(`var chunk2 = 'chunk-2';\nconsole.log(chunk2);\n`); + expect(chunk2.map).toBeFalsy(); +}); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 9d4678c..af0c52b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1055,6 +1055,11 @@ estraverse@^4.2.0: version "4.2.0" resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.2.0.tgz#0dee3fed31fcd469618ce7342099fc1afa0bdb13" +estree-walker@^0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362" + integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w== + esutils@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b" @@ -2869,6 +2874,13 @@ rimraf@^2.6.2: dependencies: glob "^7.1.3" +rollup-pluginutils@^2.8.1: + version "2.8.1" + resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.1.tgz#8fa6dd0697344938ef26c2c09d2488ce9e33ce97" + integrity sha512-J5oAoysWar6GuZo0s+3bZ6sVZAC0pfqKz68De7ZgDi5z63jOVZn1uJL/+z1jeKHNbGII8kAyHF5q8LnxSX5lQg== + dependencies: + estree-walker "^0.6.1" + rollup@^1.12.3: version "1.12.3" resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.12.3.tgz#068b1957d5bebf6c0a758cfe42609b512add35a9"