Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  17] [ 4]  / answers: 1 / hits: 20772  / 8 Years ago, thu, april 21, 2016, 12:00:00

I need to execute one JavaScript function before the Webpack starts its building process. The function just takes .scss files and concatenate them into one.



After that Webpack should take the result file. Is there an option to do that?



At the moment I run the function before the module.exports in webpack.config.js, but it seems that its not synchronous operation. Module.exports execute before the concat() function ends and Webpack can't find .scss file.



function concat(opts) {
(...)
}

concat({ src : styles, dest : './css/style.scss' });

module.exports = [
(...)
]

More From » build

 Answers
4

It seems a little bit odd to concat scss files before running Webpack as those kind of operations are usually handled by Webpack itself.



That being said, there's a few way of solving this.



The most obvious way would be to extract the concat parts to a separate file (e.g. prepare.js) and then run start the build process by running something along this line: node prepare.js && webpack. That'll first run prepare and if that exits without error webpack will be run. Usually that'll be added to the scripts part of your package.json, e.g.



scripts: {
build: node prepare.js && webpack
}


To achieve the same but in a more Webpack integrated way you could do the same thing where you extract the concat part to a separate file and then let Webpack execute that file, before build starts, with the help of Webpack Shell Plugin, e.g.



const WebpackShellPlugin = require('webpack-shell-plugin');
module.exports = {
...
plugins: [
new WebpackShellPlugin({
onBuildStart:['node prepare.js']
})
],
...
}

[#62448] Wednesday, April 20, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jamilab

Total Points: 687
Total Questions: 88
Total Answers: 86

Location: Antigua and Barbuda
Member since Sat, Apr 24, 2021
3 Years ago
;