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

pre-bundling error:pre bundling CommonJS dependencies, cannot transform require image / svg successful #2492

Closed
HermitGeek opened this issue Mar 13, 2021 · 18 comments · Fixed by #8459

Comments

@HermitGeek
Copy link

HermitGeek commented Mar 13, 2021

Problem description

Pre bundling CommonJS dependencies, cannot transform require image / svg successful

Example

  • The npm dependencies demo in node_modules
    image

  • After npm run dev --force in my project successful
    Look at _metadata.json file in /node_modules/.vite

{
  "hash": "f35b935a",
  "browserHash": "d36b7f42",
  "optimized": {
    "vue": {
      "file": "/Users/my-code/vue - test/vue3-vite/node_modules/.vite/vue.js",
      "src": "/Users/my-code/vue - test/vue3-vite/node_modules/vue/dist/vue.runtime.esm-bundler.js",
      "needsInterop": false
    },
    "package-demo": {
      "file": "/Users/my-code/vue - test/vue3-vite/node_modules/.vite/package-demo.js",
      "src": "/Users/my-code/vue - test/vue3-vite/node_modules/package-demo/dist/index.js",
      "needsInterop": true
    }
  }
}
  • But there are still require('.../mage.png') in /node_modules/.vite/package-demo.js
var __commonJS = (callback, module) => () => {
  if (!module) {
    module = {exports: {}};
    callback(module.exports, module);
  }
  return module.exports;
};

// node_modules/package-demo/dist/index.js
var require_dist = __commonJS((exports, module) => {
  var obj = {
    image: require("/Users/my-code/vue - \u9879\u76EE\u6D4B\u8BD5/vue3-vite/node_modules/package-demo/dist/image.png")
  };
  module.exports = {
    obj
  };
});

// dep:package-demo
var package_demo_default = require_dist();
export {
  package_demo_default as default
};
//# sourceMappingURL=package-demo.js.map

Looking forward to being answered, thanks

vite version: 2.0.5
npm version: 7.6.3
computer operating system: macOS 10.15.4

@HermitGeek
Copy link
Author

@underfin hello What is the progress of solving this problem?

@Aslemammad
Copy link

Aslemammad commented Mar 23, 2021

@ZXvictory Hmm, Could you send a GitHub/Codesandbox link of this project you have? As a small repro, I'd like to investigate it.

@ci010
Copy link

ci010 commented Mar 26, 2021

@ZXvictory I wonder what do you expect the require('./image.png') to be? Is it something like './image.png.js', or should it be resolved as an url string?

@HermitGeek
Copy link
Author

@ci010 All right, But I think there shouldn't be "require" After pre-bundling

@HermitGeek
Copy link
Author

@ci010 @Aslemammad thanks

@patak-dev
Copy link
Member

@ZXvictory would you create a minimal reproduction in GitHub for this issue using the latest version? Thanks!

@github-actions
Copy link

github-actions bot commented Apr 2, 2021

Hello @ZXvictory. Please provide a online reproduction by codesandbox or a minimal GitHub repository. Issues labeled by need reproduction will be closed if no activities in 3 days.

@HermitGeek
Copy link
Author

HermitGeek commented Apr 3, 2021

@Aslemammad @patak-js ok, this is GitHub demo
https://github.com/ZXvictory/vite-demo-pre-bundling-bug

after npm i vite-dev-demo@1.3.0 -Snpm run dev
there arr Uncaught ReferenceError: require is not defined error

Thanks!

@Aslemammad
Copy link

@ZXvictory Thanks, let me investigate it.

@Aslemammad
Copy link

@ZXvictory @patak-js Vitejs doesn't support commonjs, and vite-dev-demo uses require to load the img.png.
it solves the problem when I use:

import img from 'vite-dev-demo/src/img.png'

@HermitGeek
Copy link
Author

@Aslemammad Brother, I think Vite can support commonjs by pre-bundling
I update GitHub demo https://github.com/ZXvictory/vite-demo-pre-bundling-bug
I require a JS file, after pre-bundling, Vite polyfill require JS file, but require image still exists

Thanks

image

@Aslemammad
Copy link

@ZXvictory Yea, got that, I'm investigating, thanks.

@ygj6
Copy link
Member

ygj6 commented Jun 5, 2021

I looked into this probelm, vite use esbuild in pre-bundling, and mark .svg/.png/.css file as external.
https://github.com/vitejs/vite/blob/main/packages/vite/src/node/optimizer/esbuildDepPlugin.ts#L79

 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:

You can mark a file or a package as external to exclude it from your build. Instead of being bundled, the import will be preserved (using require for the iife and cjs formats and using import for the esm format) and will be evaluated at run time instead.

So far I didn't have a good solution yet.

@ygj6
Copy link
Member

ygj6 commented Jun 12, 2021

@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, modules.exports should be module.exports in your vite-dev-demo.

@SheldonWatson
Copy link

@Aslemammad Brother, I think Vite can support commonjs by pre-bundling
I update GitHub demo https://github.com/ZXvictory/vite-demo-pre-bundling-bug
I require a JS file, after pre-bundling, Vite polyfill require JS file, but require image still exists

Thanks

image

I also encounter this problem, can require some js but can't require image such as .png

@SheldonWatson
Copy link

@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, modules.exports should be module.exports in your vite-dev-demo.

Does this plugin work? I tried it, but still have this error said require is not defined

@bfischer1121
Copy link

@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, modules.exports should be module.exports in your vite-dev-demo.

Thanks @ygj6 -- this worked for me.

Specified the library, per their docs (e.g., esbuildCommonjs(['@react-navigation/elements']))

@bfischer1121
Copy link

Just a suggestion but it'd be awesome if this was transparently handled by Vite

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants