首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >webpack:定制风格在生产建设后消失了。

webpack:定制风格在生产建设后消失了。
EN

Stack Overflow用户
提问于 2018-10-08 12:40:14
回答 1查看 6.4K关注 0票数 5

我更新了webpack的档案。现在,它不会将我的自定义样式应用于主样式包。没有错误,有自定义样式的类只是缺少在包中。当我使用development模式运行build时,一切都很好,而且我的样式都存在于包中。只有在production构建时才会出现这种情况。

我尝试了extract-text-webpack-plugin而不是mini-css-extract-plugin,但是结果是一样的--在推动构建时,我的样式丢失了。

我会非常乐意得到任何帮助的。

以下是文件:

webpack.config.js

代码语言:javascript
复制
const path = require('path');
const fs = require('fs');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const autoprefixer = require('autoprefixer');
const lessToJs = require('less-vars-to-js');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const publicPath = 'public';
const themeVariables = lessToJs(
  fs.readFileSync(path.join(__dirname, './assets/style/ant-theme-vars.less'), 'utf8'),
);

module.exports = (env, options) => {
  const mode = env ? env.mode : options.mode;
  return {
    entry: './assets/app.jsx',
    output: {
      path: path.resolve(__dirname, publicPath),
      filename: 'bundle.js',
    },
    module: {
      rules: [
        {
          test: /\.less$/,
          use: [
            MiniCssExtractPlugin.loader,
            {
              loader: 'css-loader',
            },
            {
              loader: 'postcss-loader',
              options: {
                plugins: [
                  autoprefixer({
                    browsers: 'last 2 version',
                  }),
                ],
              },
            },
            {
              loader: 'less-loader',
              options: {
                javascriptEnabled: true,
                modifyVars: themeVariables,
              },
            },
          ],
        },
        {
          test: /\.s?css$/,
          exclude: [/node_modules/],
          use: [
            MiniCssExtractPlugin.loader,
            {
              loader: 'css-loader',
            },
            {
              loader: 'postcss-loader',
              options: {
                plugins: [
                  autoprefixer({
                    browsers: 'last 2 version',
                  }),
                ],
              },
            },
            {
              loader: 'sass-loader',
            },
          ],
        },
        {
          test: /\.jsx?$/,
          exclude: [/node_modules/],
          loaders: ['babel-loader'],
          resolve: { extensions: ['.js', '.jsx'] },
        },
        {
          test: /\.(png|jpg|jpeg|gif|svg|ico)$/,
          exclude: [/node_modules/],
          use: [
            {
              loader: 'file-loader',
              options: {
                name: 'img/[name].[ext',
              },
            },
            {
              loader: 'image-webpack-loader',
              options: {
                mozjpeg: {
                  progressive: true,
                  quality: 70,
                },
              },
            },
          ],
        },
        {
          test: /\.(otf|ttf|woff|woff2)$/,
          exclude: [/node_modules/],
          loader: 'file-loader',
          options: {
            name: 'fonts/[name].[ext]',
          },
        },
      ],
    },
    plugins: [
      new CleanWebpackPlugin(publicPath, {}),
      new MiniCssExtractPlugin({
        filename: 'bundle.css',
      }),
      new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './assets/www/index.html',
      }),
    ],
    optimization: {
      minimizer: [
        new UglifyJsPlugin({
          cache: true,
          parallel: true,
          uglifyOptions: {
            compress: true,
            mangle: true,
            warnings: false,
            drop_console: true,
            unsafe: true,
          },
          sourceMap: true,
        }),
      ],
    },
    devServer: {
      contentBase: path.join(__dirname),
      compress: true,
      port: 9000,
      publicPath: '/',
      historyApiFallback: true,
    },
    devtool: mode === 'development' ? 'cheap-inline-module-source-map' : '',
  };
};

package.json

