@vorplex/compiler 0.0.63 → 0.0.65
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/dist/compiler.util.js
CHANGED
|
@@ -29,11 +29,14 @@ export class Compiler {
|
|
|
29
29
|
const task = options.task ?? new Task('Compile');
|
|
30
30
|
try {
|
|
31
31
|
task.log(`Compiling $[files] using entry file path (${options.entryFilePath})`, { attachments: { files: { type: 'yaml', value: stringify(options.files) } } });
|
|
32
|
+
const importOrigin = {};
|
|
32
33
|
return await Bundler.bundle({
|
|
33
34
|
path: options.entryFilePath,
|
|
34
35
|
script: options.files[options.entryFilePath].content,
|
|
35
36
|
sourcemaps: options.sourcemaps,
|
|
36
|
-
resolve: async ({ importerPath, importPath
|
|
37
|
+
resolve: async ({ importerPath, importPath }) => {
|
|
38
|
+
const origin = importerPath in options.files ? importerPath : importOrigin[importerPath] ?? importerPath;
|
|
39
|
+
importOrigin[importPath] ??= origin;
|
|
37
40
|
return await task.do(`Resolving: ${importPath}`, async (task) => {
|
|
38
41
|
if (importPath in options.files) {
|
|
39
42
|
task.log('Returning local file $[content]', { attachments: { content: { type: 'typescript', value: options.files[importPath].content } } });
|
|
@@ -66,7 +69,7 @@ export class Compiler {
|
|
|
66
69
|
}
|
|
67
70
|
}
|
|
68
71
|
try {
|
|
69
|
-
const dependencyTree = options.files[
|
|
72
|
+
const dependencyTree = options.files[origin]?.dependencyTree ?? options.files[importPath]?.dependencyTree ?? {};
|
|
70
73
|
const string = NPM.parseImportString(importPath);
|
|
71
74
|
task.log(`Package ${string.packageName} subpath ${string.subpath} version ${string.version}`);
|
|
72
75
|
if (!string.version) {
|
|
@@ -32,44 +32,34 @@ export class Bundler {
|
|
|
32
32
|
name: 'module-loader-resolver',
|
|
33
33
|
setup: (build) => {
|
|
34
34
|
const files = {};
|
|
35
|
-
const importParent = {};
|
|
36
|
-
const importPathAlias = {};
|
|
37
35
|
build.onResolve({ filter: /.*/ }, async (args) => {
|
|
38
36
|
const relative = args.path.startsWith('.') || args.path.startsWith('/');
|
|
39
37
|
const importerPath = args.importer === '<stdin>' ? options.path : args.importer;
|
|
40
38
|
const importPath = relative ? $Path.join(importerPath, `../${args.path}`) : args.path;
|
|
41
|
-
importParent[importPath] ??= importerPath;
|
|
42
|
-
let importStack = [];
|
|
43
|
-
const visited = new Set();
|
|
44
|
-
let parentPath = importerPath;
|
|
45
|
-
while (parentPath && !visited.has(parentPath)) {
|
|
46
|
-
visited.add(parentPath);
|
|
47
|
-
importStack.push(parentPath);
|
|
48
|
-
parentPath = importParent[importPathAlias[parentPath]] ?? importParent[parentPath];
|
|
49
|
-
}
|
|
50
39
|
const resolved = await options.resolve({
|
|
51
40
|
namespace: args.path,
|
|
52
41
|
importerPath,
|
|
53
42
|
importPath,
|
|
54
43
|
relative,
|
|
55
|
-
importStack
|
|
56
44
|
});
|
|
57
|
-
if (!resolved)
|
|
58
|
-
return { path: importPath, namespace: 'mock' };
|
|
59
|
-
if ('content' in resolved) {
|
|
60
|
-
files[resolved.path ?? importPath] = resolved.content;
|
|
61
|
-
if (resolved.path)
|
|
62
|
-
importPathAlias[resolved.path] = importPath;
|
|
45
|
+
if (!resolved) {
|
|
63
46
|
return {
|
|
64
|
-
path:
|
|
65
|
-
namespace: '
|
|
47
|
+
path: importPath,
|
|
48
|
+
namespace: 'mock',
|
|
66
49
|
};
|
|
67
50
|
}
|
|
68
|
-
|
|
51
|
+
if ('content' in resolved) {
|
|
52
|
+
const resolvedPath = resolved.path ?? importPath;
|
|
53
|
+
files[resolvedPath] = resolved.content;
|
|
69
54
|
return {
|
|
70
|
-
path:
|
|
71
|
-
|
|
55
|
+
path: resolvedPath,
|
|
56
|
+
namespace: 'virtual',
|
|
72
57
|
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
path: resolved.namespace,
|
|
61
|
+
external: true,
|
|
62
|
+
};
|
|
73
63
|
});
|
|
74
64
|
build.onLoad({ filter: /.*/, namespace: 'virtual' }, (args) => {
|
|
75
65
|
const loaders = {
|
|
@@ -93,8 +83,8 @@ export class Bundler {
|
|
|
93
83
|
},
|
|
94
84
|
],
|
|
95
85
|
sourcemap: options.sourcemaps ? 'inline' : false,
|
|
96
|
-
mainFields: ['module', 'main'],
|
|
97
|
-
conditions: ['import', 'module'],
|
|
86
|
+
mainFields: ['browser', 'module', 'main'],
|
|
87
|
+
conditions: ['browser', 'import', 'module'],
|
|
98
88
|
bundle: true,
|
|
99
89
|
format: 'cjs',
|
|
100
90
|
target: 'esnext',
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { $Path, $String, InMemoryStorage } from '@vorplex/core';
|
|
2
|
+
import { NPM } from '../npm.util';
|
|
2
3
|
export class JsDelivr {
|
|
3
4
|
static url = 'https://cdn.jsdelivr.net/npm';
|
|
4
5
|
static dataUrl = 'https://data.jsdelivr.com/v1/packages/npm';
|
|
@@ -123,67 +124,11 @@ export class JsDelivr {
|
|
|
123
124
|
if (cached)
|
|
124
125
|
return cached;
|
|
125
126
|
const packageJson = await this.getPackageJson(packageName, version);
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const resolveExportPath = (path) => {
|
|
132
|
-
if (!path)
|
|
133
|
-
return;
|
|
134
|
-
if (typeof path === 'string')
|
|
135
|
-
return path;
|
|
136
|
-
if (typeof path !== 'object')
|
|
137
|
-
return;
|
|
138
|
-
const priorities = ['browser', 'import', 'es2015', 'module', 'default', 'node', 'require'];
|
|
139
|
-
for (const priority of priorities) {
|
|
140
|
-
const resolvedPath = resolveExportPath(path[priority]);
|
|
141
|
-
if (resolvedPath)
|
|
142
|
-
return resolvedPath;
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
for (const [path, value] of Object.entries(exports)) {
|
|
146
|
-
let targetPath = resolveExportPath(value);
|
|
147
|
-
if (!targetPath || targetPath === './')
|
|
148
|
-
continue;
|
|
149
|
-
if (targetPath.startsWith('./'))
|
|
150
|
-
targetPath = targetPath.slice(2);
|
|
151
|
-
if ($Path.getExtension(targetPath) === '.cjs')
|
|
152
|
-
continue;
|
|
153
|
-
if (targetPath.includes('*')) {
|
|
154
|
-
const regex = new RegExp(`^${$String.sanitizeForRegex(targetPath).replace('\\*', '(.+)')}$`);
|
|
155
|
-
const files = await this.getFilePaths(packageName, semanticVersion, regex);
|
|
156
|
-
for (const file of files) {
|
|
157
|
-
const fileExtension = $Path.getExtension(file);
|
|
158
|
-
if (!['.js', '.mjs', '.json', '.jsx'].includes(fileExtension))
|
|
159
|
-
continue;
|
|
160
|
-
const wildcard = file.match(regex)[1];
|
|
161
|
-
const resolvedPath = path.replaceAll('*', wildcard);
|
|
162
|
-
const resolvedTargetPath = targetPath.replaceAll('*', wildcard);
|
|
163
|
-
if (Object.values(result).includes($Path.relative(resolvedTargetPath)))
|
|
164
|
-
continue;
|
|
165
|
-
result[resolvedPath] = $Path.relative(resolvedTargetPath);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
if (Object.values(result).includes($Path.relative(targetPath)))
|
|
170
|
-
continue;
|
|
171
|
-
result[path] = $Path.relative(targetPath);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return result;
|
|
175
|
-
};
|
|
176
|
-
const exports = await resolveExports(packageJson.exports);
|
|
177
|
-
if (!subpath && exports['.']) {
|
|
178
|
-
const result = $Path.absolute(exports['.']);
|
|
179
|
-
await this.cache.set('cache', 'file-path', key, result);
|
|
180
|
-
return result;
|
|
181
|
-
}
|
|
182
|
-
if (subpath && exports[$Path.relative(subpath)]) {
|
|
183
|
-
const result = $Path.absolute(exports[$Path.relative(subpath)]);
|
|
184
|
-
await this.cache.set('cache', 'file-path', key, result);
|
|
185
|
-
return result;
|
|
186
|
-
}
|
|
127
|
+
const filePaths = await this.getFilePaths(packageName, version);
|
|
128
|
+
const entryPoint = NPM.getPackageEntryPoint(packageJson, filePaths, subpath);
|
|
129
|
+
if (entryPoint) {
|
|
130
|
+
await this.cache.set('cache', 'file-path', key, entryPoint);
|
|
131
|
+
return entryPoint;
|
|
187
132
|
}
|
|
188
133
|
if (subpath) {
|
|
189
134
|
const file = await this.resolveFilePath(packageName, version, subpath);
|
|
@@ -192,11 +137,6 @@ export class JsDelivr {
|
|
|
192
137
|
return file;
|
|
193
138
|
}
|
|
194
139
|
}
|
|
195
|
-
else {
|
|
196
|
-
const result = $Path.absolute(packageJson.module || packageJson.main || 'index.js');
|
|
197
|
-
await this.cache.set('cache', 'file-path', key, result);
|
|
198
|
-
return result;
|
|
199
|
-
}
|
|
200
140
|
throw new Error(`Failed to resolve file path for import (${packageName}${subpath ? `/${subpath}` : ''}@${semanticVersion})`);
|
|
201
141
|
}
|
|
202
142
|
}
|
package/dist/npm/npm.util.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export declare class NPM {
|
|
|
15
15
|
}, task?: Task): Promise<DependencyTree>;
|
|
16
16
|
static flattenDependencyTree(tree: DependencyTree): Record<string, string[]>;
|
|
17
17
|
static resolveDependencyVersion(tree: DependencyTree, parent: string, version: string, name: string): string;
|
|
18
|
+
static getPackageEntryPoint(packageJson: PackageJson, filePaths: string[], subpath?: string): string | null;
|
|
18
19
|
static getScriptImports(script: string): string[];
|
|
19
20
|
static parseImportString(string: string): {
|
|
20
21
|
packageName: string;
|
package/dist/npm/npm.util.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { $Path, Task } from '@vorplex/core';
|
|
1
|
+
import { $Path, $String, Task } from '@vorplex/core';
|
|
2
2
|
import { satisfies } from 'semver';
|
|
3
3
|
import { stringify } from 'yaml';
|
|
4
4
|
export class NPM {
|
|
@@ -106,6 +106,75 @@ export class NPM {
|
|
|
106
106
|
};
|
|
107
107
|
return resolve(tree);
|
|
108
108
|
}
|
|
109
|
+
static getPackageEntryPoint(packageJson, filePaths, subpath) {
|
|
110
|
+
if (packageJson.exports) {
|
|
111
|
+
const resolveExports = (exports) => {
|
|
112
|
+
const result = {};
|
|
113
|
+
if (typeof exports === 'string')
|
|
114
|
+
return { '.': $Path.relative(exports) };
|
|
115
|
+
const resolveExportPath = (path) => {
|
|
116
|
+
if (!path)
|
|
117
|
+
return;
|
|
118
|
+
if (typeof path === 'string')
|
|
119
|
+
return path;
|
|
120
|
+
if (typeof path !== 'object')
|
|
121
|
+
return;
|
|
122
|
+
for (const priority of ['browser', 'import', 'es2015', 'module', 'default', 'node', 'require']) {
|
|
123
|
+
const resolvedPath = resolveExportPath(path[priority]);
|
|
124
|
+
if (resolvedPath)
|
|
125
|
+
return resolvedPath;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
for (const [path, value] of Object.entries(exports)) {
|
|
129
|
+
let targetPath = resolveExportPath(value);
|
|
130
|
+
if (!targetPath || targetPath === './')
|
|
131
|
+
continue;
|
|
132
|
+
if (targetPath.startsWith('./'))
|
|
133
|
+
targetPath = targetPath.slice(2);
|
|
134
|
+
if ($Path.getExtension(targetPath) === '.cjs')
|
|
135
|
+
continue;
|
|
136
|
+
if (targetPath.includes('*')) {
|
|
137
|
+
const regex = new RegExp(`^${$String.sanitizeForRegex(targetPath).replace('\\*', '(.+)')}$`);
|
|
138
|
+
for (const path of filePaths.filter(path => regex.test(path))) {
|
|
139
|
+
const extension = $Path.getExtension(path);
|
|
140
|
+
if (!['.js', '.mjs', '.json', '.jsx'].includes(extension))
|
|
141
|
+
continue;
|
|
142
|
+
const wildcard = path.match(regex)[1];
|
|
143
|
+
const resolvedPath = path.replaceAll('*', wildcard);
|
|
144
|
+
const resolvedTargetPath = targetPath.replaceAll('*', wildcard);
|
|
145
|
+
if (Object.values(result).includes($Path.relative(resolvedTargetPath)))
|
|
146
|
+
continue;
|
|
147
|
+
result[resolvedPath] = $Path.relative(resolvedTargetPath);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
if (Object.values(result).includes($Path.relative(targetPath)))
|
|
152
|
+
continue;
|
|
153
|
+
result[path] = $Path.relative(targetPath);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return result;
|
|
157
|
+
};
|
|
158
|
+
const exports = resolveExports(packageJson.exports);
|
|
159
|
+
if (!subpath && exports['.'])
|
|
160
|
+
return $Path.absolute(exports['.']);
|
|
161
|
+
if (subpath && exports[$Path.relative(subpath)])
|
|
162
|
+
return $Path.absolute(exports[$Path.relative(subpath)]);
|
|
163
|
+
}
|
|
164
|
+
if (!subpath) {
|
|
165
|
+
let entryPoint = $Path.absolute(packageJson.module || packageJson.main || 'index.js');
|
|
166
|
+
if (typeof packageJson.browser === 'string') {
|
|
167
|
+
entryPoint = $Path.absolute(packageJson.browser);
|
|
168
|
+
}
|
|
169
|
+
else if (packageJson.browser != null && typeof packageJson.browser === 'object') {
|
|
170
|
+
const remapped = Object.entries(packageJson.browser).find(([key]) => $Path.absolute(key) === entryPoint)?.[1];
|
|
171
|
+
if (typeof remapped === 'string')
|
|
172
|
+
entryPoint = $Path.absolute(remapped);
|
|
173
|
+
}
|
|
174
|
+
return entryPoint;
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
109
178
|
static getScriptImports(script) {
|
|
110
179
|
return Array.from(script.matchAll(/import\s+[\s\S]*?\s+from\s+['"]([^'"]+)['"]|require\(['"]([^'"]+)['"]\)/g)).map((match) => match[1] ?? match[2]);
|
|
111
180
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vorplex/compiler",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.65",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"files": [
|
|
@@ -15,16 +15,16 @@
|
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
16
|
"main": "./dist/index.js",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@vorplex/core": "0.0.63",
|
|
19
18
|
"esbuild-wasm": "0.25.12",
|
|
20
|
-
"tslib": "2.8.1",
|
|
21
19
|
"semver": "7.7.3",
|
|
20
|
+
"tslib": "2.8.1",
|
|
21
|
+
"@vorplex/core": "0.0.65",
|
|
22
22
|
"typescript": "5.9.3",
|
|
23
23
|
"yaml": "2.8.2"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"
|
|
27
|
-
"
|
|
26
|
+
"vitest": "3.2.4",
|
|
27
|
+
"@types/semver": "7.7.1"
|
|
28
28
|
},
|
|
29
29
|
"scripts": {
|
|
30
30
|
"build": "tsc -b --force tsconfig.build.json",
|