当我使用 webpack 4 拆分 js 文件时,我得到 dist 目录包含:app.js、vendor.js 和另外一个 js 文件名 vendor~app.js,它是如何以及为什么出现的?
以下是这些文件:
这是我的 webpack 配置:
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
module.exports = {
mode: 'production',
entry: {
vendor: ['react', 'react-dom', 'redux', 'react-redux', 'react-router-dom'],
app: './src/entry.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
chunkFilename: '[name].js',
publicPath: '/',
filename: '[name].js'
},
module: {
rules: [
{
test: /\.js[x]?$/,
exclude: /node_modules/,
include: path.resolve(__dirname, 'src'),
use: [{
loader: 'babel-loader',
options: {
cacheDirectory: true,
babelrc: false,
presets: [['@babel/preset-env', {
"modules": false
}], '@babel/preset-react'],
plugins: ['@babel/plugin-transform-runtime', "@babel/plugin-proposal-class-properties"]
}
}]
},
]
},
optimization: {
runtimeChunk: {
name: 'manifest'
},
splitChunks: {
minChunks: 1,
minSize: 2,
chunks: 'all',
name: true,
cacheGroups: {
common: {
test: 'vendor',
chunks: 'initial',
name: 'vendor',
enforce: true,
}
}
}
},
plugins: [
new CleanWebpackPlugin(['dist']),
]
}
在我的 entry.js 中:
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
class App extends Component {
constructor (props) {
super(props);
}
render () {
return (
<div>Hello World</div>
)
}
}
ReactDOM.render(<App/>, document.getElementById('app'))