I'm trying to take all the sass files in a particular folder, and copy/transform them into css files in a different folder, preserving the folder structure. Like:
- src
- folder1
- test1.scss
- folder2
- test2.scss
- folder3
- test3.scss
- folder1
into
- dest
- folder1
- test1.css
- folder2
- test2.css
- folder3
- test3.css
- folder1
I'm trying to use copy webpack plugin to copy all the src scss files, and the transform option to use node-sass to convert them, which seems to work, except it won't rename the files to .css and i can't figure how to fix this. Any ideas?
snip from my webpack.config.js:
{
context: path.join(__dirname, "src"),
from: "**/*.scss",
to: path.join(__dirname, "dest"),
transform(content, path) {
const result = sass.renderSync({ file: path });
return result.css.toString();
}
}
Transforming files by using
webpack-copy-pluginis not really a right way to process your assets. It is more like hackish/anti-pattern way. Instead you, should rely on proper loader pipeline. Webpack loaders are especially designed to handle this task. You need 4 things to accomplish you task.sass-loader.css-loader to convert CSS into CommonJS format.mini-css-extract-pluginto extract CSS from JS code into actual CSS file.Your configuration would look like:
Adjust your
filenamewith the patterns you need.Alternately, if you wish to stick to the same approach, the
toparameter of copy plugin configuration accepts the webpack style output patterns. Something similar can work for you:Finally, as one last option, if there are too many SASS files, then you can dynamically construct the Webpack
entryobject using glob package and then using that in your module configuration.