Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Can't extract multiple .css files from a single .js entry (imported multiple .scss files)? #428

Closed
tiendq opened this issue Feb 24, 2017 · 8 comments

Comments

@tiendq
Copy link

tiendq commented Feb 24, 2017

How can I extract main.scss and home.scss into main.css and home.css in the following example?

Result: There was no error, but only home.css was created.

home.js

import '../scss/main.scss';
import '../scss/home.scss';

main.scss

.test1 { color: #f00; }

home.scss

.test2 { color: #00f; }

webpack.config.js

let webpack = require('webpack');
let ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  module: {
    rules: [{
      test: /\.js$/,
      exclude: /node_modules/,
      use: 'babel-loader'
    }, {
      test: /\.scss$/,
      use: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
    }]
  },
  entry: {
    'home': './scripts/home.js'
  },
  output: {
    filename: '[name].js',
    path: __dirname + '/static/scripts',
    publicPath: '/static/'
  },
  plugins: [
    new ExtractTextPlugin('../css/[name].css')
  ]
};
@scottdj92
Copy link
Contributor

Use ExtractTextPlugin.extract({use: ['css-loader', 'sass-loader']});

@tiendq
Copy link
Author

tiendq commented Feb 24, 2017

@scottdj92 What is difference? is mine a short form of yours? And it doesn't work.

@scottdj92
Copy link
Contributor

I'm not sure I can answer this question: @bebraw any input?

@bebraw
Copy link
Contributor

bebraw commented Feb 24, 2017

I would do two extract/plugin pairs to deal with it and match the cases separately like in the readme example.

@tiendq
Copy link
Author

tiendq commented Feb 27, 2017

@bebraw Did you mean Multiple Instances example? Could you give me a bit of webpack config for my case? I guess I need to hard code some file name but it's not a solution, what if I have more Scss files?

Thanks,

@tiendq
Copy link
Author

tiendq commented Feb 27, 2017

It seems that there is the only way to extract multiple CSS files:

But regardless, the most important thing to grasp is I have 2 separate new ExtractTextPlugin instances—one for styling and one for fonts. You need one new ExtractTextPlugin instance for every separate CSS bundle you want. If you tried to use the same instance for both, the plugin would simply lump those both together.

https://blog.madewithenvy.com/webpack-2-postcss-cssnext-fdcd2fd7d0bd

@bebraw
Copy link
Contributor

bebraw commented Feb 27, 2017

Here's the example you requested for.

Can you re-open at Stack Overflow (the issue tracker is not for support) for more input?

@bebraw bebraw closed this as completed Feb 27, 2017
@julkue
Copy link

julkue commented Sep 17, 2017

This is the issue on StackOverflow created by @tiendq:

https://stackoverflow.com/questions/42478964/cant-extract-multiple-css-files-from-a-single-js-entry-imported-multiple-sc

I'm having the same situation: One entry that imports two SCSS files and I want to export these two SCSS files as separate CSS files. So not bundling CSS files. The filename callback function would be a great place for that, but unfortunately it doesn't pass the original filename, just the getPath() function with whom it's impossible to restore the original filename with replaced .scss with .css.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants