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

multiple transform for some commonjs package #5

Open
SheldonWatson opened this issue Aug 6, 2021 · 9 comments
Open

multiple transform for some commonjs package #5

SheldonWatson opened this issue Aug 6, 2021 · 9 comments

Comments

@SheldonWatson
Copy link

SheldonWatson commented Aug 6, 2021

image

look at this screenshot,this file has been transformed by some "esm to cjs"tools,and was compatibled by the function __importDefault, so 'no_data_png_1' will by wrapped with 'default' property, and with your plugin, you transfromed 'require'
to 'import', which will be transformed twice and wrapped another 'default' property.

In summary, this plugin is not suitable for package that is transformed by 'esm to cjs' tools!

@SheldonWatson SheldonWatson changed the title multiple transform for co multiple transform for some commonjs package Aug 6, 2021
@flyfishzy
Copy link
Member

Hi, please provide more details, eg. a reproduction repo will be better, Thx!

@SheldonWatson
Copy link
Author

SheldonWatson commented Aug 6, 2021

npm: 7.17.0
node: v16.1.0
vite: 2.4.4

其实很好懂呀,如果一个包是 由esm转成cjs,他就会被__importDefault这种转一遍,多了个default属性;
这个插件转成import后,vite的应该又会转一遍,就又多了个default

@flyfishzy
Copy link
Member

Try to use exclude option:

exclude: string[] Dependencies to exclude from transform.

import { viteCommonjs } from '@originjs/vite-plugin-commonjs'

export default {
    plugins: [
        viteCommonjs({
       exclude: 'your_lib'
    })
    ]
}

@SheldonWatson
Copy link
Author

Try to use exclude option:

exclude: string[] Dependencies to exclude from transform.

import { viteCommonjs } from '@originjs/vite-plugin-commonjs'

export default {
    plugins: [
        viteCommonjs({
       exclude: 'your_lib'
    })
    ]
}

I tried this, but still don't work...

@SheldonWatson
Copy link
Author

SheldonWatson commented Aug 6, 2021

I upload my demo:https://github.com/SheldonWatson/vite-commonjs-isure

image

@SheldonWatson
Copy link
Author

BTW,when I import viteCommonjs, the reactRefresh plugin throw this error:
image

@jiawulin001
Copy link
Member

I have released a new version of @originjs/[email protected] that fixes this issue.

Again, just add the viteCommonjs plugin to vite.config.js:

plugins: [
    viteCommonjs()
  ]

The esbuildCommonjs plugin is used to convert commonJS code that is not handled in Pre-bundling, it is not needed in this demo you provided.

@SheldonWatson
Copy link
Author

BTW,when I import viteCommonjs, the reactRefresh plugin throw this error:
image

Thx, It does work when I only used the 'viteCommonjs'plugin, but did you uncomment this line:
image
the react Refresh will throw the error!

@SheldonWatson
Copy link
Author

I have released a new version of @originjs/[email protected] that fixes this issue.

Again, just add the viteCommonjs plugin to vite.config.js:

plugins: [
    viteCommonjs()
  ]

The esbuildCommonjs plugin is used to convert commonJS code that is not handled in Pre-bundling, it is not needed in this demo you provided.

As far as I know, vite will pre-bundling all files that are used by the current page, can you tell me what scene that the code will not handled in Pre-bundling?

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