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

CJS dependencies are not being pre-bundled #7241

Closed
7 tasks done
bestickley opened this issue Mar 9, 2022 · 8 comments
Closed
7 tasks done

CJS dependencies are not being pre-bundled #7241

bestickley opened this issue Mar 9, 2022 · 8 comments

Comments

@bestickley
Copy link

bestickley commented Mar 9, 2022

Describe the bug

I've got an example Vite app here that has an ESM library, gboost-ui, which has CJS dependencies and peerDependencies which are CJS. When I run pnpm dev I get an error in the browser: The requested module '/node_modules/.pnpm/[email protected]/node_modules/prop-types/index.js?v=f8148f89' does not provide an export named 'default'. So I try adding prop-types to optimizeDeps.include but then I get: The requested module '/@fs/Users/stickb/Code/scratch/tdmp-edge-mgmt-ui/node_modules/.pnpm/[email protected]/node_modules/zen-observable/index.js?v=2906f0d0' does not provide an export named 'default'. so I add zen-observable to optimizeDeps.includes. But then Uncaught SyntaxError: The requested module '/@fs/Users/stickb/Code/scratch/tdmp-edge-mgmt-ui/node_modules/.pnpm/[email protected]/node_modules/url/url.js?v=f14ea013' does not provide an export named 'format' and so forth. I think this is happening for all CJS dependencies gboost-ui imports. Why isn't vite pre-bundling these dependencies?

Strangely, if I clone gboost-ui and run pnpm add ../../path/to/gboost-ui it works.

I saw the issue: #3024 where it says:

if it's not in node_modules it does not get optimized

So I tried install with npm (since PNPM symlinks) but that didn't work either :/

Reproduction

https://github.com/bestickley/vite-gboost-ui-error

System Info

System:
    OS: macOS 12.2.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 58.18 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - ~/Library/pnpm/node
    npm: 8.1.2 - ~/Library/pnpm/npm
  Browsers:
    Chrome: 99.0.4844.51
    Firefox: 91.6.0
    Safari: 15.3
  npmPackages:
    @vitejs/plugin-react: ^1.0.7 => 1.2.0 
    vite: ^2.8.0 => 2.8.6

Used Package Manager

pnpm

Logs

vite:config bundled config file loaded in 207.83ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:react-babel',
  vite:config     'vite:react-refresh',
  vite:config     'vite:react-jsx',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:client-inject',
  vite:config     'vite:import-analysis'
  vite:config   ],
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   resolve: { dedupe: [ 'react', 'react-dom' ], alias: [ [Object], [Object] ] },
  vite:config   optimizeDeps: {
  vite:config     include: [ 'react/jsx-dev-runtime' ],
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   },
  vite:config   configFile: '/Users/stickb/Code/scratch/vite-gboost-ui-error/vite.config.ts',
  vite:config   configFileDependencies: [ 'vite.config.ts' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     server: {}
  vite:config   },
  vite:config   root: '/Users/stickb/Code/scratch/vite-gboost-ui-error',
  vite:config   base: '/',
  vite:config   publicDir: '/Users/stickb/Code/scratch/vite-gboost-ui-error/public',
  vite:config   cacheDir: '/Users/stickb/Code/scratch/vite-gboost-ui-error/node_modules/.vite',
  vite:config   command: 'serve',
  vite:config   mode: 'development',
  vite:config   isProduction: false,
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'development', DEV: true, PROD: false },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) {},
  vite:config   createResolver: [Function: createResolver],
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object]
  vite:config     ],
  vite:config     rollupOptions: {}
  vite:config   }
  vite:config } +6ms
  vite:deps Hash is consistent. Skipping. Use --force to override. +0ms

Validations

@bestickley
Copy link
Author

I notice that when I use the cloned version of gboost-ui ("gboost-ui": "link:../../../green-boost/packages/gboost-ui") I have significantly fewer network requests too.
image
vs
image

@bestickley
Copy link
Author

I changed gboost-ui to have a peerDependency of aws-amplify hoping that would fix it since Vite wouldn't have to find nested CJS dependency since it's installed directly in project but that didn't work.

@bestickley bestickley changed the title ESM dependency's CJS dependencies are not being pre-bundled CJS dependencies are not being pre-bundled Mar 9, 2022
@bestickley
Copy link
Author

bestickley commented Mar 9, 2022

Issue is resolved for npm installation by adding this optimizeDeps.include:

"aws-amplify",
"@aws-amplify/ui-react",
"prop-types",
"zen-observable",
"@aws-amplify/core",
"@aws-amplify/auth",
"@aws-amplify/api-rest",
"@aws-amplify/pubsub",
"amazon-cognito-identity-js"

However, this still doesn't work for PNPM as I get: Error: Failed to resolve force included dependency: zen-observable Will document how to resolve once I do.

@bestickley
Copy link
Author

bestickley commented Mar 9, 2022

I'm stuck at trying to get this to work with PNPM. I got through the first couple errors but I cannot resolve: Uncaught SyntaxError: The requested module '/node_modules/.pnpm/[email protected]/node_modules/url/url.js?v=1ada62cd' does not provide an export named 'format'. There are 100s of places where url is used by aws-amplify dependencies so it's hard to know which one I need to target to include. Here is what I ended with:

"prop-types",
"aws-amplify",
"@aws-amplify/ui-react",
"aws-amplify > zen-observable-ts > zen-observable",
"aws-amplify > @aws-amplify/core > @aws-sdk/client-cloudwatch-logs > @aws-sdk/url-parser-native > url",
"aws-amplify > @aws-amplify/core > @aws-sdk/client-cognito-identity > @aws-sdk/url-parser-native > url",
"aws-amplify > @aws-amplify/core > @aws-sdk/credential-provider-cognito-identity > @aws-sdk/client-cognito-identity > @aws-sdk/url-parser-native > url",
"aws-amplify > @aws-sdk/url-parser-native > url",

@bestickley
Copy link
Author

bestickley commented Mar 9, 2022

Not happy with it, but I had to shamefully hoist. Repo shows working state: https://github.com/bestickley/vite-gboost-ui-error

@bestickley
Copy link
Author

I'm still confused why it works when locally linking the package. If anyone has an answer, please comment!

@bluwy
Copy link
Member

bluwy commented Mar 10, 2022

This looks like a similar issue as #3910. It seems that gboost-ui is exporting raw JSX files, and I'm not sure if that has been historically supported in Vite. The issue is that in the prebundling step, when prebundling gboost-ui, it encounters imports to JSX files, these files are considered "external", so they are not prebundled and instead point to the raw path inside node_modules instead. So probably the best way forward now is to have gboost-ui export compiled JSX (into .js) files instead, unless plugin-react supports #6801.

Of course, I could be wrong too as I'm not using React a lot.

@bestickley
Copy link
Author

@bluwy, thank you so much for your insight. This explains everything.
To change gboost-ui's output to .js from .jsx was trivial. In tsconfig.json: "jsx": "preserve" => "react-jsx"
Thanks, again!

@github-actions github-actions bot locked and limited conversation to collaborators Mar 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants