9

I have multiple theme files, which are basically SASS files with different variables that specify colors, fonts etc. specific to each theme. The rest of the SASS files in the project use these variables in their CSS rules.

I would like to configure webpack in such a way that it will generate 1 CSS file for each theme file.

For example:

main.scss:

body {
  background-color: $theme-specific-color;
}

theme1.scss:

$theme-specific-color: blue;

theme2.scss:

$theme-specific-color: green;

The desired configuration would output 2 CSS files:

theme1.css:

body {
  background-color: blue;
}

theme2.css:

body {
  background-color: green;
}

I'm currently using ExtractTextPlugin to extract the stylesheets into CSS.

2
  • I'm facing the same issue. Did you find a solution to your problem? Commented Apr 23, 2018 at 15:33
  • 1
    @Stilltorik I'm using multiple webpack compilers (an array of webpack configs) to generate different CSS theme files, but it's not an ideal solution at all. Commented Apr 23, 2018 at 16:26

1 Answer 1

1

I finally found a solution. What I did is generate the css with a variable ($theme)having different values for each generated css file. And in my code, my style depends on the value of $theme.

It is working with (yes, these are old version. Sorry):

"webpack": "2.2.0",
"webpack-combine-loaders": "2.0.3",
"multi-loader": "git://github.com/sshmyg/multi-loader.git#cbaa35f8936a939968adb78301be0204e36f30cd",
"extract-text-webpack-plugin": "git://github.com/ONE-LOGIC/extract-text-webpack-plugin.git#831af7b65ce749069993e60bd9cb51c637d4e98b",

then my webpack config contains this:

const extractDark = new ExtractTextPlugin({ filename: `css/style.dark.css`, allChunks: true });
const extractLight = new ExtractTextPlugin({ filename: `css/style.light.css`, allChunks: true });
...
config.module: {
  rules: [
    {
    test: /\.css$/,
    use: extractDark.extract({
      fallback: 'style-loader',
      use: [
        { loader: 'css-loader', query: { sourceMap: true } },
        { loader: 'postcss-loader' },
      ],
    }),
  }, {
    test: /\.css$/,

    use: extractLight.extract({
      fallbackLoader: 'style-loader',
      use: [
        { loader: 'css-loader', query: { sourceMap: true } },
        { loader: 'postcss-loader' },
      ],
    }),
  },
  {
    test: /\.scss$/,
    use: multi(
      combineLoaders(extractDark.extract({
        fallback: 'style-loader',
        use: [
          {
            loader: 'css-loader',
          }, {
            loader: 'resolve-url-loader',
          }, {
            loader: 'sass-loader',
            options: {
              includePaths: [],
              sourceMap: true,
              data: '$theme: dark;',
            },
          },
        ],
      })),
      combineLoaders(extractLight.extract({
        fallback: 'style-loader',
        use: [
          {
            loader: 'css-loader',
          }, {
            loader: 'resolve-url-loader',
          }, {
            loader: 'sass-loader',
            options: {
              includePaths: [],
              sourceMap: true,
              data: '$theme: light;',
            },
          },
        ],
      }))
    ),
  }
  ]
};
....
config.plugins.push(extractDark, extractLight);

Finally, my stylesheets have code like this:

$backgroundcolor: white;

@if $theme == dark {
  $backgroundcolor: black;
}

.myClass {
  background-color: $backgroundcolor;
}
Sign up to request clarification or add additional context in comments.

1 Comment

For more recent versions of webpack, extract-text-webpack-plugin is not supported any more, and you should check github.com/webpack-contrib/mini-css-extract-plugin . However, it might not work just yet (github.com/webpack-contrib/mini-css-extract-plugin/issues/45)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.