Skip to content

Commit

Permalink
fix: webpack plugin only apply method
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Oct 13, 2018
1 parent 54f3cf8 commit b1e7cb2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ coverage.lcov
dist
app
.vscode
yarn.lock
10 changes: 9 additions & 1 deletion lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class WebpackBaseBuilder extends Config {
} else if (this.utils.isObject(plugin) && this.utils.isFunction(plugin.name) && plugin.name.name) {
sourcePlugins[plugin.name.name] = plugin;
}
} else if (this.utils.isFunction(plugin.apply)) { // only apply method plugin
const label = this.utils.getPluginLabel(plugin);
sourcePlugins[label] = { name: plugin };
} else if (Object.keys(plugin).length === 1) {
const label = Object.keys(plugin)[0];
sourcePlugins[label] = plugin[label];
Expand Down Expand Up @@ -138,6 +141,8 @@ class WebpackBaseBuilder extends Config {
}
} else if (this.isWebpackPlugin(configPlugin) || this.isConfigPlugin(configPlugin)) {
target[name] = configPlugin;
} else if (this.utils.isObject(configPlugin) && this.utils.isFunction(configPlugin.apply)) {
target[name] = configPlugin;
}
});

Expand Down Expand Up @@ -413,7 +418,10 @@ class WebpackBaseBuilder extends Config {
pluginName = configInfo.constructor.name;
} else if (this.utils.isObject(configInfo.name)) { // plugin object
plugin = configInfo.name;
pluginName = plugin.constructor && plugin.constructor.name;
pluginName = plugin.constructor && plugin.constructor.name || name;
} else if (this.utils.isObject(configInfo) && this.utils.isFunction(configInfo.apply)) {
plugin = configInfo;
pluginName = name;
} else if (this.utils.isString(configInfo.name) || this.utils.isFunction(configInfo.name)) {
let Clazz = configInfo.name;
if (this.utils.isString(configInfo.name)) {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"lodash.clonedeepwith": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.uniq": "^4.5.0",
"md5": "^2.2.1",
"mini-css-extract-plugin": "^0.4.0",
"node-noop": "^1.0.0",
"node-tool-utils": "^1.0.0",
Expand All @@ -63,6 +64,7 @@
"chai": "^4.0.0",
"codecov": "^2.3.0",
"conventional-changelog-cli": "^1.3.5",
"copy-webpack-plugin": "^4.5.3",
"coveralls": "^2.13.1",
"eslint-config-eslint": "^4.0.0",
"imagemin-webpack-plugin": "^1.5.2",
Expand Down
46 changes: 38 additions & 8 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';
const expect = require('chai').expect;
const WebpackTool = require('webpack-tool');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = WebpackTool.webpack;
const WebpackBaseBuilder = require('../lib/base');
const path = require('path').posix;
const utils = require('../utils/utils');

// http://chaijs.com/api/bdd/
function createBuilder(config) {
Expand Down Expand Up @@ -172,6 +174,32 @@ describe('plugin.test.js', () => {
expect(!!dll).to.be.true;
});

it('should merge apply plugin test', () => {
const builder = createBuilder({
plugins: {
copy: new CopyWebpackPlugin([{ from: 'asset', to: 'public' }])
}
});
const webpackConfig = builder.create();
const plugins = webpackConfig.plugins;
const copy = getPluginByLabel('copy', plugins);
expect(!!copy).to.be.true;
});

it('should merge array plugin test', () => {
const plugin = new CopyWebpackPlugin([{ from: 'asset', to: 'public' }]);
const builder = createBuilder({
plugins: [
plugin
]
});
const webpackConfig = builder.create();
const plugins = webpackConfig.plugins;
const lable = utils.getPluginLabel(plugin);
const copy = getPluginByLabel(lable, plugins);
expect(!!copy).to.be.true;
});

it('should add webpack plugin test', () => {
const builder = createBuilder({});
builder.addPlugin(new webpack.DllPlugin({
Expand Down Expand Up @@ -237,15 +265,17 @@ describe('plugin.test.js', () => {
});
it('should merge array extend key plugin test', () => {
const builder = createBuilder({});
builder.mergePlugin([{ dll : {
enable: true,
name: webpack.DllPlugin,
args: {
path: 'manifest.json',
name: '[name]_[chunkhash]',
context: __dirname
builder.mergePlugin([{
dll: {
enable: true,
name: webpack.DllPlugin,
args: {
path: 'manifest.json',
name: '[name]_[chunkhash]',
context: __dirname
}
}
}}]);
}]);
const webpackConfig = builder.create();
const plugins = webpackConfig.plugins;
const dll = getPluginByLabel('DllPlugin', plugins);
Expand Down
5 changes: 5 additions & 0 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const uniq = require('lodash.uniq');
const _ = require('lodash.get');
const install = require('./install');
const glob = require('glob');
const md5 = require('md5');
const shell = require('shelljs');
const utils = Object.assign({}, {
_,
Expand Down Expand Up @@ -516,4 +517,8 @@ utils.isEgg = config => {
return pkg.dependencies['egg-view-vue-ssr'] || pkg.dependencies['egg-view-react-ssr'];
};

utils.getPluginLabel = plugin => {
return md5(plugin.apply.toString());
};

module.exports = utils;

0 comments on commit b1e7cb2

Please sign in to comment.