@gramlot/gramlot 0.3.38 → 0.3.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gramlot/gramlot",
3
- "version": "0.3.38",
3
+ "version": "0.3.39",
4
4
  "description": "Declarative HTML/SVG interfaces with live data binding, authored in JavaScript or Python.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -69,6 +69,7 @@
69
69
  ".": "./src/index.js",
70
70
  "./server": "./src/server/index.js",
71
71
  "./runtime": "./dist/gramlot.js",
72
+ "./build-plugins": "./src/build-plugins.js",
72
73
  "./markdown": "./src/markdown.js",
73
74
  "./page": "./src/server/page.js",
74
75
  "./gramlot-server": "./src/server/gramlot-server.js",
@@ -0,0 +1,9 @@
1
+ /** An esbuild plugin: name and setup, as esbuild declares `Plugin`. */
2
+ export interface BuildPlugin {
3
+ readonly name: string;
4
+ setup(build: unknown): void;
5
+ }
6
+ /** Replaces big.js and @xmldom/xmldom with modules whose classes throw when constructed. */
7
+ export const absentPackagePlugin: BuildPlugin;
8
+ /** Makes the dynamic import of a third-party library of a component external as `@gramlot/libraries/<name>`. */
9
+ export const libraryPlugin: BuildPlugin;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * esbuild plugins of the browser runtime build (`scripts/runtime-options.mjs`), exported as
3
+ * `@gramlot/gramlot/build-plugins` for a host that bundles the core sources for the browser, such as
4
+ * gramlot-serverless: the bundle then keeps the third-party libraries of components out, as the runtime
5
+ * does, and replaces the packages the browser runtime does not ship. Node only.
6
+ */
7
+ import {fileURLToPath} from 'node:url';
8
+ import {libraries} from './libraries.js';
9
+
10
+ /**
11
+ * Packages that `@genrojs/bag` and `@genrojs/tytx` import and the browser runtime does not ship
12
+ * (gramlot-org/gramlot#84). `@genrojs/tytx` imports big.js beside decimal.js and selects decimal.js,
13
+ * the `Decimal` of Gramlot. Both libraries parse XML with the native `DOMParser` of the window and use
14
+ * @xmldom/xmldom only where no `DOMParser` exists: Node and the serverless Web Worker, which bundle the
15
+ * core sources and keep both packages. The browser runtime replaces each package with a module whose
16
+ * classes throw when constructed.
17
+ */
18
+ const absentPackages = {
19
+ 'big.js': "export default class Big { constructor() { throw new Error('The Gramlot browser runtime has no big.js: decimals use decimal.js'); } }",
20
+ '@xmldom/xmldom': "const absent = () => { throw new Error('The Gramlot browser runtime has no @xmldom/xmldom: XML uses the native DOMParser'); };\n" +
21
+ 'export class DOMParser { constructor() { absent(); } }\nexport class XMLSerializer { constructor() { absent(); } }',
22
+ };
23
+
24
+ const srcDirectory = fileURLToPath(new URL('.', import.meta.url)).replace(/\/$/, '');
25
+
26
+ export const absentPackagePlugin = {
27
+ name: 'absent-package',
28
+ setup(build) {
29
+ const filter = new RegExp(`^(${Object.keys(absentPackages).map(name => name.replace(/[./]/g, '\\$&')).join('|')})$`);
30
+ build.onResolve({filter}, args => ({path: args.path, namespace: 'absent-package'}));
31
+ build.onLoad({filter: /.*/, namespace: 'absent-package'}, args => ({contents: absentPackages[args.path], loader: 'js'}));
32
+ },
33
+ };
34
+
35
+ /**
36
+ * The import of a third-party library on first use in the collections the runtime carries
37
+ * (`bundledCollections`): `loadLibrary` imports `@gramlot/libraries/<name>`, which the import map of the
38
+ * bootstrap resolves to the module of the library in the artifact of its collection (GC-325 §160). The
39
+ * runtime bundle holds no third-party library of a component.
40
+ */
41
+ export const libraryPlugin = {
42
+ name: 'library-module',
43
+ setup(build) {
44
+ let files = null;
45
+ build.onResolve({filter: /.*/}, async args => {
46
+ if (args.kind !== 'dynamic-import' || args.pluginData?.library) return undefined;
47
+ files ??= Promise.all(Object.entries(libraries).map(async ([name, library]) => {
48
+ const resolved = await build.resolve(library.local, {kind: 'dynamic-import', resolveDir: srcDirectory,
49
+ importer: srcDirectory + '/libraries.js', pluginData: {library: true}});
50
+ if (resolved.errors.length) throw new Error(`Library ${name}: cannot resolve ${library.local}`);
51
+ return [resolved.path, name];
52
+ })).then(entries => new Map(entries));
53
+ const target = await build.resolve(args.path, {kind: args.kind, importer: args.importer,
54
+ resolveDir: args.resolveDir, pluginData: {library: true}});
55
+ const name = (await files).get(target.path);
56
+ return name === undefined ? undefined : {path: `@gramlot/libraries/${name}`, external: true};
57
+ });
58
+ },
59
+ };
@@ -9,7 +9,7 @@
9
9
  * A key is the import specifier: the package name for a library, the path relative to this
10
10
  * directory for a runtime module. The generator refuses a collection that would bundle a module of
11
11
  * the runtime missing from this table. big.js and @xmldom/xmldom are not shared: the browser runtime
12
- * does not ship them (see `scripts/runtime-options.mjs`).
12
+ * does not ship them (see `build-plugins.js`).
13
13
  *
14
14
  * @module
15
15
  */