我正在创建我的第一个reactjs
库,它使用material-ui和react-google-charts以及许多其他 3rd 方库。我使用 webpack(版本 3.5.6)作为我的捆绑器,到目前为止我有以下webpack.config.js
文件......
const webpack = require('webpack')
const path = require('path')
const BUILD_DIR = path.resolve(__dirname, 'lib')
const APP_DIR = path.resolve(__dirname, 'src')
const WebpackConfig = {
entry: {
app: APP_DIR + '/index.js',
},
output: {
path: BUILD_DIR,
filename: 'index.js',
libraryTarget: 'umd',
library: 'TestComponent'
},
module: {
loaders: [
{
loader: 'babel-loader',
test: /.js$/,
exclude: /node_modules/,
include : APP_DIR,
options: {
presets: [ 'react', 'es2015', 'stage-2' ]
}
}
],
},
}
// webpack production config.
if ( process.env.NODE_ENV === 'production' ) {
WebpackConfig.externals = {
'react': 'react',
'react-dom': 'react-dom'
}
WebpackConfig.plugins = [
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.optimize.UglifyJsPlugin({
beautify: false,
mangle: {
screw_ie8: true,
},
compress: {
warnings: false,
screw_ie8: true
},
comments: false
}),
]
}
module.exports = WebpackConfig
在生产中,我已将react
和标记react-dom
为“外部”,因为我不希望它们被捆绑在我的库中,可以安全地假设将使用该项目的任何项目也将具有reactjs
.
我如何处理使用该库的任何项目中可能存在或不存在的其他 3rd 方库?我应该从捆绑包中排除所有第 3 方库吗?如果是,如何确保使用我的库的任何项目都可以找到它们?