Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for filter by chunk files names #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,37 @@ 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

### 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 { uglify } from "rollup-plugin-uglify";

export default {
input: "some.js",
output: [
{ file: 'some.js' },
{ file: 'some.min.js' },
{ file: 'foo.min.js' },
{ file: 'other.min.js' }
],
plugins: [
uglify({
include: /^.+\.min\.js$/,
exclude: ['foo.min.js', 'other*']
})
]
};
```

### Comments

If you'd like to preserve comments (for licensing for example), then you can specify a function to do this like so:
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const { codeFrameColumns } = require("@babel/code-frame");
const Worker = require("jest-worker").default;
const serialize = require("serialize-javascript");
const { createFilter } = require('rollup-pluginutils');

function uglify(userOptions = {}) {
if (userOptions.sourceMap != null) {
throw Error("sourceMap option is removed, use sourcemap instead");
}

const filter = createFilter( userOptions.include, userOptions.exclude, { resolve: false } );

const minifierOptions = serialize(
Object.assign({}, userOptions, {
exclude: undefined,
include: undefined,
sourceMap: userOptions.sourcemap !== false,
sourcemap: undefined,
numWorkers: undefined
Expand All @@ -24,7 +29,11 @@ function uglify(userOptions = {}) {
});
},

renderChunk(code) {
renderChunk(code, { fileName }) {
if(!filter(fileName)){
return null;
}

return this.worker.transform(code, minifierOptions).catch(error => {
const { message, line, col: column } = error;
console.error(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"@babel/code-frame": "^7.0.0",
"jest-worker": "^24.0.0",
"rollup-pluginutils": "^2.8.1",
"serialize-javascript": "^1.6.1",
"uglify-js": "^3.4.9"
},
Expand Down
47 changes: 42 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const assert = require("assert");
const { rollup } = require("rollup");
const { readFileSync: readFile } = require("fs");
const { uglify } = require("../");

test("minify", async () => {
Expand All @@ -9,7 +7,7 @@ test("minify", async () => {
plugins: [uglify()]
});
const result = await bundle.generate({ format: "cjs" });
const { code, map } = result.output[0]
const { code, map } = result.output[0];
expect(code).toEqual(
'"use strict";window.a=5,window.a<3&&console.log(4);\n'
);
Expand All @@ -25,7 +23,7 @@ test("minify via uglify options", async () => {
banner: "/* package name */",
format: "cjs"
});
const { code, map } = result.output[0]
const { code, map } = result.output[0];
expect(code).toEqual('/* package name */\n"use strict";\n');
expect(map).toBeFalsy();
});
Expand All @@ -36,7 +34,7 @@ test("minify with sourcemaps", async () => {
plugins: [uglify()]
});
const result = await bundle.generate({ format: "cjs", sourcemap: true });
const { code, map } = result.output[0]
const { code, map } = result.output[0];
expect(map).toBeTruthy();
});

Expand Down Expand Up @@ -104,3 +102,42 @@ test("allow to pass not string values to worker", async () => {
'"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: [ uglify({ 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: [ uglify({ 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: [ uglify({ include: /.+-1\.\w+/ }) ]
});
const result = await bundle.generate({ format: "es" });
const chunk1 = result.output[0];
const chunk2 = result.output[1];

expect(chunk1.code).toBe(`var chunk1="chunk-1";console.log(chunk1);\n`);
expect(chunk1.map).toBeFalsy();
expect(chunk2.code).toBe(`var chunk2 = 'chunk-2';\nconsole.log(chunk2);\n`);
expect(chunk2.map).toBeFalsy();
});
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,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"
Expand Down Expand Up @@ -3412,6 +3417,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.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.1.2.tgz#8d094b85683b810d0c05a16bd7618cf70d48eba7"
Expand Down