简体   繁体   English

Webpack:不同类型的多个入口点(JS,HTML,LESS,...)

[英]Webpack: Multiple entry points of different types (JS, HTML, LESS,…)

I would like to use webpack to package a Chrome Extension. 我想使用webpack打包Chrome扩展程序。 To this end, I need to put together a bunch of JS files, 为此,我需要整理一堆JS文件,

  • background.js , background.js
  • popup.js , popup.js
  • content.js , content.js

and a few HTML and CSS files, 以及一些HTML和CSS文件,

  • popup.html , popup.html
  • popup.css , popup.css
  • content.css . content.css

I figure I'll have to use multiple entry files, ie, 我想我将不得不使用多个条目文件,即

module.exports = {
  entry: {
    background: './src/scripts/background.js',
    content: './src/scripts/content.js',
    popup: './src/scripts/popup.js',
    html: './src/popup.html',
    ccss: './src/styles/content.less',
    pcss: './src/styles/popup.less',
  },
  // ...
}

With the loaders specified, eg, 指定加载器,例如,

module: {
  loaders: [
    { test: /\.html$/, loader: 'file-loader' },
    { test: /\.less$/, loader: 'style!css!less' },
    // ...
    ],
}

However, I'm struggling with the output specifications. 但是,我正在努力解决output规格问题。 The JS files get bundled alright, but I'd like the HTML file to end up as HTML, too. JS文件捆绑得很好,但我也希望HTML文件最终成为HTML。 With the standard 有了标准

output: {
  path: './build/',
  filename: '[name].js',
},

this won't be the case since the .js is hardcoded. 由于.js是硬编码的,因此情况并非如此。

Is there any way for JS, HTML, and CSS entry points to be output as JS, HTML, and CSS files, respectively? 是否有任何方法可以将JS,HTML和CSS入口点分别作为JS,HTML和CSS文件输出?

You don't want to include your HTML files in the webpack bundle, those will exist on their own. 您不希望将HTML文件包含在webpack包中,这些文件将自行存在。

As for bundling LESS/CSS, I recommend using the CSS and LESS loaders with the extract text webpack plugin . 至于捆绑LESS / CSS,我建议将CSS和LESS加载器与extract text webpack插件一起使用 This can be used to automatically bundle LESS/CSS modules that you import in your JavaScript modules, and then output that CSS bundle to the location of your choice. 这可以用于自动捆绑您在JavaScript模块中导入的LESS / CSS模块,然后将该CSS捆绑包输出到您选择的位置。

So your webpack.config will look like this: 所以你的webpack.config看起来像这样:

var ExtractTextPlugin = require('extract-text-webpack-plugin');    

module.exports = {
  entry: {
    background: './src/scripts/background.js',
    content: './src/scripts/content.js',
    popup: './src/scripts/popup.js'
  },
  output: {
    path: './build/',
    filename: '[name].js',
  },
  loaders: [
    { test: /\.less$/, loader: 'style!css!less' },
    // ...
  ],
  plugins: [
    new ExtractTextPlugin('[name].css')
  ]
}

Then in your entry JS, require your LESS files: 然后在您的条目JS中,需要您的LESS文件:

require('./path/to/style.less');

All of your styles will be bundled to ./build/styles.css . 您的所有样式都将捆绑到./build/styles.css

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM