I use electron to create a desktop app, and with vite to complie js file, I find vite seem not work with puppeteer, in the file vite generate, it always have error:
./build/index.cjs module not find
I check the file vite generate, it seem the problem is caused by the package yargs which import by the package of @puppeteer/[email protected],
the follow code not be complied correct:
`const {Yargs, processArgv} = require(‘./build/index.cjs’);
Argv(processArgv.hideBin(process.argv));
module.exports = Argv;`
The result in complied file is :
const{applyExtends:Yai,cjsPlatformShim:$ai,Parser:Zai,Yargs:Aoe,processArgv:esi}=require("./build/index.cjs");Aoe.applyExtends=(r,a,l)=>Yai(r,a,l,$ai);Aoe.hideBin=esi.hideBin;Aoe.Parser=Zai;module.exports=Aoe;
You can see the “./build/index.cjs” is still there, not be complied
I try to use vite common js plugin (@rollup/plugin-commonjs) and vite-plugin-require-transform plugin, also update the optimizeDeps in vite config file, but not work, my vite config file as follow:
import { defineConfig, loadEnv } from 'vite';
import alias from "@rollup/plugin-alias";
import * as path from 'path';
import { nodePolyfills } from 'vite-plugin-node-polyfills'
// import commonjs from 'vite-plugin-commonjs'
import ClosePlugin from './vite-plugin-close.ts'
// import { esbuildCommonjs } from '@originjs/vite-plugin-commonjs'
import checker from 'vite-plugin-checker'
//import { nodeResolve } from '@rollup/plugin-node-resolve';
import requireTransform from 'vite-plugin-require-transform';
import commonjs from '@rollup/plugin-commonjs';
import copy from 'rollup-plugin-copy'
import { nodeResolve } from '@rollup/plugin-node-resolve';
export default ({ mode }) => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };
return defineConfig({
plugins: [alias(),
nodePolyfills(),
nodeResolve(),
commonjs({
include: 'node_modules/**',
}),
requireTransform({fileRegex:/.ts$|.tsx$|.js$|.cjs$/}),
copy({
targets: [
{ src: 'node_modules/@puppeteer/browsers/node_modules/yargs/build/index.cjs', dest: '.vite/build/build' }
]
}),
ClosePlugin(),
checker({
// e.g. use TypeScript check
typescript: true,
}),
],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
optimizeDeps: {
include: ['node_modules/@puppeteer/browsers/node_modules/yargs/build/index.cjs'],
},
build: {
sourcemap: true,
commonjsOptions: {
include: ["node_modules/@puppeteer/browsers/node_modules/yargs/build/index.cjs"],
},
// rollupOptions: {
// plugins: [
// alias({
// entries: [
// { find: '@', replacement: path.resolve(__dirname, 'src') }
// ]
// }),
// // nodeResolve({
// // extensions: ['.js', '.jsx', '.ts', '.tsx', '.cjs']
// // }),
// ]
// },
external: [
'sqlite3'
]
},
test: {
include: ['test/vitest/utilitycode/*.test.ts'],
}
})
}
I have to use copy plugin to copy the file, but there are still some error