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

Default export exposed as named default #8

Closed
ithinkdancan opened this issue May 21, 2024 · 6 comments · Fixed by #9
Closed

Default export exposed as named default #8

ithinkdancan opened this issue May 21, 2024 · 6 comments · Fixed by #9

Comments

@ithinkdancan
Copy link

ithinkdancan commented May 21, 2024

when using this with Next.js using the string plugin format Next spits out a type error when applying the config

TypeError: require(...) is not a function

const config = {
  plugins: [
    [
      'postcss-assign-layer',
      [
        {
          include: 'core/*.module.css',
          layerName: 'core',
        },
      ],
    ],
  ],
};

module.exports = config;

From what I can tell this has to do with how the plugin is built. Next is calling require(pluginPath)(options) where as the plugin is exporting default which would require it to call require(pluginPath).default(options)

@IanVS
Copy link
Collaborator

IanVS commented May 22, 2024

I'll try to fix the packaging, but in the meantime you should be able to do this:

const postcssAssignLayer = require('postcss-assign-layer').default;

module.exports = {
  plugins: [
      postcssAssignLayer({
          include: 'core/*.module.css',
          layerName: 'core',
      }),
  ],
}

@IanVS IanVS changed the title Does not work with Next.js Default export exposed as named default May 22, 2024
@mdaoust-sidlee
Copy link

mdaoust-sidlee commented Jun 20, 2024

I'll try to fix the packaging, but in the meantime you should be able to do this:

const postcssAssignLayer = require('postcss-assign-layer').default;

module.exports = {
  plugins: [
      postcssAssignLayer({
          include: 'core/*.module.css',
          layerName: 'core',
      }),
  ],
}

Actually, Next does not seem to support require() to import the PostCSS Plugins as seen :

I am currently in the same situation, it works on Storybook for example, but not on Next.js

@IanVS
Copy link
Collaborator

IanVS commented Jun 20, 2024

@mdaoust-sidlee If you need to use esm, you can still do that with a workaround until I fix the packaging here:

import {default as postcssAssignLayer} from 'postcss-assign-layer';

export default {
  plugins: [
      postcssAssignLayer({
          include: 'core/*.module.css',
          layerName: 'core',
      }),
  ],
}

@IanVS
Copy link
Collaborator

IanVS commented Jun 20, 2024

Oh I see, next.js requires commonjs, but no use of require. Interesting limitation on their part. Yeah you'll probably be blocked until I fix this. Will try to get to it soon. But also, boo for next.js being weird like that.

@IanVS
Copy link
Collaborator

IanVS commented Jun 20, 2024

I released a fix in version 0.4.0. Please try it and let me know how it works.

@mdaoust-sidlee
Copy link

mdaoust-sidlee commented Jun 20, 2024

I released a fix in version 0.4.0. Please try it and let me know how it works.

It works!! Many thanks for the super quick fix 🙏.

For anyone else working with Next.js, here is what the postcss config should look like with this plugin.

// postcss.config.js
module.exports = {
  plugins: [
    // your other plugins ...
    
    ['postcss-assign-layer', [{ include: './src/components/**/*.module.css', layerName: 'components' }]],
  ],
};

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

Successfully merging a pull request may close this issue.

3 participants