【发布时间】:2020-10-03 00:39:48
【问题描述】:
我的 webpack 配置大部分都可以工作,但生产版本没有应用 css 样式。
如果我使用此节点脚本 webpack --mode development 运行 webpack,然后在我的浏览器中打开 ./dist/index.html 文件,网站将按预期运行并且内容样式正确。
如果我使用此节点脚本 webpack --mode production 运行 webpack,然后在我的浏览器中打开 ./dist/index.html 文件,则会出现网站内容,但根本没有对内容应用任何样式。
webpack 开发服务器与样式化内容、实时更新等完美配合。
我修改了我的webpack.config.js 以注释掉生产特定的设置,以便生产和开发构建使用完全相同的 webpack 配置。我很难解释为什么开发版本有效,但生产版本甚至没有相同的配置。关于如何调试问题的任何想法?
我的webpack.config.js 看起来像这样:
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = (env, { mode }) => {
var config = {
entry: {
app: './src/app.js',
react: 'react',
},
devtool: 'none',
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
hot: true,
port: 9000,
},
resolve: {
extensions: ['.js', '.jsx', '.css', '.scss', '.sass'],
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.[hash].js',
chunkFilename: '[name].[contenthash].js',
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
},
plugins: [
new HtmlWebpackPlugin({
minify: false,
template: require('html-webpack-template'),
inject: true,
title: 'Octopedia',
favicon: './public/images/favicon/favicon.ico',
appMountId: 'app',
meta: [{ name: 'viewport', content: 'width=device-width, initial-scale=1.0' }],
links: [{ href: '/site.webmanifest', rel: 'manifest' }],
appMountHtmlSnippet: '<div class="app-spinner"></div>',
headHtmlSnippet:
'<style>div.app-spinner{position:fixed;top:50%;left:50%;border:16px solid #f3f3f3;border-top:16px solid #3498db;border-radius:50%;width:120px;height:120px;margin:-60px 0 0 -60px;z-index:1;animation:spin 2s linear infinite;}@keyframes spin{0%{transform:rotate(0deg);}100%{transform:rotate(360deg);}}</style >',
}),
new CopyWebpackPlugin({
patterns: [{ from: 'public' }],
}),
],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
cacheCompression: false,
presets: ['@babel/preset-env', '@babel/preset-react'],
},
},
},
],
},
};
// if (mode === 'development') {
config.mode = 'development';
config.devtool = 'eval-source-map';
config.plugins.push(new webpack.HotModuleReplacementPlugin());
config.module.rules.push({
test: /\.(sass|scss|css)/,
exclude: /node_modules/,
use: ['style-loader', 'css-loader', 'sass-loader'],
});
// } else {
// config.mode = 'production';
// config.devtool = 'none';
// config.plugins.push(new MiniCssExtractPlugin({ filename: 'index.css' }));
// config.module.rules.push({
// test: /\.(sass|scss|css)/,
// exclude: /node_modules/,
// use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
// });
// }
return config;
};
webpack 在开发模式下的输出如下所示:
Asset Size Chunks Chunk Names
app.6335bee170373d18f82e.js 39.8 KiB app [emitted] [immutable] app
favicon.ico 15 KiB [emitted]
images/.DS_Store 6 KiB [emitted]
images/favicon/.DS_Store 6 KiB [emitted]
images/favicon/android-chrome-192x192.png 8.1 KiB [emitted]
images/favicon/android-chrome-512x512.png 23.3 KiB [emitted]
images/favicon/apple-touch-icon.png 7.25 KiB [emitted]
images/favicon/favicon-16x16.png 534 bytes [emitted]
images/favicon/favicon-32x32.png 1.06 KiB [emitted]
images/favicon/favicon.ico 15 KiB [emitted]
index.87da9b99395c61fa6b02.js 33.4 KiB runtime [emitted] [immutable] runtime
index.html 1.04 KiB [emitted]
react.a0fc75f932f5dd9cdda1.js 133 bytes react [emitted] [immutable] react
site.webmanifest 447 bytes [emitted]
vendors.404f5d4ed29b35b04b91.js 2.41 MiB vendors [emitted] [immutable] vendors
Entrypoint app = index.87da9b99395c61fa6b02.js vendors.404f5d4ed29b35b04b91.js app.6335bee170373d18f82e.js
Entrypoint react = index.87da9b99395c61fa6b02.js vendors.404f5d4ed29b35b04b91.js react.a0fc75f932f5dd9cdda1.js
生产模式下 webpack 的输出如下所示:
Asset Size Chunks Chunk Names
app.170943a8008bc467199f.js 2.77 KiB 2 [emitted] [immutable] app
favicon.ico 15 KiB [emitted]
images/.DS_Store 6 KiB [emitted]
images/favicon/.DS_Store 6 KiB [emitted]
images/favicon/android-chrome-192x192.png 8.1 KiB [emitted]
images/favicon/android-chrome-512x512.png 23.3 KiB [emitted]
images/favicon/apple-touch-icon.png 7.25 KiB [emitted]
images/favicon/favicon-16x16.png 534 bytes [emitted]
images/favicon/favicon-32x32.png 1.06 KiB [emitted]
images/favicon/favicon.ico 15 KiB [emitted]
index.a162f7e38dd204127c0f.js 9.03 KiB 0 [emitted] [immutable] runtime
index.html 1.04 KiB [emitted]
react.f87e0cf2654ac4f74576.js 71 bytes 3 [emitted] [immutable] react
site.webmanifest 447 bytes [emitted]
vendors.dd9d4a26464d8d880c74.js 352 KiB 1 [emitted] [immutable] [big] vendors
【问题讨论】:
-
我刚刚发现我可以做 'webpack --config webpack.config.js --mode production --profile --json > compilation-stats.json'。生成的compilation-stats.json 没有错误,并且对scss 文件有很多引用。什么都没有跳出来。
-
从我今天的阅读来看,我认为这可能与摇树和模块副作用有关。因为没有任何东西直接引用 css 中的任何东西,所以 webpack 可能会将其作为优化删除。