@docx4j/core-ts 0.1.3 → 0.1.4

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.
@@ -3,10 +3,32 @@ import type { BooleanConversionMode, XPathEngine, XPathValue } from './xpath.mjs
3
3
  * XPath 3.1 through FontoXPath, for the `xpath2` boolean conversion mode and for expressions
4
4
  * written in XPath 2.0 syntax. `select`, `selectValue` and the `java` and `xpath1` modes answer as
5
5
  * `DefaultXPathEngine` does; `xpath2` is the mode this engine exists for.
6
+ *
7
+ * **In a browser or any bundled application, pass the module in.** The no-argument form loads
8
+ * `fontoxpath` by a dynamic `import()` of the bare specifier, which Node resolves and a browser
9
+ * does not: there is no import map, and the `@vite-ignore` that keeps a bundler from rewriting the
10
+ * specifier (needed so that a bundle never carries the optional peer against the consumer's will)
11
+ * also keeps it from resolving it. So the application imports the module itself, where its own
12
+ * bundler resolves it statically, and hands it over:
13
+ *
14
+ * import * as fontoxpath from 'fontoxpath';
15
+ * import { FontoXPathEngine } from '@docx4j/core-ts/xpath-fonto';
16
+ * pkg.xpathEngine = new FontoXPathEngine(fontoxpath); // ready at once
17
+ *
18
+ * In Node, `new FontoXPathEngine()` and `await ready()` still work and still say what to install
19
+ * if the peer is absent. Which module is loaded, and when, is the consumer's business either way -
20
+ * the same principle as `pkg.xpathEngine` being settable at all (CR-005 phase A, 2026-09-26).
6
21
  */
7
22
  export declare class FontoXPathEngine implements XPathEngine {
8
23
  private module;
9
24
  private loading;
25
+ /**
26
+ * @param fontoxpath the `fontoxpath` module, imported by the caller. Omit it in Node to have
27
+ * `ready()` import it; supply it in a browser or a bundle, where a bare dynamic specifier
28
+ * cannot be resolved. Either the module's namespace object or its default export is accepted,
29
+ * the published ESM build having no named exports under Node's interop.
30
+ */
31
+ constructor(fontoxpath?: unknown);
10
32
  get isReady(): boolean;
11
33
  ready(): Promise<void>;
12
34
  select(expression: string, context: Node, namespaces?: Record<string, string>): Node[];
@@ -21,8 +21,33 @@ const ANY_TYPE = 0;
21
21
  * XPath 3.1 through FontoXPath, for the `xpath2` boolean conversion mode and for expressions
22
22
  * written in XPath 2.0 syntax. `select`, `selectValue` and the `java` and `xpath1` modes answer as
23
23
  * `DefaultXPathEngine` does; `xpath2` is the mode this engine exists for.
24
+ *
25
+ * **In a browser or any bundled application, pass the module in.** The no-argument form loads
26
+ * `fontoxpath` by a dynamic `import()` of the bare specifier, which Node resolves and a browser
27
+ * does not: there is no import map, and the `@vite-ignore` that keeps a bundler from rewriting the
28
+ * specifier (needed so that a bundle never carries the optional peer against the consumer's will)
29
+ * also keeps it from resolving it. So the application imports the module itself, where its own
30
+ * bundler resolves it statically, and hands it over:
31
+ *
32
+ * import * as fontoxpath from 'fontoxpath';
33
+ * import { FontoXPathEngine } from '@docx4j/core-ts/xpath-fonto';
34
+ * pkg.xpathEngine = new FontoXPathEngine(fontoxpath); // ready at once
35
+ *
36
+ * In Node, `new FontoXPathEngine()` and `await ready()` still work and still say what to install
37
+ * if the peer is absent. Which module is loaded, and when, is the consumer's business either way -
38
+ * the same principle as `pkg.xpathEngine` being settable at all (CR-005 phase A, 2026-09-26).
24
39
  */
25
40
  export class FontoXPathEngine {
41
+ /**
42
+ * @param fontoxpath the `fontoxpath` module, imported by the caller. Omit it in Node to have
43
+ * `ready()` import it; supply it in a browser or a bundle, where a bare dynamic specifier
44
+ * cannot be resolved. Either the module's namespace object or its default export is accepted,
45
+ * the published ESM build having no named exports under Node's interop.
46
+ */
47
+ constructor(fontoxpath) {
48
+ if (fontoxpath !== undefined)
49
+ this.module = moduleOf(fontoxpath);
50
+ }
26
51
  get isReady() {
27
52
  return this.module !== undefined;
28
53
  }
@@ -31,15 +56,14 @@ export class FontoXPathEngine {
31
56
  if (this.isReady)
32
57
  return;
33
58
  (_a = this.loading) !== null && _a !== void 0 ? _a : (this.loading = (async () => {
34
- var _a;
35
59
  const specifier = 'fontoxpath';
36
60
  try {
37
- // The published ESM build has no named exports under Node's interop, only a default.
38
61
  const loaded = await import(/* @vite-ignore */ specifier);
39
- this.module = (_a = loaded.default) !== null && _a !== void 0 ? _a : loaded;
62
+ this.module = moduleOf(loaded);
40
63
  }
41
64
  catch (e) {
42
- throw new Docx4JException("FontoXPathEngine needs the optional peer dependency 'fontoxpath' (npm install fontoxpath)", { cause: e });
65
+ throw new Docx4JException("FontoXPathEngine needs the optional peer dependency 'fontoxpath' (npm install fontoxpath); "
66
+ + 'in a browser or a bundle, import it yourself and pass it: new FontoXPathEngine(fontoxpath)', { cause: e });
43
67
  }
44
68
  })());
45
69
  await this.loading;
@@ -107,6 +131,20 @@ export class FontoXPathEngine {
107
131
  return this.module;
108
132
  }
109
133
  }
134
+ /**
135
+ * The module, whether given as a namespace object or as a default export, checked for the one
136
+ * function every call goes through so that the wrong module is refused where it is passed rather
137
+ * than at the first evaluation.
138
+ */
139
+ function moduleOf(loaded) {
140
+ const candidate = loaded;
141
+ const module = typeof (candidate === null || candidate === void 0 ? void 0 : candidate.evaluateXPath) === 'function' ? candidate : candidate === null || candidate === void 0 ? void 0 : candidate.default;
142
+ if (!module || typeof module.evaluateXPath !== 'function') {
143
+ throw new Docx4JException('FontoXPathEngine was given something that is not the fontoxpath module: '
144
+ + "expected its evaluateXPath, as in new FontoXPathEngine(await import('fontoxpath'))");
145
+ }
146
+ return module;
147
+ }
110
148
  /** A FontoXPath result in the four-type union `selectValue` reports. */
111
149
  function valueOf(value) {
112
150
  var _a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docx4j/core-ts",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "docx4j-core for TypeScript: Open Packaging (docx/pptx/xlsx zip and flat OPC), typed parts and relationships, style, numbering and font resolution over @docx4j/generated-objects-ts. Node, browsers, Word add-ins.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",