@afterpack/vite 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/README.md +6 -4
- package/dist/index.cjs +67 -4
- package/dist/index.js +69 -6
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -28,9 +28,11 @@ untouched.
|
|
|
28
28
|
Files Vite copies as-is from `publicDir` are not part of the bundle and are not obfuscated. Import
|
|
29
29
|
anything you want protected.
|
|
30
30
|
|
|
31
|
-
Each build writes a protection receipt
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
Each build writes a protection receipt to `.afterpack/receipts/`, one per output directory
|
|
32
|
+
(`.afterpack/receipts/dist.json` for `dist`). `.afterpack/` sits next to your project's lockfile,
|
|
33
|
+
`afterpack.json` or `.git` (failing those, its `package.json`), otherwise beside the build
|
|
34
|
+
directory, so the receipt never ships with your build. Run `npx afterpack verify dist` in your
|
|
35
|
+
deploy step to check that what you ship is what was obfuscated.
|
|
34
36
|
|
|
35
37
|
## Options
|
|
36
38
|
|
|
@@ -44,7 +46,7 @@ afterpackVite({ preset: "hard", seed: "git" });
|
|
|
44
46
|
| [`seed`][seed] | a number or string; `"git"` uses the current commit | a new random seed per build |
|
|
45
47
|
| [`identifiers.reserved`][identifiers.reserved] | names never to rename | none |
|
|
46
48
|
| [`paths.exclude`][paths.exclude] | globs for files to leave untouched | none |
|
|
47
|
-
| [`sourceMap.enabled`][sourceMap.enabled] | write source maps for the obfuscated output | on in development when Vite emits a map
|
|
49
|
+
| [`sourceMap.enabled`][sourceMap.enabled] | write source maps for the obfuscated output | off; on in development mode (`build.mode` or `NODE_ENV=development`) when Vite emits a map |
|
|
48
50
|
| [`protectionMap.enabled`][protectionMap.enabled] | write the Protection Map | on when Vite emits source maps |
|
|
49
51
|
| [`build.autorun`][build.autorun] | `false` turns AfterPack off | `true` |
|
|
50
52
|
|
package/dist/index.cjs
CHANGED
|
@@ -21,6 +21,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
21
|
}) : target, mod));
|
|
22
22
|
|
|
23
23
|
//#endregion
|
|
24
|
+
const node_crypto = __toESM(require("node:crypto"));
|
|
24
25
|
const node_path = __toESM(require("node:path"));
|
|
25
26
|
const __afterpack_core = __toESM(require("@afterpack/core"));
|
|
26
27
|
const __afterpack_integration_utils = __toESM(require("@afterpack/integration-utils"));
|
|
@@ -42,22 +43,75 @@ function afterpackVite(options = {}) {
|
|
|
42
43
|
let outDir = "";
|
|
43
44
|
let root = "";
|
|
44
45
|
let buildSourcemap = false;
|
|
46
|
+
let building = false;
|
|
45
47
|
const preMinifyCaptureByModuleId = new Map();
|
|
46
48
|
const captureDiagnostics = [];
|
|
47
49
|
const deferredReceiptByOutDir = new Map();
|
|
50
|
+
const workerOutputs = new Set();
|
|
51
|
+
let hashSalt = null;
|
|
48
52
|
const leg = options.leg;
|
|
49
53
|
const label = leg ? `afterpack-vite:${leg}` : "afterpack-vite";
|
|
54
|
+
const directiveSalt = (moduleIds) => {
|
|
55
|
+
if (!moduleIds || preMinifyCaptureByModuleId.size === 0) return "";
|
|
56
|
+
const parts = [];
|
|
57
|
+
for (const id of moduleIds) {
|
|
58
|
+
const cap = preMinifyCaptureByModuleId.get(id);
|
|
59
|
+
if (cap && (cap.directives.length > 0 || cap.renameGlobals)) parts.push(JSON.stringify({
|
|
60
|
+
id,
|
|
61
|
+
d: cap.directives,
|
|
62
|
+
g: cap.renameGlobals
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
if (parts.length === 0) return "";
|
|
66
|
+
return (0, node_crypto.createHash)("sha256").update(parts.sort().join("\0")).digest("hex").slice(0, 16);
|
|
67
|
+
};
|
|
68
|
+
const chunkSalt = (moduleIds) => hashSalt ? hashSalt.salt + directiveSalt(moduleIds) : void 0;
|
|
69
|
+
const workerNamesPlugin = {
|
|
70
|
+
name: `${label}:worker-names`,
|
|
71
|
+
augmentChunkHash(chunk) {
|
|
72
|
+
return chunkSalt(chunk?.moduleIds);
|
|
73
|
+
},
|
|
74
|
+
generateBundle(_outputOptions, bundle) {
|
|
75
|
+
for (const fileName of Object.keys(bundle)) workerOutputs.add(fileName);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
50
78
|
return {
|
|
51
79
|
name: label,
|
|
52
80
|
enforce: "pre",
|
|
53
81
|
config(config) {
|
|
54
|
-
|
|
55
|
-
|
|
82
|
+
const overrides = {};
|
|
83
|
+
if (artifactOptions.protectionMap?.enabled === true && config.build?.sourcemap === void 0) overrides.build = { sourcemap: true };
|
|
84
|
+
if (autorun) {
|
|
85
|
+
const userWorkerPlugins = config.worker?.plugins;
|
|
86
|
+
overrides.worker = { plugins: () => [...userWorkerPlugins?.() ?? [], workerNamesPlugin] };
|
|
87
|
+
}
|
|
88
|
+
return overrides;
|
|
56
89
|
},
|
|
57
90
|
configResolved(config) {
|
|
58
91
|
root = config.root;
|
|
59
92
|
outDir = (0, node_path.resolve)(config.root, config.build.outDir);
|
|
60
93
|
buildSourcemap = Boolean(config.build.sourcemap);
|
|
94
|
+
building = config.command === "build";
|
|
95
|
+
if (autorun && building) {
|
|
96
|
+
const refusal = (0, __afterpack_integration_utils.sriPluginRefusal)(config.plugins, label);
|
|
97
|
+
if (refusal) throw new Error(refusal);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
async buildStart() {
|
|
101
|
+
hashSalt = null;
|
|
102
|
+
workerOutputs.clear();
|
|
103
|
+
if (!autorun || !building || !outDir) return;
|
|
104
|
+
hashSalt = await (0, __afterpack_integration_utils.prepareHashSalt)({
|
|
105
|
+
seed: settings.seed,
|
|
106
|
+
root: options.projectRoot ?? root ?? outDir,
|
|
107
|
+
leg: leg ?? outDir,
|
|
108
|
+
localEngineVersion: await (0, __afterpack_integration_utils.readEngineVersion)({ version: __afterpack_core.version }),
|
|
109
|
+
settings: (0, __afterpack_integration_utils.saltSettings)((0, __afterpack_integration_utils.passSettings)(resolved), { ...(0, __afterpack_integration_utils.reportPolicySaltBits)(process.env, artifactOptions, buildSourcemap) }),
|
|
110
|
+
warn: (message) => console.warn(message)
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
augmentChunkHash(chunk) {
|
|
114
|
+
return chunkSalt(chunk?.moduleIds);
|
|
61
115
|
},
|
|
62
116
|
transform(code, id) {
|
|
63
117
|
if (!directivesEnabled) return null;
|
|
@@ -82,6 +136,8 @@ function afterpackVite(options = {}) {
|
|
|
82
136
|
const projectRoot = options.projectRoot ?? root ?? outDir;
|
|
83
137
|
const { files, inputs, entries } = (0, __afterpack_integration_utils.collectBundleJs)(bundle, outDir);
|
|
84
138
|
if (files.length === 0) return;
|
|
139
|
+
const unsalted = (0, __afterpack_integration_utils.unsaltedAssetsWarning)(entries, workerOutputs);
|
|
140
|
+
if (unsalted) console.warn(`[${label}] ${unsalted}`);
|
|
85
141
|
const capturedByFile = new Map();
|
|
86
142
|
if (preMinifyCaptureByModuleId.size > 0) for (const [filePath, entry] of entries) {
|
|
87
143
|
const mods = [];
|
|
@@ -111,22 +167,29 @@ function afterpackVite(options = {}) {
|
|
|
111
167
|
label,
|
|
112
168
|
cwd: projectRoot,
|
|
113
169
|
buildLeg: leg,
|
|
170
|
+
resolvedSeed: hashSalt?.resolvedSeed,
|
|
171
|
+
cloudEngineExpectation: hashSalt?.engine,
|
|
114
172
|
startedAt,
|
|
115
173
|
capturedByFile: capturedByFile.size > 0 ? capturedByFile : void 0,
|
|
116
174
|
combinedProtectionMap: {
|
|
117
175
|
buildDir: outDir,
|
|
118
|
-
|
|
176
|
+
subdir: leg
|
|
119
177
|
},
|
|
120
178
|
...(0, __afterpack_integration_utils.passSettings)(resolved),
|
|
121
179
|
sourceType: (0, __afterpack_integration_utils.sourceTypeForBundle)(outputOptions.format, entries.values()),
|
|
122
180
|
hasBundlerSourcemap: buildSourcemap,
|
|
123
181
|
messages: { autoEnableBundlerSourcemap: "protectionMap:true but no bundler sourcemap was found; enabled build.sourcemap so future builds can render original source in the map." }
|
|
124
182
|
});
|
|
183
|
+
const obfuscated = new Set();
|
|
125
184
|
for (const out of result.outputs ?? []) {
|
|
126
185
|
const entry = entries.get(out.path);
|
|
127
|
-
if (entry)
|
|
186
|
+
if (entry) {
|
|
187
|
+
(0, __afterpack_integration_utils.applyBundleOutput)(bundle, entry, out, result.policy);
|
|
188
|
+
obfuscated.add(entry.fileName);
|
|
189
|
+
}
|
|
128
190
|
}
|
|
129
191
|
if (!result.policy.sourceMap) (0, __afterpack_integration_utils.stripBundleCssSourceMaps)(bundle);
|
|
192
|
+
(0, __afterpack_integration_utils.regenerateCompressedSiblings)(bundle, obfuscated, label);
|
|
130
193
|
if (result.deferredReceipt) deferredReceiptByOutDir.set(outDir, result.deferredReceipt);
|
|
131
194
|
}
|
|
132
195
|
},
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { resolve } from "node:path";
|
|
2
3
|
import { processBatch, version } from "@afterpack/core";
|
|
3
|
-
import { applyBundleOutput, collectBundleJs, createTelemetryReporter, passSettings, resolveClientIdentity, resolvePluginConfig, runObfuscationPass, scanDirectives, sourceTypeForBundle, stripBundleCssSourceMaps, writeDeferredProtectionReceipt } from "@afterpack/integration-utils";
|
|
4
|
+
import { applyBundleOutput, collectBundleJs, createTelemetryReporter, passSettings, prepareHashSalt, readEngineVersion, regenerateCompressedSiblings, reportPolicySaltBits, resolveClientIdentity, resolvePluginConfig, runObfuscationPass, saltSettings, scanDirectives, sourceTypeForBundle, sriPluginRefusal, stripBundleCssSourceMaps, unsaltedAssetsWarning, writeDeferredProtectionReceipt } from "@afterpack/integration-utils";
|
|
4
5
|
|
|
5
6
|
//#region src/index.ts
|
|
6
7
|
const LOCAL_KEYS = ["leg", "projectRoot"];
|
|
@@ -19,22 +20,75 @@ function afterpackVite(options = {}) {
|
|
|
19
20
|
let outDir = "";
|
|
20
21
|
let root = "";
|
|
21
22
|
let buildSourcemap = false;
|
|
23
|
+
let building = false;
|
|
22
24
|
const preMinifyCaptureByModuleId = new Map();
|
|
23
25
|
const captureDiagnostics = [];
|
|
24
26
|
const deferredReceiptByOutDir = new Map();
|
|
27
|
+
const workerOutputs = new Set();
|
|
28
|
+
let hashSalt = null;
|
|
25
29
|
const leg = options.leg;
|
|
26
30
|
const label = leg ? `afterpack-vite:${leg}` : "afterpack-vite";
|
|
31
|
+
const directiveSalt = (moduleIds) => {
|
|
32
|
+
if (!moduleIds || preMinifyCaptureByModuleId.size === 0) return "";
|
|
33
|
+
const parts = [];
|
|
34
|
+
for (const id of moduleIds) {
|
|
35
|
+
const cap = preMinifyCaptureByModuleId.get(id);
|
|
36
|
+
if (cap && (cap.directives.length > 0 || cap.renameGlobals)) parts.push(JSON.stringify({
|
|
37
|
+
id,
|
|
38
|
+
d: cap.directives,
|
|
39
|
+
g: cap.renameGlobals
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
if (parts.length === 0) return "";
|
|
43
|
+
return createHash("sha256").update(parts.sort().join("\0")).digest("hex").slice(0, 16);
|
|
44
|
+
};
|
|
45
|
+
const chunkSalt = (moduleIds) => hashSalt ? hashSalt.salt + directiveSalt(moduleIds) : void 0;
|
|
46
|
+
const workerNamesPlugin = {
|
|
47
|
+
name: `${label}:worker-names`,
|
|
48
|
+
augmentChunkHash(chunk) {
|
|
49
|
+
return chunkSalt(chunk?.moduleIds);
|
|
50
|
+
},
|
|
51
|
+
generateBundle(_outputOptions, bundle) {
|
|
52
|
+
for (const fileName of Object.keys(bundle)) workerOutputs.add(fileName);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
27
55
|
return {
|
|
28
56
|
name: label,
|
|
29
57
|
enforce: "pre",
|
|
30
58
|
config(config) {
|
|
31
|
-
|
|
32
|
-
|
|
59
|
+
const overrides = {};
|
|
60
|
+
if (artifactOptions.protectionMap?.enabled === true && config.build?.sourcemap === void 0) overrides.build = { sourcemap: true };
|
|
61
|
+
if (autorun) {
|
|
62
|
+
const userWorkerPlugins = config.worker?.plugins;
|
|
63
|
+
overrides.worker = { plugins: () => [...userWorkerPlugins?.() ?? [], workerNamesPlugin] };
|
|
64
|
+
}
|
|
65
|
+
return overrides;
|
|
33
66
|
},
|
|
34
67
|
configResolved(config) {
|
|
35
68
|
root = config.root;
|
|
36
69
|
outDir = resolve(config.root, config.build.outDir);
|
|
37
70
|
buildSourcemap = Boolean(config.build.sourcemap);
|
|
71
|
+
building = config.command === "build";
|
|
72
|
+
if (autorun && building) {
|
|
73
|
+
const refusal = sriPluginRefusal(config.plugins, label);
|
|
74
|
+
if (refusal) throw new Error(refusal);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
async buildStart() {
|
|
78
|
+
hashSalt = null;
|
|
79
|
+
workerOutputs.clear();
|
|
80
|
+
if (!autorun || !building || !outDir) return;
|
|
81
|
+
hashSalt = await prepareHashSalt({
|
|
82
|
+
seed: settings.seed,
|
|
83
|
+
root: options.projectRoot ?? root ?? outDir,
|
|
84
|
+
leg: leg ?? outDir,
|
|
85
|
+
localEngineVersion: await readEngineVersion({ version }),
|
|
86
|
+
settings: saltSettings(passSettings(resolved), { ...reportPolicySaltBits(process.env, artifactOptions, buildSourcemap) }),
|
|
87
|
+
warn: (message) => console.warn(message)
|
|
88
|
+
});
|
|
89
|
+
},
|
|
90
|
+
augmentChunkHash(chunk) {
|
|
91
|
+
return chunkSalt(chunk?.moduleIds);
|
|
38
92
|
},
|
|
39
93
|
transform(code, id) {
|
|
40
94
|
if (!directivesEnabled) return null;
|
|
@@ -59,6 +113,8 @@ function afterpackVite(options = {}) {
|
|
|
59
113
|
const projectRoot = options.projectRoot ?? root ?? outDir;
|
|
60
114
|
const { files, inputs, entries } = collectBundleJs(bundle, outDir);
|
|
61
115
|
if (files.length === 0) return;
|
|
116
|
+
const unsalted = unsaltedAssetsWarning(entries, workerOutputs);
|
|
117
|
+
if (unsalted) console.warn(`[${label}] ${unsalted}`);
|
|
62
118
|
const capturedByFile = new Map();
|
|
63
119
|
if (preMinifyCaptureByModuleId.size > 0) for (const [filePath, entry] of entries) {
|
|
64
120
|
const mods = [];
|
|
@@ -88,22 +144,29 @@ function afterpackVite(options = {}) {
|
|
|
88
144
|
label,
|
|
89
145
|
cwd: projectRoot,
|
|
90
146
|
buildLeg: leg,
|
|
147
|
+
resolvedSeed: hashSalt?.resolvedSeed,
|
|
148
|
+
cloudEngineExpectation: hashSalt?.engine,
|
|
91
149
|
startedAt,
|
|
92
150
|
capturedByFile: capturedByFile.size > 0 ? capturedByFile : void 0,
|
|
93
151
|
combinedProtectionMap: {
|
|
94
152
|
buildDir: outDir,
|
|
95
|
-
|
|
153
|
+
subdir: leg
|
|
96
154
|
},
|
|
97
155
|
...passSettings(resolved),
|
|
98
156
|
sourceType: sourceTypeForBundle(outputOptions.format, entries.values()),
|
|
99
157
|
hasBundlerSourcemap: buildSourcemap,
|
|
100
158
|
messages: { autoEnableBundlerSourcemap: "protectionMap:true but no bundler sourcemap was found; enabled build.sourcemap so future builds can render original source in the map." }
|
|
101
159
|
});
|
|
160
|
+
const obfuscated = new Set();
|
|
102
161
|
for (const out of result.outputs ?? []) {
|
|
103
162
|
const entry = entries.get(out.path);
|
|
104
|
-
if (entry)
|
|
163
|
+
if (entry) {
|
|
164
|
+
applyBundleOutput(bundle, entry, out, result.policy);
|
|
165
|
+
obfuscated.add(entry.fileName);
|
|
166
|
+
}
|
|
105
167
|
}
|
|
106
168
|
if (!result.policy.sourceMap) stripBundleCssSourceMaps(bundle);
|
|
169
|
+
regenerateCompressedSiblings(bundle, obfuscated, label);
|
|
107
170
|
if (result.deferredReceipt) deferredReceiptByOutDir.set(outDir, result.deferredReceipt);
|
|
108
171
|
}
|
|
109
172
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@afterpack/vite",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Vite plugin for AfterPack: obfuscate your production JavaScript during vite build",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@afterpack/core": "~0.2.
|
|
32
|
-
"@afterpack/integration-utils": "0.2.
|
|
31
|
+
"@afterpack/core": "~0.2.5",
|
|
32
|
+
"@afterpack/integration-utils": "0.2.4"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@biomejs/biome": "2.4.4",
|