简体   繁体   English

Webpack 5 - 一些 styles 在生产中缺失,但在开发中存在

[英]Webpack 5 - some styles are missing in production but exist in development

I found that some of the styles are not applied in Production website, but working in development env(localhost).我发现一些 styles 没有应用于生产网站,而是在开发环境(本地主机)中工作。

But Inspecting in Browser Inspector > Sources, I found that in Production sources(css/main.c938xxxxx.css), it missed #roadmap.slick-slide{padding:0 18px}}但是在 Browser Inspector > Sources 中检查,我发现在 Production sources(css/main.c938xxxxx.css) 中,它错过了#roadmap.slick-slide{padding:0 18px}} 在此处输入图像描述

Which exist in localhost 存在于本地主机中  在此处输入图像描述

Not sure what's wrong for the production webpack setting.不确定生产 webpack 设置有什么问题。 I am using webpack 5我正在使用 webpack 5

Webpack.base.js Webpack.base.js

const webpack = require("webpack");

const utils = require("./utils");

const HtmlWebpackPlugin = require("html-webpack-plugin");
const BundleAnalyzerPlugin =
  require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const ProgressBarPlugin = require("progress-bar-webpack-plugin");
const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");

const chalk = require("chalk");
const smp = new SpeedMeasurePlugin();

module.exports = smp.wrap({
  entry: "./src/index.jsx",
  resolve: {
    alias: {
      "@": utils.resolve("src"),
    },
    extensions: ["*", ".js", ".jsx"],
    fallback: {…},
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        commons: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors',
          chunks: 'all',
        },
        styles: {
          name: 'styles',
          test: /\.css$/,
          chunks: 'all',
          enforce: true,
        },
      },
    }
  },
  module: {
    rules: [
      {
        test: /\.(sa|sc|c)ss$/,
        use: [
          {
            loader: "style-loader",
          },
          {
            loader: "css-loader",
          },
          {
            loader: "sass-loader",
          },
          {
            loader: "sass-resources-loader",
            options: {
              resources: ["./src/assets/scss/main.scss"],
            },
          },
        ],
      },
      {
        test: /\.(png|jpg|gif|svg)$/i,
        type: "asset",
        parser: {
          dataUrlCondition: {
            maxSize: 8192
          }
        }
      },
      {
        test: /\.(ico)$/,
        exclude: /node_modules/,
        use: ["file-loader?name=[name].[ext]"], // ?name=[name].[ext] is only necessary to preserve the original file name
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "public/index.html",
      filename: "index.html",
      favicon: "public/favicon.ico",
      manifest: "public/manifest.json",
      inject: true,
    }),
    new webpack.ProvidePlugin({
      Buffer: ["buffer", "Buffer"],
    }),
    new webpack.ProvidePlugin({
      process: "process/browser",
    }),
    new ProgressBarPlugin({
      format: `  :msg [:bar] ${chalk.green.bold(":percent")} (:elapsed s)`,
    }),
  ],
});

webpack.dev.js webpack.dev.js

const { merge } = require("webpack-merge");
const path = require("path");

const base = require("./webpack.base");
const utils = require("./utils");

const Dotenv = require("dotenv-webpack");

module.exports = merge(base, {
  mode: "development",
  devtool: "inline-source-map",
  output: {
    publicPath: '/',
    assetModuleFilename: (pathData) => {
      const filepath = path
        .dirname(pathData.filename)
        .split("/")
        .slice(1)
        .join("/");
      return `${filepath}/[name].[hash][ext][query]`;
    },
  },
  devServer: {
    port: 3300,
    static: [
      { directory: utils.resolve("dist") },
      { directory: utils.resolve("public") },
    ],
  },
  plugins: [new Dotenv({ path: "./.env.dev })],
});

webpack.uat.js webpack.uat.js

const { merge } = require("webpack-merge");
const base = require("./webpack.base");
const path = require("path");

const Dotenv = require("dotenv-webpack");

const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const CompressionPlugin = require("compression-webpack-plugin");

const productionGzipExtensions = ["js", "css"];

module.exports = merge(base, {
  mode: "production",
  output: {
    path: path.join(__dirname, "../dist"),
    filename: "js/[name].[contenthash].js",
    assetModuleFilename: (pathData) => {
      const filepath = path
        .dirname(pathData.filename)
        .split("/")
        .slice(1)
        .join("/");
      return `${filepath}/[name].[hash][ext][query]`;
    },
  },
  optimization: {
    minimizer: [`...`, new CssMinimizerPlugin()],
  },
  module: {
    rules: [
      {
        test: /\.(sa|sc|c)ss$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: "css-loader",
          },
          "sass-loader",
          {
            loader: "sass-resources-loader",
            options: {
              resources: ["./src/assets/scss/main.scss"],
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new Dotenv({ path: "./.env.uat" }),
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin({
      filename: "css/[name].[contenthash].css",
    }),
    new CompressionPlugin({
      filename: "[path][name].gz[query]",
      algorithm: "gzip",
      test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
      threshold: 10240,
      minRatio: 0.8,
    })
  ],
});

utils.js实用程序.js

const path = require('path')

module.exports = {
  resolve: function(dir) {
    return path.join(__dirname, '..', dir)
  }
}

Add "style-loader" in webpack.uat.js file.在 webpack.uat.js 文件中添加“style-loader”。

rules: [
      {
        test: /\.(sa|sc|c)ss$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: "style-loader",
          },
          {
            loader: "css-loader",
          },
          "sass-loader",
          {
            loader: "sass-resources-loader",
            options: {
              resources: ["./src/assets/scss/main.scss"],
            },
          },
        ],
      },
    ],

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vue和Webpack:全球开发和生产变量 - Vue & Webpack: Global development and production variables webpack:在生产构建后缺少自定义样式 - webpack: custom style is missing after production build webpack开发和生产构建模式之间有什么区别? - What are the differences between webpack development and production build modes? webpack production build 总是返回 这个页面使用的是 React 的开发 build - Webpack production build always returns This page is using the development build of React Rails Webpack 错误:自定义 JS 在开发中解决,但在生产中未解决 - Rails Webpack Error: Custom JS resolves in Development but not in Production Babel / Webpack:找不到“ yaml-loader”(开发中的产品,而非生产中的产品) - Babel / Webpack: Cannot find “yaml-loader” (works in development, not production) webpack-如何在生产和开发环境中使用不同的变量 - webpack - how to have different variable for production and development environment 在webpack生产模式下发生错误,但在开发模式下运行良好 - An error happens in webpack production mode, but but works well at development mode 为什么在开发和生产模式下运行Webpack时复制的文件不同 - Why are copied files different when running Webpack in development and production modes `export` 或 `import` 在 webpack 生产与开发模式下的行为不同 - `export` or `import` behaving different in webpack production than in development mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM