@gjsify/rolldown-plugin-gjsify 0.64.41-next.335 → 0.64.42
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.
|
@@ -29,6 +29,7 @@ import { dirname, join, relative, resolve } from 'node:path';
|
|
|
29
29
|
import { inlineStaticReads, parseSource } from '../utils/inline-static-reads.js';
|
|
30
30
|
import { declareBuildInput } from '../utils/declare-build-input.js';
|
|
31
31
|
import { hasZipSegment } from '../utils/zip-path.js';
|
|
32
|
+
import { findUrlAssets, UrlAssets } from '../utils/url-assets.js';
|
|
32
33
|
export const REWRITE_FILTER = /\.(m?js|cjs|[cm]?tsx?)$/;
|
|
33
34
|
const DIRNAME_DECL_RE = /(?:var|let|const)\s+__dirname\b|export\s+(?:var|let|const)\s+__dirname\b/;
|
|
34
35
|
const FILENAME_DECL_RE = /(?:var|let|const)\s+__filename\b|export\s+(?:var|let|const)\s+__filename\b/;
|
|
@@ -463,8 +464,14 @@ export function rewriteContents(args, srcInput, bundleDir, runtimeResolve, decla
|
|
|
463
464
|
*/
|
|
464
465
|
export function nodeModulesPathRewritePlugin(options) {
|
|
465
466
|
const runtimeResolve = options.runtimeResolve ?? false;
|
|
467
|
+
const assets = new UrlAssets();
|
|
466
468
|
return {
|
|
467
469
|
name: 'gjsify-node-modules-path-rewrite',
|
|
470
|
+
// Not `emitFile`: the native engine has none. Copy beside the bundle, where the
|
|
471
|
+
// shim's no-node_modules fallback looks and where `gjsify ship` stages from.
|
|
472
|
+
writeBundle() {
|
|
473
|
+
assets.writeTo(options.bundleDir);
|
|
474
|
+
},
|
|
468
475
|
transform: {
|
|
469
476
|
order: 'post',
|
|
470
477
|
filter: { id: REWRITE_FILTER },
|
|
@@ -476,6 +483,13 @@ export function nodeModulesPathRewritePlugin(options) {
|
|
|
476
483
|
// `utils/declare-build-input.ts`.
|
|
477
484
|
const declare = (abs) => declareBuildInput(this, abs);
|
|
478
485
|
if (id.includes('node_modules')) {
|
|
486
|
+
if (runtimeResolve && shouldRewrite(id)) {
|
|
487
|
+
for (const asset of findUrlAssets(code, id)) {
|
|
488
|
+
const clash = assets.add(asset);
|
|
489
|
+
if (clash)
|
|
490
|
+
this.warn?.(clash);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
479
493
|
const result = rewriteContents({ path: id }, code, options.bundleDir, runtimeResolve, declare);
|
|
480
494
|
if (!result)
|
|
481
495
|
return null;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface UrlAsset {
|
|
2
|
+
/** Absolute path of the asset beside the original source file. */
|
|
3
|
+
source: string;
|
|
4
|
+
/** Bundle-dir-relative, POSIX-separated destination the runtime fallback resolves to. */
|
|
5
|
+
dest: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The non-code files `src` (living at `path`) references through `new URL(<literal>,
|
|
9
|
+
* import.meta.url)` that exist on disk. JS/TS modules, non-literal specifiers, absolute or
|
|
10
|
+
* scheme URLs, and anything that would land outside the bundle directory are ignored.
|
|
11
|
+
*/
|
|
12
|
+
export declare function findUrlAssets(src: string, path: string): UrlAsset[];
|
|
13
|
+
/** Collects assets over a build; first source wins a destination, a clash is reported. */
|
|
14
|
+
export declare class UrlAssets {
|
|
15
|
+
#private;
|
|
16
|
+
add(asset: UrlAsset): string | null;
|
|
17
|
+
entries(): UrlAsset[];
|
|
18
|
+
/** Copy every collected asset beside the bundle; returns the bundle-relative files written. */
|
|
19
|
+
writeTo(bundleDir: string): string[];
|
|
20
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Sibling assets a bundled node_modules file loads with `new URL('./x.wasm', import.meta.url)`.
|
|
2
|
+
//
|
|
3
|
+
// The module-resolve shim answers such a URL from the installed package, and when no
|
|
4
|
+
// `node_modules` sits beside the bundle it falls back to the BUNDLE file itself — so the
|
|
5
|
+
// asset is looked up as `<bundle dir>/<relative path>`. `gjsify ship` stages the bundle
|
|
6
|
+
// directory wholesale, so copying the asset to exactly that place at build time makes dev and
|
|
7
|
+
// ship agree; when `node_modules` IS present the original path still wins (the shim resolves
|
|
8
|
+
// the package first). Real case: `@mtcute/wasm` loading `./mtcute-simd.wasm`.
|
|
9
|
+
import { copyFileSync, existsSync, mkdirSync, statSync } from 'node:fs';
|
|
10
|
+
import { dirname, isAbsolute, join, normalize, relative, resolve, sep } from 'node:path';
|
|
11
|
+
import { parseSource } from './inline-static-reads.js';
|
|
12
|
+
/** Code is bundled, never copied: a `new URL` to one of these is a module reference. */
|
|
13
|
+
const CODE_EXT_RE = /\.(?:[cm]?[jt]sx?|node)$/i;
|
|
14
|
+
const SCHEME_RE = /^[a-z][a-z0-9+.-]*:/i;
|
|
15
|
+
function isImportMetaUrl(node) {
|
|
16
|
+
const object = node?.object;
|
|
17
|
+
const property = node?.property;
|
|
18
|
+
return (node?.type === 'MemberExpression' &&
|
|
19
|
+
!node.computed &&
|
|
20
|
+
object?.type === 'MetaProperty' &&
|
|
21
|
+
property?.name === 'url');
|
|
22
|
+
}
|
|
23
|
+
/** String-literal specifiers of every `new URL(<literal>, import.meta.url)` in `src`. */
|
|
24
|
+
function urlLiterals(src, path) {
|
|
25
|
+
let ast;
|
|
26
|
+
try {
|
|
27
|
+
ast = parseSource(src, path);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// acorn trails the bundler on new syntax; no detection beats a failed build.
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
const out = [];
|
|
34
|
+
const stack = [ast];
|
|
35
|
+
while (stack.length > 0) {
|
|
36
|
+
const node = stack.pop();
|
|
37
|
+
if (node === null || typeof node !== 'object')
|
|
38
|
+
continue;
|
|
39
|
+
if (Array.isArray(node)) {
|
|
40
|
+
stack.push(...node);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const callee = node.callee;
|
|
44
|
+
const args = node.arguments;
|
|
45
|
+
if (node.type === 'NewExpression' &&
|
|
46
|
+
callee?.type === 'Identifier' &&
|
|
47
|
+
callee.name === 'URL' &&
|
|
48
|
+
args?.length === 2 &&
|
|
49
|
+
args[0].type === 'Literal' &&
|
|
50
|
+
typeof args[0].value === 'string' &&
|
|
51
|
+
isImportMetaUrl(args[1])) {
|
|
52
|
+
out.push(args[0].value);
|
|
53
|
+
}
|
|
54
|
+
for (const value of Object.values(node))
|
|
55
|
+
if (value !== null && typeof value === 'object')
|
|
56
|
+
stack.push(value);
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The non-code files `src` (living at `path`) references through `new URL(<literal>,
|
|
62
|
+
* import.meta.url)` that exist on disk. JS/TS modules, non-literal specifiers, absolute or
|
|
63
|
+
* scheme URLs, and anything that would land outside the bundle directory are ignored.
|
|
64
|
+
*/
|
|
65
|
+
export function findUrlAssets(src, path) {
|
|
66
|
+
if (!src.includes('import.meta.url') || !src.includes('new URL'))
|
|
67
|
+
return [];
|
|
68
|
+
const assets = [];
|
|
69
|
+
for (const spec of new Set(urlLiterals(src, path))) {
|
|
70
|
+
if (spec === '' || spec.startsWith('/') || SCHEME_RE.test(spec) || /[?#]/.test(spec))
|
|
71
|
+
continue;
|
|
72
|
+
if (CODE_EXT_RE.test(spec))
|
|
73
|
+
continue;
|
|
74
|
+
const dest = normalize(spec);
|
|
75
|
+
if (isAbsolute(dest) || dest === '..' || dest.startsWith(`..${sep}`))
|
|
76
|
+
continue;
|
|
77
|
+
const source = resolve(dirname(path), spec);
|
|
78
|
+
if (!existsSync(source) || !statSync(source).isFile())
|
|
79
|
+
continue;
|
|
80
|
+
assets.push({ source, dest: dest.split(sep).join('/') });
|
|
81
|
+
}
|
|
82
|
+
return assets;
|
|
83
|
+
}
|
|
84
|
+
/** Collects assets over a build; first source wins a destination, a clash is reported. */
|
|
85
|
+
export class UrlAssets {
|
|
86
|
+
#byDest = new Map();
|
|
87
|
+
add(asset) {
|
|
88
|
+
const known = this.#byDest.get(asset.dest);
|
|
89
|
+
if (known === undefined) {
|
|
90
|
+
this.#byDest.set(asset.dest, asset.source);
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return known === asset.source
|
|
94
|
+
? null
|
|
95
|
+
: `${asset.dest} is wanted from both ${known} and ${asset.source}; keeping the first`;
|
|
96
|
+
}
|
|
97
|
+
entries() {
|
|
98
|
+
return [...this.#byDest].map(([dest, source]) => ({ dest, source }));
|
|
99
|
+
}
|
|
100
|
+
/** Copy every collected asset beside the bundle; returns the bundle-relative files written. */
|
|
101
|
+
writeTo(bundleDir) {
|
|
102
|
+
const written = [];
|
|
103
|
+
for (const { source, dest } of this.entries()) {
|
|
104
|
+
const target = join(bundleDir, dest);
|
|
105
|
+
if (relative(bundleDir, target).startsWith('..'))
|
|
106
|
+
continue;
|
|
107
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
108
|
+
copyFileSync(source, target);
|
|
109
|
+
written.push(dest);
|
|
110
|
+
}
|
|
111
|
+
return written;
|
|
112
|
+
}
|
|
113
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/rolldown-plugin-gjsify",
|
|
3
|
-
"version": "0.64.
|
|
3
|
+
"version": "0.64.42",
|
|
4
4
|
"description": "Rolldown / Rollup / Vite plugin orchestrator for GJS, Node, and Browser targets",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -67,13 +67,13 @@
|
|
|
67
67
|
],
|
|
68
68
|
"license": "MIT",
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@gjsify/console": "0.64.
|
|
71
|
-
"@gjsify/resolve-npm": "0.64.
|
|
72
|
-
"@gjsify/rolldown-plugin-deepkit": "0.64.
|
|
73
|
-
"@gjsify/rolldown-plugin-pnp": "0.64.
|
|
74
|
-
"@gjsify/utils": "0.64.
|
|
75
|
-
"@gjsify/vite-plugin-blueprint": "0.64.
|
|
76
|
-
"@gjsify/vite-plugin-gettext": "0.64.
|
|
70
|
+
"@gjsify/console": "^0.64.42",
|
|
71
|
+
"@gjsify/resolve-npm": "^0.64.42",
|
|
72
|
+
"@gjsify/rolldown-plugin-deepkit": "^0.64.42",
|
|
73
|
+
"@gjsify/rolldown-plugin-pnp": "^0.64.42",
|
|
74
|
+
"@gjsify/utils": "^0.64.42",
|
|
75
|
+
"@gjsify/vite-plugin-blueprint": "^0.64.42",
|
|
76
|
+
"@gjsify/vite-plugin-gettext": "^0.64.42",
|
|
77
77
|
"@rollup/pluginutils": "^5.4.0",
|
|
78
78
|
"acorn": "^8.17.0",
|
|
79
79
|
"acorn-typescript": "^1.4.13",
|
|
@@ -83,7 +83,7 @@
|
|
|
83
83
|
"sass": "^1.101.0"
|
|
84
84
|
},
|
|
85
85
|
"peerDependencies": {
|
|
86
|
-
"@gjsify/lightningcss-native": "0.64.
|
|
86
|
+
"@gjsify/lightningcss-native": "^0.64.42",
|
|
87
87
|
"rolldown": "^1.1.4"
|
|
88
88
|
},
|
|
89
89
|
"peerDependenciesMeta": {
|
|
@@ -95,8 +95,8 @@
|
|
|
95
95
|
}
|
|
96
96
|
},
|
|
97
97
|
"devDependencies": {
|
|
98
|
-
"@gjsify/cli": "0.64.
|
|
99
|
-
"@gjsify/unit": "0.64.
|
|
98
|
+
"@gjsify/cli": "^0.64.42",
|
|
99
|
+
"@gjsify/unit": "^0.64.42",
|
|
100
100
|
"@types/node": "^25.9.2",
|
|
101
101
|
"rolldown": "^1.1.4",
|
|
102
102
|
"typescript": "^6.0.3"
|
|
@@ -108,6 +108,5 @@
|
|
|
108
108
|
"darwin": "supported",
|
|
109
109
|
"win32": "supported"
|
|
110
110
|
}
|
|
111
|
-
}
|
|
112
|
-
"gitHead": "b42bb6c4b1b1b2344bd3695eb3c9f7c14498c7a0"
|
|
111
|
+
}
|
|
113
112
|
}
|