Webpack - use CopyWebpackPlugin for scss files

[亡魂溺海] 提交于 2019-12-10 09:56:34

问题


I want to convert some files from *.scss extension to *.css one and then copy them to the target folder .

These files aren't a part of a module so I can't import them by the *.scss loader (i.e import "myStyle.scss").

I know how to copy files by the CopyWebpackPlugin plugin -

plugins: [
    new CopyWebpackPlugin([    
        { from: 'source/myStyle.scss', to: 'myStyle.css' }
    ])
]

So now all I need is to add it a conversion from scss to css before to copying .

How could I achieve that ?


回答1:


You can use transform option of CopyWebpackPlugin plugin in conjunction with node-sass. I.e. use sass.renderSync function from node-sass:

const sass = require('node-sass');

plugins: [
  new CopyWebpackPlugin(
    [
      {
        from: 'style.scss',
        to: 'style.css',
        transform (content, path) {
          const result = sass.renderSync({
            file: path
          });

          return result.css.toString();
        },
      }
    ],
  ),
]



回答2:


You'll need sass-loader for that. For more info, check out https://github.com/webpack-contrib/sass-loader

Update: Try something like this.

  module: {
    rules: [
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          use: ['css-loader', 'sass-loader']
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('myStyle.css')
  ]


来源:https://stackoverflow.com/questions/45395132/webpack-use-copywebpackplugin-for-scss-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!