1

我正在尝试将我的两个.scss文件转换为两个不同的.css文件:一个名为timer_player.css,另一个名为style.css.

style.css不能有任何timer_player.css风格。

ExtractTextPlugin我可以通过创建两个实例来做到这一点,但这个包已被弃用。

我的文件夹树看起来像这样:

- src
  - js
    - popup.js
    - content_scripts
      - timer_player
        - index.js
  - css
    - popup.css
    - timer_player.css
4

1 回答 1

0

经过长时间阅读此问题后,我能够做到这一点。部分技巧是使用optimization属性并按块名称过滤。

我的 webpack.config.js 现在看起来像这样:

module.exports = {
  entry: {
    popup: ['./src/js/popup.js', './src/css/popup.scss'],
    timer_player: [
      './src/js/content_scripts/timer_player/index.js',
      './src/css/timer_player.scss',
    ],
  },
  output: {
    publicPath: '.',
    path: resolve(__dirname, 'dist/'),
    filename: '[name].js',
    libraryTarget: 'umd',
  },
  plugins: [
    new WebpackReloaderPlugin({
      port: 9090, // Which port use to create the server
      reloadPage: true, // Force the reload of the page also
      entries: {
        // The entries used for the content/background scripts
        contentScript: 'popup', // Use the entry names, not the file name or the path
        background: 'background', // *REQUIRED
      },
    }),

    new MiniCssExtractPlugin({ filename: '[name].css' }),
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src', 'popup.html'),
      filename: 'popup.html',
      chunks: ['popup'],
    }),
    new CopyWebpackPlugin([
      { from: './src/manifest.json' },
      { from: './src/images', to: 'images' },
    ]),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
    }),
  ],
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.scss$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader', // translates CSS into CommonJS
          {
            loader: 'sass-loader',
            options: {
              includePaths: ['node_modules', 'node_modules/@material/*']
                .map(d => path.join(__dirname, d))
                .map(g => glob.sync(g))
                .reduce((a, c) => a.concat(c), []),
            },
          },
        ],
      },
      {
        test: /\.txt$/,
        use: 'raw-loader',
      },
    ],
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        extractPopupStyles: {
          name: 'style',
          chunks: chunk => chunk.name === 'popup',
        },
      },
    },
  },
};

欢迎任何建议

于 2019-09-17T19:05:32.657 回答