@afterpack/angular 0.2.3 → 0.2.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/index.cjs +158 -5
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +159 -7
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -25,6 +25,7 @@ const node_path = __toESM(require("node:path"));
|
|
|
25
25
|
const __afterpack_core = __toESM(require("@afterpack/core"));
|
|
26
26
|
const __afterpack_integration_utils = __toESM(require("@afterpack/integration-utils"));
|
|
27
27
|
const node_fs = __toESM(require("node:fs"));
|
|
28
|
+
const node_crypto = __toESM(require("node:crypto"));
|
|
28
29
|
|
|
29
30
|
//#region src/browser-dir.ts
|
|
30
31
|
function findAngularBrowserDir(distRoot) {
|
|
@@ -38,6 +39,148 @@ function findAngularBrowserDir(distRoot) {
|
|
|
38
39
|
throw new Error(`@afterpack/angular: could not locate an Angular browser output under ${distRoot}; pass browserDir explicitly`);
|
|
39
40
|
}
|
|
40
41
|
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/integrity.ts
|
|
44
|
+
const SRI_ATTR = /\b(src|href)\s*=\s*"([^"]+)"[^>]*\bintegrity\s*=\s*"(sha(?:256|384|512))-[^"]*"/g;
|
|
45
|
+
const SRI_ATTR_REVERSED = /\bintegrity\s*=\s*"(sha(?:256|384|512))-[^"]*"[^>]*\b(src|href)\s*=\s*"([^"]+)"/g;
|
|
46
|
+
function digest(bytes, algo, enc) {
|
|
47
|
+
return (0, node_crypto.createHash)(algo).update(bytes).digest(enc);
|
|
48
|
+
}
|
|
49
|
+
function fileFor(baseDir, ref) {
|
|
50
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(ref) || ref.startsWith("//")) return null;
|
|
51
|
+
const path = ref.split(/[?#]/)[0];
|
|
52
|
+
if (path === "") return null;
|
|
53
|
+
return (0, node_path.join)(baseDir, path);
|
|
54
|
+
}
|
|
55
|
+
function recomputeHtmlIntegrity(browserDir) {
|
|
56
|
+
for (const html of htmlFilesUnder(browserDir)) {
|
|
57
|
+
const text = (0, node_fs.readFileSync)(html, "utf8");
|
|
58
|
+
const rewrite = (algo, ref, match) => {
|
|
59
|
+
const file = fileFor(browserDir, ref);
|
|
60
|
+
if (file === null || !(0, node_fs.existsSync)(file)) return match;
|
|
61
|
+
const hash = digest((0, node_fs.readFileSync)(file), algo, "base64");
|
|
62
|
+
return match.replace(/integrity\s*=\s*"sha(?:256|384|512)-[^"]*"/, `integrity="${algo}-${hash}"`);
|
|
63
|
+
};
|
|
64
|
+
let next = text.replace(SRI_ATTR, (match, _attr, ref, algo) => rewrite(algo, ref, match));
|
|
65
|
+
next = next.replace(SRI_ATTR_REVERSED, (match, algo, _attr, ref) => rewrite(algo, ref, match));
|
|
66
|
+
if (next !== text) (0, node_fs.writeFileSync)(html, next);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function htmlFilesUnder(dir) {
|
|
70
|
+
const out = [];
|
|
71
|
+
for (const entry of (0, node_fs.readdirSync)(dir, {
|
|
72
|
+
recursive: true,
|
|
73
|
+
encoding: "utf8"
|
|
74
|
+
})) if (entry.endsWith(".html")) out.push((0, node_path.join)(dir, entry));
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
function recomputeNgswHashTable(browserDir) {
|
|
78
|
+
const ngsw = (0, node_path.join)(browserDir, "ngsw.json");
|
|
79
|
+
if (!(0, node_fs.existsSync)(ngsw)) return;
|
|
80
|
+
const config = JSON.parse((0, node_fs.readFileSync)(ngsw, "utf8"));
|
|
81
|
+
const table = config.hashTable;
|
|
82
|
+
if (!table) return;
|
|
83
|
+
for (const url of Object.keys(table)) {
|
|
84
|
+
const file = (0, node_path.join)(browserDir, url.replace(/^\//, ""));
|
|
85
|
+
if (!(0, node_fs.existsSync)(file) || !(0, node_fs.statSync)(file).isFile()) throw new Error(`@afterpack/angular: ngsw.json names ${url}, which is not in the browser output after obfuscation. AfterPack cannot recompute the service worker hash table. Rebuild, or disable the service worker for this build.`);
|
|
86
|
+
table[url] = (0, node_crypto.createHash)("sha1").update((0, node_fs.readFileSync)(file)).digest("hex");
|
|
87
|
+
}
|
|
88
|
+
(0, node_fs.writeFileSync)(ngsw, `${JSON.stringify(config, null, 2)}\n`);
|
|
89
|
+
}
|
|
90
|
+
function refuseStaleServerManifest(outputRoot) {
|
|
91
|
+
const serverDir = (0, node_path.join)(outputRoot, "server");
|
|
92
|
+
if (!(0, node_fs.existsSync)(serverDir)) return;
|
|
93
|
+
for (const entry of (0, node_fs.readdirSync)(serverDir)) {
|
|
94
|
+
if (!/angular-app-(?:engine-)?manifest\.mjs$/.test(entry)) continue;
|
|
95
|
+
const text = (0, node_fs.readFileSync)((0, node_path.join)(serverDir, entry), "utf8");
|
|
96
|
+
if (/\bhash\s*:\s*['"`]/.test(text) || /\bintegrity\s*:/.test(text)) throw new Error(`@afterpack/angular: ${entry} embeds asset hashes that AfterPack cannot recompute after renaming and obfuscating the browser output, so the SSR ETags would be stale. This Angular server build is not supported — obfuscate only the browser output, or remove @afterpack/angular from the SSR build.`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function recomputeAngularIntegrity(outputRoot, browserDir) {
|
|
100
|
+
refuseStaleServerManifest(outputRoot);
|
|
101
|
+
recomputeHtmlIntegrity(browserDir);
|
|
102
|
+
recomputeNgswHashTable(browserDir);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/source-type.ts
|
|
107
|
+
const SCRIPT_TAG_RE = /<script\b([^>]*)>/gi;
|
|
108
|
+
const MODULEPRELOAD_RE = /<link\b([^>]*)>/gi;
|
|
109
|
+
const STATIC_SPECIFIER_RE = /(?:\bfrom|\bimport)\s*["']([^"']+)["']/g;
|
|
110
|
+
const DYNAMIC_SPECIFIER_RE = /\bimport\(\s*["']([^"']+)["']\s*\)/g;
|
|
111
|
+
const MODULE_WORKER_RE = /\bnew\s+(?:Shared)?Worker\(\s*new\s+URL\(\s*["']([^"']+)["']\s*,\s*import\.meta\.url\s*\)\s*,\s*\{\s*type\s*:\s*["']module["']/g;
|
|
112
|
+
function attribute(tag, name) {
|
|
113
|
+
const match = new RegExp(`(?:^|\\s)${name}\\s*=\\s*("([^"]*)"|'([^']*)'|([^\\s"'>]+))`, "i").exec(tag);
|
|
114
|
+
if (!match) return null;
|
|
115
|
+
return match[2] ?? match[3] ?? match[4] ?? "";
|
|
116
|
+
}
|
|
117
|
+
function htmlFiles(dir) {
|
|
118
|
+
return (0, node_fs.readdirSync)(dir, {
|
|
119
|
+
recursive: true,
|
|
120
|
+
encoding: "utf8"
|
|
121
|
+
}).filter((name) => name.endsWith(".html")).map((name) => (0, node_path.join)(dir, name));
|
|
122
|
+
}
|
|
123
|
+
function localTarget(base, ref, browserDir) {
|
|
124
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(ref) || ref.startsWith("//")) return null;
|
|
125
|
+
const path = ref.split(/[?#]/)[0];
|
|
126
|
+
if (path === "") return null;
|
|
127
|
+
const target = path.startsWith("/") ? (0, node_path.join)(browserDir, path) : (0, node_path.resolve)(base, path);
|
|
128
|
+
const rel = (0, node_path.relative)(browserDir, target);
|
|
129
|
+
if (rel.startsWith("..") || rel.startsWith(node_path.sep)) return null;
|
|
130
|
+
return target;
|
|
131
|
+
}
|
|
132
|
+
function classifyAngularScripts(browserDir, files) {
|
|
133
|
+
const collected = new Set(files.map((file) => (0, node_path.resolve)(file)));
|
|
134
|
+
const module$1 = new Set();
|
|
135
|
+
const classic = new Set();
|
|
136
|
+
const pending = [];
|
|
137
|
+
const claimModule = (target) => {
|
|
138
|
+
if (target === null || !collected.has(target) || module$1.has(target)) return;
|
|
139
|
+
module$1.add(target);
|
|
140
|
+
pending.push(target);
|
|
141
|
+
};
|
|
142
|
+
for (const html of htmlFiles(browserDir)) {
|
|
143
|
+
const text = (0, node_fs.readFileSync)(html, "utf8");
|
|
144
|
+
const base = (0, node_path.dirname)(html);
|
|
145
|
+
for (const [, tag] of text.matchAll(SCRIPT_TAG_RE)) {
|
|
146
|
+
const src = attribute(tag, "src");
|
|
147
|
+
if (src === null) continue;
|
|
148
|
+
const target = localTarget(base, src, browserDir);
|
|
149
|
+
if ((attribute(tag, "type") ?? "").trim().toLowerCase() === "module") claimModule(target);
|
|
150
|
+
else if (target !== null && collected.has(target)) classic.add(target);
|
|
151
|
+
}
|
|
152
|
+
for (const [, tag] of text.matchAll(MODULEPRELOAD_RE)) {
|
|
153
|
+
if ((attribute(tag, "rel") ?? "").trim().toLowerCase() !== "modulepreload") continue;
|
|
154
|
+
const href = attribute(tag, "href");
|
|
155
|
+
if (href !== null) claimModule(localTarget(base, href, browserDir));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
while (pending.length > 0) {
|
|
159
|
+
const file = pending.pop();
|
|
160
|
+
const code = (0, node_fs.readFileSync)(file, "utf8");
|
|
161
|
+
const base = (0, node_path.dirname)(file);
|
|
162
|
+
for (const re of [
|
|
163
|
+
STATIC_SPECIFIER_RE,
|
|
164
|
+
DYNAMIC_SPECIFIER_RE,
|
|
165
|
+
MODULE_WORKER_RE
|
|
166
|
+
]) for (const [, ref] of code.matchAll(re)) {
|
|
167
|
+
const relativeRef = re === MODULE_WORKER_RE && !/^[./]/.test(ref) ? `./${ref}` : ref;
|
|
168
|
+
if (!/^\.{1,2}\//.test(relativeRef) && !relativeRef.startsWith("/")) continue;
|
|
169
|
+
claimModule(localTarget(base, relativeRef, browserDir));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const sorted = (paths) => [...paths].sort();
|
|
173
|
+
return {
|
|
174
|
+
module: sorted([...module$1].filter((file) => !classic.has(file))),
|
|
175
|
+
classic: sorted(classic),
|
|
176
|
+
unclaimed: sorted([...collected].filter((file) => !module$1.has(file) && !classic.has(file)))
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function angularSourceType(kinds) {
|
|
180
|
+
if (kinds.module.length === 0) return void 0;
|
|
181
|
+
return kinds.classic.length === 0 && kinds.unclaimed.length === 0 ? "module" : void 0;
|
|
182
|
+
}
|
|
183
|
+
|
|
41
184
|
//#endregion
|
|
42
185
|
//#region src/index.ts
|
|
43
186
|
const LOCAL_KEYS = [
|
|
@@ -47,6 +190,11 @@ const LOCAL_KEYS = [
|
|
|
47
190
|
];
|
|
48
191
|
const DIRECTIVES_UNSUPPORTED = "the Angular application builder is sealed (no plugin hook), so this postbuild pass only ever sees already-minified output and cannot capture `/* @afterpack ... */` comments. Remove the key; to use directives, build through a bundler AfterPack can hook (@afterpack/vite, @afterpack/webpack, @afterpack/esbuild).";
|
|
49
192
|
const DIRECTIVES_ALWAYS_DISABLED = false;
|
|
193
|
+
const ESBUILD_HASHED_NAME_RE = /-([A-Z0-9]{8})\.(?:js|mjs|css)$/;
|
|
194
|
+
const angularHashToken = (relativePath) => ESBUILD_HASHED_NAME_RE.exec((0, node_path.basename)(relativePath))?.[1] ?? null;
|
|
195
|
+
function applicationOutputRoot(browserDir) {
|
|
196
|
+
return (0, node_path.basename)(browserDir) === "browser" ? (0, node_path.dirname)(browserDir) : browserDir;
|
|
197
|
+
}
|
|
50
198
|
async function afterpackAngular(options = {}) {
|
|
51
199
|
const startedBeforeDirDiscovery = Date.now();
|
|
52
200
|
const cwd = options.cwd ?? process.cwd();
|
|
@@ -65,7 +213,7 @@ async function afterpackAngular(options = {}) {
|
|
|
65
213
|
const browserDir = options.browserDir ?? findAngularBrowserDir((0, node_path.resolve)(cwd, options.distRoot ?? "dist"));
|
|
66
214
|
const files = (0, __afterpack_integration_utils.collectJsFiles)(browserDir, { include: settings.paths?.include });
|
|
67
215
|
if (files.length === 0) throw new Error(`@afterpack/angular: no .js/.mjs/.cjs files found under ${browserDir}`);
|
|
68
|
-
const
|
|
216
|
+
const outputRoot = applicationOutputRoot(browserDir);
|
|
69
217
|
await (0, __afterpack_integration_utils.runObfuscationPass)({
|
|
70
218
|
files,
|
|
71
219
|
engine: {
|
|
@@ -77,14 +225,19 @@ async function afterpackAngular(options = {}) {
|
|
|
77
225
|
label: "afterpack-angular",
|
|
78
226
|
cwd,
|
|
79
227
|
startedAt: startedBeforeDirDiscovery,
|
|
80
|
-
combinedProtectionMap: {
|
|
81
|
-
|
|
82
|
-
|
|
228
|
+
combinedProtectionMap: { buildDir: browserDir },
|
|
229
|
+
sourceMapSweepDir: browserDir,
|
|
230
|
+
hashedNames: {
|
|
231
|
+
root: outputRoot,
|
|
232
|
+
tokenOf: angularHashToken
|
|
83
233
|
},
|
|
84
234
|
...(0, __afterpack_integration_utils.passSettings)(resolved),
|
|
235
|
+
sourceType: angularSourceType(classifyAngularScripts(browserDir, files)),
|
|
85
236
|
directivesEnabled: DIRECTIVES_ALWAYS_DISABLED
|
|
86
237
|
});
|
|
238
|
+
recomputeAngularIntegrity(outputRoot, browserDir);
|
|
87
239
|
}
|
|
88
240
|
|
|
89
241
|
//#endregion
|
|
90
|
-
exports.afterpackAngular = afterpackAngular;
|
|
242
|
+
exports.afterpackAngular = afterpackAngular;
|
|
243
|
+
exports.angularHashToken = angularHashToken;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AfterpackPluginOptions } from "@afterpack/integration-utils";
|
|
1
|
+
import { AfterpackPluginOptions, HashTokenOf } from "@afterpack/integration-utils";
|
|
2
2
|
|
|
3
3
|
//#region src/index.d.ts
|
|
4
4
|
type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
|
|
@@ -6,6 +6,7 @@ type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
|
|
|
6
6
|
distRoot?: string;
|
|
7
7
|
browserDir?: string;
|
|
8
8
|
};
|
|
9
|
+
declare const angularHashToken: HashTokenOf;
|
|
9
10
|
declare function afterpackAngular(options?: AfterpackAngularOptions): Promise<void>;
|
|
10
11
|
//#endregion
|
|
11
|
-
export { AfterpackAngularOptions, afterpackAngular };
|
|
12
|
+
export { AfterpackAngularOptions, afterpackAngular, angularHashToken };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AfterpackPluginOptions } from "@afterpack/integration-utils";
|
|
1
|
+
import { AfterpackPluginOptions, HashTokenOf } from "@afterpack/integration-utils";
|
|
2
2
|
|
|
3
3
|
//#region src/index.d.ts
|
|
4
4
|
type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
|
|
@@ -6,6 +6,7 @@ type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
|
|
|
6
6
|
distRoot?: string;
|
|
7
7
|
browserDir?: string;
|
|
8
8
|
};
|
|
9
|
+
declare const angularHashToken: HashTokenOf;
|
|
9
10
|
declare function afterpackAngular(options?: AfterpackAngularOptions): Promise<void>;
|
|
10
11
|
//#endregion
|
|
11
|
-
export { AfterpackAngularOptions, afterpackAngular };
|
|
12
|
+
export { AfterpackAngularOptions, afterpackAngular, angularHashToken };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { join, resolve } from "node:path";
|
|
1
|
+
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
2
2
|
import { processBatch, version } from "@afterpack/core";
|
|
3
3
|
import { collectJsFiles, createTelemetryReporter, passSettings, resolveClientIdentity, resolvePluginConfig, runObfuscationPass } from "@afterpack/integration-utils";
|
|
4
|
-
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
4
|
+
import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
5
6
|
|
|
6
7
|
//#region src/browser-dir.ts
|
|
7
8
|
function findAngularBrowserDir(distRoot) {
|
|
@@ -15,6 +16,148 @@ function findAngularBrowserDir(distRoot) {
|
|
|
15
16
|
throw new Error(`@afterpack/angular: could not locate an Angular browser output under ${distRoot}; pass browserDir explicitly`);
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/integrity.ts
|
|
21
|
+
const SRI_ATTR = /\b(src|href)\s*=\s*"([^"]+)"[^>]*\bintegrity\s*=\s*"(sha(?:256|384|512))-[^"]*"/g;
|
|
22
|
+
const SRI_ATTR_REVERSED = /\bintegrity\s*=\s*"(sha(?:256|384|512))-[^"]*"[^>]*\b(src|href)\s*=\s*"([^"]+)"/g;
|
|
23
|
+
function digest(bytes, algo, enc) {
|
|
24
|
+
return createHash(algo).update(bytes).digest(enc);
|
|
25
|
+
}
|
|
26
|
+
function fileFor(baseDir, ref) {
|
|
27
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(ref) || ref.startsWith("//")) return null;
|
|
28
|
+
const path = ref.split(/[?#]/)[0];
|
|
29
|
+
if (path === "") return null;
|
|
30
|
+
return join(baseDir, path);
|
|
31
|
+
}
|
|
32
|
+
function recomputeHtmlIntegrity(browserDir) {
|
|
33
|
+
for (const html of htmlFilesUnder(browserDir)) {
|
|
34
|
+
const text = readFileSync(html, "utf8");
|
|
35
|
+
const rewrite = (algo, ref, match) => {
|
|
36
|
+
const file = fileFor(browserDir, ref);
|
|
37
|
+
if (file === null || !existsSync(file)) return match;
|
|
38
|
+
const hash = digest(readFileSync(file), algo, "base64");
|
|
39
|
+
return match.replace(/integrity\s*=\s*"sha(?:256|384|512)-[^"]*"/, `integrity="${algo}-${hash}"`);
|
|
40
|
+
};
|
|
41
|
+
let next = text.replace(SRI_ATTR, (match, _attr, ref, algo) => rewrite(algo, ref, match));
|
|
42
|
+
next = next.replace(SRI_ATTR_REVERSED, (match, algo, _attr, ref) => rewrite(algo, ref, match));
|
|
43
|
+
if (next !== text) writeFileSync(html, next);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function htmlFilesUnder(dir) {
|
|
47
|
+
const out = [];
|
|
48
|
+
for (const entry of readdirSync(dir, {
|
|
49
|
+
recursive: true,
|
|
50
|
+
encoding: "utf8"
|
|
51
|
+
})) if (entry.endsWith(".html")) out.push(join(dir, entry));
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
function recomputeNgswHashTable(browserDir) {
|
|
55
|
+
const ngsw = join(browserDir, "ngsw.json");
|
|
56
|
+
if (!existsSync(ngsw)) return;
|
|
57
|
+
const config = JSON.parse(readFileSync(ngsw, "utf8"));
|
|
58
|
+
const table = config.hashTable;
|
|
59
|
+
if (!table) return;
|
|
60
|
+
for (const url of Object.keys(table)) {
|
|
61
|
+
const file = join(browserDir, url.replace(/^\//, ""));
|
|
62
|
+
if (!existsSync(file) || !statSync(file).isFile()) throw new Error(`@afterpack/angular: ngsw.json names ${url}, which is not in the browser output after obfuscation. AfterPack cannot recompute the service worker hash table. Rebuild, or disable the service worker for this build.`);
|
|
63
|
+
table[url] = createHash("sha1").update(readFileSync(file)).digest("hex");
|
|
64
|
+
}
|
|
65
|
+
writeFileSync(ngsw, `${JSON.stringify(config, null, 2)}\n`);
|
|
66
|
+
}
|
|
67
|
+
function refuseStaleServerManifest(outputRoot) {
|
|
68
|
+
const serverDir = join(outputRoot, "server");
|
|
69
|
+
if (!existsSync(serverDir)) return;
|
|
70
|
+
for (const entry of readdirSync(serverDir)) {
|
|
71
|
+
if (!/angular-app-(?:engine-)?manifest\.mjs$/.test(entry)) continue;
|
|
72
|
+
const text = readFileSync(join(serverDir, entry), "utf8");
|
|
73
|
+
if (/\bhash\s*:\s*['"`]/.test(text) || /\bintegrity\s*:/.test(text)) throw new Error(`@afterpack/angular: ${entry} embeds asset hashes that AfterPack cannot recompute after renaming and obfuscating the browser output, so the SSR ETags would be stale. This Angular server build is not supported — obfuscate only the browser output, or remove @afterpack/angular from the SSR build.`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function recomputeAngularIntegrity(outputRoot, browserDir) {
|
|
77
|
+
refuseStaleServerManifest(outputRoot);
|
|
78
|
+
recomputeHtmlIntegrity(browserDir);
|
|
79
|
+
recomputeNgswHashTable(browserDir);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/source-type.ts
|
|
84
|
+
const SCRIPT_TAG_RE = /<script\b([^>]*)>/gi;
|
|
85
|
+
const MODULEPRELOAD_RE = /<link\b([^>]*)>/gi;
|
|
86
|
+
const STATIC_SPECIFIER_RE = /(?:\bfrom|\bimport)\s*["']([^"']+)["']/g;
|
|
87
|
+
const DYNAMIC_SPECIFIER_RE = /\bimport\(\s*["']([^"']+)["']\s*\)/g;
|
|
88
|
+
const MODULE_WORKER_RE = /\bnew\s+(?:Shared)?Worker\(\s*new\s+URL\(\s*["']([^"']+)["']\s*,\s*import\.meta\.url\s*\)\s*,\s*\{\s*type\s*:\s*["']module["']/g;
|
|
89
|
+
function attribute(tag, name) {
|
|
90
|
+
const match = new RegExp(`(?:^|\\s)${name}\\s*=\\s*("([^"]*)"|'([^']*)'|([^\\s"'>]+))`, "i").exec(tag);
|
|
91
|
+
if (!match) return null;
|
|
92
|
+
return match[2] ?? match[3] ?? match[4] ?? "";
|
|
93
|
+
}
|
|
94
|
+
function htmlFiles(dir) {
|
|
95
|
+
return readdirSync(dir, {
|
|
96
|
+
recursive: true,
|
|
97
|
+
encoding: "utf8"
|
|
98
|
+
}).filter((name) => name.endsWith(".html")).map((name) => join(dir, name));
|
|
99
|
+
}
|
|
100
|
+
function localTarget(base, ref, browserDir) {
|
|
101
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(ref) || ref.startsWith("//")) return null;
|
|
102
|
+
const path = ref.split(/[?#]/)[0];
|
|
103
|
+
if (path === "") return null;
|
|
104
|
+
const target = path.startsWith("/") ? join(browserDir, path) : resolve(base, path);
|
|
105
|
+
const rel = relative(browserDir, target);
|
|
106
|
+
if (rel.startsWith("..") || rel.startsWith(sep)) return null;
|
|
107
|
+
return target;
|
|
108
|
+
}
|
|
109
|
+
function classifyAngularScripts(browserDir, files) {
|
|
110
|
+
const collected = new Set(files.map((file) => resolve(file)));
|
|
111
|
+
const module = new Set();
|
|
112
|
+
const classic = new Set();
|
|
113
|
+
const pending = [];
|
|
114
|
+
const claimModule = (target) => {
|
|
115
|
+
if (target === null || !collected.has(target) || module.has(target)) return;
|
|
116
|
+
module.add(target);
|
|
117
|
+
pending.push(target);
|
|
118
|
+
};
|
|
119
|
+
for (const html of htmlFiles(browserDir)) {
|
|
120
|
+
const text = readFileSync(html, "utf8");
|
|
121
|
+
const base = dirname(html);
|
|
122
|
+
for (const [, tag] of text.matchAll(SCRIPT_TAG_RE)) {
|
|
123
|
+
const src = attribute(tag, "src");
|
|
124
|
+
if (src === null) continue;
|
|
125
|
+
const target = localTarget(base, src, browserDir);
|
|
126
|
+
if ((attribute(tag, "type") ?? "").trim().toLowerCase() === "module") claimModule(target);
|
|
127
|
+
else if (target !== null && collected.has(target)) classic.add(target);
|
|
128
|
+
}
|
|
129
|
+
for (const [, tag] of text.matchAll(MODULEPRELOAD_RE)) {
|
|
130
|
+
if ((attribute(tag, "rel") ?? "").trim().toLowerCase() !== "modulepreload") continue;
|
|
131
|
+
const href = attribute(tag, "href");
|
|
132
|
+
if (href !== null) claimModule(localTarget(base, href, browserDir));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
while (pending.length > 0) {
|
|
136
|
+
const file = pending.pop();
|
|
137
|
+
const code = readFileSync(file, "utf8");
|
|
138
|
+
const base = dirname(file);
|
|
139
|
+
for (const re of [
|
|
140
|
+
STATIC_SPECIFIER_RE,
|
|
141
|
+
DYNAMIC_SPECIFIER_RE,
|
|
142
|
+
MODULE_WORKER_RE
|
|
143
|
+
]) for (const [, ref] of code.matchAll(re)) {
|
|
144
|
+
const relativeRef = re === MODULE_WORKER_RE && !/^[./]/.test(ref) ? `./${ref}` : ref;
|
|
145
|
+
if (!/^\.{1,2}\//.test(relativeRef) && !relativeRef.startsWith("/")) continue;
|
|
146
|
+
claimModule(localTarget(base, relativeRef, browserDir));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const sorted = (paths) => [...paths].sort();
|
|
150
|
+
return {
|
|
151
|
+
module: sorted([...module].filter((file) => !classic.has(file))),
|
|
152
|
+
classic: sorted(classic),
|
|
153
|
+
unclaimed: sorted([...collected].filter((file) => !module.has(file) && !classic.has(file)))
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function angularSourceType(kinds) {
|
|
157
|
+
if (kinds.module.length === 0) return void 0;
|
|
158
|
+
return kinds.classic.length === 0 && kinds.unclaimed.length === 0 ? "module" : void 0;
|
|
159
|
+
}
|
|
160
|
+
|
|
18
161
|
//#endregion
|
|
19
162
|
//#region src/index.ts
|
|
20
163
|
const LOCAL_KEYS = [
|
|
@@ -24,6 +167,11 @@ const LOCAL_KEYS = [
|
|
|
24
167
|
];
|
|
25
168
|
const DIRECTIVES_UNSUPPORTED = "the Angular application builder is sealed (no plugin hook), so this postbuild pass only ever sees already-minified output and cannot capture `/* @afterpack ... */` comments. Remove the key; to use directives, build through a bundler AfterPack can hook (@afterpack/vite, @afterpack/webpack, @afterpack/esbuild).";
|
|
26
169
|
const DIRECTIVES_ALWAYS_DISABLED = false;
|
|
170
|
+
const ESBUILD_HASHED_NAME_RE = /-([A-Z0-9]{8})\.(?:js|mjs|css)$/;
|
|
171
|
+
const angularHashToken = (relativePath) => ESBUILD_HASHED_NAME_RE.exec(basename(relativePath))?.[1] ?? null;
|
|
172
|
+
function applicationOutputRoot(browserDir) {
|
|
173
|
+
return basename(browserDir) === "browser" ? dirname(browserDir) : browserDir;
|
|
174
|
+
}
|
|
27
175
|
async function afterpackAngular(options = {}) {
|
|
28
176
|
const startedBeforeDirDiscovery = Date.now();
|
|
29
177
|
const cwd = options.cwd ?? process.cwd();
|
|
@@ -42,7 +190,7 @@ async function afterpackAngular(options = {}) {
|
|
|
42
190
|
const browserDir = options.browserDir ?? findAngularBrowserDir(resolve(cwd, options.distRoot ?? "dist"));
|
|
43
191
|
const files = collectJsFiles(browserDir, { include: settings.paths?.include });
|
|
44
192
|
if (files.length === 0) throw new Error(`@afterpack/angular: no .js/.mjs/.cjs files found under ${browserDir}`);
|
|
45
|
-
const
|
|
193
|
+
const outputRoot = applicationOutputRoot(browserDir);
|
|
46
194
|
await runObfuscationPass({
|
|
47
195
|
files,
|
|
48
196
|
engine: {
|
|
@@ -54,14 +202,18 @@ async function afterpackAngular(options = {}) {
|
|
|
54
202
|
label: "afterpack-angular",
|
|
55
203
|
cwd,
|
|
56
204
|
startedAt: startedBeforeDirDiscovery,
|
|
57
|
-
combinedProtectionMap: {
|
|
58
|
-
|
|
59
|
-
|
|
205
|
+
combinedProtectionMap: { buildDir: browserDir },
|
|
206
|
+
sourceMapSweepDir: browserDir,
|
|
207
|
+
hashedNames: {
|
|
208
|
+
root: outputRoot,
|
|
209
|
+
tokenOf: angularHashToken
|
|
60
210
|
},
|
|
61
211
|
...passSettings(resolved),
|
|
212
|
+
sourceType: angularSourceType(classifyAngularScripts(browserDir, files)),
|
|
62
213
|
directivesEnabled: DIRECTIVES_ALWAYS_DISABLED
|
|
63
214
|
});
|
|
215
|
+
recomputeAngularIntegrity(outputRoot, browserDir);
|
|
64
216
|
}
|
|
65
217
|
|
|
66
218
|
//#endregion
|
|
67
|
-
export { afterpackAngular };
|
|
219
|
+
export { afterpackAngular, angularHashToken };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@afterpack/angular",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Angular 17+ integration for AfterPack: obfuscate the browser bundle after ng build",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"dist"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@afterpack/core": "~0.2.
|
|
29
|
-
"@afterpack/integration-utils": "0.2.
|
|
28
|
+
"@afterpack/core": "~0.2.5",
|
|
29
|
+
"@afterpack/integration-utils": "0.2.4"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@biomejs/biome": "2.4.4",
|