@hpcc-js/esbuild-plugins 1.5.1 → 1.6.0

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": "@hpcc-js/esbuild-plugins",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "description": "Various esbuild plugins",
5
5
  "type": "module",
6
6
  "exports": {
@@ -37,17 +37,17 @@
37
37
  "update": "npx --yes npm-check-updates -u -t minor"
38
38
  },
39
39
  "dependencies": {
40
- "esbuild": "0.25.10",
40
+ "esbuild": "0.25.11",
41
41
  "esbuild-copy-static-files": "0.1.0",
42
42
  "esbuild-plugin-inline-css": "0.0.1",
43
43
  "esbuild-plugin-umd-wrapper": "3.0.0",
44
44
  "fzstd": "0.1.1",
45
45
  "vite-plugin-css-injected-by-js": "3.5.2",
46
- "vite-plugin-static-copy": "3.1.2"
46
+ "vite-plugin-static-copy": "3.1.4"
47
47
  },
48
48
  "devDependencies": {
49
- "@hpcc-js/wasm-base91": "1.6.0",
50
- "@hpcc-js/wasm-zstd": "1.5.0"
49
+ "@hpcc-js/wasm-base91": "1.8.0",
50
+ "@hpcc-js/wasm-zstd": "1.7.0"
51
51
  },
52
52
  "keywords": [
53
53
  "esbuild",
@@ -63,5 +63,5 @@
63
63
  },
64
64
  "homepage": "https://hpcc-systems.github.io/hpcc-js-wasm/",
65
65
  "license": "Apache-2.0",
66
- "gitHead": "111cb62ebdf2ab518e094f321223ca3ebdc628bd"
66
+ "gitHead": "bfefa70bf4e4232dcdaaa8498a2985a4e9aaadb5"
67
67
  }
package/src/build.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as process from "process";
2
- import { readFileSync, existsSync, writeFileSync } from "fs";
2
+ import { readFileSync } from "fs";
3
3
  import * as path from "path";
4
4
  import * as esbuild from "esbuild";
5
5
  import type { BuildOptions, Format, Loader, Plugin } from "esbuild";
