如何有多个条目使用SAME CSS文件(而不是每个捆绑在一起都有自己的副本)

时间:2018-09-29 16:02:41

标签: css duplicates materialize bundling-and-minification webpack-4

我有使用materialize.min.css的popup.js,还有使用materialize.min.css(以及.js)的bookmarkManager.js。

在我当前的输出文件夹设置中,创建了2个捆绑包,每个捆绑包中都包含它的OWN副本materialize.min.css(它们是同一文件,大约200个附加基布)。

有没有一种方法可以重用CSS文件,而不是每个捆绑包都自己复制?在这种情况下,我想拥有一个materialize.min.css文件,并指向它多个HTML文件,以节省大小。

这是我当前的webpack.config:

var path = require('path');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var Buffer = require('buffer/').Buffer;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

var fileExtensions = [
  'jpg',
  'jpeg',
  'png',
  'gif',
  'eot',
  'otf',
  'svg',
  'ttf',
  'woff',
  'woff2'
];

var options = {
  entry: {
    popup: path.join(__dirname, 'src', 'js', 'popup.js'),
    options: path.join(__dirname, 'src', 'js', 'options.js'),
    background: path.join(__dirname, 'src', 'js', 'background.js'),
    bookmarkManager: path.join(__dirname, 'src', 'js', 'bookmarkManager.js')
  },
  output: {
    path: path.join(__dirname, 'build'),
    filename: '[name].bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader
          },
          'css-loader'
        ]
      },
      {
        test: new RegExp('.(' + fileExtensions.join('|') + ')$'),
        loader: 'file-loader?name=[name].[ext]',
        exclude: /node_modules/
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: { minimize: true }
          }
        ],
        exclude: /node_modules/
      }
    ]
  },
  plugins: [
    // clean the build folder
    new CleanWebpackPlugin(['build']),
    new CopyWebpackPlugin([
      {
        from: 'src/manifest.json',
        transform: function(content, path) {
          // generates the manifest file using the package.json informations
          return Buffer.from(
            JSON.stringify({
              description: process.env.npm_package_description,
              version: process.env.npm_package_version,
              ...JSON.parse(content.toString())
            })
          );
        }
      }
    ]),
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src', 'popup.html'),
      filename: 'popup.html',
      chunks: ['popup']
    }),
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src', 'options.html'),
      filename: 'options.html',
      chunks: ['options']
    }),
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src', 'background.html'),
      filename: 'background.html',
      chunks: ['background']
    }),
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src', 'bookmarkManager.html'),
      filename: 'bookmarkManager.html',
      chunks: ['bookmarkManager']
    }),
    new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // both options are optional
      filename: '[name].css',
      chunkFilename: '[id].css'
    })
  ]
};

module.exports = options;

奖金问题:我在文档中看到,如果要使用minicssextractplugin进行缩小,我还必须使用js缩小器,在该示例中是uglify js,这会打破通天塔吗?可以与babel一起使用吗?还是我应该使用其他东西?

0 个答案:

没有答案