Skip to content

Run test with the source files#9609

Merged
Falke-Design merged 20 commits into
Leaflet:mainfrom
Falke-Design:feat/test_from_source_files
Mar 24, 2025
Merged

Run test with the source files#9609
Falke-Design merged 20 commits into
Leaflet:mainfrom
Falke-Design:feat/test_from_source_files

Conversation

@Falke-Design

Copy link
Copy Markdown
Member

With changing the source files to be completley runnable without any bundler, we can include them directly in the tests.

Loading the version from the package.json caused the problem.

Solution: In the source files we have now the version 0.0.0 and with running rollup, we replace the version with the version from package.json.

Fixes: #8937

@Falke-Design Falke-Design requested a review from jonkoops March 2, 2025 21:56
@Falke-Design Falke-Design force-pushed the feat/test_from_source_files branch from b8fab1f to 7fdc9cc Compare March 2, 2025 21:56
@IvanSanchez

Copy link
Copy Markdown
Member

@Falke-Design Can you please try with...

import {version} from 'package.json' with { type: "json" };

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with - and yes, browsers (except Firefox, it seems) can import json files now.

@Falke-Design

Copy link
Copy Markdown
Member Author

I already tried before the same like you suggested import {version} from 'package.json' with { type: "json" }; but it didn't work.

Today I found out that named imports are not possible with json:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with

Note: JSON modules only have one default export. You cannot do named imports from them (like import { name } from "data.json").

But this works:

import pkg from '../package.json' with { type: "json" };
export const version = pkg.version;

And the best part is, that it will be shipped in Firefox with version 138 too: https://www.mozilla.org/en-US/firefox/138.0a1/releasenotes/ (29. April https://whattrainisitnow.com/calendar/)

@Falke-Design

Falke-Design commented Mar 14, 2025

Copy link
Copy Markdown
Member Author

@jonkoops @IvanSanchez any ideas how we can fix this test?

Firefox 136.0 (Linux x86_64) General namespace extension FAILED
"foo" is read-only
@spec/suites/core/GeneralSpec.js:7:3

Chrome Headless 133.0.0.0 (Mac OS 10.15.7) General namespace extension FAILED
TypeError: Cannot add property foo, object is not extensible
at Context. (spec/suites/core/GeneralSpec.js:7:14)

it('namespace extension', () => {
L.Util.foo = 'bar';
L.Foo = 'Bar';
expect(L.Util.foo).to.eql('bar');
expect(L.Foo).to.eql('Bar');
});

@jonkoops

Copy link
Copy Markdown
Collaborator

I don't think we can fix that test, when loading from source the modules are not bundled up into a single file, so that means they are actual standalone modules, which are read-only. I think we need to make a decision on if we still want to allow namespace extension going forwards. If we want to keep some sort of backwards compatibility, we perhaps still need to bundle up the source.

@Falke-Design

Copy link
Copy Markdown
Member Author

I think we should always provide a bundled file because it can be minified, used in older projects and plugins which will not be maintained can be still working (same namespace).

@jonkoops

Copy link
Copy Markdown
Collaborator

Perhaps there is some way we could include the bundler as part of the tests so it is automatically rebuilt?

@Falke-Design

Copy link
Copy Markdown
Member Author

For the namespace extension I'm now using the bundled leaflet-src.js.
For the tests in Firefox I added a Karma preprocessor which replaces only for Firefox the import with a static variable: 84af927 This can be removed after Firefox support import with attributes.

@Falke-Design

Copy link
Copy Markdown
Member Author

@jonkoops please review again

@Falke-Design Falke-Design added this to the 2.0 milestone Mar 23, 2025

@jonkoops jonkoops left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread eslint.config.js Outdated
Comment thread spec/karma-version-preprocessor.cjs
Comment thread eslint.config.js Outdated
@Falke-Design Falke-Design merged commit 2e998c5 into Leaflet:main Mar 24, 2025
@jonkoops

Copy link
Copy Markdown
Collaborator

Nice work @Falke-Design!

@simon04

simon04 commented Apr 20, 2025

Copy link
Copy Markdown
Contributor

Changing the import to import pkg from '../package.json' with { type: 'json' } causes the entire package.json to end up in the leaflet-src.js as well as leaflet.js bundle.

Please consider my #9657 for an alternative and simpler approach. Thank you!

/* @preserve
 * Leaflet 1.9.2+main.c2946c42, a JS library for interactive maps. https://leafletjs.com
 * (c) 2010-2025 Volodymyr Agafonkin, (c) 2010-2011 CloudMade
 */

