Dev*_*Dev 4 javascript configuration external node.js webpack
嗨,我目前正在使用webpack将我的项目文件捆绑到一个文件中.但是,我不希望webpack捆绑我的config.js文件,其中所有配置都已设置.我想在输出文件夹中保持单独但不确定是否实现此目的.
我目前的设置是
//index.js file
#!/usr/bin/env node
'use strict';
let config = require('config.js);
let read = require('read.js);
console.log('i am running through command line');
//read.js file
'use strict'
console.log('read a text file');
//config.js
'use strict';
module.exports = {
name: 'test'
}
//webpack.config.js
let webpack = require('webpack');
let path = require('path');
let fs = require('fs');
let nodeModules = {};
fs.readdirSync('node_modules')
.filter(function (x) {
return [ '.bin' ].indexOf(x) === -1;
})
.forEach(function (mod) {
nodeModules[mod] = 'commonjs ' + mod;
});
module.exports = {
entry: [ 'babel-polyfill', './index.js' ],
target: 'node',
node: {
__dirname: true
},
output: {
path: path.join(__dirname, 'webpack_bundle'),
filename: '[name].js',
libraryTarget: 'commonjs'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'shebang-loader'
},
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: [ 'es2015' ]
}
} ]
},
resolve: {
extensions: [ '.js' ]
},
plugins: [
new webpack.BannerPlugin({banner: '#!/usr/bin/env node', raw: true
})
]
,
externals: nodeModules
};
Run Code Online (Sandbox Code Playgroud)
注意:为了简洁起见,我已经大大简化了代码示例
目前,当我运行webpack命令时,我得到一个webpack_bundle包含index.js文件的文件夹- index.js文件包含依赖项config.js和read.js.但是,我想要的是将read.js依赖项捆绑到index.js文件中,但config.js依赖于将外部保留在捆绑的webpack输出所需的单独文件中.因此,webpack_bundle运行webpack命令后文件夹应包含两个文件- index.js和config.js.我已经尝试通过将以下键值添加到外部对象来修改外部,config: './config.js'但这不起作用.我还通过将config.js指定为入口点来创建了一个额外的入口点,但这也没有用.我无法弄明白这一点,webpack文档对于如何实现这一目标并不清楚.请帮忙!
如果您希望将配置放在单独的包中,可以通过以下方式动态导入文件来创建拆分点:config.jsrequire.ensure
require.ensure([], function() {
let config = require('./config.js');
});
Run Code Online (Sandbox Code Playgroud)
然后,您的配置将在一个单独的包中.
编辑:
如果您不希望Webpack捆绑您的配置文件,我认为您可以使用IgnorePlugin:
module.exports = {
//...
plugins: [new webpack.IgnorePlugin(/^\.\/config\.js$/)]
}
Run Code Online (Sandbox Code Playgroud)
并使用copy-webpack-plugin复制您的config.js文件.
| 归档时间: |
|
| 查看次数: |
4160 次 |
| 最近记录: |