简体   繁体   English

Webpack使用插件数组

[英]Webpack using plugins array

We have created webpack config. 我们已经创建了webpack配置。 We want to generate multiple html pages using HtmlWebpackPlugin . 我们想使用HtmlWebpackPlugin生成多个html页面。 So I was trying to genrate mutltiple pages using array of new HTMLWebpackPlugin() like this : 所以我试图使用这样的新HTMLWebpackPlugin()数组生成多页:

var HtmlWebpackPluginArray = [new HtmlWebpackPlugin({
    title: "TV Grid_1---",
    template: APP_DIR + '/index.template.ejs',
    filename: "grid_1.html",
    minify: {
        collapseWhitespace: true,
        removeComments: true,
        removeRedundantAttributes: true,
        removeScriptTypeAttributes: true,
        removeStyleLinkTypeAttributes: true
    }
}), new HtmlWebpackPlugin({
    title: "TV Grid_2---",
    template: APP_DIR + '/index.template.ejs',
    filename: "grid_2.html",
    minify: {
        collapseWhitespace: true,
        removeComments: true,
        removeRedundantAttributes: true,
        removeScriptTypeAttributes: true,
        removeStyleLinkTypeAttributes: true
    }
})];

Webpack config : Webpack配置:

plugins: [
    new CleanWebpackPlugin(DIST_PATH),
    new ExtractTextPlugin({
        filename: 'styles.[chunkhash].css',
        // disable: false,
        allChunks: true
    }),
    // new webpack.HashedModuleIdsPlugin(),
    new webpack.optimize.CommonsChunkPlugin({
        name: 'vendor',
        filename: 'vendor.[chunkhash].js'
    }),
    new webpack.optimize.CommonsChunkPlugin({
        name: 'manifest'
    }),
    new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery"
    }),
  HtmlWebpackPluginArray,
]

I get following error : 我收到以下错误:

TypeError: arguments[i].apply is not a function

Can we use array of plugins ? 我们可以使用一系列插件吗?

Can we use array of plugins ? 我们可以使用一系列插件吗?

Yes, but your code is currently producing an array of plugins with another array of plugins inside it: 是的,但是您的代码当前正在生成一个插件数组,其中包含另一个插件数组:

// this (your code with plugin config omitted):
plugins: [
    new CleanWebpackPlugin(…),
    new ExtractTextPlugin(…),
    new webpack.optimize.CommonsChunkPlugin(…),
    new webpack.optimize.CommonsChunkPlugin(…),
    new webpack.ProvidePlugin(…),
    HtmlWebpackPluginArray
]

// results in:
plugins: [
    new CleanWebpackPlugin(…),
    new ExtractTextPlugin(…),
    new webpack.optimize.CommonsChunkPlugin(…),
    new webpack.optimize.CommonsChunkPlugin(…),
    new webpack.ProvidePlugin(…),
    [
         new HtmlWebpackPlugin(…),
         new HtmlWebpackPlugin(…)
    ];
]

This won't work, because Webpack tries to call .apply() on an array. 这将不起作用,因为Webpack尝试在数组上调用.apply() The error message you posted comes from that. 您发布的错误消息由此而来。

You need to merge the arrays rather than put one into another: 您需要合并数组,而不是将它们放入另一个数组:

// this:
plugins: [
    new CleanWebpackPlugin(…),
    new ExtractTextPlugin(…),
    new webpack.optimize.CommonsChunkPlugin(…),
    new webpack.optimize.CommonsChunkPlugin(…),
    new webpack.ProvidePlugin(…),
].concat(HtmlWebpackPluginArray)

// results in:
plugins: [
    new CleanWebpackPlugin(…),
    new ExtractTextPlugin(…),
    new webpack.optimize.CommonsChunkPlugin(…),
    new webpack.optimize.CommonsChunkPlugin(…),
    new webpack.ProvidePlugin(…),
    new HtmlWebpackPlugin(…),
    new HtmlWebpackPlugin(…)
]
// …which is valid config and will work

The .concat() method merges two (or more) arrays into a new one without altering the original arrays. .concat()方法将两个(或多个)数组合并为一个新数组,而无需更改原始数组。 Another way would be to use an array spread like so: 另一种方法是使用数组扩展,如下所示:

plugins: [
    new CleanWebpackPlugin(…),
    new ExtractTextPlugin(…),
    new webpack.optimize.CommonsChunkPlugin(…),
    new webpack.optimize.CommonsChunkPlugin(…),
    new webpack.ProvidePlugin(…),
    ...HtmlWebpackPluginArray
] // -> same result as from .concat above

Btw. 顺便说一句。 if you plan to update to Webpack 4, you can simplify the plugin config a bit – there's no CommonsChunkPlugin anymore, and chunks are configured in webpack's config object directly. 如果您打算更新到Webpack 4,则可以稍微简化插件配置-不再有CommonsChunkPlugin ,并且直接在webpack的config对象中配置块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Fullcalendar 未使用 Webpack Encore 加载视图插件 - Fullcalendar not loading view plugins using Webpack Encore 带有 webpack (SignalR) 的 jQuery 和插件 - jQuery and plugins with webpack (SignalR) React webpack 配置:是否可以只为数组中的一个插件替换配置,而不重置插件数组? - React webpack config: is it possible to replace config for only one plugin in array, without reset an array of plugins? Bootstrap 5 Webpack 插件功能不完整 - Bootstrap 5 Webpack plugins are not fully functional 使用 webpack 将插件安装到 Laravel 5 中的 CKEditor 5 - Installing plugins to CKEditor 5 in Laravel 5 with webpack 如何使用 webpack 在 ES6 项目中加载 Bootstrap 5 jQuery 插件? - How do I load the Bootstrap 5 jQuery plugins in an ES6 project using webpack? 在加载程序数组中使用Webpack自定义加载程序 - Using Webpack custom loader in loaders array webpack 配置中的 resolve.plugins 和 plugins 字段有什么区别? - What is the difference between resolve.plugins and plugins field in webpack configuration? Node 和 Webpack,使用 require 指令初始化插件类 - Node and Webpack, initialize plugins classes with require instruction 如何使用 Rails + Webpack 配置 Bootstrap 插件? - How to configure Bootstrap plugins with Rails + Webpack?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM