1- import { chmod , copyFile , cp , mkdir , readFile , writeFile } from 'node:fs/promises' ;
1+ import { chmod , copyFile , cp , mkdir , readFile , stat , writeFile } from 'node:fs/promises' ;
22import { join , parse , resolve } from 'node:path' ;
33import { fileURLToPath } from 'node:url' ;
44
@@ -7,7 +7,7 @@ import { build, type BuildOptions } from 'rolldown';
77import { dts } from 'rolldown-plugin-dts' ;
88import { glob } from 'tinyglobby' ;
99
10- import { CopyAddonPlugin } from './copy-addon-plugin ' ;
10+ import { RewriteImportsPlugin } from './build-support/rewrite-imports ' ;
1111import pkgJson from './package.json' with { type : 'json' } ;
1212import viteRolldownConfig from './vite-rolldown.config' ;
1313
@@ -32,12 +32,13 @@ await build({
3232 external : [ / ^ n o d e : / , 'vitest' ] ,
3333 plugins : [
3434 {
35- name : 'rewrite-vite- import-path' ,
35+ name : 'rewrite-import-path' ,
3636 transform ( _ , id , meta ) {
3737 const moduleInfo = this . getModuleInfo ( id ) ;
3838 const { magicString } = meta ;
3939 if ( moduleInfo ?. isEntry && magicString ) {
4040 magicString . replaceAll ( `'vite'` , `'${ pkgJson . name } /vite'` ) ;
41+ magicString . replaceAll ( `resolve('tsdown/run')` , `resolve('${ pkgJson . name } /tsdown/run')` ) ;
4142 return {
4243 code : magicString ,
4344 } ;
@@ -72,8 +73,8 @@ const newViteRolldownConfig = viteRolldownConfig.map((config) => {
7273 if ( Array . isArray ( config . external ) ) {
7374 config . external = config . external . filter ( ( external ) => {
7475 return ! ( typeof external === 'string' &&
75- ( external . includes ( 'rolldown' ) || external === 'picomatch' || external === 'tinyglobby' ||
76- external === 'fdir' ) ) && ! ( external instanceof RegExp && external . test ( 'rolldown/' ) ) ;
76+ ( external === 'picomatch' || external === 'tinyglobby' ||
77+ external === 'fdir' ) ) ;
7778 } ) ;
7879 }
7980
@@ -108,7 +109,7 @@ const newViteRolldownConfig = viteRolldownConfig.map((config) => {
108109 fileURLToPath(import.meta.url),
109110 '../../..',
110111)` ,
111- `export const VITE_PACKAGE_DIR: string = path.join(fileURLToPath(import.meta.url), '../../../../../../packages/cli ')` ,
112+ `export const VITE_PACKAGE_DIR: string = path.join(fileURLToPath(/** #__KEEP__ */ import.meta.url), '..', '..', '..', '..', '.. ')` ,
112113 ) ;
113114 magicString . replace (
114115 `export const CLIENT_ENTRY: string = resolve(
@@ -124,6 +125,12 @@ const newViteRolldownConfig = viteRolldownConfig.map((config) => {
124125)` ,
125126 `export const ENV_ENTRY = path.join(VITE_PACKAGE_DIR, 'dist/vite/client/env.mjs')` ,
126127 ) ;
128+ magicString . replace (
129+ `const { version } = JSON.parse(
130+ readFileSync(new URL('../../package.json', import.meta.url)).toString(),
131+ )` ,
132+ `import { version } from '../../package.json' with { type: 'json' }` ,
133+ ) ;
127134 return {
128135 code : magicString ,
129136 } ;
@@ -136,26 +143,11 @@ const newViteRolldownConfig = viteRolldownConfig.map((config) => {
136143 plugin . name === 'rollup-plugin-license' ) ;
137144 } ) . map ( ( plugin ) => {
138145 if ( typeof plugin === 'object' && plugin !== null && 'name' in plugin && plugin . name === 'externalize-vite' ) {
139- return {
140- name : 'externalize-vite' ,
141- resolveId ( id ) {
142- if ( id . startsWith ( 'vite/' ) ) {
143- return { id : id . replace ( / ^ v i t e \/ / , `${ pkgJson . name } /vite/` ) , external : true } ;
144- }
145- } ,
146- } ;
146+ return RewriteImportsPlugin ;
147147 }
148148 return plugin ;
149149 } ) ,
150150 ] ;
151- // cli config
152- if ( typeof config . input === 'object' && config . input [ 'cli' ] ) {
153- config . plugins . push ( CopyAddonPlugin ( {
154- isCI : ! ! process . env . CI ,
155- isReleasingPkgInCI : ! ! process . env . RELEASING ,
156- desireWasmFiles : false ,
157- } ) ) ;
158- }
159151 }
160152
161153 if ( config . experimental ) {
@@ -171,9 +163,38 @@ const newViteRolldownConfig = viteRolldownConfig.map((config) => {
171163
172164await build ( newViteRolldownConfig as BuildOptions [ ] ) ;
173165
174- const rolldownViteSourceDir = resolve ( projectDir , '..' , '..' , 'rolldown-vite' , 'packages' , 'vite' ) ;
166+ const rolldownPluginUtilsDir = resolve ( projectDir , '..' , '..' , 'rolldown' , 'packages' , 'pluginutils' ) ;
167+
168+ await mkdir ( join ( projectDir , 'dist' , 'pluginutils' ) , { recursive : true } ) ;
169+
170+ await cp ( join ( rolldownPluginUtilsDir , 'dist' ) , join ( projectDir , 'dist' , 'pluginutils' ) , {
171+ recursive : true ,
172+ } ) ;
173+
174+ const rolldownSourceDir = resolve ( projectDir , '..' , '..' , 'rolldown' , 'packages' , 'rolldown' ) ;
175+
176+ await mkdir ( join ( projectDir , 'dist/rolldown' ) , { recursive : true } ) ;
177+
178+ const rolldownFiles = new Set < string > ( ) ;
179+
180+ await cp ( join ( rolldownSourceDir , 'dist' ) , join ( projectDir , 'dist/rolldown' ) , {
181+ recursive : true ,
182+ filter : async ( from , to ) => {
183+ if ( ( await stat ( from ) ) . isFile ( ) ) {
184+ rolldownFiles . add ( to ) ;
185+ }
186+ return true ;
187+ } ,
188+ } ) ;
175189
176- console . log ( rolldownViteSourceDir ) ;
190+ for ( const file of rolldownFiles ) {
191+ const source = await readFile ( file , 'utf-8' ) ;
192+ if ( source . includes ( '"@rolldown/pluginutils"' ) ) {
193+ await writeFile ( file , source . replaceAll ( '"@rolldown/pluginutils"' , `"${ pkgJson . name } /rolldown/pluginutils"` ) ) ;
194+ }
195+ }
196+
197+ const rolldownViteSourceDir = resolve ( projectDir , '..' , '..' , 'rolldown-vite' , 'packages' , 'vite' ) ;
177198
178199await cp ( join ( rolldownViteSourceDir , 'misc' ) , join ( projectDir , 'dist/vite/misc' ) , {
179200 recursive : true ,
@@ -194,9 +215,12 @@ for (const dtsFile of dtsFiles) {
194215 ) ;
195216 await writeFile (
196217 dstFilePath ,
197- file . replaceAll ( `"rolldown-vite/` , `"${ pkgJson . name } /vite/ ` ) . replaceAll (
218+ file . replaceAll ( `"rolldown-vite/` , `"${ pkgJson . name } /` ) . replaceAll (
198219 `"rolldown-vite"` ,
199220 `"${ pkgJson . name } /vite"` ,
221+ ) . replaceAll ( `"rolldown/` , `"${ pkgJson . name } /rolldown/` ) . replaceAll (
222+ `"rolldown"` ,
223+ `"${ pkgJson . name } /rolldown"` ,
200224 ) ,
201225 ) ;
202226}
@@ -217,3 +241,22 @@ for (const srcDtsFile of srcTypeFiles) {
217241await writeFile ( './dist/vite/node/vite' , `#!/usr/bin/env node\n\nimport './cli.js'\n` ) ;
218242
219243await chmod ( './dist/vite/node/vite' , 0o755 ) ;
244+
245+ await mkdir ( join ( projectDir , 'dist/tsdown/dist' ) , { recursive : true } ) ;
246+
247+ const tsdownExternal = Object . keys ( pkgJson . peerDependencies ) ;
248+
249+ // re-build tsdown cli
250+ await build ( {
251+ input : resolve ( projectDir , 'node_modules/tsdown/dist/run.mjs' ) ,
252+ output : {
253+ format : 'esm' ,
254+ cleanDir : true ,
255+ dir : join ( projectDir , 'dist/tsdown' ) ,
256+ } ,
257+ platform : 'node' ,
258+ external : ( id : string ) => tsdownExternal . some ( ( e ) => id . startsWith ( e ) ) ,
259+ plugins : [
260+ RewriteImportsPlugin ,
261+ ] ,
262+ } ) ;
0 commit comments