@afterpack/angular 0.2.3 → 0.2.5

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 CHANGED
@@ -38,6 +38,96 @@ function findAngularBrowserDir(distRoot) {
38
38
  throw new Error(`@afterpack/angular: could not locate an Angular browser output under ${distRoot}; pass browserDir explicitly`);
39
39
  }
40
40
 
41
+ //#endregion
42
+ //#region src/source-type.ts
43
+ const SCRIPT_TAG_RE = /<script\b([^>]*)>/gi;
44
+ const MODULEPRELOAD_RE = /<link\b([^>]*)>/gi;
45
+ const STATIC_SPECIFIER_RE = /(?:\bfrom|\bimport)\s*["']([^"']+)["']/g;
46
+ const DYNAMIC_SPECIFIER_RE = /\bimport\(\s*["']([^"']+)["']\s*\)/g;
47
+ 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;
48
+ function attribute(tag, name) {
49
+ const match = new RegExp(`(?:^|\\s)${name}\\s*=\\s*("([^"]*)"|'([^']*)'|([^\\s"'>]+))`, "i").exec(tag);
50
+ if (!match) return null;
51
+ return match[2] ?? match[3] ?? match[4] ?? "";
52
+ }
53
+ function htmlFiles(dir) {
54
+ return (0, node_fs.readdirSync)(dir, {
55
+ recursive: true,
56
+ encoding: "utf8"
57
+ }).filter((name) => name.endsWith(".html")).map((name) => (0, node_path.join)(dir, name));
58
+ }
59
+ function localTarget(base, ref, browserDir) {
60
+ if (/^[a-z][a-z0-9+.-]*:/i.test(ref) || ref.startsWith("//")) return null;
61
+ const path = ref.split(/[?#]/)[0];
62
+ if (path === "") return null;
63
+ const target = path.startsWith("/") ? (0, node_path.join)(browserDir, path) : (0, node_path.resolve)(base, path);
64
+ const rel = (0, node_path.relative)(browserDir, target);
65
+ if (rel.startsWith("..") || rel.startsWith(node_path.sep)) return null;
66
+ return target;
67
+ }
68
+ function classifyAngularScripts(browserDir, files) {
69
+ const collected = new Set(files.map((file) => (0, node_path.resolve)(file)));
70
+ const module$1 = new Set();
71
+ const classic = new Set();
72
+ const pending = [];
73
+ const claimModule = (target) => {
74
+ if (target === null || !collected.has(target) || module$1.has(target)) return;
75
+ module$1.add(target);
76
+ pending.push(target);
77
+ };
78
+ for (const html of htmlFiles(browserDir)) {
79
+ const text = (0, node_fs.readFileSync)(html, "utf8");
80
+ const base = (0, node_path.dirname)(html);
81
+ for (const [, tag] of text.matchAll(SCRIPT_TAG_RE)) {
82
+ const src = attribute(tag, "src");
83
+ if (src === null) continue;
84
+ const target = localTarget(base, src, browserDir);
85
+ if ((attribute(tag, "type") ?? "").trim().toLowerCase() === "module") claimModule(target);
86
+ else if (target !== null && collected.has(target)) classic.add(target);
87
+ }
88
+ for (const [, tag] of text.matchAll(MODULEPRELOAD_RE)) {
89
+ if ((attribute(tag, "rel") ?? "").trim().toLowerCase() !== "modulepreload") continue;
90
+ const href = attribute(tag, "href");
91
+ if (href !== null) claimModule(localTarget(base, href, browserDir));
92
+ }
93
+ }
94
+ while (pending.length > 0) {
95
+ const file = pending.pop();
96
+ const code = (0, node_fs.readFileSync)(file, "utf8");
97
+ const base = (0, node_path.dirname)(file);
98
+ for (const re of [
99
+ STATIC_SPECIFIER_RE,
100
+ DYNAMIC_SPECIFIER_RE,
101
+ MODULE_WORKER_RE
102
+ ]) for (const [, ref] of code.matchAll(re)) {
103
+ const relativeRef = re === MODULE_WORKER_RE && !/^[./]/.test(ref) ? `./${ref}` : ref;
104
+ if (!/^\.{1,2}\//.test(relativeRef) && !relativeRef.startsWith("/")) continue;
105
+ claimModule(localTarget(base, relativeRef, browserDir));
106
+ }
107
+ }
108
+ const sorted = (paths) => [...paths].sort();
109
+ return {
110
+ module: sorted([...module$1].filter((file) => !classic.has(file))),
111
+ classic: sorted(classic),
112
+ unclaimed: sorted([...collected].filter((file) => !module$1.has(file) && !classic.has(file)))
113
+ };
114
+ }
115
+ function angularSourceType(kinds) {
116
+ if (kinds.module.length === 0) return void 0;
117
+ return kinds.classic.length === 0 && kinds.unclaimed.length === 0 ? "module" : void 0;
118
+ }
119
+ function angularFileSourceTypes(kinds, files) {
120
+ const module$1 = new Set(kinds.module);
121
+ const classic = new Set(kinds.classic);
122
+ const out = new Map();
123
+ for (const file of files) {
124
+ const path = (0, node_path.resolve)(file);
125
+ if (module$1.has(path)) out.set(file, "module");
126
+ else if (classic.has(path)) out.set(file, "script");
127
+ }
128
+ return out;
129
+ }
130
+
41
131
  //#endregion
42
132
  //#region src/index.ts
43
133
  const LOCAL_KEYS = [
@@ -47,6 +137,9 @@ const LOCAL_KEYS = [
47
137
  ];
48
138
  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
139
  const DIRECTIVES_ALWAYS_DISABLED = false;
140
+ function applicationOutputRoot(browserDir) {
141
+ return (0, node_path.basename)(browserDir) === "browser" ? (0, node_path.dirname)(browserDir) : browserDir;
142
+ }
50
143
  async function afterpackAngular(options = {}) {
51
144
  const startedBeforeDirDiscovery = Date.now();
52
145
  const cwd = options.cwd ?? process.cwd();
@@ -65,7 +158,8 @@ async function afterpackAngular(options = {}) {
65
158
  const browserDir = options.browserDir ?? findAngularBrowserDir((0, node_path.resolve)(cwd, options.distRoot ?? "dist"));
66
159
  const files = (0, __afterpack_integration_utils.collectJsFiles)(browserDir, { include: settings.paths?.include });
67
160
  if (files.length === 0) throw new Error(`@afterpack/angular: no .js/.mjs/.cjs files found under ${browserDir}`);
68
- const gitignoredNonServedAfterpackDir = (0, node_path.join)(cwd, ".afterpack");
161
+ const outputRoot = applicationOutputRoot(browserDir);
162
+ const kinds = classifyAngularScripts(browserDir, files);
69
163
  await (0, __afterpack_integration_utils.runObfuscationPass)({
70
164
  files,
71
165
  engine: {
@@ -77,14 +171,20 @@ async function afterpackAngular(options = {}) {
77
171
  label: "afterpack-angular",
78
172
  cwd,
79
173
  startedAt: startedBeforeDirDiscovery,
80
- combinedProtectionMap: {
81
- buildDir: browserDir,
82
- afterpackDir: gitignoredNonServedAfterpackDir
174
+ combinedProtectionMap: { buildDir: browserDir },
175
+ sourceMapSweepDir: browserDir,
176
+ hashedNames: {
177
+ root: outputRoot,
178
+ tokenOf: __afterpack_integration_utils.angularHashToken
83
179
  },
84
180
  ...(0, __afterpack_integration_utils.passSettings)(resolved),
181
+ sourceType: angularSourceType(kinds),
182
+ fileSourceTypes: angularFileSourceTypes(kinds, files),
85
183
  directivesEnabled: DIRECTIVES_ALWAYS_DISABLED
86
184
  });
185
+ (0, __afterpack_integration_utils.recomputeAngularIntegrity)(outputRoot, browserDir);
87
186
  }
88
187
 
89
188
  //#endregion
90
- exports.afterpackAngular = afterpackAngular;
189
+ exports.afterpackAngular = afterpackAngular;
190
+ exports.angularHashToken = __afterpack_integration_utils.angularHashToken;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { AfterpackPluginOptions } from "@afterpack/integration-utils";
1
+ import { AfterpackPluginOptions, angularHashToken } from "@afterpack/integration-utils";
2
2
 
3
3
  //#region src/index.d.ts
4
4
  type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
@@ -8,4 +8,4 @@ type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
8
8
  };
9
9
  declare function afterpackAngular(options?: AfterpackAngularOptions): Promise<void>;
10
10
  //#endregion
11
- export { AfterpackAngularOptions, afterpackAngular };
11
+ export { AfterpackAngularOptions, afterpackAngular, angularHashToken };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AfterpackPluginOptions } from "@afterpack/integration-utils";
1
+ import { AfterpackPluginOptions, angularHashToken } from "@afterpack/integration-utils";
2
2
 
3
3
  //#region src/index.d.ts
4
4
  type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
@@ -8,4 +8,4 @@ type AfterpackAngularOptions = Omit<AfterpackPluginOptions, "directives"> & {
8
8
  };
9
9
  declare function afterpackAngular(options?: AfterpackAngularOptions): Promise<void>;
10
10
  //#endregion
11
- export { AfterpackAngularOptions, afterpackAngular };
11
+ export { AfterpackAngularOptions, afterpackAngular, angularHashToken };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
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
- import { collectJsFiles, createTelemetryReporter, passSettings, resolveClientIdentity, resolvePluginConfig, runObfuscationPass } from "@afterpack/integration-utils";
4
- import { existsSync, readdirSync, statSync } from "node:fs";
3
+ import { angularHashToken, collectJsFiles, createTelemetryReporter, passSettings, recomputeAngularIntegrity, resolveClientIdentity, resolvePluginConfig, runObfuscationPass } from "@afterpack/integration-utils";
4
+ import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
5
5
 
6
6
  //#region src/browser-dir.ts
7
7
  function findAngularBrowserDir(distRoot) {
@@ -15,6 +15,96 @@ function findAngularBrowserDir(distRoot) {
15
15
  throw new Error(`@afterpack/angular: could not locate an Angular browser output under ${distRoot}; pass browserDir explicitly`);
16
16
  }
17
17
 
18
+ //#endregion
19
+ //#region src/source-type.ts
20
+ const SCRIPT_TAG_RE = /<script\b([^>]*)>/gi;
21
+ const MODULEPRELOAD_RE = /<link\b([^>]*)>/gi;
22
+ const STATIC_SPECIFIER_RE = /(?:\bfrom|\bimport)\s*["']([^"']+)["']/g;
23
+ const DYNAMIC_SPECIFIER_RE = /\bimport\(\s*["']([^"']+)["']\s*\)/g;
24
+ 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;
25
+ function attribute(tag, name) {
26
+ const match = new RegExp(`(?:^|\\s)${name}\\s*=\\s*("([^"]*)"|'([^']*)'|([^\\s"'>]+))`, "i").exec(tag);
27
+ if (!match) return null;
28
+ return match[2] ?? match[3] ?? match[4] ?? "";
29
+ }
30
+ function htmlFiles(dir) {
31
+ return readdirSync(dir, {
32
+ recursive: true,
33
+ encoding: "utf8"
34
+ }).filter((name) => name.endsWith(".html")).map((name) => join(dir, name));
35
+ }
36
+ function localTarget(base, ref, browserDir) {
37
+ if (/^[a-z][a-z0-9+.-]*:/i.test(ref) || ref.startsWith("//")) return null;
38
+ const path = ref.split(/[?#]/)[0];
39
+ if (path === "") return null;
40
+ const target = path.startsWith("/") ? join(browserDir, path) : resolve(base, path);
41
+ const rel = relative(browserDir, target);
42
+ if (rel.startsWith("..") || rel.startsWith(sep)) return null;
43
+ return target;
44
+ }
45
+ function classifyAngularScripts(browserDir, files) {
46
+ const collected = new Set(files.map((file) => resolve(file)));
47
+ const module = new Set();
48
+ const classic = new Set();
49
+ const pending = [];
50
+ const claimModule = (target) => {
51
+ if (target === null || !collected.has(target) || module.has(target)) return;
52
+ module.add(target);
53
+ pending.push(target);
54
+ };
55
+ for (const html of htmlFiles(browserDir)) {
56
+ const text = readFileSync(html, "utf8");
57
+ const base = dirname(html);
58
+ for (const [, tag] of text.matchAll(SCRIPT_TAG_RE)) {
59
+ const src = attribute(tag, "src");
60
+ if (src === null) continue;
61
+ const target = localTarget(base, src, browserDir);
62
+ if ((attribute(tag, "type") ?? "").trim().toLowerCase() === "module") claimModule(target);
63
+ else if (target !== null && collected.has(target)) classic.add(target);
64
+ }
65
+ for (const [, tag] of text.matchAll(MODULEPRELOAD_RE)) {
66
+ if ((attribute(tag, "rel") ?? "").trim().toLowerCase() !== "modulepreload") continue;
67
+ const href = attribute(tag, "href");
68
+ if (href !== null) claimModule(localTarget(base, href, browserDir));
69
+ }
70
+ }
71
+ while (pending.length > 0) {
72
+ const file = pending.pop();
73
+ const code = readFileSync(file, "utf8");
74
+ const base = dirname(file);
75
+ for (const re of [
76
+ STATIC_SPECIFIER_RE,
77
+ DYNAMIC_SPECIFIER_RE,
78
+ MODULE_WORKER_RE
79
+ ]) for (const [, ref] of code.matchAll(re)) {
80
+ const relativeRef = re === MODULE_WORKER_RE && !/^[./]/.test(ref) ? `./${ref}` : ref;
81
+ if (!/^\.{1,2}\//.test(relativeRef) && !relativeRef.startsWith("/")) continue;
82
+ claimModule(localTarget(base, relativeRef, browserDir));
83
+ }
84
+ }
85
+ const sorted = (paths) => [...paths].sort();
86
+ return {
87
+ module: sorted([...module].filter((file) => !classic.has(file))),
88
+ classic: sorted(classic),
89
+ unclaimed: sorted([...collected].filter((file) => !module.has(file) && !classic.has(file)))
90
+ };
91
+ }
92
+ function angularSourceType(kinds) {
93
+ if (kinds.module.length === 0) return void 0;
94
+ return kinds.classic.length === 0 && kinds.unclaimed.length === 0 ? "module" : void 0;
95
+ }
96
+ function angularFileSourceTypes(kinds, files) {
97
+ const module = new Set(kinds.module);
98
+ const classic = new Set(kinds.classic);
99
+ const out = new Map();
100
+ for (const file of files) {
101
+ const path = resolve(file);
102
+ if (module.has(path)) out.set(file, "module");
103
+ else if (classic.has(path)) out.set(file, "script");
104
+ }
105
+ return out;
106
+ }
107
+
18
108
  //#endregion
19
109
  //#region src/index.ts
20
110
  const LOCAL_KEYS = [
@@ -24,6 +114,9 @@ const LOCAL_KEYS = [
24
114
  ];
25
115
  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
116
  const DIRECTIVES_ALWAYS_DISABLED = false;
117
+ function applicationOutputRoot(browserDir) {
118
+ return basename(browserDir) === "browser" ? dirname(browserDir) : browserDir;
119
+ }
27
120
  async function afterpackAngular(options = {}) {
28
121
  const startedBeforeDirDiscovery = Date.now();
29
122
  const cwd = options.cwd ?? process.cwd();
@@ -42,7 +135,8 @@ async function afterpackAngular(options = {}) {
42
135
  const browserDir = options.browserDir ?? findAngularBrowserDir(resolve(cwd, options.distRoot ?? "dist"));
43
136
  const files = collectJsFiles(browserDir, { include: settings.paths?.include });
44
137
  if (files.length === 0) throw new Error(`@afterpack/angular: no .js/.mjs/.cjs files found under ${browserDir}`);
45
- const gitignoredNonServedAfterpackDir = join(cwd, ".afterpack");
138
+ const outputRoot = applicationOutputRoot(browserDir);
139
+ const kinds = classifyAngularScripts(browserDir, files);
46
140
  await runObfuscationPass({
47
141
  files,
48
142
  engine: {
@@ -54,14 +148,19 @@ async function afterpackAngular(options = {}) {
54
148
  label: "afterpack-angular",
55
149
  cwd,
56
150
  startedAt: startedBeforeDirDiscovery,
57
- combinedProtectionMap: {
58
- buildDir: browserDir,
59
- afterpackDir: gitignoredNonServedAfterpackDir
151
+ combinedProtectionMap: { buildDir: browserDir },
152
+ sourceMapSweepDir: browserDir,
153
+ hashedNames: {
154
+ root: outputRoot,
155
+ tokenOf: angularHashToken
60
156
  },
61
157
  ...passSettings(resolved),
158
+ sourceType: angularSourceType(kinds),
159
+ fileSourceTypes: angularFileSourceTypes(kinds, files),
62
160
  directivesEnabled: DIRECTIVES_ALWAYS_DISABLED
63
161
  });
162
+ recomputeAngularIntegrity(outputRoot, browserDir);
64
163
  }
65
164
 
66
165
  //#endregion
67
- export { afterpackAngular };
166
+ export { afterpackAngular, angularHashToken };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@afterpack/angular",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
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.4",
29
- "@afterpack/integration-utils": "0.2.3"
28
+ "@afterpack/core": "~0.2.6",
29
+ "@afterpack/integration-utils": "0.2.5"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@biomejs/biome": "2.4.4",