作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我使用 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'))
最佳答案
从入口点移除 vendor 。即使在语义上也是错误的。
这些文件名旨在防止代码重复。如果您有真正的第二个入口点:anotheApp.js,您将有两个选择将什么加载到页面:
将每个包放入自己的缝隙:
optimization: { runtimeChunk: 'single', splitChunks: {
chunks: 'all',
maxInitialRequests: infinity,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
vendorname(v) {
var name = v.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
return `npm.${name.replace('@', '_')}`;
},
},
},
关于javascript - webpack split js 文件,再得到一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52623038/
我有本地更改和远程更改。 有人告诉我必须先推,再 pull 。这背后有什么原因吗? 最佳答案 那个人错了:正确的模型是pull-before-you-push,而不是相反。 当您pull时,git 将
我正在使用最新版本的 Flat UI Pro 1.3.2 ( http://designmodo.com/flat/ ),jQuery 插件 flatui-radiocheck v0.1.0 和 iO
我是一名优秀的程序员,十分优秀!