Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kroko opened this issue Aug 12, 2020 · 7 comments
Closed

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

kroko opened this issue Aug 12, 2020 · 7 comments

Comments

@kroko
Copy link
Contributor

kroko commented Aug 12, 2020

  • 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!

@alexander-akait
Copy link
Member

@kroko
Copy link
Contributor Author

kroko commented Aug 12, 2020

    // --------
    // SCSS GENERAL
    {
      test: /\.(scss)$/,
      exclude: /\.module\.(scss)$/,
      use: [
        // ...
        {
          loader: 'css-loader',
          options: {
            importLoaders: 3,
            sourceMap: true,
            modules: {
              compileType: 'icss'
            }
          }
        },
        // ...
      ]
    },
    // --------
    // SCSS MODULES
    {
      test: /\.module\.(scss)$/,
      // ...
    },

This setup works

Tried compileType before submitting the issue and it did not work. Was using it as shown below as I did not want modules for this loader chain

            modules: {
              compileType: 'icss'
              auto: false
            }

Apparently auto needs not to be defined. Together with the fact that previously modules could be left out altogether (see OP Expected Behavior / Situation) the behaviour has changed quite a lot.

What about having loader behave as

            modules: {
              compileType: 'icss'
            }

if modules key is left out, instead of behaving as

            modules: undefined

as that would kind of resemble as what css-loader in practice did before v4?

@alexander-akait
Copy link
Member

There are two cases:

  • icss and modules together
  • only icss and no modules

So both configuration is valid, but maybe we should docs this

@alexander-akait
Copy link
Member

Using compileType: 'icss' is rare case, let's add docs about it

@kroko
Copy link
Contributor Author

kroko commented Aug 12, 2020

see the pull request

Using compileType: 'icss' is rare case, let's add docs about it

containing project variables in some variables.scss (and not variables.module.scss) and then @import'ing them in both component.module.scss'es as well as JavaScript files directly IMHO is not some edge case.

@kroko
Copy link
Contributor Author

kroko commented Aug 12, 2020

@evilebottnawi PR for doc addition passing now, please review. thanks!

@alexander-akait
Copy link
Member

Fixed #1163, logic for icss and auto will be in the next major release (because it is potential breaking change), but it will be soon, because postcss@8 will be released in near future

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

No branches or pull requests

2 participants