代码语言:javascript
复制
{
  "name": "react-templates",
  "version": "1.0.0",
  "description": "",
  "main": "bundle.js",
  "sideEffects": false,
  "scripts": {
    "build": "webpack --progress --mode production",
    "build-dev": "webpack -p --progress --mode development",
    "start": "webpack-dev-server --mode production --open",
    "eslint": "eslint . --ext .js --ext .jsx",
    "stylelint": "stylelint assets/scss",
    "deploy-current-branch-dev": "npm run build-dev && firebase deploy",
    "deploy-dev": "git checkout -- . && git clean -fd && git checkout develop && git remote update && git pull  && npm run build-dev && firebase deploy"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "antd": "3.8.2",
    "autoprefixer": "9.0.1",
    "brace": "0.11.1",
    "clean-webpack-plugin": "0.1.19",
    "extract-text-webpack-plugin": "4.0.0-beta.0",
    "fast-async": "^6.3.8",
    "file-system": "2.2.2",
    "firebase": "5.3.0",
    "history": "4.7.2",
    "html-webpack-plugin": "^3.2.0",
    "less-vars-to-js": "1.3.0",
    "lodash": "^4.17.11",
    "mini-css-extract-plugin": "^0.4.3",
    "moment": "2.22.2",
    "optimize-css-assets-webpack-plugin": "5.0.0",
    "prop-types": "15.6.2",
    "react": "15.6.1",
    "react-ace": "6.1.4",
    "react-copy-to-clipboard": "5.0.1",
    "react-dom": "15.6.1",
    "react-favicon": "0.0.14",
    "react-redux": "5.0.7",
    "react-router": "4.3.1",
    "react-router-dom": "4.3.1",
    "react-sticky": "^6.0.3",
    "redux": "4.0.0",
    "redux-devtools-extension": "2.13.5",
    "redux-logger": "3.0.6",
    "redux-thunk": "2.3.0",
    "uglifyjs-webpack-plugin": "^2.0.1",
    "webpack": "^4.16.1",
    "webpack-merge": "^4.1.4"
  },
  "devDependencies": {
    "@babel/core": "^7.1.2",
    "@babel/plugin-proposal-class-properties": "^7.1.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "babel-eslint": "8.2.6",
    "babel-loader": "^8.0.4",
    "babel-plugin-import": "^1.9.1",
    "css-loader": "1.0.0",
    "eslint": "4.19.1",
    "eslint-config-airbnb": "17.0.0",
    "eslint-plugin-import": "2.12.0",
    "eslint-plugin-jsx-a11y": "6.0.3",
    "eslint-plugin-react": "7.9.1",
    "file-loader": "^2.0.0",
    "husky": "1.0.0-rc.13",
    "image-webpack-loader": "^4.3.1",
    "less": "3.8.1",
    "less-loader": "4.1.0",
    "node-sass": "4.9.2",
    "postcss-loader": "2.1.6",
    "sass-loader": "7.0.3",
    "style-loader": "0.21.0",
    "stylelint": "9.4.0",
    "stylelint-config-recommended": "2.1.0",
    "webpack-bundle-analyzer": "^3.0.2",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.5"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm run eslint && npm run stylelint"
    }
  }
}

.babelrc

代码语言:javascript
复制
{
"presets": [
        "@babel/preset-env",
        "@babel/preset-react"
    ],
    "plugins": [
        "@babel/plugin-proposal-class-properties",
        [
            "import",
            {
                "libraryName": "antd",
                "style": true
            }
        ]
    ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-10 17:00:28

问题是sideEffects: false在我的package.json文件中。

我找到了一个关于Github的问题,有一些与它相关的问题。

我这么做的主要原因-我想在我的发展模式下做一个树干。它如我所料,但在生产模式下,我所有的定制样式都丢失了。为了解决这个问题,我刚刚删除了sideEffects: false。因此,我在开发模式中失去了树柄(认为在开发模式下使它不是一个好的解决方案,但无论如何)。

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52702536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档