Webpack - use CopyWebpackPlugin for scss files
Asked Answered
F

2

5

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 ?

Filamentary answered 29/7, 2017 at 23:58 Comment(0)
A
8

You can use transform option of CopyWebpackPlugin plugin in conjunction with sass. I.e. use the sass.compile function:

const sass = require('sass');

plugins: [
  new CopyWebpackPlugin(
    [
      {
        from: 'style.scss',
        to: 'style.css',
        transform: (content, path) => {
          return sass.compile(path).css
        },
      }
    ],
  ),
]

Note that older answers may recommend using sass.renderSync(), but this is now deprecated.

Avent answered 12/10, 2018 at 10:39 Comment(0)
S
0

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')
  ]
Spooner answered 30/7, 2017 at 0:27 Comment(1)
See my edit. There's no way around the rules section, unfortunately.Spooner

© 2022 - 2024 — McMap. All rights reserved.