-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
pre-bundling error:pre bundling CommonJS dependencies, cannot transform require image / svg successful #2492
Comments
@underfin hello What is the progress of solving this problem? |
@ZXvictory Hmm, Could you send a GitHub/Codesandbox link of this project you have? As a small repro, I'd like to investigate it. |
@ZXvictory I wonder what do you expect the |
@ci010 All right, But I think there shouldn't be "require" After pre-bundling |
@ci010 @Aslemammad thanks |
@ZXvictory would you create a minimal reproduction in GitHub for this issue using the latest version? Thanks! |
Hello @ZXvictory. Please provide a online reproduction by codesandbox or a minimal GitHub repository. Issues labeled by |
@Aslemammad @patak-js ok, this is GitHub demo after Thanks! |
@ZXvictory Thanks, let me investigate it. |
@ZXvictory @patak-js Vitejs doesn't support commonjs, and vite-dev-demo uses require to load the img.png. import img from 'vite-dev-demo/src/img.png' |
@Aslemammad Brother, I think Vite can support commonjs by pre-bundling Thanks |
@ZXvictory Yea, got that, I'm investigating, thanks. |
I looked into this probelm, vite use esbuild in pre-bundling, and mark .svg/.png/.css file as external. build.onResolve(
{
filter: new RegExp(`\\.(` + externalTypes.join('|') + `)(\\?.*)?$`)
},
async ({ path: id, importer, kind }) => {
const resolved = await resolve(id, importer, kind)
if (resolved) {
return {
path: resolved,
external: true
}
}
}
) According to the esbuild API documentation:
So far I didn't have a good solution yet. |
@ZXvictory you can try this plugin https://github.com/originjs/vite-plugins/tree/main/packages/vite-plugin-commonjs import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import {esbuildCommonjs} from '@originjs/vite-plugin-commonjs'
export default defineConfig({
plugins: [vue()],
optimizeDeps:{
esbuildOptions:{
plugins:[
esbuildCommonjs()
]
}
}
}) FYI, |
I also encounter this problem, can require some js but can't require image such as .png |
Does this plugin work? I tried it, but still have this error said require is not defined |
Thanks @ygj6 -- this worked for me. Specified the library, per their docs (e.g., |
Just a suggestion but it'd be awesome if this was transparently handled by Vite |
Problem description
Pre bundling CommonJS dependencies, cannot transform require image / svg successful
Example
The npm dependencies demo in node_modules
After
npm run dev
--force in my project successfulLook at _metadata.json file in
/node_modules/.vite
require('.../mage.png')
in/node_modules/.vite/package-demo.js
Looking forward to being answered, thanks
vite version: 2.0.5
npm version: 7.6.3
computer operating system: macOS 10.15.4
The text was updated successfully, but these errors were encountered: