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

feat: support advanced import rules option #35

Merged
merged 2 commits into from Aug 26, 2023

Conversation

Jinjiang
Copy link
Collaborator

To support #34

Add an option: advanced?.importRules?: ImportType | ((id: string) => ImportType)

e.g.:

type ImportType = 'defaultFirst' | 'namedFirst' | 'merge'

commonjs({
  advanced: {
    importRules: 'merge'
  }
})

commonjs({
  advanced: {
    importRules: 'namedFirst'
  }
})

commonjs({
  advanced: {
    importRules(id) {
      if (id.includes('named-first-package-path')) {
        return 'namedFirst'
      }
      // ...
    }
  }
})

generated code:

// defaultFirst
import * as foo from 'foo'
foo.default || foo

// namedFirst
import * as foo from 'foo'
Object.keys(foo).join('') !== 'default' ? foo : foo.default

// merge
import * as foo from 'foo'
foo.default ? Object.assign(foo.default, foo) : foo

Thanks.

Copy link
Contributor

@caoxiemeihao caoxiemeihao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@caoxiemeihao caoxiemeihao merged commit 9072b3c into vite-plugin:main Aug 26, 2023
@yejimeiming yejimeiming mentioned this pull request Aug 26, 2023
@yejimeiming yejimeiming linked an issue Aug 26, 2023 that may be closed by this pull request
@Jinjiang Jinjiang deleted the feat/import-rules branch August 27, 2023 03:11
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.

Issue Title: Failed to read named export from a cjs package
2 participants