vue build的时候app css CssSyntaxError错误,run dev可以正常启动
错误信息如下
- building for production...Error processing file: static/css/app.97c058584faa589ff57590333e2787e8.css
(node:3172) UnhandledPromiseRejectionWarning: CssSyntaxError: G:\vue\recms-adminVue\static\css\app.97c058584faa589ff57590333e2787e8.css:1293:8: Unknown word
at Input.error (G:\vue\recms-adminVue\node_modules\_postcss@7.0.17@postcss\lib\input.js:130:16)
at Parser.unknownWord (G:\vue\recms-adminVue\node_modules\_postcss@7.0.17@postcss\lib\parser.js:563:22)
at Parser.decl (G:\vue\recms-adminVue\node_modules\_postcss@7.0.17@postcss\lib\parser.js:235:16)
at Parser.other (G:\vue\recms-adminVue\node_modules\_postcss@7.0.17@postcss\lib\parser.js:133:18)
at Parser.parse (G:\vue\recms-adminVue\node_modules\_postcss@7.0.17@postcss\lib\parser.js:77:16)
at parse (G:\vue\recms-adminVue\node_modules\_postcss@7.0.17@postcss\lib\parse.js:17:12)
at new LazyResult (G:\vue\recms-adminVue\node_modules\_postcss@7.0.17@postcss\lib\lazy-result.js:60:16)
at Processor.<anonymous> (G:\vue\recms-adminVue\node_modules\_postcss@7.0.17@postcss\lib\processor.js:138:12)
at Processor.process (G:\vue\recms-adminVue\node_modules\_postcss@7.0.17@postcss\lib\processor.js:117:23)
at Function.creator.process (G:\vue\recms-adminVue\node_modules\_postcss@7.0.17@postcss\lib\postcss.js:148:43)
at OptimizeCssAssetsPlugin.processCss (G:\vue\recms-adminVue\node_modules\_optimize-css-assets-webpack-plugin@3.2.1@optimize-css-assets-webpack-plugin\index.js
:63:19)
at Object.processor (G:\vue\recms-adminVue\node_modules\_optimize-css-assets-webpack-plugin@3.2.1@optimize-css-assets-webpack-plugin\index.js:29:23)
at G:\vue\recms-adminVue\node_modules\_last-call-webpack-plugin@2.1.2@last-call-webpack-plugin\index.js:139:8
at arrayEach (G:\vue\recms-adminVue\node_modules\_lodash@4.17.14@lodash\_arrayEach.js:15:9)
at forEach (G:\vue\recms-adminVue\node_modules\_lodash@4.17.14@lodash\forEach.js:38:10)
at LastCallWebpackPlugin.process (G:\vue\recms-adminVue\node_modules\_last-call-webpack-plugin@2.1.2@last-call-webpack-plugin\index.js:136:3)
(node:3172) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch blo
ck, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:3172) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the No
de.js process with a non-zero exit code.
build.js的代码
const ora = require('ora')
const rm = require('rimraf')
const path = require('path')
const chalk = require('chalk')
const webpack = require('webpack')
const config = require('../config')
const webpackConfig = require('./webpack.prod.conf')
const spinner = ora('building for production...')
spinner.start()
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
if (err) throw err
webpack(webpackConfig, function (err, stats) {
spinner.stop()
if (err) throw err
process.stdout.write(stats.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
}) + '\n\n')
if (stats.hasErrors()) {
console.log(chalk.red(' Build failed with errors.\n'))
process.exit(1)
}
console.log(chalk.cyan(' Build complete.\n'))
console.log(chalk.yellow(
' Tip: built files are meant to be served over an HTTP server.\n' +
' Opening index.html over file:// won\'t work.\n'
))
})
})
webpack.prod.conf.js的配置
const env = require('../config/prod.env')
const webpackConfig = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({
sourceMap: config.build.productionSourceMap,
extract: true,
usePostCSS: true
})
},
devtool: config.build.productionSourceMap ? config.build.devtool : false,
output: {
path: config.build.assetsRoot,
filename: utils.assetsPath('js/[name].[chunkhash].js'),
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
},
plugins: [
// http://vuejs.github.io/vue-loader/en/workflow/production.html
new webpack.DefinePlugin({
'process.env': env
}),
// UglifyJs do not support ES6+, you can also use babel-minify for better treeshaking: https://github.com/babel/minify
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
sourceMap: config.build.productionSourceMap,
parallel: true
}),
// extract css into its own file
new ExtractTextPlugin({
filename: utils.assetsPath('css/[name].[contenthash].css'),
// set the following option to `true` if you want to extract CSS from
// codesplit chunks into this main css file as well.
// This will result in *all* of your app's CSS being loaded upfront.
allChunks: false,
}),
// Compress extracted CSS. We are using this plugin so that possible
// duplicated CSS from different components can be deduped.
new OptimizeCSSPlugin({
cssProcessorOptions: config.build.productionSourceMap
? { safe: true, map: { inline: false } }
: { safe: true }
}),
// generate dist index.html with correct asset hash for caching.
// you can customize output by editing /index.html
// see https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: config.build.index,
template: 'index.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency'
}),
// keep module.id stable when vender modules does not change
new webpack.HashedModuleIdsPlugin(),
// enable scope hoisting
new webpack.optimize.ModuleConcatenationPlugin(),
// split vendor js into its own file
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module) {
// any required modules inside node_modules are extracted to vendor
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf(
path.join(__dirname, '../node_modules')
) === 0
)
}
}),
// extract webpack runtime and module manifest to its own file in order to
// prevent vendor hash from being updated whenever app bundle is updated
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
minChunks: Infinity
}),
// This instance extracts shared chunks from code splitted chunks and bundles them
// in a separate chunk, similar to the vendor chunk
// see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
new webpack.optimize.CommonsChunkPlugin({
name: 'app',
async: 'vendor-async',
children: true,
minChunks: 3
}),
// copy custom static assets
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
}
])
]
})
if (config.build.productionGzip) {
const CompressionWebpackPlugin = require('compression-webpack-plugin')
webpackConfig.plugins.push(
new CompressionWebpackPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp(
'\\.(' +
config.build.productionGzipExtensions.join('|') +
')$'
),
threshold: 10240,
minRatio: 0.8
})
)
}
if (config.build.bundleAnalyzerReport) {
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}
module.exports = webpackConfig
我今天也遇到 了同样的问题,一个个排除后,居然发现是因为,有一个文件里面的一句css代码 我用了 ‘//’来注释了。 去掉之后 ,打包就通过了
感谢,对,最终解决方案是:
先注释webpack.prod.conf.js的
//const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
//new OptimizeCSSPlugin({
// cssProcessorOptions: config.build.productionSourceMap
//? { safe: true, map: { inline: false } }
// : { safe: true }
//}),
PS:OptimizeCSSPlugin 是用来优化和压缩文件的,注释掉就不会压缩,再次build打包的时候可以看到dist目录下多了很多文件。
然后测试,再执行一次npm run build,在dist/static/css目录下看到css文件有报错,定位到报错的哪一行,就可以发现有一个文件里面的一句css代码,用了 ‘//’来注释了,
其实是不可以这样子注释的,我们找到这个原文件,把css前的//去掉,然后把刚才注释的//const OptimizeCSSPlugin恢复,重新npm run build就可以了
那问题来了,为什么突然就有这个问题,之前我的项目是可以正常跑起来的,今天从git上拉取回来就出这个问题。
探索了原因:
1.首先看看OptimizeCSSPlugin 这个插件是那里引入,查看package.json,搜索到"optimize-css-assets-webpack-plugin": {"version": "3.2.0"},但是在package-lock.json看到optimize-css-assets-webpack-plugin": {"version": "3.2.1"}
2.发现package.json和package-lock.json的optimize-css-assets-webpack-plugin插件版本没对上
3.分析发现,optimize-css-assets-webpack-plugin-3.2.0没有严格校验,所以可以打包通过,3.2.1有严格校验,所以打包出异常了
4.再次分析,为什么重新执行了npm install,会出现package.json和package-lock.json的依赖版本对不上?正常来说是一致的
5.再查询资料,结论是:npm或者cnpm 安装依赖,不会完全按照package.json中的版本号来,会有稍微的差异,这样的差异可能导致项目起不来,或者报错, 因为某些包只有特定的版本才能正常运行。
https://www.cnblogs.com/YKSlu/p/7131694.html
1、暂时解决办法
webpack.prod.conf.js
//const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
解决办法,暂时注释掉
//const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
//new OptimizeCSSPlugin({
// cssProcessorOptions: config.build.productionSourceMap
//? { safe: true, map: { inline: false } }
// : { safe: true }
//}),