Having started moving to sass for my project and including the required bits in my webpack configuration (blog post), the next issue I ran into was that importing didn’t seem to work as expected.

Require not Import?

One of the additions I made to my webpack config was to add a resolve section, allowing me to use more convenient and simpler require lines in my javascript.

resolve: { modulesDirectories: ['node_modules', 'components', 'css', 'fonts'], extensions: ['', '.js', '.jsx', '.css', '.scss'] },

This worked exactly as expected wherever I used a require statement, so I had expected that this would transfer to import statements in css and sass files – but it didn’t. As it seems such an obvious thing to do, I had another look at the README for the sass-loader and found what I was looking for.

~, but not as you know it

For my testing I had created as simple a file as I could think of, test.scss.

@import ('../components/_component.scss')

This very simple file just imports another file (which happens to be sass) that belongs to a component I have in the ‘components’ directory. Nothing special, but why do I need the full import path? This was what I needed to get things working, but after looking at the sass-loader again I realised that using the ‘~’ would use the webpack resolve routines – which is what I was hoping. A small change to the file,

@import ('~_component.scss')

resulted in things working as I wanted.

NB the README cautions against using ~ as you may expect (if you’re a command line groupie) as using ~/ implies the home directory and probably isn’t what you want.

Multiple Outputs?

Having decided that I don’t want css to be included in the javascript output, I added the ExtractText plugin which allowed me to bundle all css into a single css file. This is fine, but what if I wanted to have different css bundles? What if I wanted to have different javascript bundles? My current configuration didn’t seem to allow this.

entry: { 'webpack-dev-server/client?', // WebpackDevServer host and port 'webpack/hot/only-dev-server', path.resolve(__dirname, 'components/App.js'), }

Thankfully, webpack has this covered. Instead of having a single entry you can have multiple, each of which you can supply a name. Additionally I realised that the entry point doesn’t need to be a javascript file as long as it’s a file that can be processed. So I changed the entry section to this.

entry: { bundle: [ 'webpack-dev-server/client?', // WebpackDevServer host and port 'webpack/hot/only-dev-server', path.resolve(__dirname, 'components/App.js'), ], test: [ path.resolve(__dirname, 'css/app.scss'), ] },

Running webpack didn’t give me what I expected as I also needed to change the output definition.

output: { path: path.resolve(__dirname, 'html'), filename: '[name].js' },

Using the [name] simply replaces the name I used in the entry definition with that text, which offers additional possibilities. With the changes made, running webpack produces

html/ bundle.js bundle.css test.js test.css

The test.js file is a little annoying and in an ideal world it wouldn’t be created, but so far I can’t find any way of preventing it from being created.

To control the output location even more, simply changing the definitiion is all that’s required for simple changes. Updating it to

entry: { ... 'css/test': [ path.resolve(__dirname, 'css/app.scss'), ] },

results in the files being created in html/css, ie

html/ bundle.js bundle.css css/ test.js test.css

NB when using a path the name needs to be in quotes.

Using this setup, component css is still included in the bundle.css and the only things appearing in test.css are those that I have specifically included in the entry file, which opens up a lot of possibilities for splitting things up. As I’m using bootstrap for the project one possibility is to use this to output a customised bootstrap file.

Hot Reload

At present hot reloading of css doesn’t seem to be working. I changed my configuration to this

entry: { 'webpack-dev-server/client?', // WebpackDevServer host and port 'webpack/hot/only-dev-server', bundle: [ path.resolve(__dirname, 'components/App.js'), ], test: [ path.resolve(__dirname, 'css/app.scss'), ] },

which still provides hot reloading of the javascript, but the css files don’t seem to work. This seems to be a common issue, but as it’s not a serious one for me at present I’m not going to spend too much time looking for solutions. If anyone knows, then I’d love to hear from you.