diff --git a/.gitignore b/.gitignore index 045b9df..cf52113 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ web/.classpath web/.project backend/.classpath backend/.project +assets/index.json.tmp diff --git a/assets/index.json b/assets/index.json index 9b7f784..4aec7c6 100644 --- a/assets/index.json +++ b/assets/index.json @@ -2,6 +2,7 @@ "plugins": [ { "name": "SampleExtension", + "version": "@@VERSION@@", "dependencies": [ "Toolbar" ] diff --git a/build/extension/prod-webpack.config.js b/build/extension/prod-webpack.config.js index 4ac7652..96fccf7 100644 --- a/build/extension/prod-webpack.config.js +++ b/build/extension/prod-webpack.config.js @@ -1,17 +1,34 @@ const path = require("path"); +const fs = require("fs"); const createExtensionWebpackConfig = require('../../MapStore2/build/createExtensionWebpackConfig'); const CopyPlugin = require('copy-webpack-plugin'); const ZipPlugin = require('zip-webpack-plugin'); +const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const {name} = require('../../config'); const commons = require('./commons'); +// read version and produce a temporary updated index.json +const { version: versionText } = require('../../package.json'); +const indexSrc = path.resolve(__dirname, "..", "..", "assets", "index.json"); +const tmpIndex = path.resolve(__dirname, "..", "..", "assets", "index.json.tmp"); +try { + const indexContent = JSON.parse(fs.readFileSync(indexSrc, 'utf8')); + if (Array.isArray(indexContent.plugins)) { + indexContent.plugins = indexContent.plugins.map(p => p && p.name === name ? { ...p, version: versionText } : p); + } + fs.writeFileSync(tmpIndex, JSON.stringify(indexContent, null, 4), 'utf8'); +} catch (e) { + // keep behavior silent here; build will fail later if necessary + console.error('Error updating index.json from version.txt:', e); +} + // the build configuration for production allow to create the final zip file, compressed accordingly const plugins = [ new CopyPlugin([ { from: path.resolve(__dirname, "..", "..", "assets", "translations"), to: "translations" }, - { from: path.resolve(__dirname, "..", "..", "assets", "index.json"), to: "index.json" } + { from: tmpIndex, to: 'index.json' } ]), new ZipPlugin({ filename: `${name}.zip`, @@ -22,6 +39,10 @@ const plugins = [ // other files have to be placed in the root, with the same name return path.basename(assetPath); } + }), + new CleanWebpackPlugin({ + cleanOnceBeforeBuildPatterns: [], + cleanAfterEveryBuildPatterns: [tmpIndex] }) ]; module.exports = createExtensionWebpackConfig({ prod: true, name, ...commons, plugins}); diff --git a/package.json b/package.json index c018ca3..422b904 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "babel-plugin-object-assign": "1.2.1", "babel-plugin-react-transform": "2.0.2", "babel-plugin-transform-imports": "2.0.0", + "clean-webpack-plugin": "^4.0.0", "copy-webpack-plugin": "5.0.2", "css-loader": "5.1.2", "css-minimizer-webpack-plugin": "3.0.2",