Skip to content

Commit

Permalink
fix: Remove handling of templates in Plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Feb 14, 2019
1 parent 570f378 commit e2cf0cd
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 51 deletions.
48 changes: 6 additions & 42 deletions src/options/OptionHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
const deepForEach = require('deep-for-each');
const defaults = require('./default');

const PLUGIN_PROPS = [
'plugins',
'resolve.plugins',
'webpack.plugins',
'webpack.resolve.plugins'
];

class OptionHelper {

constructor(grunt, taskName, target) {
Expand All @@ -18,13 +11,13 @@ class OptionHelper {
}

generateOptions() {
const baseOptions = this.getWithPlugins([this.taskName, 'options']);
const baseOptions = this.getRawConfig([this.taskName, 'options']);
if (Array.isArray(baseOptions)) throw new Error('webpack.options must be an object, but array was provided');

return defaults.mergeOptions(
this.getDefaultOptions(),
baseOptions,
this.getWithPlugins([this.taskName, this.target])
this.getRawConfig([this.taskName, this.target])
);
}

Expand Down Expand Up @@ -55,49 +48,20 @@ class OptionHelper {
return typeof option === 'function' ? option(options) : option;
}

getWithPlugins(ns) {
getRawConfig(ns) {
let obj = this.grunt.config.getRaw(ns) || {};

if (typeof obj === 'function') {
obj = obj();
obj = obj(this.grunt.config.get(), this.grunt);
}

deepForEach(obj, function (value, key, parent, path) {
deepForEach(obj, (value, key, parent) => {
if (typeof value === 'string') {
parent[key] = this.grunt.config.process(value);
} else if (PLUGIN_PROPS.indexOf(path) !== -1 && Array.isArray(value)) {
parent[key] = value.map(plugin => this.fixPlugin(plugin));
}
}.bind(this));

return obj;
}

fixPlugin(plugin) {
if (typeof plugin === 'function') return plugin;

// Operate on a copy of the plugin, since the webpack task
// can be called multiple times for one instance of a plugin
const instance = Object.create(plugin);
const grunt = this.grunt;
Object.keys(plugin).forEach((key) => {
if (typeof plugin[key] === 'string') {
instance[key] = grunt.config.process(plugin[key]);
} else if (typeof plugin[key] === 'function') {
instance[key] = function () {
let value = plugin[key].apply(instance, arguments);
if (typeof value === 'string') {
value = grunt.config.process(value);
}

return value;
};
} else {
instance[key] = plugin[key];
}
});

return instance;
return obj;
}

filterGruntOptions(options) {
Expand Down
12 changes: 7 additions & 5 deletions tests/fixtures/webpack/banner-plugin-webpack-4/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ const loadGruntWebpackTasks = require('../../../utils/loadGruntWebpackTasks');

module.exports = function (grunt) {
grunt.initConfig({
name: "Webpack",
outputFileName: "output.js",
pkg: {
copyright: 'Webpack',
copyright: '<%= name %>',
version: '6.55.345',
},
webpack: {
test: {
test: (config, grunt) =>({
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
filename: "output.js",
filename: "<%= outputFileName %>",
},
plugins: [
new webpack.BannerPlugin({
banner: '/*! <%= pkg.copyright %> - Version <%= pkg.version %> dated <%= grunt.template.today() %> */',
banner: `/*! ${config.pkg.copyright} - Version ${config.pkg.version} dated ${grunt.template.today()} */`,
raw: true,
}),
],
},
}),
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ plugin.circle = plugin;
module.exports = function (grunt) {
grunt.initConfig({
webpack: {
test: function() {
return {
test: {
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
Expand All @@ -18,8 +17,7 @@ module.exports = function (grunt) {
plugins: [
plugin,
]
};
},
}
},
});

Expand Down

0 comments on commit e2cf0cd

Please sign in to comment.