【问题标题】:Webpack output multiple scss files (one compiled and one not compiled)webpack输出多个scss文件(一个编译一个未编译)
【发布时间】:2022-09-27 13:00:54
【问题描述】:

是否可以配置 webpack 使其在已编译的 scss 旁边输出未编译的 scss 文件?

我希望以这种方式公开 scss 变量、mixin 和函数。

文件:

styles/
  index.scss

webpack.config.js:

module.exports = {
  entry: path.resolve(__dirname, \"./src/index.js\"),
  plugins: [
    new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // both options are optional
      filename: \"./lib/index.css\",
    }),
  ],
  module: {
    rules: [
      {
        test: /\\.scss$/i,
        use: [
          MiniCssExtractPlugin.loader,
          \"css-loader\",
          {
            loader: require.resolve(\"sass-loader\"),
            options: {
              warnRuleAsWarning: true,
              sourceMap: true,
              sassOptions: {
                includePaths: [\"../../node_modules\"],
                outputStyle: \"compressed\",
                outFile: path.resolve(__dirname, \"./lib/index.css\"),
              },
            },
          },
        ],
      }
    ]
  },
  resolve: {
    extensions: [\".tsx\", \".ts\", \".js\", \".css\", \".scss\"],
  },
  output: {
    filename: \"[name].js\",
    library: {
      type: \"umd\",
      name: \"design-system\",
    }
  }
};

示例输出:

lib/
  index.css (compiled version)
  index.scss (uncompiled version)

    标签: webpack sass sass-loader


    【解决方案1】:

    我认为除非你使用不同的插件,否则这是不可能的,所以我建议在这个用例中使用copy-webpack-plugin

    // npm i -D copy-webpack-plugin
    // yarn add -D copy-webpack-plugin
    
    const CopyPlugin = require("copy-webpack-plugin");
    
    module.exports = {
      // ...
      plugins: [
        new CopyPlugin({
          patterns: [
            { from: "./styles/index.scss", to: "./lib/" },
          ],
        }),
      ],
    };
    

    【讨论】:

      猜你喜欢
      • 2020-10-25
      • 2019-10-31
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 1970-01-01
      相关资源
      最近更新 更多