@caryhu/codemine-forge 0.0.1-alpha.2 → 0.0.1-alpha.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.
- package/dist/build-pipeline/angular-resources.d.ts +12 -0
- package/dist/build-pipeline/angular-resources.js +316 -0
- package/dist/build-pipeline/browser-bundle.d.ts +6 -0
- package/dist/build-pipeline/browser-bundle.js +128 -227
- package/dist/build-pipeline/diagnostics.d.ts +5 -0
- package/dist/build-pipeline/diagnostics.js +52 -1
- package/dist/build-pipeline/index.d.ts +2 -2
- package/dist/build-pipeline/index.js +6 -4
- package/dist/build-pipeline/pipeline.js +178 -11
- package/dist/build-pipeline/types.d.ts +7 -1
- package/dist/dev-server-preview/index.d.ts +2 -2
- package/dist/dev-server-preview/index.js +1 -4
- package/dist/dev-server-preview/server.d.ts +1 -1
- package/dist/dev-server-preview/server.js +9 -18
- package/dist/dev-server-preview/types.d.ts +2 -2
- package/dist/framework-compatibility.d.ts +27 -0
- package/dist/framework-compatibility.js +109 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +18 -8
- package/dist/package-resolution/cdn-source.js +13 -1
- package/dist/package-resolution/diagnostics.d.ts +3 -0
- package/dist/package-resolution/diagnostics.js +28 -1
- package/dist/package-resolution/index.d.ts +2 -1
- package/dist/package-resolution/index.js +4 -1
- package/dist/package-resolution/resolver.d.ts +3 -3
- package/dist/package-resolution/resolver.js +146 -7
- package/dist/package-resolution/types.d.ts +9 -1
- package/dist/terminal-commands/npm-dev.js +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ForgeVirtualFileSystem } from "../virtual-file-system";
|
|
2
|
+
import type { ForgeBuildPipelineDiagnostic } from "./types";
|
|
3
|
+
export interface InlineAngularComponentResourcesOptions {
|
|
4
|
+
readonly fileSystem: ForgeVirtualFileSystem;
|
|
5
|
+
readonly sourcePath: string;
|
|
6
|
+
readonly sourceText: string;
|
|
7
|
+
}
|
|
8
|
+
export interface InlineAngularComponentResourcesResult {
|
|
9
|
+
readonly outputText: string;
|
|
10
|
+
readonly diagnostics: readonly ForgeBuildPipelineDiagnostic[];
|
|
11
|
+
}
|
|
12
|
+
export declare function inlineAngularComponentResources(options: InlineAngularComponentResourcesOptions): Promise<InlineAngularComponentResourcesResult>;
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.inlineAngularComponentResources = void 0;
|
|
27
|
+
const ts = __importStar(require("typescript"));
|
|
28
|
+
const virtual_file_system_1 = require("../virtual-file-system");
|
|
29
|
+
const diagnostics_1 = require("./diagnostics");
|
|
30
|
+
const COMPONENT_RESOURCE_PROPERTIES = new Set([
|
|
31
|
+
"templateUrl",
|
|
32
|
+
"styleUrl",
|
|
33
|
+
"styleUrls",
|
|
34
|
+
]);
|
|
35
|
+
async function inlineAngularComponentResources(options) {
|
|
36
|
+
try {
|
|
37
|
+
const sourceFile = ts.createSourceFile(options.sourcePath, options.sourceText, ts.ScriptTarget.Latest, true, getScriptKind(options.sourcePath));
|
|
38
|
+
const componentObjects = collectComponentMetadataObjects(sourceFile);
|
|
39
|
+
if (componentObjects.length === 0) {
|
|
40
|
+
return {
|
|
41
|
+
outputText: options.sourceText,
|
|
42
|
+
diagnostics: [],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const diagnostics = [];
|
|
46
|
+
const rewrites = new Map();
|
|
47
|
+
for (const componentObject of componentObjects) {
|
|
48
|
+
const rewrite = await createComponentObjectRewrite(options, componentObject);
|
|
49
|
+
diagnostics.push(...rewrite.diagnostics);
|
|
50
|
+
if (rewrite.value !== undefined) {
|
|
51
|
+
rewrites.set(componentObject, rewrite.value);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (diagnostics.length > 0) {
|
|
55
|
+
return {
|
|
56
|
+
outputText: options.sourceText,
|
|
57
|
+
diagnostics,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (rewrites.size === 0) {
|
|
61
|
+
return {
|
|
62
|
+
outputText: options.sourceText,
|
|
63
|
+
diagnostics: [],
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const transformer = (context) => {
|
|
67
|
+
const visitor = (node) => {
|
|
68
|
+
if (ts.isCallExpression(node) &&
|
|
69
|
+
node.arguments.length > 0 &&
|
|
70
|
+
ts.isObjectLiteralExpression(node.arguments[0]) &&
|
|
71
|
+
isAngularComponentDecoratorCall(node)) {
|
|
72
|
+
const componentObject = node.arguments[0];
|
|
73
|
+
const rewrite = rewrites.get(componentObject);
|
|
74
|
+
if (rewrite !== undefined) {
|
|
75
|
+
return ts.factory.updateCallExpression(node, node.expression, node.typeArguments, [
|
|
76
|
+
updateComponentObjectLiteral(componentObject, rewrite),
|
|
77
|
+
...node.arguments.slice(1),
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return ts.visitEachChild(node, visitor, context);
|
|
82
|
+
};
|
|
83
|
+
return (node) => ts.visitNode(node, visitor);
|
|
84
|
+
};
|
|
85
|
+
const result = ts.transform(sourceFile, [transformer]);
|
|
86
|
+
try {
|
|
87
|
+
const transformedSourceFile = result.transformed[0];
|
|
88
|
+
const printer = ts.createPrinter({
|
|
89
|
+
newLine: ts.NewLineKind.LineFeed,
|
|
90
|
+
});
|
|
91
|
+
return {
|
|
92
|
+
outputText: printer.printFile(transformedSourceFile),
|
|
93
|
+
diagnostics: [],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
result.dispose();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
return {
|
|
102
|
+
outputText: options.sourceText,
|
|
103
|
+
diagnostics: [
|
|
104
|
+
(0, diagnostics_1.createAngularResourceTransformFailedDiagnostic)(options.sourcePath, error),
|
|
105
|
+
],
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.inlineAngularComponentResources = inlineAngularComponentResources;
|
|
110
|
+
function collectComponentMetadataObjects(sourceFile) {
|
|
111
|
+
const componentObjects = [];
|
|
112
|
+
function visit(node) {
|
|
113
|
+
if (ts.isCallExpression(node) &&
|
|
114
|
+
node.arguments.length > 0 &&
|
|
115
|
+
ts.isObjectLiteralExpression(node.arguments[0]) &&
|
|
116
|
+
isAngularComponentDecoratorCall(node) &&
|
|
117
|
+
hasComponentResourceProperty(node.arguments[0])) {
|
|
118
|
+
componentObjects.push(node.arguments[0]);
|
|
119
|
+
}
|
|
120
|
+
ts.forEachChild(node, visit);
|
|
121
|
+
}
|
|
122
|
+
visit(sourceFile);
|
|
123
|
+
return componentObjects;
|
|
124
|
+
}
|
|
125
|
+
async function createComponentObjectRewrite(options, componentObject) {
|
|
126
|
+
const diagnostics = [];
|
|
127
|
+
const styleResourceProperties = new Set();
|
|
128
|
+
const styleResources = [];
|
|
129
|
+
const templateResources = new Map();
|
|
130
|
+
for (const property of componentObject.properties) {
|
|
131
|
+
if (!ts.isPropertyAssignment(property)) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const propertyName = getPropertyNameText(property.name);
|
|
135
|
+
if (propertyName === "templateUrl") {
|
|
136
|
+
const resourcePath = getStringLiteralText(property.initializer);
|
|
137
|
+
if (resourcePath === undefined) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
const resource = await readComponentResource(options, resourcePath);
|
|
141
|
+
diagnostics.push(...resource.diagnostics);
|
|
142
|
+
if (resource.content !== undefined) {
|
|
143
|
+
templateResources.set(property, resource.content);
|
|
144
|
+
}
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (propertyName === "styleUrl") {
|
|
148
|
+
const resourcePath = getStringLiteralText(property.initializer);
|
|
149
|
+
if (resourcePath === undefined) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const resource = await readComponentResource(options, resourcePath);
|
|
153
|
+
diagnostics.push(...resource.diagnostics);
|
|
154
|
+
if (resource.content !== undefined) {
|
|
155
|
+
styleResourceProperties.add(property);
|
|
156
|
+
styleResources.push(resource.content);
|
|
157
|
+
}
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (propertyName === "styleUrls") {
|
|
161
|
+
const resourcePaths = getStringArrayLiteralText(property.initializer);
|
|
162
|
+
if (resourcePaths === undefined) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
for (const resourcePath of resourcePaths) {
|
|
166
|
+
const resource = await readComponentResource(options, resourcePath);
|
|
167
|
+
diagnostics.push(...resource.diagnostics);
|
|
168
|
+
if (resource.content !== undefined) {
|
|
169
|
+
styleResources.push(resource.content);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (resourcePaths.length > 0) {
|
|
173
|
+
styleResourceProperties.add(property);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (diagnostics.length > 0 ||
|
|
178
|
+
(styleResources.length === 0 && templateResources.size === 0)) {
|
|
179
|
+
return { diagnostics };
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
value: {
|
|
183
|
+
styleResourceProperties,
|
|
184
|
+
styleResources,
|
|
185
|
+
templateResources,
|
|
186
|
+
},
|
|
187
|
+
diagnostics,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function updateComponentObjectLiteral(componentObject, rewrite) {
|
|
191
|
+
const existingStylesProperty = componentObject.properties.find((property) => ts.isPropertyAssignment(property) &&
|
|
192
|
+
getPropertyNameText(property.name) === "styles");
|
|
193
|
+
const firstStyleResourceProperty = componentObject.properties.find((property) => ts.isPropertyAssignment(property) &&
|
|
194
|
+
rewrite.styleResourceProperties.has(property));
|
|
195
|
+
const updatedProperties = [];
|
|
196
|
+
for (const property of componentObject.properties) {
|
|
197
|
+
if (ts.isPropertyAssignment(property) &&
|
|
198
|
+
rewrite.templateResources.has(property)) {
|
|
199
|
+
updatedProperties.push(ts.factory.createPropertyAssignment("template", ts.factory.createStringLiteral(rewrite.templateResources.get(property) ?? "")));
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (ts.isPropertyAssignment(property) &&
|
|
203
|
+
rewrite.styleResourceProperties.has(property)) {
|
|
204
|
+
if (existingStylesProperty === undefined &&
|
|
205
|
+
property === firstStyleResourceProperty) {
|
|
206
|
+
updatedProperties.push(createStylesProperty(rewrite.styleResources));
|
|
207
|
+
}
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (property === existingStylesProperty) {
|
|
211
|
+
updatedProperties.push(updateStylesProperty(existingStylesProperty, rewrite.styleResources));
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
updatedProperties.push(property);
|
|
215
|
+
}
|
|
216
|
+
if (existingStylesProperty === undefined &&
|
|
217
|
+
firstStyleResourceProperty === undefined &&
|
|
218
|
+
rewrite.styleResources.length > 0) {
|
|
219
|
+
updatedProperties.push(createStylesProperty(rewrite.styleResources));
|
|
220
|
+
}
|
|
221
|
+
return ts.factory.updateObjectLiteralExpression(componentObject, updatedProperties);
|
|
222
|
+
}
|
|
223
|
+
function createStylesProperty(styles) {
|
|
224
|
+
return ts.factory.createPropertyAssignment("styles", ts.factory.createArrayLiteralExpression(styles.map((style) => ts.factory.createStringLiteral(style))));
|
|
225
|
+
}
|
|
226
|
+
function updateStylesProperty(property, styleResources) {
|
|
227
|
+
const existingStyles = ts.isArrayLiteralExpression(property.initializer)
|
|
228
|
+
? property.initializer.elements
|
|
229
|
+
: [property.initializer];
|
|
230
|
+
return ts.factory.updatePropertyAssignment(property, property.name, ts.factory.createArrayLiteralExpression([
|
|
231
|
+
...existingStyles,
|
|
232
|
+
...styleResources.map((style) => ts.factory.createStringLiteral(style)),
|
|
233
|
+
]));
|
|
234
|
+
}
|
|
235
|
+
async function readComponentResource(options, resourcePath) {
|
|
236
|
+
if (isExternalResourceUrl(resourcePath)) {
|
|
237
|
+
return {
|
|
238
|
+
diagnostics: [
|
|
239
|
+
(0, diagnostics_1.createAngularExternalResourceUnsupportedDiagnostic)(options.sourcePath, resourcePath),
|
|
240
|
+
],
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
for (const candidate of createResourcePathCandidates(options.sourcePath, resourcePath)) {
|
|
244
|
+
if (await options.fileSystem.exists(candidate)) {
|
|
245
|
+
return {
|
|
246
|
+
content: await options.fileSystem.readText(candidate),
|
|
247
|
+
diagnostics: [],
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
diagnostics: [
|
|
253
|
+
(0, diagnostics_1.createAngularResourceNotFoundDiagnostic)(options.sourcePath, resourcePath),
|
|
254
|
+
],
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function createResourcePathCandidates(sourcePath, resourcePath) {
|
|
258
|
+
const candidates = [];
|
|
259
|
+
const relativeCandidate = resourcePath.startsWith("/")
|
|
260
|
+
? resourcePath
|
|
261
|
+
: `${dirname(sourcePath)}/${resourcePath}`;
|
|
262
|
+
const rootCandidate = resourcePath.startsWith("/")
|
|
263
|
+
? resourcePath
|
|
264
|
+
: `/${resourcePath.replace(/^\.\//, "")}`;
|
|
265
|
+
for (const candidate of [relativeCandidate, rootCandidate]) {
|
|
266
|
+
const normalized = (0, virtual_file_system_1.normalizeForgePath)(candidate);
|
|
267
|
+
if (normalized.ok && !candidates.includes(normalized.path)) {
|
|
268
|
+
candidates.push(normalized.path);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return candidates;
|
|
272
|
+
}
|
|
273
|
+
function hasComponentResourceProperty(componentObject) {
|
|
274
|
+
return componentObject.properties.some((property) => ts.isPropertyAssignment(property) &&
|
|
275
|
+
COMPONENT_RESOURCE_PROPERTIES.has(getPropertyNameText(property.name) ?? ""));
|
|
276
|
+
}
|
|
277
|
+
function isAngularComponentDecoratorCall(node) {
|
|
278
|
+
return (ts.isDecorator(node.parent) &&
|
|
279
|
+
ts.isIdentifier(node.expression) &&
|
|
280
|
+
node.expression.text === "Component");
|
|
281
|
+
}
|
|
282
|
+
function getPropertyNameText(name) {
|
|
283
|
+
if (ts.isIdentifier(name) || ts.isStringLiteral(name)) {
|
|
284
|
+
return name.text;
|
|
285
|
+
}
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
function getStringLiteralText(node) {
|
|
289
|
+
return ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)
|
|
290
|
+
? node.text
|
|
291
|
+
: undefined;
|
|
292
|
+
}
|
|
293
|
+
function getStringArrayLiteralText(node) {
|
|
294
|
+
if (!ts.isArrayLiteralExpression(node)) {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
const values = [];
|
|
298
|
+
for (const element of node.elements) {
|
|
299
|
+
const value = getStringLiteralText(element);
|
|
300
|
+
if (value === undefined) {
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
values.push(value);
|
|
304
|
+
}
|
|
305
|
+
return values;
|
|
306
|
+
}
|
|
307
|
+
function isExternalResourceUrl(resourcePath) {
|
|
308
|
+
return /^(?:https?:|data:|blob:)/i.test(resourcePath);
|
|
309
|
+
}
|
|
310
|
+
function getScriptKind(path) {
|
|
311
|
+
return path.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
312
|
+
}
|
|
313
|
+
function dirname(path) {
|
|
314
|
+
const index = path.lastIndexOf("/");
|
|
315
|
+
return index <= 0 ? "/" : path.slice(0, index);
|
|
316
|
+
}
|
|
@@ -10,9 +10,15 @@ interface CreateBrowserBundleOptions {
|
|
|
10
10
|
readonly entry: string;
|
|
11
11
|
readonly fileSystem: ForgeVirtualFileSystem;
|
|
12
12
|
readonly packageResolution: ForgePackageResolutionResult;
|
|
13
|
+
readonly angularCompatibility?: ForgeBrowserBundleAngularCompatibility;
|
|
13
14
|
readonly scriptTransformer?: ForgeBrowserBundleScriptTransformer;
|
|
14
15
|
readonly viteConfig?: ForgeViteConfigMetadata;
|
|
15
16
|
}
|
|
17
|
+
interface ForgeBrowserBundleAngularCompatibility {
|
|
18
|
+
readonly injectZoneJs: boolean;
|
|
19
|
+
readonly inlineComponentResources: boolean;
|
|
20
|
+
readonly linker: "auto" | "diagnostic" | "disabled";
|
|
21
|
+
}
|
|
16
22
|
export type ForgeBrowserBundleScriptLoader = "ts" | "tsx";
|
|
17
23
|
export interface ForgeBrowserBundleScriptTransformRequest {
|
|
18
24
|
readonly loader: ForgeBrowserBundleScriptLoader;
|