Skip to content
This repository was archived by the owner on Jun 15, 2025. It is now read-only.

Commit 010ef7a

Browse files
committed
improve vite config creation
1 parent 0bea488 commit 010ef7a

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

index.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,6 @@ viteProcess.on('close', (code) => {
9797
const modifiedData = indexHTML.replace('<head>', '<head>\n' + CSP + '\n')
9898
writeFileSync(indexHTMLPath, modifiedData, 'utf8')
9999

100-
process.stdout.write('\nPatching package.json\n')
101-
102-
const packageJSONPath = resolve(cwd, 'package.json')
103-
const packageJSON = readFileSync(packageJSONPath, 'utf-8')
104-
const modifiedPackageJSON = packageJSON.replace(
105-
'"tsc && vite build"',
106-
'"tsc && vite build --"'
107-
)
108-
writeFileSync(packageJSONPath, modifiedPackageJSON, 'utf8')
109-
110100
// So TypeScript doesn't complain about about String#startsWith in the vite.config.ts
111101
if (existsSync(resolve(cwd, 'tsconfig.node.json'))) {
112102
process.stdout.write('\nPatching tsconfig.node.json\n')
@@ -123,21 +113,48 @@ viteProcess.on('close', (code) => {
123113
}
124114
}
125115

126-
// patching the vite.config.ts to make socket:* modules external
127-
if (existsSync(resolve(cwd, 'vite.config.ts'))) {
128-
process.stdout.write('\nPatching vite.config.ts\n')
116+
let isViteConfigExists = false
117+
['js', 'ts'].forEach((ext) => {
118+
// patching the vite.config.js to make socket:* modules external
119+
if (existsSync(resolve(cwd, `vite.config.${ext}`))) {
120+
process.stdout.write('\nPatching vite.config.' + ext + '\n')
121+
122+
const viteConfigPath = resolve(cwd, 'vite.config.js')
123+
const viteConfig = readFileSync(viteConfigPath, 'utf-8')
124+
125+
// make socket:* modules external for build
126+
if (!viteConfig.includes('external(id)')) {
127+
const modifiedViteConfig = viteConfig.replace(
128+
'defineConfig({',
129+
'defineConfig({\n build: {\n rollupOptions: {\n external(id) {\n return id.startsWith(\'socket:\')\n }\n }\n },'
130+
)
131+
writeFileSync(viteConfigPath, modifiedViteConfig, 'utf8')
132+
}
133+
}
134+
isViteConfigExists = true
135+
})
136+
137+
if (!isViteConfigExists) {
138+
process.stdout.write('\nCreating vite.config.js\n')
129139

130-
const viteConfigTSPath = resolve(cwd, 'vite.config.ts')
131-
const viteConfigTS = readFileSync(viteConfigTSPath, 'utf-8')
140+
const viteConfigPath = resolve(cwd, 'vite.config.js')
141+
const viteConfig = `
142+
import { defineConfig } from 'vite'
132143
133-
if (!viteConfigTS.includes('external(id)')) {
134-
const modifiedViteConfigTS = viteConfigTS.replace(
135-
'defineConfig({',
136-
'defineConfig({\n build: {\n rollupOptions: {\n external(id) {\n return id.startsWith(\'socket:\')\n }\n }\n },'
137-
)
138-
writeFileSync(viteConfigTSPath, modifiedViteConfigTS, 'utf8')
144+
// https://vitejs.dev/config/
145+
export default defineConfig({
146+
build: { // HERE
147+
rollupOptions: {
148+
external(id) {
149+
return id.startsWith('socket:')
150+
}
139151
}
152+
},
153+
})
154+
`
155+
writeFileSync(viteConfigPath, viteConfig, 'utf8')
140156
}
141157

158+
142159
process.stdout.write(`\nDone!\n\nBuild and run your app with:\nssc build -r ${projectName}`)
143160
})

0 commit comments

Comments
 (0)