Webpack输出多个scss文件(一个已编译,一个未编译)

mm9b1k5b  于 2022-11-13  发布在  Webpack
关注(0)|答案(1)|浏览(176)

是否可以配置webpack,使其输出未编译的scss文件和已编译的scss文件?
我希望以这种方式公开scss变量、mixin和函数。

档案:

styles/
  index.scss

网络包配置.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)
mccptt67

mccptt671#

我认为这是不可能的,除非你为它使用一个不同的插件,所以我建议在这个用例中使用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/" },
      ],
    }),
  ],
};

相关问题