Set version in Leaflet.js when npm version is called (#9717)

This commit is contained in:
Florian Bischof
2025-05-14 18:45:43 +02:00
committed by GitHub
parent d9100e0bbf
commit f4eeb7feea
5 changed files with 27 additions and 27 deletions

21
build/version.js Normal file
View File

@ -0,0 +1,21 @@
import {readFileSync, writeFileSync} from 'node:fs';
// TODO: Replace this with a regular import when ESLint adds support for import assertions.
// See: https://rollupjs.org/guide/en/#importing-packagejson
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url)));
const fileContent = readFileSync(new URL('../src/Leaflet.js', import.meta.url), 'utf8');
const newContent = fileContent.replace(
/(\/\/ !!! NEXT LINE IS AUTO-GENERATED VIA `NPM VERSION` !!!\s+export const version = ')[^']+(')/,
`$1${pkg.version}$2`
);
if (newContent === fileContent) {
console.error('Version replacement failed: pattern in Leaflet.js not found.');
process.exit(1);
}
writeFileSync(new URL('../src/Leaflet.js', import.meta.url), newContent);
console.log(`Version updated to ${pkg.version}`);

View File

@ -1,6 +1,6 @@
{
"name": "leaflet",
"version": "1.9.2",
"version": "1.9.4",
"homepage": "https://leafletjs.com/",
"description": "JavaScript library for mobile-friendly interactive maps",
"devDependencies": {
@ -60,7 +60,8 @@
"uglify-global": "uglifyjs dist/leaflet-global-src.js -c -m -o dist/leaflet-global.js --source-map filename=dist/leaflet-global.js.map --source-map content=dist/leaflet-global-src.js.map --source-map url=leaflet-global.js.map --comments",
"bundlemon": "bundlemon --subProject js --defaultCompression none && bundlemon --subProject js-gzip --defaultCompression gzip",
"serve": "cd docs && bundle exec jekyll serve",
"prepare": "husky"
"prepare": "husky",
"version": "node ./build/version.js && git add ./src/Leaflet.js"
},
"repository": {
"type": "git",

View File

@ -1,18 +0,0 @@
// Replace version import from package.json with static version for Firefox which currently not supports Import Attributes.
// TODO: After Firefox supports Import Attributes, remove this preprocessor and update the karma.conf.cjs file.
function replaceVersionPreprocessor(config) {
const replaceVersionImport = config.browsers.some(browser => browser.includes('Firefox'));
return function (content, file, done) {
if (replaceVersionImport) {
content = content.replace(
/import pkg from '\.\.\/package\.json' with \{ type: 'json' \};/,
'const pkg = {version: "0.0.0"};'
);
// we use error to make it visible in the console
console.error('Version import replaced with static variable', file.originalPath, config.browsers.join(', '));
}
done(content);
};
}
module.exports = replaceVersionPreprocessor;

View File

@ -11,12 +11,8 @@ module.exports = function (/** @type {import('karma').Config} */ config) {
'karma-chrome-launcher',
'karma-safarinative-launcher',
'karma-firefox-launcher',
'karma-time-stats-reporter',
{'preprocessor:replaceVersion': ['factory', require('./karma-version-preprocessor.cjs')]}
'karma-time-stats-reporter'
],
preprocessors: {
'src/Leaflet.js': ['replaceVersion'] // Replace version import from package.json with static version for Firefox which currently not supports Import Attributes.
},
frameworks: ['mocha'],
customContextFile: 'spec/context.html',
customDebugFile: 'spec/debug.html',

View File

@ -1,5 +1,5 @@
import pkg from '../package.json' with { type: 'json' };
export const version = pkg.version;
// !!! NEXT LINE IS AUTO-GENERATED VIA `NPM VERSION` !!!
export const version = '1.9.4';
// control
export * from './control/index.js';