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

Problem with Vite and CommonJS package in monorepo #5668

Closed
7 tasks done
doomsower opened this issue Nov 13, 2021 · 12 comments · Fixed by #7008
Closed
7 tasks done

Problem with Vite and CommonJS package in monorepo #5668

doomsower opened this issue Nov 13, 2021 · 12 comments · Fixed by #7008

Comments

@doomsower
Copy link

Describe the bug

I'm trying to migrate CRA to Vite in a monorepo and couldn't make it work with shared cjs packages. So I built a minimal reproduction.

A basic monorepo with 3 packages

  • Vite app (/packages/app)
  • CommonJS library (/packages/lib-cjs)
  • ESM Library (/packages/lib-esm)

Vite app depends on both libraries and also a copy of CommonJS library is installed from github (https://github.com/doomsower/vite-cjs-test-lib).

Vite app builds and launches successfully if I use ESM library or CommonJS library from github.
Hovewer, with local cjs library (import statement) vite build fails with error:

'test' is not exported by ../lib-cjs/dist/index.js, imported by src/App.tsx

From documentation it seems to me that just adding CommonJS package's name (@vite-mono/lib-cjs) to optimizeDeps.include should do it. I've searched around and couldn't find a definitive answer whether it's a bug or inteded behavior. Nor could I find any workaround.

Full error log
pnpm build                                                                          20:56:16 

> @vite-mono/app@0.0.0 build /Volumes/Projects/_temp/vite-mono/packages/app
> tsc && vite build

vite v2.6.14 building for production...
transforming (30) ../../node_modules/.pnpm/scheduler@0.20.2/node_modules/scheduler/cjs/scheduler.production.min.jsError when using sourcemap for reporting an error:   Can't resolve original location of error.
✓ 34 modules transformed.
'test' is not exported by ../lib-cjs/dist/index.js, imported by src/App.tsx
file: /Volumes/Projects/_temp/vite-mono/packages/app/src/App.tsx:4:9
2: import logo from "./logo.svg";
3: import "./App.css";
4: import { test } from "@vite-mono/lib-cjs";
            ^
5: import { jsx as _jsx } from "react/jsx-runtime";
6: import { jsxs as _jsxs } from "react/jsx-runtime";
error during build:
Error: 'test' is not exported by ../lib-cjs/dist/index.js, imported by src/App.tsx
    at error (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:158:30)
    at Module.error (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:12382:16)
    at Module.traceVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:12767:29)
    at ModuleScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:11559:39)
    at FunctionScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6930:38)
    at ChildScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6930:38)
    at Identifier.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6419:40)
    at CallExpression.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:5025:23)
    at CallExpression.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:9396:15)
    at TemplateLiteral.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:5021:31)
 ELIFECYCLE  Command failed with exit code 1.
Pnpm links
ls -la node_modules/@vite-mono                                                        20:49:33 
total 24
drwxr-xr-x   5 doomsower  admin  170 Nov 13 20:47 .
drwxr-xr-x  11 doomsower  admin  374 Nov 13 20:47 ..
lrwxr-xr-x   1 doomsower  admin   16 Nov 13 19:37 lib-cjs -> ../../../lib-cjs
lrwxr-xr-x   1 doomsower  admin  149 Nov 13 20:47 lib-cjs-remote -> ../../../../node_modules/.pnpm/github.com+doomsower+vite-cjs-test-lib@8c6e40dc2c8098d23a84f498dd1022d30f03ef3c/node_modules/@vite-mono/lib-cjs-remote
lrwxr-xr-x   1 doomsower  admin   16 Nov 13 19:37 lib-esm -> ../../../lib-esm

Reproduction

https://github.com/doomsower/vite-mono-test/

System Info

System:
    OS: macOS 11.5.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 910.94 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v14.18.1/bin/yarn
    npm: 8.1.3 - ~/.nvm/versions/node/v14.18.1/bin/npm
    Watchman: 2021.10.11.00 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 95.1.31.88
    Chrome: 95.0.4638.69
    Firefox: 93.0
    Firefox Developer Edition: 95.0
    Safari: 14.1.2
  npmPackages:
    @vitejs/plugin-react: ^1.0.0 => 1.0.8 
    vite: ^2.6.4 => 2.6.14

Used Package Manager

pnpm

Logs

pnpm vite build --debug                                                         127 ↵  21:34:59  
  vite:config bundled config file loaded in 112.36ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  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-script-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   optimizeDeps: {
  vite:config     include: [ '@vite-mono/lib-cjs', 'react/jsx-dev-runtime' ],
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   },
  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   resolve: { dedupe: [ 'react', 'react-dom' ], alias: [ [Object], [Object] ] },
  vite:config   configFile: '/Volumes/Projects/_temp/vite-mono/packages/app/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     build: {}
  vite:config   },
  vite:config   root: '/Volumes/Projects/_temp/vite-mono/packages/app',
  vite:config   base: '/',
  vite:config   publicDir: '/Volumes/Projects/_temp/vite-mono/packages/app/public',
  vite:config   cacheDir: '/Volumes/Projects/_temp/vite-mono/packages/app/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: true,
  vite:config   server: { fs: { strict: undefined, allow: [Array] } },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  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   createResolver: [Function: createResolver]
  vite:config } +5ms
