升级到 Webpack 4 和 babel-preset-env 后,我遇到了一个奇怪的问题。当我将 es2015 作为预设删除时,它不会分离 css。其他一切都正常编译,但除非我包含该预设,否则不会编译 css 文件。

我的 .babelrc 文件:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "modules": false,
        "useBuiltIns": "usage",
        "targets": "> 5%, last 2 versions",
        "forceAllTransforms": true
      }
    ],
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-object-rest-spread",
    ["@babel/plugin-proposal-decorators", { "legacy": true, "decoratorsLegacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true}],
    [
      "module-resolver",
      {
        "root": [
          "./src"
        ]
      }
    ]
  ],
  "env": {
    "development": {
      "plugins": [
        "react-hot-loader/babel"
      ]
    },
    "production": {
      "plugins": [
        "transform-imports"
      ]
    }
  }
}

我的 webpack 配置:
module.exports = {
  mode: 'production',
  entry: ['babel-polyfill', path.resolve(__dirname,'./src/index.js')],
  output: {
    chunkFilename: '[name].[chunkhash:4].bundle.js',
    filename: '[name].[chunkhash:4].bundle.js',
    publicPath: '/'
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: './src/index.html',
      filename: './index.html'
    }),
    new CleanWebpackPlugin(['dist/']),
    new webpack.optimize.ModuleConcatenationPlugin(),
    new MiniCssExtractPlugin({
      filename: '[name].[contenthash:4].css'
    })
  ],
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: [path.resolve(__dirname, './node_modules/')],
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: { minimize: false }
          }
        ]
      },
      {
        test: /\.(sass|scss|css)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: 'sass-loader',
            options: {
              includePaths: ['./node_modules'],
              sourceMap: true
            }
          }
        ]
      }
    ]
  },
  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        uglifyOptions: {
          keep_fnames: true,
          keep_classnames: true,
          compress: {
            dead_code: true,
            drop_console: true
          },
          mangle: {
            keep_fnames: true
          }
        },
        cache: true,
        parallel: true,
        sourceMap: true
      }),
      new OptimizeCSSAssetsPlugin({})
    ],
    runtimeChunk: false,
    splitChunks: {
      cacheGroups: {
        styles: {
          name: 'styles',
          test: /\.css$/,
          chunks: 'all',
          enforce: true
        },
        commons: {
          test: /[\\/]node_modules[\\/].*js/,
          name: 'vendor',
          chunks: 'all',
          minChunks: 2
        }
      },
      chunks: 'all'
    }
  }
};

我可以不理会它,但是包含 es2015 也会阻止创建正确的块。

最佳答案

我首先安装最新的 deps,它为 Babel 移动到一个新的命名空间下 npm i @babel/core @babel/preset-env @babel/preset-react babel-loader@latestnpm un babel-core babel-preset-env babel-preset-react ,同时更新 .babelrc 以反射(reflect) "presets": ["@babel/preset-env", ...] 等的变化。

然后你真正需要做的是将你的两个 plugins 属性合并为一个......现在你的配置是:

config = {
  ...
  plugins: [],
  ...
  plugins: [],
  ...
}

第二组插件可能会覆盖 MiniCssExtractPlugin

关于javascript - Webpack 4 在没有 babel-preset-es2015 的情况下不会创建 CSS 文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52106713/

10-16 00:55