Skip to content

[Bug] Module build fails if custom SWC uses multiple entries #20

Description

@kmonahan

Describe the bug

If a custom SWC config file contains multiple entries, the custom and default SWC config are not merged together correctly. Instead of an array containing multiple SWC configs, the config becomes an object with keys '0' and '1', resulting in the error:

Module build failed (from ./node_modules/swc-loader/src/index.js):
Error: unknown field `0` at line 1 column 713

Steps to reproduce the behavior

  1. Create a .swcrc file with multiple entries, such as different configuration for .js(x) versus .ts(x)
  2. Attempt to build Storybook
  3. Observe that the build fails with the above error

Here is the .swcrc file I started with:

[
  {
    "test": ".*\\.jsx?$",
    "env": {
      "mode": "usage",
      "loose": true,
      "corejs": "3"
    },
    "jsc": {
      "loose": true,
      "parser": {
        "syntax": "ecmascript",
        "jsx": true
      }
    }
  },
  {
    "test": ".*\\.tsx?$",
    "env": {
      "mode": "usage",
      "loose": true,
      "corejs": "3"
    },
    "jsc": {
      "loose": true,
      "parser": {
        "syntax": "typescript",
        "tsx": true
      }
    }
  }
]

I console.logged the config to try to see what was going on and saw

{
  '0': {
    test: '.*\\.jsx?$',
    env: { mode: 'usage', loose: true, corejs: '3' },
    jsc: { loose: true, parser: [Object] }
  },
  '1': {
    test: '.*\\.tsx?$',
    env: { mode: 'usage', loose: true, corejs: '3' },
    jsc: { loose: true, parser: [Object] }
  },
  env: { bugfixes: true },
  jsc: {
    transform: { react: [Object] },
    parser: { tsx: true, dynamicImport: true, syntax: 'typescript' }
  }
}

Expected behavior

I suspect that for multiple entries, the default config values need to be merged into each config item in the array rather than the array converted to an object?

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions