@@ -2,43 +2,34 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs'
22import { resolve } from 'node:path'
33import type { Plugin } from 'vite'
44
5+ function getPkgPath ( pkgName : string , fileName ) {
6+ return resolve ( __dirname , `../../../node_modules/@vue-flow/${ pkgName } /dist/${ fileName } ` )
7+ }
8+
9+ function getPublicPath ( fileName : string ) {
10+ return resolve ( __dirname , `../../public/${ fileName } ` )
11+ }
12+
513function copyFiles ( emit : any ) {
6- ; [
7- { path : '../../../node_modules/@vue-flow/core/dist/' , pkgName : 'vue-flow-core.mjs' } ,
8- {
9- path : '../../../node_modules/@vue-flow/background/dist/' ,
10- pkgName : 'vue-flow-background.mjs' ,
11- } ,
12- {
13- path : '../../../node_modules/@vue-flow/controls/dist/' ,
14- pkgName : 'vue-flow-controls.mjs' ,
15- } ,
16- {
17- path : '../../../node_modules/@vue-flow/minimap/dist/' ,
18- pkgName : 'vue-flow-minimap.mjs' ,
19- } ,
20- {
21- path : '../../../node_modules/@vue-flow/node-resizer/dist/' ,
22- pkgName : 'vue-flow-node-resizer.mjs' ,
23- } ,
24- {
25- path : '../../../node_modules/@vue-flow/node-toolbar/dist/' ,
26- pkgName : 'vue-flow-node-toolbar.mjs' ,
27- } ,
28- ] . forEach ( ( { path, pkgName } ) => {
29- const filePath = resolve ( __dirname , `${ path } /${ pkgName } ` )
14+ ; [ 'core' , 'background' , 'controls' , 'minimap' , 'node-resizer' , 'node-toolbar' ] . forEach ( ( name ) => {
15+ const fileName = `vue-flow-${ name } .mjs`
16+
17+ const filePath = resolve ( __dirname , getPkgPath ( name , fileName ) )
18+
19+ console . log ( filePath )
20+
3021 if ( ! existsSync ( filePath ) ) {
31- throw new Error ( `${ pkgName } not built. ` + `Run "pnpm -w build" first.` )
22+ throw new Error ( `${ name } not built. ` + `Run "pnpm -w build" first.` )
3223 }
3324
3425 emit ( {
3526 type : 'asset' ,
36- fileName : pkgName ,
27+ fileName,
3728 filePath,
3829 source : readFileSync ( filePath , 'utf-8' ) ,
3930 } )
4031
41- console . log ( `Copied ${ filePath } to ${ resolve ( __dirname , `../../public/ ${ pkgName } ` ) } ` )
32+ console . log ( `Copied ${ filePath } to ${ getPublicPath ( fileName ) } ` )
4233 } )
4334
4435 console . log ( 'Copied vue-flow files' )
@@ -49,13 +40,23 @@ export function copyVueFlowPlugin(): Plugin {
4940 buildStart ( ) {
5041 // use fs to copy files
5142 copyFiles ( ( file : any ) => {
52- writeFileSync ( resolve ( __dirname , `../../public/${ file . fileName } ` ) , file . source )
43+ // remove existing files
44+ if ( existsSync ( getPublicPath ( file . fileName ) ) ) {
45+ writeFileSync ( getPublicPath ( file . fileName ) , '' )
46+ }
47+
48+ writeFileSync ( getPublicPath ( file . fileName ) , file . source )
5349 } )
5450 } ,
5551 watchChange ( ) {
5652 // use fs to copy files
5753 copyFiles ( ( file : any ) => {
58- writeFileSync ( resolve ( __dirname , `../../public/${ file . fileName } ` ) , file . source )
54+ // remove existing files
55+ if ( existsSync ( getPublicPath ( file . fileName ) ) ) {
56+ writeFileSync ( getPublicPath ( file . fileName ) , '' )
57+ }
58+
59+ writeFileSync ( getPublicPath ( file . fileName ) , file . source )
5960 } )
6061 } ,
6162 generateBundle ( ) {
0 commit comments