Skip to content
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

Add style-loader 2.0 support #661

Open
ai opened this issue Nov 26, 2020 · 3 comments
Open

Add style-loader 2.0 support #661

ai opened this issue Nov 26, 2020 · 3 comments

Comments

@ai
Copy link
Collaborator

ai commented Nov 26, 2020

style-loader 2.0 now returns ES module and breaks Astroturf’s require()


Right now code like:

const styles = css`
  .button {
    color: black;
    border: 1px solid black;
    background-color: white;
  }
`

console.log(styles.button)

will be compiled by astroturf/loader to:

const styles = require('./index-styles.astroturf.pcss')

console.log(styles)

But webpack 4 will return ESM module on .astroturf.pcss import:

{
  __esModule: true
  default: {
    button: "index-styles-astroturf_button-9b607"
  }
}

As the result, styled doesn’t work because it expects to get { cls1, cls2 } from require but gets { __esModule: true, … }

Details

module.exports = {
  mode: IS_PRODUCTION ? 'production' : 'development',
  devtool: IS_PRODUCTION ? false : 'eval-cheap-module-source-map',
  entry: {
    app: join(__dirname, 'src', 'index.tsx')
  },
  output: {
    publicPath: '/',
    filename: '[name].[hash:8].js',
    chunkFilename: '[name]-[id].[hash:8].js'
  },
  devServer: {
    hot: true,
    open: true
  },
  plugins,
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'babel-loader'
          },
          {
            loader: 'astroturf/loader',
            options: {
              extension: '.astroturf.pcss'
            }
          }
        ]
      },
      {
        test: /\.astroturf\.pcss$/,
        use: [
          IS_PRODUCTION ? MiniCssExtractPlugin.loader : 'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1,
              modules: true
            }
          },
          {
            loader: 'postcss-loader'
          }
        ]
      }
    ]
  }
}
  • webpack: 4.44.2
  • css-loader: 5.0.1
  • style-loader: 2.0.0

Possible solutions

  1. Move to import
  2. Add { __esModule: true, … } unwrapper
@ai ai changed the title Use import instead of require to load CSS file in Babel plugin Add style-loader 2.0 support Nov 26, 2020
@jquense
Copy link
Contributor

jquense commented Nov 26, 2020

I have esm support in the v1 branch, we should probably back port if it's not too much trouble

@ai
Copy link
Collaborator Author

ai commented Nov 26, 2020

It will be great and improve onboarding

@avionbg
Copy link

avionbg commented Dec 9, 2020

Another solution is to just turn off the esModule with style-loader option like this:
{ loader: 'style-loader', options: { esModule: false, } },

btw. MiniCssExtractPlugin has also that option in case you hit the same issue with production build..

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

No branches or pull requests

3 participants