-
Notifications
You must be signed in to change notification settings - Fork 8
fet升级到2.x版本迁移步骤
angrytoro edited this page Dec 4, 2019
·
4 revisions
- 执行
npm install -g npm-check
- 执行
npm-check -u
- 按空格键选择要升级的(特别注意:只升级编译需要用到的安装包,其他的不要升级,否则可能会引起你的项目出现问题。),选择完成之后,按enter键执行升级
- 由于
mini-css-extract-plugin
的bug,所以你只能先用比较低版本的,在升级完成之后,你要指定安装mini-css-extract-plugin
到0.6.0
版本,否则会报错
原先配置
"devDependencies": {
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.6",
"babel-loader": "^7.1.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-2": "^6.24.1",
"css-loader": "^0.28.7",
"eslint": "^5.2.0",
"extract-text-webpack-plugin": "^3.0.1",
"file-loader": "^1.1.5",
"mustache-loader": "^1.4.0",
"node-sass": "^4.6.1",
"sass-loader": "^4.1.1",
"style-loader": "^0.19.0",
"url-loader": "^0.5.9",
"webpack": "^3.5.5",
"webpack-merge": "^4.1.0"
}
升级后配置
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.6.3",
"@babel/runtime": "^7.6.3",
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
"css-loader": "^3.2.0",
"eslint": "^6.5.1",
"file-loader": "^4.2.0",
"mini-css-extract-plugin": "^0.6.0",
"mustache-loader": "^1.4.3",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.0",
"style-loader": "^1.0.0",
"url-loader": "^2.2.0",
"webpack": "^4.41.2",
"webpack-merge": "^4.2.2"
}
将原先ft.config.js
名字改成fet.config.js
将module.rules
中test
为/\.js$/
的配置改成
{
test: /\.js$/,
include: [resolve('src')], // 注意这行,resolve是你自己定义的函数,根据自己实际情况来写
use: {
loader: 'babel-loader',
options: {
presets: [[require.resolve('@babel/preset-env'),{
"modules": false,
"targets": {
"browsers": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
}]],
plugins: [require.resolve('@babel/plugin-transform-runtime')],
cacheDirectory: true
}
}
}
将.babelrc
配置删除
原先extract-text-webpack-plugin
插件废弃,换成mini-css-extract-plugin
,所以要引入mini-css-extract-plugin
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
- 将
test: /\.css$/
变更为
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
'css-loader'
]
}
- 变更对应的
sass
或者less
配置,以sass
为例,将配置变更成如下
{
test: /\.(scss|sass)$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
'css-loader',
'sass-loader'
]
}
将原先new ExtractTextPlugin(config.output.filename.replace('[ext]', '.css'))
删除,然后新增
new MiniCssExtractPlugin({
filename: config.output.filename.replace('[ext]', '.css'),
chunkFilename: config.output.filename.replace('[ext]', '.css')
})
编译的时候也需要对ccs中的图片依赖进行处理,之前处理是这样的
{
test: /\.(scss|sass)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
options: {
alias: {
'./images': resolve('images')
}
}
}, 'sass-loader']
})
有没有发现里面有个alias
配置,将css中的./images
指向resolve('images')
这个目录,升级之后,已经不能这么写了。
所以需要在fet.config.js
的cssConfig
配置加如下配置
cssConfig.resolve.alias = {
'./images': resolve('images')
}
特别注意:cssConfig
这个字段是根据fet.config.js
中的webpackConfig: function (jsConfig, cssConfig, options)
这个配置传过来的,所以名字要根据实际情况而定。
一定要删除掉没有用到的代码,以及没有引用到的组件,不要遗留冗余的代码