@runtime-type-inspector/plugin-webpack5 5.0.2 → 5.0.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.
Files changed (3) hide show
  1. package/index.cjs +3 -0
  2. package/loader.cjs +29 -1
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -3,6 +3,9 @@ const {Compilation, Compiler} = require('webpack');
3
3
  * @typedef OptionsProps
4
4
  * @property {RegExp} [test] - Test for file extensions.
5
5
  * @property {RegExp} [exclude] - Test for exclusion.
6
+ * @property {string} [projectVersion] - Host project version for `Download
7
+ * log` meta (forwarded to the loader). Defaults to the host package.json
8
+ * version; unset when none is readable (the log then nudges to set it).
6
9
  */
7
10
  /**
8
11
  * @typedef {OptionsProps & import('@runtime-type-inspector/transpiler').Options} Options
package/loader.cjs CHANGED
@@ -1,11 +1,39 @@
1
+ const {existsSync, readFileSync} = require('fs');
2
+ const {join} = require('path');
1
3
  const {addTypeChecks, expandType} = require('@runtime-type-inspector/transpiler');
2
4
  const loaderUtils = require('loader-utils');
5
+ /**
6
+ * Host project version funneled into the emitted header (`setProjectVersion`
7
+ * in `Download log` meta): explicit loader option wins, else the version
8
+ * from the host package.json in the build working directory, else unset
9
+ * (the log then nudges toward setting it).
10
+ * @param {string} [explicit] - Explicit `projectVersion` option.
11
+ * @returns {string|undefined} Resolved version or undefined.
12
+ */
13
+ function resolveProjectVersion(explicit) {
14
+ if (explicit !== undefined && explicit !== null) {
15
+ return explicit;
16
+ }
17
+ try {
18
+ const path = join(process.cwd(), 'package.json');
19
+ if (!existsSync(path)) {
20
+ return undefined;
21
+ }
22
+ const {version} = JSON.parse(readFileSync(path, 'utf8'));
23
+ if (typeof version === 'string' && version) {
24
+ return version;
25
+ }
26
+ } catch {
27
+ // No readable host package.json: leave unset (log says so).
28
+ }
29
+ return undefined;
30
+ }
3
31
  /**
4
32
  * @param {string} source - The source.
5
33
  */
6
34
  module.exports = function (source) {
7
35
  /** @type {import('@runtime-type-inspector/transpiler').Options} */
8
36
  const options = loaderUtils.getOptions(this);
9
- source = addTypeChecks(source, {...options, expandType});
37
+ source = addTypeChecks(source, {...options, expandType, projectVersion: resolveProjectVersion(options.projectVersion)});
10
38
  this.callback(null, source);
11
39
  };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@runtime-type-inspector/plugin-webpack5",
3
- "version": "5.0.2",
3
+ "version": "5.0.4",
4
4
  "description": "Webpack 5 plugin for https://runtimetypeinspector.org/",
5
5
  "main": "index.cjs",
6
6
  "author": "Hermann Rolfes (kungfooman)",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@runtime-type-inspector/transpiler": "^5.0.2",
9
+ "@runtime-type-inspector/transpiler": "^5.0.4",
10
10
  "loader-utils": "^2.0.4",
11
11
  "webpack": "^5.89.0"
12
12
  }