Skip to content

Keep :export also for non-CSS-modules #1161

Closed
@kroko

Description

@kroko
  • Operating System: nix
  • Node Version: v12.17.0
  • NPM Version: 6.14.5
  • webpack Version: 4.44.1
  • css-loader Version: 4.2.1

Expected Behavior / Situation

Previously my setup was as follows

   // --------
    // SCSS GENERAL
    {
      test: /\.(scss)$/,
      exclude: /\.module\.(scss)$/,
      use: [
        // ...
        {
          loader: 'css-loader',
          options: {
            importLoaders: 3,
            sourceMap: true
          }
        },
        // ...
      ]
    },
    // --------
    // SCSS MODULES
    {
      test: /\.module\.(scss)$/,
      use: [
        // ...
        {
          loader: 'css-loader',
          options: {
            importLoaders: 3,
            sourceMap: true,
            modules: {
              mode: 'local',
              localIdentName: (development) ? '[name]__[local]--[hash:base64:5]' : '[hash:base64:16]'
            },
            localsConvention: 'asIs',
            onlyLocals: false
          }
        },
        // ...
      ]
    },

and having :export directive in non-module scss file worked

variables.scss

:export {
  canvasTouchPointColor: $colorSelectionText;
  canvasBackgroundColor: $colorHeaderBackground;
}

somejavascript.js

import styles from 'shared/scss/variables.scss';
console.log('styles.canvasTouchPointColor', styles.canvasTouchPointColor);
console.log('styles.canvasBackgroundColor', styles.canvasBackgroundColor);

Actual Behavior / Situation

Now, using the new configuration syntax

    // --------
    // SCSS GENERAL
    {
      test: /\.(scss)$/,
      exclude: /\.module\.(scss)$/,
      use: [
        // ...
        {
          loader: 'css-loader',
          options: {
            importLoaders: 3,
            sourceMap: true,
            modules: {auto: false} // unnecessary, but let us be verbose 
          }
        },
        // ...
      ]
    },
    // --------
    // SCSS MODULES
    {
      test: /\.module\.(scss)$/,
      use: [
        // ...
        {
          loader: 'css-loader',
          options: {
            importLoaders: 3,
            sourceMap: true,
            modules: {
              mode: 'local',
              localIdentName: (development) ? '[name]__[local]--[hash:base64:5]' : '[hash:base64:16]',
              exportLocalsConvention: 'asIs',
              exportOnlyLocals: false
            }
          }
        },
        // ...
      ]
    },

bringing SCSS variables into JavaScript does not work for SCSS files which are not considered modules.

Note, that moving :export to module file does bring back variables into JS ->

variables.module.scss

@import "variables.scss";
:export {
  canvasTouchPointColor: $colorSelectionText;
  canvasBackgroundColor: $colorHeaderBackground;
}

somejavascript.js

import styles from 'shared/scss/variables.module.scss';
console.log('styles.canvasTouchPointColor', styles.canvasTouchPointColor);
console.log('styles.canvasBackgroundColor', styles.canvasBackgroundColor);

does work.

Modification Proposal

Is the assumption that the new implementation allows SCSS :export only to be used when modules are true?
If so can it be reverted to the previous behaviour?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions