创建目录多捆在那里找到的js文件

问题描述:

我SRC结构是这样的:创建目录多捆在那里找到的js文件

--src/ 
----index.js 
----index.html 
----background/ 
------background.js 
------background.html 
----editor/ 
------editor.js 
------editor.html 

我是新来的WebPack但我能得到它与刚工作index.js是这样的:

const CopyWebpackPlugin = require('copy-webpack-plugin'); 
const path = require('path'); 
const webpack = require('webpack'); 

module.exports = function (env) { 
    return { 
     devtool: 'cheap-module-source-map', 
     entry: ['babel-polyfill', './src/web/index.js'], 
     output: { 
      path: path.join(__dirname, '../dist/web'), 
      filename: 'index.bundle.js', 
      publicPath: '/static/' 
     }, 
     resolve: { 
      extensions: ['.js'] 
     }, 
     module: { 
      loaders: [ 
       { test:/\.js$/, exclude:/node_modules/, loader:'string-replace-loader?search=^.*?console\.[a-zA-Z].*?$&flags=gm&replace=', enforce:'pre' }, 
       { test:/\.js$/, exclude:/node_modules/, loader:'babel-loader' } 
      ] 
     }, 
     plugins: [ 
      new CopyWebpackPlugin([ 
       { from:'./src/web' }, 
      ], { 
       ignore: ['*.js'] 
      }) 
     ] 
    } 
} 

它正确地吐出了根目录下的index.bundle.js。但我也希望它在后台目录中创建一个(1)background.bundle.js,并在编辑器目录中创建一个(2)editor.bundle.js。是否有可能自动检测名称并在其各自的目录中创建一个包?

我认为你必须将entry更改为object以启用多项输入。

{ 
    entry: { 
     web: './src/web/index.js', 
     background: './src/background/background.js', 
     editor: './src/editor/editor.js' 
    } 
} 

,改变output.filename

{ 
    output:{ 
     filename: '[name]/[name].bundle.js' 
    } 
} 

看到https://webpack.js.org/configuration/output/#output-filename了解更多详情。

+1

@Noitidart你应该添加一个'.babelrc'文件来配置你的'babel-loader'。请参阅http://babeljs.io/docs/usage/babelrc/和https://babeljs.io/docs/usage/api/。 – wuxiandiejia

+0

谢谢你的babel笔记。我正在玩这个,它的工作!然而index.js是在'web/index.bundle.js'中创建的,索引是否可以保留在根目录下,而不是进入subdir? – Noitidart

+1

@Noitidart我看不到'web'目录,它在'src'目录下吗?在你的问题中,'index.js'在'src'中,而不是'src/web'。 – wuxiandiejia