var name = "leaflet";
var version$1 = "1.9.2+main.e76f97cb";
var homepage = "https://leafletjs.com/";
var description = "JavaScript library for mobile-friendly interactive maps";
var devDependencies = {
	"@mapbox/eslint-plugin-script-tags": "^1.0.0",
	"@rollup/plugin-json": "^6.1.0",
	bundlemon: "^3.0.3",
	chai: "^5.1.1",
	eslint: "^9.23.0",
	"eslint-config-mourner": "^4.0.2",
	"eslint-plugin-import-x": "^4.9.1",
	"http-server": "^14.1.1",
	husky: "^9.1.4",
	karma: "^6.4.3",
	"karma-chrome-launcher": "^3.2.0",
	"karma-coverage": "^2.2.1",
	"karma-firefox-launcher": "^2.1.3",
	"karma-mocha": "^2.0.1",
	"karma-safarinative-launcher": "^1.1.0",
	"karma-time-stats-reporter": "^0.1.0",
	leafdoc: "^2.3.0",
	"lint-staged": "^15.2.7",
	mocha: "^10.7.0",
	"prosthetic-hand": "^2.0.0",
	rollup: "^4.19.1",
	"rollup-plugin-git-version": "^0.3.1",
	"simple-git": "^3.25.0",
	sinon: "^18.0.0",
	ssri: "^10.0.6",
	"uglify-js": "^3.19.1",
	"ui-event-simulator": "^2.0.0"
};
var type = "module";
var exports = {
	".": "./dist/leaflet-src.js"
};
var style = "dist/leaflet.css";
var files = [
	"dist",
	"src",
	"!dist/leaflet.zip",
	"!*.leafdoc",
	"CHANGELOG.md"
];
var scripts = {
	debug: "http-server -c-1",
	docs: "node ./build/docs.js && node ./build/integrity.js",
	test: "karma start ./spec/karma.conf.cjs",
	coverage: "karma start ./spec/karma.conf.cjs --coverage --single-run",
	build: "npm run rollup && npm run uglify",
	lint: "eslint .",
	lintfix: "npm run lint -- --fix",
	rollup: "rollup -c build/rollup-config.js",
	watch: "rollup -w -c build/rollup-config.js",
	uglify: "uglifyjs dist/leaflet-src.js -c -m -o dist/leaflet.js --source-map filename=dist/leaflet.js.map --source-map content=dist/leaflet-src.js.map --source-map url=leaflet.js.map --comments",
	bundlemon: "bundlemon --subProject js --defaultCompression none && bundlemon --subProject js-gzip --defaultCompression gzip",
	serve: "cd docs && bundle exec jekyll serve",
	prepare: "husky"
};
var repository = {
	type: "git",
	url: "git://github.com/Leaflet/Leaflet.git"
};
var keywords = [
	"gis",
	"map"
];
var license = "BSD-2-Clause";
var pkg = {
	name: name,
	version: version$1,
	homepage: homepage,
	description: description,
	devDependencies: devDependencies,
	type: type,
	exports: exports,
	style: style,
	files: files,
	scripts: scripts,
	repository: repository,
	keywords: keywords,
	license: license,
	"lint-staged": {
	"*.(js|mjs|md)": "eslint --cache --fix"
}
};
/* @preserve
 * Leaflet 1.9.2+main.c2946c42, a JS library for interactive maps. https://leafletjs.com
 * (c) 2010-2025 Volodymyr Agafonkin, (c) 2010-2011 CloudMade
 */
var name="leaflet",version$1="1.9.2+main.e76f97cb",homepage="https://leafletjs.com/",description="JavaScript library for mobile-friendly interactive maps",devDependencies={"@mapbox/eslint-plugin-script-tags":"^1.0.0","@rollup/plugin-json":"^6.1.0",bundlemon:"^3.0.3",chai:"^5.1.1",eslint:"^9.23.0","eslint-config-mourner":"^4.0.2","eslint-plugin-import-x":"^4.9.1","http-server":"^14.1.1",husky:"^9.1.4",karma:"^6.4.3","karma-chrome-launcher":"^3.2.0","karma-coverage":"^2.2.1","karma-firefox-launcher":"^2.1.3","karma-mocha":"^2.0.1","karma-safarinative-launcher":"^1.1.0","karma-time-stats-reporter":"^0.1.0",leafdoc:"^2.3.0","lint-staged":"^15.2.7",mocha:"^10.7.0","prosthetic-hand":"^2.0.0",rollup:"^4.19.1","rollup-plugin-git-version":"^0.3.1","simple-git":"^3.25.0",sinon:"^18.0.0",ssri:"^10.0.6","uglify-js":"^3.19.1","ui-event-simulator":"^2.0.0"},type="module",exports={".":"./dist/leaflet-src.js"},style="dist/leaflet.css",files=["dist","src","!dist/leaflet.zip","!*.leafdoc","CHANGELOG.md"],scripts={debug:"http-server -c-1",docs:"node ./build/docs.js && node ./build/integrity.js",test:"karma start ./spec/karma.conf.cjs",coverage:"karma start ./spec/karma.conf.cjs --coverage --single-run",build:"npm run rollup && npm run uglify",lint:"eslint .",lintfix:"npm run lint -- --fix",rollup:"rollup -c build/rollup-config.js",watch:"rollup -w -c build/rollup-config.js",uglify:"uglifyjs dist/leaflet-src.js -c -m -o dist/leaflet.js --source-map filename=dist/leaflet.js.map --source-map content=dist/leaflet-src.js.map --source-map url=leaflet.js.map --comments",bundlemon:"bundlemon --subProject js --defaultCompression none && bundlemon --subProject js-gzip --defaultCompression gzip",serve:"cd docs && bundle exec jekyll serve",prepare:"husky"},repository={type:"git",url:"git://github.com/Leaflet/Leaflet.git"},keywords=["gis","map"],license="BSD-2-Clause",pkg={name:name,version:version$1,homepage:homepage,description:description,devDependencies:devDependencies,type:type,exports:exports,style:style,files:files,scripts:scripts,repository:repository,keywords:keywords,license:license,"lint-staged":{"*.(js|mjs|md)":"eslint --cache --fix"}};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Run the tests from the source files

4 participants