vite v2.6.14 building for production...
transforming (23) ../../node_modules/.pnpm/object-assign@4.1.1/node_modules/object-assign/index.jsError when using sourcemap for reporting an error: Can't resolve original location of error.
✓ 34 modules transformed.
'test' is not exported by ../lib-cjs/dist/index.js, imported by src/App.tsx
file: /Volumes/Projects/_temp/vite-mono/packages/app/src/App.tsx:4:9
2: import logo from "./logo.svg";
3: import "./App.css";
4: import { test } from "@vite-mono/lib-cjs";
            ^
5: import { jsx as _jsx } from "react/jsx-runtime";
6: import { jsxs as _jsxs } from "react/jsx-runtime";
error during build:
Error: 'test' is not exported by ../lib-cjs/dist/index.js, imported by src/App.tsx
    at error (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:158:30)
    at Module.error (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:12382:16)
    at Module.traceVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:12767:29)
    at ModuleScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:11559:39)
    at FunctionScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6930:38)
    at ChildScope.findVariable (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6930:38)
    at Identifier.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:6419:40)
    at CallExpression.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:5025:23)
    at CallExpression.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:9396:15)
    at TemplateLiteral.bind (/Volumes/Projects/_temp/vite-mono/node_modules/.pnpm/rollup@2.60.0/node_modules/rollup/dist/shared/rollup.js:5021:31)

Validations

@doomsower
Copy link
Author

doomsower commented Nov 13, 2021

Just discovered that changing import statement to

import * as lib from "@vite-mono/lib-cjs";

and then invocation to

<p>{`Hello Vite ${lib.test()}!`}</p>

allows me to build it, but during runtime it throws TypeError: (void 0) is not a function. Because it transpiles the invocation above into

o('p', {
   children: `Hello Vite ${ (void 0) () }!`
}),

@doomsower
Copy link
Author

Ok, I was able to fix it by adding my cjs lib into build.commonjsOptions.include:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  optimizeDeps: {
    include: ["@vite-mono/lib-cjs"],
  },
  build: {
    commonjsOptions: {
      include: [/lib-cjs/, /node_modules/],
    },
  },
});

Definitely was not obvious. Maybe content of optimizeDeps.include should be automatically added to build.commonjsOptions.include?

@Toilal
Copy link

Toilal commented Dec 6, 2021

Thanks for the workaround @doomsower, I switched back from ESM to CommonJS inside my monorepo and hit this issue.

I added a trailing slash / in commonjsOptions includes as it seems safer to me.

include: [/lib-cjs\//, /node_modules\//],

@Toilal
Copy link

Toilal commented Dec 7, 2021

I still have issues with this workaround. If I change sources from monorepo dependencies, changes are not affected. It seems it bundles the dependency at server start, but doesn't bundle them again afterwards.

@aleclarson
Copy link
Member

I have a solution that fixes this. If someone here wants to sponsor me, I'll submit a PR. 😄

@mcabs3
Copy link

mcabs3 commented Jan 24, 2022

I am also running into this issue with a mono repo, I have a NestJS /app that uses a local commonjs lib that I want to share with a Vite front end. I get the same issue.

@tobloef
Copy link

tobloef commented Jan 30, 2022

I have a solution that fixes this. If someone here wants to sponsor me, I'll submit a PR. 😄

Could you tell us more about this solution?

@fwouts
Copy link
Contributor

fwouts commented Feb 19, 2022

I have a solution that fixes this. If someone here wants to sponsor me, I'll submit a PR. 😄

Done! :)

@mcabs3
Copy link

mcabs3 commented Feb 20, 2022

@patak-dev can you explain why the documentation PR would mean this could be closed?

@patak-dev
Copy link
Member

I think @bluwy closed it with the doc PR because it showed a way to work around the issue, using:

export default defineConfig({
  optimizeDeps: {
    include: ['linked-dep']
  },
  build: {
    commonjsOptions: {
      include: [/linked-dep/, /node_modules/]
    }
  }
})

@aleclarson, maybe you had a better idea to tackle this issue, please reopen it in that case.

@bluwy
Copy link
Member

bluwy commented Feb 20, 2022

Yeah. I think the docs show more of how Vite expects local linked packages to be ESM. And if it's CJS instead, there is a "not so nice" way around it, mainly to push the ecosystem towards ESM.

@aleclarson
Copy link
Member

Thank you @fwouts, I will submit a PR sometime this week. 😄

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