Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Drop include/exclude support
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed May 23, 2020
1 parent 368383f commit 112927f
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 117 deletions.
43 changes: 6 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { terser } from "rollup-plugin-terser";

rollup({
input: "main.js",
plugins: [terser()]
plugins: [terser()],
});
```

Expand Down Expand Up @@ -56,12 +56,6 @@ 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> | string | RegExp`

`options.exclude: Array<string | RegExp> | string | RegExp`

Specifically include/exclude chunk files names (minimatch pattern, or array of minimatch patterns), By default all chunk files will be minify.

## Examples

### Using as output plugin
Expand All @@ -75,33 +69,8 @@ export default {
output: [
{ file: "lib.js", format: "cjs" },
{ file: "lib.min.js", format: "cjs", plugins: [terser()] },
{ file: "lib.esm.js", format: "esm" }
]
};
```

### 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: "esm" },
{ dir: ".", entryFileNames: "lib-[format].js", format: "iife" }
],
plugins: [
terser({
include: [/^.+\.min\.js$/, "*esm*"],
exclude: ["some*"]
})
]
};
```

Expand All @@ -112,15 +81,15 @@ If you'd like to preserve comments (for licensing for example), then you can spe
```js
terser({
output: {
comments: function(node, comment) {
comments: function (node, comment) {
var text = comment.value;
var type = comment.type;
if (type == "comment2") {
// multiline comment
return /@preserve|@license|@cc_on/i.test(text);
}
}
}
},
},
});
```

Expand All @@ -129,8 +98,8 @@ Alternatively, you can also choose to keep all comments (e.g. if a licensing hea
```js
terser({
output: {
comments: "all"
}
comments: "all",
},
});
```

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"license": "MIT",
"dependencies": {
"@babel/code-frame": "^7.8.3",
"@rollup/pluginutils": "^3.0.10",
"jest-worker": "^26.0.0",
"serialize-javascript": "^3.0.0",
"terser": "^4.7.0"
Expand Down
6 changes: 0 additions & 6 deletions rollup-plugin-terser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import { Plugin } from "rollup";
import { MinifyOptions } from "terser";

export interface Options extends Omit<MinifyOptions, "sourceMap"> {
/**
* Specifically include/exclude chunk files names (minimatch pattern, or array of minimatch patterns), By default all chunk files will be minify.
*/
include?: Array<string | RegExp> | string | RegExp | null;
exclude?: Array<string | RegExp> | string | RegExp | null;

/**
* Amount of workers to spawn. Defaults to the number of CPUs minus 1.
*/
Expand Down
11 changes: 1 addition & 10 deletions rollup-plugin-terser.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
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,
Expand All @@ -43,7 +34,7 @@ function terser(userOptions = {}) {
const normalizedOptions = Object.assign({}, defaultOptions, userOptions);

// remove plugin specific options
for (let key of ["include", "exclude", "sourcemap", "numWorkers"]) {
for (let key of ["sourcemap", "numWorkers"]) {
if (normalizedOptions.hasOwnProperty(key)) {
delete normalizedOptions[key];
}
Expand Down
43 changes: 0 additions & 43 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,49 +284,6 @@ test("allow to pass not string values to worker", async () => {
);
});

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();
});

test("terser accepts the nameCache option", async () => {
const nameCache = {
props: {
Expand Down
21 changes: 1 addition & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,6 @@
"@types/yargs" "^15.0.0"
chalk "^4.0.0"

"@rollup/pluginutils@^3.0.10":
version "3.0.10"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.0.10.tgz#a659b9025920378494cd8f8c59fbf9b3a50d5f12"
integrity sha512-d44M7t+PjmMrASHbhgpSbVgtL6EFyX7J4mYxwQ/c5eoaE6N2VgCgEcWVzNnwycIloti+/MpwFr8qfw+nRw00sw==
dependencies:
"@types/estree" "0.0.39"
estree-walker "^1.0.1"
picomatch "^2.2.2"

"@sinonjs/commons@^1.7.0":
version "1.8.0"
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.0.tgz#c8d68821a854c555bba172f3b06959a0039b236d"
Expand Down Expand Up @@ -508,11 +499,6 @@
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==

"@types/estree@0.0.39":
version "0.0.39"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/graceful-fs@^4.1.2":
version "4.1.3"
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.3.tgz#039af35fe26bec35003e8d86d2ee9c586354348f"
Expand Down Expand Up @@ -1148,11 +1134,6 @@ estraverse@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.2.0.tgz#0dee3fed31fcd469618ce7342099fc1afa0bdb13"

estree-walker@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-1.0.1.tgz#31bc5d612c96b704106b477e6dd5d8aa138cb700"
integrity sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==

esutils@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
Expand Down Expand Up @@ -2503,7 +2484,7 @@ performance-now@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"

picomatch@^2.0.4, picomatch@^2.0.5, picomatch@^2.2.2:
picomatch@^2.0.4, picomatch@^2.0.5:
version "2.2.2"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad"
integrity sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==
Expand Down

0 comments on commit 112927f

Please sign in to comment.