javascript - 如何将每个 scss 文件编译到其自己的文件夹中而不将它们合并为一个?

标签 javascript css node.js webpack sass

我有 Angular 组件。每个组件在其文件夹中都有一个 CSS 文件。我使用 SCSS 预处理器,所以我需要让 Webpack 只编译 SCSS 文件而不将它们合并为一个。例如


Before:
src
|-app
  |_component1
  | component1.html
  | component1.ts
  | component1.scss
  |
  |_component2
  | component2.html
  | component2.ts
  | component2.scss


After:
src
|-app
  |_component1
  | component1.html
  | component1.ts
  | component1.scss
  | component1.css
  |
  |_component2
  | component2.html
  | component2.ts
  | component2.scss
  |  component2.css

最佳答案

您可以通过在您的 webpack 中进行以下配置来实现这一点

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
{
...
  plugins: [
    ...
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    })
    ...
  ],
  module: {
    rules: [
       ...
      {
        test: /\.(css|scss)$/,
        use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"]
      }
      ...
    ]
  }
...
};

关于javascript - 如何将每个 scss 文件编译到其自己的文件夹中而不将它们合并为一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57468252/

相关文章:

jquery - 使用 JQuery 更改跨度文本的颜色

javascript - 定义的函数坚持其未定义的

node.js - 如何将 NODE_ENV=production 传递给 pm2?

javascript - 在javascript中使用相同的id获取不同的值

c# - 可以在使用 asp.net 按钮回发后运行 javascript 函数吗?

javascript - jQuery 正则表达式测试

node.js - HTTP 范围 header Openshift 可扩展应用程序

javascript - 正则表达式 - 如何匹配整个字符串并仅捕获具有一定长度的子组

html - .htaccess - 隐藏包含文件的文件夹

.net - 自动宽度和溢出 div