capi icon indicating copy to clipboard operation
capi copied to clipboard

`capi serve --target dev` broken in vite

Open ryanleecode opened this issue 2 years ago • 2 comments

When changing the dev command in the package json of this PR to capi serve --target dev -- vite, vite throws an error only after the browser webpage is opened.

3:21:40 PM [vite] Internal server error: Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.
  Plugin: vite:import-analysis
  File: /home/ryan/Documents/Repositories/capi-vite-example/vite/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/client/env.mjs:4:0
  1  |  const context = (() => {
     |  ^
  2  |      if (typeof globalThis !== 'undefined') {
  3  |          return globalThis;
      at formatError (file:///home/ryan/Documents/Repositories/capi-vite-example/vite/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:42645:46)
      at TransformContext.error (file:///home/ryan/Documents/Repositories/capi-vite-example/vite/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:42641:19)
      at TransformContext.transform (file:///home/ryan/Documents/Repositories/capi-vite-example/vite/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:40447:22)
      at async Object.transform (file:///home/ryan/Documents/Repositories/capi-vite-example/vite/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:42919:30)
      at async loadAndTransform (file:///home/ryan/Documents/Repositories/capi-vite-example/vite/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:53385:29)

ryanleecode avatar Jul 12 '23 19:07 ryanleecode

Initial assessment is it doesn't like the CAPI_SERVER environment variable. When removed it works

ryanleecode avatar Jul 17 '23 23:07 ryanleecode

It works when you add JSON.stringify to the vite env variables similar to webpack. docs.capi.dev will need to be updated.

define: {
  "process.env.CAPI_SERVER": JSON.stringify(process.env.CAPI_SERVER),
  "process.env.CAPI_TARGET": JSON.stringify(process.env.CAPI_TARGET),
}

ryanleecode avatar Jul 18 '23 00:07 ryanleecode