From b1e7cb26ef8fa31a77e73c82273a58e08835f8bf Mon Sep 17 00:00:00 2001 From: hubcarl Date: Sat, 13 Oct 2018 17:29:50 +0800 Subject: [PATCH] fix: webpack plugin only apply method --- .gitignore | 1 + lib/base.js | 10 +++++++++- package.json | 2 ++ test/plugin.test.js | 46 +++++++++++++++++++++++++++++++++++++-------- utils/utils.js | 5 +++++ 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index caf6efc..6fdebbd 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ coverage.lcov dist app .vscode +yarn.lock diff --git a/lib/base.js b/lib/base.js index 4f7ed29..ebce3a3 100644 --- a/lib/base.js +++ b/lib/base.js @@ -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]; @@ -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; } }); @@ -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)) { diff --git a/package.json b/package.json index 5ea098c..2a3eb7d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/test/plugin.test.js b/test/plugin.test.js index c4e2bed..a39fb6e 100644 --- a/test/plugin.test.js +++ b/test/plugin.test.js @@ -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) { @@ -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({ @@ -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); diff --git a/utils/utils.js b/utils/utils.js index 0f21377..0a35300 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -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({}, { _, @@ -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; \ No newline at end of file