@@ -23,15 +23,6 @@ export async function buildWatch(inputs: string[] | Record<string, string> | { i
23
23
  const isWatch = process.argv.includes("--watch");
24
24
  const isProduction = !isDevelopment;
25
25
 
26
- if (isProduction && existsSync(path.join(process.cwd(), "../../package.json"))) {
27
- const rootPkg = JSON.parse(readFileSync(path.join(process.cwd(), "../../package.json"), "utf8"));
28
- writeFileSync(path.join(process.cwd(), "src/__package__.ts"), `\
29
- export const PKG_NAME = "${pkg.name}";
30
- export const PKG_VERSION = "${pkg.version}";
31
- export const BUILD_VERSION = "${rootPkg.version}";
32
- `, "utf8");
33
- }
34
-
35
26
  config = {
36
27
  entryPoints: inputs,
37
28
  format: config.format ?? "esm",
package/src/index.ts CHANGED
@@ -5,3 +5,4 @@ export * from "./rebuild-logger.ts";
5
5
  export * from "./remove-strict.ts";
6
6
  export * from "./inline-css.ts";
7
7
  export * from "./vite-utils.ts";
8
+ export * from "./package-version-plugin.ts";
@@ -0,0 +1,87 @@
1
+ import type { Plugin } from "vite";
2
+
3
+ export interface PackageVersionPluginOptions {
4
+ /**
5
+ * Package.json object containing name and version
6
+ */
7
+ pkg: {
8
+ name: string;
9
+ version: string;
10
+ };
11
+ /**
12
+ * Build version (typically from root package.json)
13
+ * If not provided, defaults to pkg.version
14
+ */
15
+ buildVersion?: string;
16
+ /**
17
+ * Placeholder for package name (default: "__PACKAGE_NAME__")
18
+ */
19
+ namePlaceholder?: string;
20
+ /**
21
+ * Placeholder for package version (default: "__PACKAGE_VERSION__")
22
+ */
23
+ versionPlaceholder?: string;
24
+ /**
25
+ * Placeholder for build version (default: "__BUILD_VERSION__")
26
+ */
27
+ buildVersionPlaceholder?: string;
28
+ }
29
+
30
+ /**
31
+ * Vite plugin to replace package version placeholders during build
32
+ * This allows keeping version information in source files as placeholders
33
+ * that get replaced with actual values from package.json during the build
34
+ */
35
+ export function packageVersionPlugin(options: PackageVersionPluginOptions): Plugin {
36
+ const {
37
+ pkg,
38
+ buildVersion = pkg.version,
39
+ namePlaceholder = "__PACKAGE_NAME__",
40
+ versionPlaceholder = "__PACKAGE_VERSION__",
41
+ buildVersionPlaceholder = "__BUILD_VERSION__"
42
+ } = options;
43
+
44
+ return {
45
+ name: "hpcc-package-version-plugin",
46
+ enforce: "pre",
47
+ transform(code: string, id: string) {
48
+ // Only process TypeScript/JavaScript files
49
+ if (!id.endsWith(".ts") && !id.endsWith(".js")) {
50
+ return null;
51
+ }
52
+
53
+ // Check if the file contains any placeholders
54
+ if (!code.includes(namePlaceholder) &&
55
+ !code.includes(versionPlaceholder) &&
56
+ !code.includes(buildVersionPlaceholder)) {
57
+ return null;
58
+ }
59
+
60
+ // Replace placeholders with actual values
61
+ let transformedCode = code;
62
+ if (code.includes(namePlaceholder)) {
63
+ transformedCode = transformedCode.replace(
64
+ new RegExp(namePlaceholder, "g"),
65
+ pkg.name
66
+ );
67
+ }
68
+ if (code.includes(versionPlaceholder)) {
69
+ transformedCode = transformedCode.replace(
70
+ new RegExp(versionPlaceholder, "g"),
71
+ pkg.version
72
+ );
73
+ }
74
+ if (code.includes(buildVersionPlaceholder)) {
75
+ transformedCode = transformedCode.replace(
76
+ new RegExp(buildVersionPlaceholder, "g"),
77
+ buildVersion
78
+ );
79
+ }
80
+
81
+ return {
82
+ code: transformedCode,
83
+ map: null // Could generate source map if needed
84
+ };
85
+ }
86
+ };
87
+ }
package/src/vite-utils.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  import { configDefaults, defineConfig, ViteUserConfig } from "vitest/config";
2
2
  import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";
3
3
  import { viteStaticCopy } from "vite-plugin-static-copy";
4
+ import { packageVersionPlugin } from "./package-version-plugin.ts";
5
+ import { readFileSync } from "fs";
6
+ import { resolve, dirname } from "path";
7
+ import { fileURLToPath } from "url";
4
8
 
5
9
  const alias = {
6
10
  "d3-array": "@hpcc-js/common",
@@ -20,6 +24,46 @@ const alias = {
20
24
  "d3-zoom": "@hpcc-js/common"
21
25
  };
22
26
 
27
+ /**
28
+ * Find and read the root package.json (monorepo root)
29
+ * Walks up the directory tree looking for a package.json with "workspaces"
30
+ */
31
+ function getRootPackageVersion(): string {
32
+ try {
33
+ // Try to find root package.json by walking up from current file
34
+ let currentDir = dirname(fileURLToPath(import.meta.url));
35
+ let attempts = 0;
36
+ const maxAttempts = 10;
37
+
38
+ while (attempts < maxAttempts) {
39
+ try {
40
+ const pkgPath = resolve(currentDir, "package.json");
41
+ const pkgContent = readFileSync(pkgPath, "utf-8");
42
+ const pkg = JSON.parse(pkgContent);
43
+
44
+ // Check if this is the root by looking for workspaces
45
+ if (pkg.workspaces) {
46
+ return pkg.version;
47
+ }
48
+ } catch {
49
+ // File doesn't exist or couldn't be read, continue up
50
+ }
51
+
52
+ const parentDir = dirname(currentDir);
53
+ if (parentDir === currentDir) {
54
+ // Reached filesystem root
55
+ break;
56
+ }
57
+ currentDir = parentDir;
58
+ attempts++;
59
+ }
60
+ } catch (error) {
61
+ console.warn("Could not read root package.json, using package version as build version:", error);
62
+ }
63
+
64
+ return "";
65
+ }
66
+
23
67
  export function hpccBundleNames(pkg: any) {
24
68
  const external: string[] = [];
25
69
  const globals: { [id: string]: string } = {};
@@ -118,7 +162,11 @@ export function createHpccViteConfig(pkg: any, options: ViteHpccConfigOptions =
118
162
  const { alias, external, globals } = hpccBundleNames(pkg);
119
163
  const allExternals = [...external, ...additionalExternal];
120
164
 
165
+ // Get build version from root package.json
166
+ const buildVersion = getRootPackageVersion() || pkg.version;
167
+
121
168
  const allPlugins = [
169
+ packageVersionPlugin({ pkg, buildVersion }),
122
170
  cssInjectedByJsPlugin({
123
171
  topExecutionPriority: false
124
172
  }),
package/types/index.d.ts CHANGED
@@ -5,3 +5,4 @@ export * from "./rebuild-logger.ts";
5
5
  export * from "./remove-strict.ts";
6
6
  export * from "./inline-css.ts";
7
7
  export * from "./vite-utils.ts";
8
+ export * from "./package-version-plugin.ts";
@@ -0,0 +1,33 @@
1
+ import type { Plugin } from "vite";
2
+ export interface PackageVersionPluginOptions {
3
+ /**
4
+ * Package.json object containing name and version
5
+ */
6
+ pkg: {
7
+ name: string;
8
+ version: string;
9
+ };
10
+ /**
11
+ * Build version (typically from root package.json)
12
+ * If not provided, defaults to pkg.version
13
+ */
14
+ buildVersion?: string;
15
+ /**
16
+ * Placeholder for package name (default: "__PACKAGE_NAME__")
17
+ */
18
+ namePlaceholder?: string;
19
+ /**
20
+ * Placeholder for package version (default: "__PACKAGE_VERSION__")
21
+ */
22
+ versionPlaceholder?: string;
23
+ /**
24
+ * Placeholder for build version (default: "__BUILD_VERSION__")
25
+ */
26
+ buildVersionPlaceholder?: string;
27
+ }
28
+ /**
29
+ * Vite plugin to replace package version placeholders during build
30
+ * This allows keeping version information in source files as placeholders
31
+ * that get replaced with actual values from package.json during the build
32
+ */
33
+ export declare function packageVersionPlugin(options: PackageVersionPluginOptions): Plugin;