11
22const path = require ( "path" ) ;
3+ const fs = require ( "fs" ) ;
34
45const createExtensionWebpackConfig = require ( '../../MapStore2/build/createExtensionWebpackConfig' ) ;
56const CopyPlugin = require ( 'copy-webpack-plugin' ) ;
67const ZipPlugin = require ( 'zip-webpack-plugin' ) ;
8+ const { CleanWebpackPlugin } = require ( 'clean-webpack-plugin' ) ;
79const { name} = require ( '../../config' ) ;
810const commons = require ( './commons' ) ;
911
12+ // read version and produce a temporary updated index.json
13+ const { version : versionText } = require ( '../../package.json' ) ;
14+ const indexSrc = path . resolve ( __dirname , ".." , ".." , "assets" , "index.json" ) ;
15+ const tmpIndex = path . resolve ( __dirname , ".." , ".." , "assets" , "index.json.tmp" ) ;
16+ try {
17+ const indexContent = JSON . parse ( fs . readFileSync ( indexSrc , 'utf8' ) ) ;
18+ if ( Array . isArray ( indexContent . plugins ) ) {
19+ indexContent . plugins = indexContent . plugins . map ( p => p && p . name === name ? { ...p , version : versionText } : p ) ;
20+ }
21+ fs . writeFileSync ( tmpIndex , JSON . stringify ( indexContent , null , 4 ) , 'utf8' ) ;
22+ } catch ( e ) {
23+ // keep behavior silent here; build will fail later if necessary
24+ console . error ( 'Error updating index.json from version.txt:' , e ) ;
25+ }
26+
1027// the build configuration for production allow to create the final zip file, compressed accordingly
1128const plugins = [
1229 new CopyPlugin ( [
1330 { from : path . resolve ( __dirname , ".." , ".." , "assets" , "translations" ) , to : "translations" } ,
14- { from : path . resolve ( __dirname , ".." , ".." , "assets" , "index.json" ) , to : " index.json" }
31+ { from : tmpIndex , to : ' index.json' }
1532 ] ) ,
1633 new ZipPlugin ( {
1734 filename : `${ name } .zip` ,
@@ -22,6 +39,10 @@ const plugins = [
2239 // other files have to be placed in the root, with the same name
2340 return path . basename ( assetPath ) ;
2441 }
42+ } ) ,
43+ new CleanWebpackPlugin ( {
44+ cleanOnceBeforeBuildPatterns : [ ] ,
45+ cleanAfterEveryBuildPatterns : [ tmpIndex ]
2546 } )
2647] ;
2748module . exports = createExtensionWebpackConfig ( { prod : true , name, ...commons , plugins} ) ;
0 commit comments