Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat: add filters;unit tests; plugin not change
Browse files Browse the repository at this point in the history
BREAKING CHANGE: wrap a plugin and not change it
  • Loading branch information
wmzy committed May 13, 2019
1 parent 1c914fc commit 7c2f4ad
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ npm i -D rollup-plugin-by-output
// rollup.config.js
import babel from 'rollup-plugin-babel';
import terser from 'rollup-plugin-terser';
import plugins from 'rollup-plugin-by-output';
import plugins, {file} from 'rollup-plugin-by-output';


export default {
// ...
plugins: plugins(babel(), [o => (o.file === pkg.browser), terser()]),
plugins: plugins(babel(), [file(pkg.browser), terser()]),
output: {
globals: {
lodash: '_'
Expand Down
16 changes: 15 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,19 @@ module.exports = {
}
}
]
]
],
env: {
test: {
presets: [
[
'@babel/env',
{
targets: {
node: true
}
}
]
]
}
}
};
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,8 @@
"hooks": {
"pre-commit": "lint-staged"
}
},
"dependencies": {
"lodash": "^4.17.11"
}
}
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import pkg from './package.json';

export default {
input: 'src/index.js',
external: ['lodash'],
plugins: [babel({exclude: ['node_modules/**']})],
output: [
{
Expand Down
46 changes: 34 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import assert from 'assert';
import _ from 'lodash';

const wrappers = {
outputOptions(filter, fn) {
return function outputOptionsWrapper(outputOptions) {
Expand All @@ -21,25 +24,44 @@ const wrappers = {
export function when(filter, plugin) {
const {name, ...rest} = plugin;

Object.keys(rest).forEach(k => {
const wrapper = wrappers[k];
if (!wrapper) {
throw new Error(`[rollup-plugin-by-output] not support plugin: ${name}`);
}

plugin[k] = wrapper(filter, plugin[k]);
});
return {
name,
..._.mapValues(rest, (v, k) => {
const wrapper = wrappers[k];
assert(wrapper, `not support plugin: ${name} - ${k}`);

return plugin;
return wrapper(filter, v);
})
};
}

export function whenAll(filter, plugins) {
return plugins.map(p => when(filter, p));
}

export default function plugins(...pluginList) {
return pluginList.map(plugin => {
if (!Array.isArray(plugin)) return plugin;
return when(...plugin);
return _.flatMap(pluginList, plugin => {
if (!_.isArray(plugin)) return plugin;

const [filter, plugins] = plugin;
return _.isArray(plugins)
? whenAll(filter, plugins)
: when(filter, plugins);
});
}

export function prop(key, filter) {
if (_.isString(filter)) return out => out[key] === filter;
if (_.isRegExp(filter)) return out => filter.test(out[key]);
if (_.isFunction(filter)) return out => filter(out[key]);

throw new Error('not support filter type');
}

export function format(filter) {
return prop('format', filter);
}

export function file(filter) {
return prop('file', filter);
}
81 changes: 78 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,82 @@
import plugins, {when, whenAll} from '../src';
import should from 'should';
import sinon from 'sinon';
import plugins, {when, file, format} from '../src';

const stubPlugin = {
name: 'StubPlugin',
outputOptions: sinon.stub(),
generateBundle: sinon.stub(),
renderChunk: sinon.stub()
};

const notSupportPlugin = {
name: 'not-support',
renderStart: sinon.stub()
};

afterEach(function() {
sinon.restore();
});

describe('rollup-plugin-by-output', function() {
it('when should be work', function() {
// todo: test
describe('filter helps', function() {
it('should work with regexp', function() {
const filter = file(/abc/);
filter({file: '123abcd'}).should.be.true();
filter({file: '123a bcd'}).should.be.false();
});

it('should work with string', function() {
const filter = format('abc');
filter({format: 'abc'}).should.be.true();
filter({format: 'abcd'}).should.be.false();
});

it('should work with function', function() {
const filter = file(f => f);
filter({file: 'abc'}).should.be.ok();
filter({file: ''}).should.not.be.ok();
});
});

describe('plugin wrapper', function() {
it('[when] should be work', function() {
const wrappedPlugin = when(o => o.file === 'f', stubPlugin);
wrappedPlugin.should.not.equal(stubPlugin);

should(wrappedPlugin.outputOptions({})).be.null();
stubPlugin.outputOptions.should.not.be.called();
wrappedPlugin.outputOptions({file: 'f'});
stubPlugin.outputOptions.should.be.called();

should(wrappedPlugin.generateBundle({})).be.undefined();
stubPlugin.generateBundle.should.not.be.called();
wrappedPlugin.generateBundle({file: 'f'});
stubPlugin.generateBundle.should.be.called();

should(wrappedPlugin.renderChunk(null, null, {})).be.null();
stubPlugin.renderChunk.should.not.be.called();
wrappedPlugin.renderChunk(null, null, {file: 'f'});
stubPlugin.renderChunk.should.be.called();
});

it('[when] should throw with not support hook', function() {
should(() => {
when(o => o.file, notSupportPlugin);
}).throw(/not support/);
});

it('[plugins] work with multi plugins', function() {
const filter = o => o.file;
const newPlugins = plugins(
[filter, stubPlugin],
[filter, [stubPlugin, stubPlugin]],
notSupportPlugin
);

newPlugins.length.should.be.equal(4);
newPlugins.should.not.containEql(stubPlugin);
newPlugins[3].should.be.equal(notSupportPlugin);
});
});
});

0 comments on commit 7c2f4ad

Please sign in to comment.