@@ -229,6 +229,63 @@ async function calculateChecksum(filePath) {
229
229
}
230
230
}
231
231
232
+ /**
233
+ * Copy extra files specified in package.json to the dist folder
234
+ *
235
+ * @param {string } [packagePath='.'] - Path to the package root containing package.json
236
+ * @returns {Promise<void> }
237
+ */
238
+ async function copyExtraDistFiles ( packagePath = '.' ) {
239
+ try {
240
+ const packageJsonPath = path . join ( packagePath , 'package.json' ) ;
241
+ if ( ! fs . existsSync ( packageJsonPath ) ) {
242
+ return ; // No package.json, nothing to do
243
+ }
244
+
245
+ const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ;
246
+ if ( ! packageJson . headlamp || ! packageJson . headlamp . extraDist ) {
247
+ return ; // No extra files to copy
248
+ }
249
+
250
+ const extraDist = packageJson . headlamp . extraDist ;
251
+ const distFolder = path . resolve ( packagePath , 'dist' ) ;
252
+
253
+ // Create dist folder if it doesn't exist (although it should by this point)
254
+ if ( ! fs . existsSync ( distFolder ) ) {
255
+ fs . mkdirSync ( distFolder , { recursive : true } ) ;
256
+ }
257
+
258
+ // Process all entries in extraDist
259
+ for ( const [ target , source ] of Object . entries ( extraDist ) ) {
260
+ const targetPath = path . join ( distFolder , target ) ;
261
+ const sourcePath = path . resolve ( packagePath , source ) ;
262
+
263
+ // Skip if source doesn't exist
264
+ if ( ! fs . existsSync ( sourcePath ) ) {
265
+ console . warn ( `Warning: extraDist source "${ sourcePath } " does not exist, skipping.` ) ;
266
+ continue ;
267
+ }
268
+
269
+ // Create target directory if needed
270
+ fs . mkdirSync ( path . dirname ( targetPath ) , { recursive : true } ) ;
271
+
272
+ // Copy based on whether it's a directory or file
273
+ const sourceStats = fs . statSync ( sourcePath ) ;
274
+ if ( sourceStats . isDirectory ( ) ) {
275
+ console . log ( `Copying extra directory "${ sourcePath } " to "${ targetPath } "` ) ;
276
+ fs . copySync ( sourcePath , targetPath ) ;
277
+ } else {
278
+ console . log ( `Copying extra file "${ sourcePath } " to "${ targetPath } "` ) ;
279
+ fs . copyFileSync ( sourcePath , targetPath ) ;
280
+ }
281
+ }
282
+
283
+ console . log ( 'Successfully copied extra dist files' ) ;
284
+ } catch ( error ) {
285
+ console . error ( 'Error copying extra dist files:' , error ) ;
286
+ }
287
+ }
288
+
232
289
/**
233
290
* Creates a tarball of the plugin package. The tarball is placed in the outputFolderPath.
234
291
* It moves files from:
@@ -274,6 +331,10 @@ async function createArchive(pluginDir, outputDir) {
274
331
275
332
// Create temporary folder
276
333
const tempFolder = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'headlamp-plugin-' ) ) ;
334
+
335
+ // Make sure any extraDist files are in the dist folder before extraction
336
+ await copyExtraDistFiles ( pluginPath ) ;
337
+
277
338
if ( extract ( pluginPath , tempFolder , false ) !== 0 ) {
278
339
console . error (
279
340
`Error: Failed to extract plugin package to "${ tempFolder } ". Not creating archive.`
@@ -381,6 +442,16 @@ async function start() {
381
442
config . build . sourcemap = 'inline' ;
382
443
}
383
444
445
+ // Add file copy hook to be executed after each build
446
+ if ( config . plugins ) {
447
+ config . plugins . push ( {
448
+ name : 'headlamp-copy-extra-dist' ,
449
+ closeBundle : async ( ) => {
450
+ await copyExtraDistFiles ( ) ;
451
+ } ,
452
+ } ) ;
453
+ }
454
+
384
455
try {
385
456
await vite . build ( config ) ;
386
457
} catch ( e ) {
@@ -579,6 +650,10 @@ async function build(packageFolder) {
579
650
const vite = await vitePromise ;
580
651
try {
581
652
await vite . build ( config . default ) ;
653
+
654
+ // Copy extra dist files after successful build
655
+ await copyExtraDistFiles ( '.' ) ;
656
+
582
657
console . log ( `Finished building "${ folder } " for production.` ) ;
583
658
} catch ( e ) {
584
659
console . error ( e ) ;
0 commit comments