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

Support for specifying plugins in Array #17

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

tooppoo
Copy link

@tooppoo tooppoo commented Jun 22, 2021

Problem

I got a build error when using the followings.

The error is as follows.

╭───────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                               │
│   ✖ Nuxt Fatal Error                                                                          │
│                                                                                               │
│   Error: Cannot find module '0'                                                               │
│   Require stack:                                                                              │
│   - /Users/path/to/app/node_modules/@nuxt/core/dist/core.js                                   │
│                                                                                               │
╰───────────────────────────────────────────────────────────────────────────────────────────────╯

Cause

In my nuxt.config.js, I was specifying build.postcss.plugins as an array.
On the other hand, @nuxt/postcss8 uses object as value of build.postcss.plugins.

My nuxt.config.js is like the following.

import path from 'path'
// (...)
export default {
  // (...)
  build: {
    postcss: {
      plugins: [
        'plugin-a',
        'plugin-b'
      ]
    }
  }
}

@nuxt/postcss8 uses defu to merge the settings.
When defu merges an object against an array, it uses the array index as the object's key.

Thus, defu would build build.postcss.plugins like the following.

{
  build: {
    postcss: {
      plugins: {
        '0': { /* option for plugin-a */ },
        '1': { /* option for plugin-b */ },
        'autoprefixer': {}
      }
    }
  }
}

You can reproduce this by checking out a57775a and then running yarn dev.

Solution

Select the format corresponding to build.postcss.plugins as defined in nuxt.config.js. If build.postcss.plugins is an array, use array format, if it is an object, use object format to merge (06f3bee).

Test

  • yarn test
  • yarn dev
  • yarn dev:legacy

Notes.

  • I wanted to easily check the results before and after the modification, so we added an automated test using Jest.
    • When import src/index.ts and run the test, TypeError: defu_1.default is not a function gotten.
    • Therefore, build it once and then import dist/index.js in the test.
  • Added dev:legacy because we wanted to check the behavior with two different nuxt.config.js.
  • Since the postcss-loader deprecates the object format, I named the configuration using the object format as legacy.

@atinux atinux requested a review from pi0 August 23, 2021 14:03
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 this pull request may close these issues.

1 participant