@tanstack/start-plugin-core 1.167.35 → 1.168.0
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/esm/import-protection/adapterUtils.d.ts +27 -0
- package/dist/esm/import-protection/adapterUtils.js +31 -0
- package/dist/esm/import-protection/adapterUtils.js.map +1 -0
- package/dist/esm/import-protection/analysis.d.ts +36 -0
- package/dist/esm/import-protection/analysis.js +407 -0
- package/dist/esm/import-protection/analysis.js.map +1 -0
- package/dist/esm/{import-protection-plugin → import-protection}/ast.js +1 -1
- package/dist/esm/import-protection/ast.js.map +1 -0
- package/dist/esm/import-protection/constants.d.ts +11 -0
- package/dist/esm/{import-protection-plugin → import-protection}/constants.js +7 -2
- package/dist/esm/import-protection/constants.js.map +1 -0
- package/dist/esm/{import-protection-plugin → import-protection}/defaults.js +1 -1
- package/dist/esm/import-protection/defaults.js.map +1 -0
- package/dist/esm/{import-protection-plugin → import-protection}/extensionlessAbsoluteIdResolver.js +1 -1
- package/dist/esm/import-protection/extensionlessAbsoluteIdResolver.js.map +1 -0
- package/dist/esm/{import-protection-plugin → import-protection}/matchers.js +1 -1
- package/dist/esm/import-protection/matchers.js.map +1 -0
- package/dist/esm/{import-protection-plugin/rewriteDeniedImports.d.ts → import-protection/rewrite.d.ts} +0 -4
- package/dist/esm/import-protection/rewrite.js +121 -0
- package/dist/esm/import-protection/rewrite.js.map +1 -0
- package/dist/esm/{import-protection-plugin → import-protection}/sourceLocation.d.ts +32 -3
- package/dist/esm/{import-protection-plugin → import-protection}/sourceLocation.js +65 -10
- package/dist/esm/import-protection/sourceLocation.js.map +1 -0
- package/dist/esm/{import-protection-plugin → import-protection}/trace.d.ts +0 -1
- package/dist/esm/{import-protection-plugin → import-protection}/trace.js +1 -1
- package/dist/esm/import-protection/trace.js.map +1 -0
- package/dist/esm/{import-protection-plugin → import-protection}/utils.d.ts +18 -1
- package/dist/esm/{import-protection-plugin → import-protection}/utils.js +12 -19
- package/dist/esm/import-protection/utils.js.map +1 -0
- package/dist/esm/import-protection/virtualModules.d.ts +25 -0
- package/dist/esm/{import-protection-plugin → import-protection}/virtualModules.js +5 -117
- package/dist/esm/import-protection/virtualModules.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +3 -1
- package/dist/esm/post-build.d.ts +9 -0
- package/dist/esm/post-build.js +37 -0
- package/dist/esm/post-build.js.map +1 -0
- package/dist/esm/prerender.d.ts +11 -0
- package/dist/esm/prerender.js +159 -0
- package/dist/esm/prerender.js.map +1 -0
- package/dist/esm/rsbuild/dev-server.d.ts +21 -0
- package/dist/esm/rsbuild/dev-server.js +76 -0
- package/dist/esm/rsbuild/dev-server.js.map +1 -0
- package/dist/esm/rsbuild/import-protection.d.ts +10 -0
- package/dist/esm/rsbuild/import-protection.js +775 -0
- package/dist/esm/rsbuild/import-protection.js.map +1 -0
- package/dist/esm/rsbuild/normalized-client-build.d.ts +18 -0
- package/dist/esm/rsbuild/normalized-client-build.js +207 -0
- package/dist/esm/rsbuild/normalized-client-build.js.map +1 -0
- package/dist/esm/rsbuild/planning.d.ts +52 -0
- package/dist/esm/rsbuild/planning.js +108 -0
- package/dist/esm/rsbuild/planning.js.map +1 -0
- package/dist/esm/rsbuild/plugin.d.ts +4 -0
- package/dist/esm/rsbuild/plugin.js +344 -0
- package/dist/esm/rsbuild/plugin.js.map +1 -0
- package/dist/esm/rsbuild/post-build.d.ts +6 -0
- package/dist/esm/rsbuild/post-build.js +57 -0
- package/dist/esm/rsbuild/post-build.js.map +1 -0
- package/dist/esm/rsbuild/schema.d.ts +3372 -0
- package/dist/esm/rsbuild/schema.js +12 -0
- package/dist/esm/rsbuild/schema.js.map +1 -0
- package/dist/esm/rsbuild/start-compiler-host.d.ts +20 -0
- package/dist/esm/rsbuild/start-compiler-host.js +150 -0
- package/dist/esm/rsbuild/start-compiler-host.js.map +1 -0
- package/dist/esm/rsbuild/start-router-plugin.d.ts +18 -0
- package/dist/esm/rsbuild/start-router-plugin.js +63 -0
- package/dist/esm/rsbuild/start-router-plugin.js.map +1 -0
- package/dist/esm/rsbuild/swc-rsc.d.ts +14 -0
- package/dist/esm/rsbuild/swc-rsc.js +93 -0
- package/dist/esm/rsbuild/swc-rsc.js.map +1 -0
- package/dist/esm/rsbuild/types.d.ts +17 -0
- package/dist/esm/rsbuild/types.js +0 -0
- package/dist/esm/rsbuild/virtual-modules.d.ts +53 -0
- package/dist/esm/rsbuild/virtual-modules.js +287 -0
- package/dist/esm/rsbuild/virtual-modules.js.map +1 -0
- package/dist/esm/schema.d.ts +43 -43
- package/dist/esm/start-compiler/compiler.d.ts +1 -1
- package/dist/esm/start-compiler/compiler.js +80 -9
- package/dist/esm/start-compiler/compiler.js.map +1 -1
- package/dist/esm/start-compiler/handleCreateServerFn.js +9 -0
- package/dist/esm/start-compiler/handleCreateServerFn.js.map +1 -1
- package/dist/esm/start-compiler/host.js +5 -1
- package/dist/esm/start-compiler/host.js.map +1 -1
- package/dist/esm/start-compiler/types.d.ts +1 -0
- package/dist/esm/utils.d.ts +1 -0
- package/dist/esm/utils.js +4 -1
- package/dist/esm/utils.js.map +1 -1
- package/dist/esm/{import-protection-plugin → vite/import-protection-plugin}/plugin.js +41 -92
- package/dist/esm/vite/import-protection-plugin/plugin.js.map +1 -0
- package/dist/esm/{import-protection-plugin → vite/import-protection-plugin}/types.d.ts +5 -5
- package/dist/esm/vite/import-protection-plugin/virtualModules.d.ts +8 -0
- package/dist/esm/vite/import-protection-plugin/virtualModules.js +49 -0
- package/dist/esm/vite/import-protection-plugin/virtualModules.js.map +1 -0
- package/dist/esm/vite/plugin.js +1 -1
- package/dist/esm/vite/plugin.js.map +1 -1
- package/dist/esm/vite/post-server-build.js +14 -32
- package/dist/esm/vite/post-server-build.js.map +1 -1
- package/dist/esm/vite/prerender.d.ts +2 -2
- package/dist/esm/vite/prerender.js +17 -147
- package/dist/esm/vite/prerender.js.map +1 -1
- package/dist/esm/vite/schema.d.ts +23 -23
- package/dist/esm/vite/start-compiler-plugin/hot-update.d.ts +2 -0
- package/dist/esm/vite/start-compiler-plugin/hot-update.js +16 -0
- package/dist/esm/vite/start-compiler-plugin/hot-update.js.map +1 -0
- package/dist/esm/vite/start-compiler-plugin/module-specifier.js +9 -4
- package/dist/esm/vite/start-compiler-plugin/module-specifier.js.map +1 -1
- package/dist/esm/vite/start-compiler-plugin/plugin.js +86 -13
- package/dist/esm/vite/start-compiler-plugin/plugin.js.map +1 -1
- package/package.json +17 -4
- package/src/import-protection/INTERNALS.md +266 -0
- package/src/import-protection/adapterUtils.ts +94 -0
- package/src/import-protection/analysis.ts +853 -0
- package/src/{import-protection-plugin → import-protection}/constants.ts +7 -0
- package/src/import-protection/rewrite.ts +229 -0
- package/src/{import-protection-plugin → import-protection}/sourceLocation.ts +125 -9
- package/src/{import-protection-plugin → import-protection}/trace.ts +0 -1
- package/src/{import-protection-plugin → import-protection}/utils.ts +35 -20
- package/src/{import-protection-plugin → import-protection}/virtualModules.ts +30 -177
- package/src/index.ts +5 -0
- package/src/post-build.ts +64 -0
- package/src/prerender.ts +292 -0
- package/src/rsbuild/INTERNALS-import-protection.md +169 -0
- package/src/rsbuild/dev-server.ts +129 -0
- package/src/rsbuild/import-protection.ts +1600 -0
- package/src/rsbuild/normalized-client-build.ts +346 -0
- package/src/rsbuild/planning.ts +234 -0
- package/src/rsbuild/plugin.ts +754 -0
- package/src/rsbuild/post-build.ts +96 -0
- package/src/rsbuild/schema.ts +31 -0
- package/src/rsbuild/start-compiler-host.ts +250 -0
- package/src/rsbuild/start-router-plugin.ts +86 -0
- package/src/rsbuild/swc-rsc.ts +166 -0
- package/src/rsbuild/types.ts +20 -0
- package/src/rsbuild/virtual-modules.ts +565 -0
- package/src/start-compiler/compiler.ts +153 -19
- package/src/start-compiler/handleCreateServerFn.ts +18 -0
- package/src/start-compiler/types.ts +1 -0
- package/src/utils.ts +4 -0
- package/src/vite/import-protection-plugin/INTERNALS.md +187 -0
- package/src/{import-protection-plugin → vite/import-protection-plugin}/plugin.ts +73 -158
- package/src/{import-protection-plugin → vite/import-protection-plugin}/types.ts +5 -5
- package/src/vite/import-protection-plugin/virtualModules.ts +122 -0
- package/src/vite/plugin.ts +1 -1
- package/src/vite/post-server-build.ts +14 -57
- package/src/vite/prerender.ts +19 -260
- package/src/vite/start-compiler-plugin/hot-update.ts +24 -0
- package/src/vite/start-compiler-plugin/module-specifier.ts +15 -5
- package/src/vite/start-compiler-plugin/plugin.ts +193 -18
- package/dist/esm/import-protection-plugin/ast.js.map +0 -1
- package/dist/esm/import-protection-plugin/constants.d.ts +0 -6
- package/dist/esm/import-protection-plugin/constants.js.map +0 -1
- package/dist/esm/import-protection-plugin/defaults.js.map +0 -1
- package/dist/esm/import-protection-plugin/extensionlessAbsoluteIdResolver.js.map +0 -1
- package/dist/esm/import-protection-plugin/matchers.js.map +0 -1
- package/dist/esm/import-protection-plugin/plugin.js.map +0 -1
- package/dist/esm/import-protection-plugin/postCompileUsage.d.ts +0 -13
- package/dist/esm/import-protection-plugin/postCompileUsage.js +0 -63
- package/dist/esm/import-protection-plugin/postCompileUsage.js.map +0 -1
- package/dist/esm/import-protection-plugin/rewriteDeniedImports.js +0 -205
- package/dist/esm/import-protection-plugin/rewriteDeniedImports.js.map +0 -1
- package/dist/esm/import-protection-plugin/sourceLocation.js.map +0 -1
- package/dist/esm/import-protection-plugin/trace.js.map +0 -1
- package/dist/esm/import-protection-plugin/utils.js.map +0 -1
- package/dist/esm/import-protection-plugin/virtualModules.d.ts +0 -78
- package/dist/esm/import-protection-plugin/virtualModules.js.map +0 -1
- package/dist/esm/start-compiler/load-module.d.ts +0 -14
- package/dist/esm/start-compiler/load-module.js +0 -18
- package/dist/esm/start-compiler/load-module.js.map +0 -1
- package/src/import-protection-plugin/INTERNALS.md +0 -700
- package/src/import-protection-plugin/postCompileUsage.ts +0 -100
- package/src/import-protection-plugin/rewriteDeniedImports.ts +0 -379
- package/src/start-compiler/load-module.ts +0 -31
- /package/dist/esm/{import-protection-plugin → import-protection}/ast.d.ts +0 -0
- /package/dist/esm/{import-protection-plugin → import-protection}/defaults.d.ts +0 -0
- /package/dist/esm/{import-protection-plugin → import-protection}/extensionlessAbsoluteIdResolver.d.ts +0 -0
- /package/dist/esm/{import-protection-plugin → import-protection}/matchers.d.ts +0 -0
- /package/dist/esm/{import-protection-plugin → vite/import-protection-plugin}/plugin.d.ts +0 -0
- /package/src/{import-protection-plugin → import-protection}/ast.ts +0 -0
- /package/src/{import-protection-plugin → import-protection}/defaults.ts +0 -0
- /package/src/{import-protection-plugin → import-protection}/extensionlessAbsoluteIdResolver.ts +0 -0
- /package/src/{import-protection-plugin → import-protection}/matchers.ts +0 -0
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
import { buildResolutionCandidates, buildSourceCandidates, canonicalizeResolvedId, checkFileDenial, clearNormalizeFilePathCache, dedupePatterns, dedupeViolationKey, isFileExcluded, normalizeFilePath } from "../import-protection/utils.js";
|
|
2
|
+
import { ImportGraph, buildTrace, formatViolation } from "../import-protection/trace.js";
|
|
3
|
+
import { getDefaultImportProtectionRules, getMarkerSpecifiers } from "../import-protection/defaults.js";
|
|
4
|
+
import { compileMatchers, matchesAny } from "../import-protection/matchers.js";
|
|
5
|
+
import { getImportProtectionEnvType, getImportProtectionRelativePath, getImportProtectionRulesForEnvironment, shouldCheckImportProtectionImporter } from "../import-protection/adapterUtils.js";
|
|
6
|
+
import { ImportLocCache, addTraceImportLocations, buildCodeSnippet, buildLineIndex, createImportSpecifierLocationIndex, findImportStatementLocationFromTransformed, findOriginalUsageLocation, findPostCompileUsageLocation, getOrCreateOriginalTransformResult, indexToLineColumn, normalizeSourceMap, pickOriginalCodeFromSourcesContent } from "../import-protection/sourceLocation.js";
|
|
7
|
+
import { findOriginalUnsafeUsagePosFromResult, getImportSources, getMockExportNamesBySource, getNamedExports } from "../import-protection/analysis.js";
|
|
8
|
+
import { rewriteDeniedImports } from "../import-protection/rewrite.js";
|
|
9
|
+
import { ExtensionlessAbsoluteIdResolver } from "../import-protection/extensionlessAbsoluteIdResolver.js";
|
|
10
|
+
import { generateDevSelfDenialModule, generateSelfContainedMockModule, loadMockEdgeModule, loadMockRuntimeModule, loadSilentMockModule } from "../import-protection/virtualModules.js";
|
|
11
|
+
import { normalizePath } from "vite";
|
|
12
|
+
import { extname, resolve } from "node:path";
|
|
13
|
+
//#region src/rsbuild/import-protection.ts
|
|
14
|
+
var importSpecifierLocationIndex = createImportSpecifierLocationIndex();
|
|
15
|
+
var IMPORT_PROTECTION_VIRTUAL_DIR = "node_modules/.virtual/import-protection";
|
|
16
|
+
var MOCK_EDGE_FILE_PREFIX = "mock-edge-";
|
|
17
|
+
var MOCK_RUNTIME_FILE_PREFIX = "mock-runtime-";
|
|
18
|
+
var MOCK_SILENT_FILE = "mock-silent.mjs";
|
|
19
|
+
function toBase64Url(input) {
|
|
20
|
+
return Buffer.from(JSON.stringify(input), "utf8").toString("base64url");
|
|
21
|
+
}
|
|
22
|
+
function fromBase64Url(input) {
|
|
23
|
+
return JSON.parse(Buffer.from(input, "base64url").toString("utf8"));
|
|
24
|
+
}
|
|
25
|
+
function getRulesForEnvironment(config, envName) {
|
|
26
|
+
return getImportProtectionRulesForEnvironment(config, envName);
|
|
27
|
+
}
|
|
28
|
+
function serializePattern(pattern) {
|
|
29
|
+
return typeof pattern === "string" ? pattern : pattern.toString();
|
|
30
|
+
}
|
|
31
|
+
function dedupeKey(info) {
|
|
32
|
+
return dedupeViolationKey(info);
|
|
33
|
+
}
|
|
34
|
+
function getOrCreateEnvState(envStates, envName) {
|
|
35
|
+
let env = envStates.get(envName);
|
|
36
|
+
if (!env) {
|
|
37
|
+
env = {
|
|
38
|
+
resolveCache: /* @__PURE__ */ new Map(),
|
|
39
|
+
seenViolations: /* @__PURE__ */ new Set(),
|
|
40
|
+
buildTransformResults: /* @__PURE__ */ new Map(),
|
|
41
|
+
deferredFileViolations: [],
|
|
42
|
+
deferredFileViolationKeys: /* @__PURE__ */ new Set()
|
|
43
|
+
};
|
|
44
|
+
envStates.set(envName, env);
|
|
45
|
+
}
|
|
46
|
+
return env;
|
|
47
|
+
}
|
|
48
|
+
function getVirtualModulePath(root, envName, filename) {
|
|
49
|
+
return normalizePath(resolve(root, IMPORT_PROTECTION_VIRTUAL_DIR, envName, filename));
|
|
50
|
+
}
|
|
51
|
+
function queuePendingWrite(shared, envName, filePath, code) {
|
|
52
|
+
let writes = shared.pendingWrites.get(envName);
|
|
53
|
+
if (!writes) {
|
|
54
|
+
writes = /* @__PURE__ */ new Map();
|
|
55
|
+
shared.pendingWrites.set(envName, writes);
|
|
56
|
+
}
|
|
57
|
+
writes.set(filePath, code);
|
|
58
|
+
}
|
|
59
|
+
function tryWriteVirtualModule(shared, envName, filePath, code) {
|
|
60
|
+
if (shared.virtualModules.get(filePath) === code) return filePath;
|
|
61
|
+
shared.virtualModules.set(filePath, code);
|
|
62
|
+
const vmPlugin = shared.vmPlugins[envName];
|
|
63
|
+
if (!vmPlugin || !shared.readyVmPlugins[envName]) {
|
|
64
|
+
queuePendingWrite(shared, envName, filePath, code);
|
|
65
|
+
return filePath;
|
|
66
|
+
}
|
|
67
|
+
vmPlugin.writeModule(filePath, code);
|
|
68
|
+
return filePath;
|
|
69
|
+
}
|
|
70
|
+
function flushPendingWrites(shared, envName) {
|
|
71
|
+
const writes = shared.pendingWrites.get(envName);
|
|
72
|
+
if (!writes?.size || !shared.readyVmPlugins[envName]) return;
|
|
73
|
+
for (const [filePath, code] of writes) {
|
|
74
|
+
shared.vmPlugins[envName]?.writeModule(filePath, code);
|
|
75
|
+
writes.delete(filePath);
|
|
76
|
+
}
|
|
77
|
+
if (writes.size === 0) shared.pendingWrites.delete(envName);
|
|
78
|
+
}
|
|
79
|
+
function ensureSilentMockModule(shared, envName) {
|
|
80
|
+
return tryWriteVirtualModule(shared, envName, getVirtualModulePath(shared.root, envName, MOCK_SILENT_FILE), loadSilentMockModule().code);
|
|
81
|
+
}
|
|
82
|
+
function ensureRuntimeMockModule(opts) {
|
|
83
|
+
const encoded = toBase64Url({
|
|
84
|
+
mode: opts.mode,
|
|
85
|
+
env: opts.env,
|
|
86
|
+
importer: opts.importer,
|
|
87
|
+
specifier: opts.specifier,
|
|
88
|
+
trace: []
|
|
89
|
+
});
|
|
90
|
+
return tryWriteVirtualModule(opts.shared, opts.envName, getVirtualModulePath(opts.shared.root, opts.envName, `${MOCK_RUNTIME_FILE_PREFIX}${encoded}.mjs`), loadMockRuntimeModule(encoded).code);
|
|
91
|
+
}
|
|
92
|
+
function ensureMockEdgeModule(opts) {
|
|
93
|
+
const encoded = toBase64Url(opts.payload);
|
|
94
|
+
return tryWriteVirtualModule(opts.shared, opts.envName, getVirtualModulePath(opts.shared.root, opts.envName, `${MOCK_EDGE_FILE_PREFIX}${encoded}.mjs`), loadMockEdgeModule(encoded).code);
|
|
95
|
+
}
|
|
96
|
+
function getMockEdgePayloadFromFile(filePath) {
|
|
97
|
+
const match = /(?:^|[\\/])mock-edge-([^/\\]+)\.mjs$/.exec(filePath);
|
|
98
|
+
if (!match) return;
|
|
99
|
+
try {
|
|
100
|
+
return fromBase64Url(match[1]);
|
|
101
|
+
} catch {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async function loadOriginalCode(cache, file, loader) {
|
|
106
|
+
let result = cache.get(file);
|
|
107
|
+
if (!result) {
|
|
108
|
+
result = loader(file);
|
|
109
|
+
cache.set(file, result);
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
async function loadOriginalCodeFromInputFileSystem(inputFileSystem, file) {
|
|
114
|
+
return new Promise((resolve) => {
|
|
115
|
+
inputFileSystem.readFile(file, (error, data) => {
|
|
116
|
+
if (error || data == null) {
|
|
117
|
+
resolve(void 0);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
resolve(typeof data === "string" ? data : data.toString("utf8"));
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
async function resolveAgainstImporter(opts) {
|
|
125
|
+
const cacheKey = `${normalizeFilePath(opts.importerId)}:${opts.source}`;
|
|
126
|
+
if (opts.envState.resolveCache.has(cacheKey)) return opts.envState.resolveCache.get(cacheKey) ?? null;
|
|
127
|
+
const importerDir = opts.ctx.context ?? opts.importerId.replace(/[/\\][^/\\]*$/, "");
|
|
128
|
+
const resolved = await new Promise((resolve, reject) => {
|
|
129
|
+
opts.ctx.resolve(importerDir, opts.source, (error, result) => {
|
|
130
|
+
if (error) {
|
|
131
|
+
reject(error);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
resolve(typeof result === "string" ? result : null);
|
|
135
|
+
});
|
|
136
|
+
}).catch(() => null);
|
|
137
|
+
if (!resolved) {
|
|
138
|
+
opts.envState.resolveCache.set(cacheKey, null);
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
const canonical = canonicalizeResolvedId(resolved, opts.config.root, (value) => opts.extensionlessResolver.resolve(value));
|
|
142
|
+
opts.envState.resolveCache.set(cacheKey, canonical);
|
|
143
|
+
return canonical;
|
|
144
|
+
}
|
|
145
|
+
function getModuleResource(module) {
|
|
146
|
+
const candidate = module;
|
|
147
|
+
return candidate.nameForCondition() ?? candidate.resourceResolveData?.resource ?? candidate.resource ?? candidate.userRequest ?? candidate.request;
|
|
148
|
+
}
|
|
149
|
+
function getModuleFile(module) {
|
|
150
|
+
return normalizeFilePath(getModuleResource(module) ?? module.identifier());
|
|
151
|
+
}
|
|
152
|
+
var IMPORT_PROTECTION_PARSEABLE_EXTENSIONS = new Set([
|
|
153
|
+
".ts",
|
|
154
|
+
".tsx",
|
|
155
|
+
".mts",
|
|
156
|
+
".cts",
|
|
157
|
+
".js",
|
|
158
|
+
".jsx",
|
|
159
|
+
".mjs",
|
|
160
|
+
".cjs"
|
|
161
|
+
]);
|
|
162
|
+
function isImportProtectionSourceFile(file) {
|
|
163
|
+
if (!file) return false;
|
|
164
|
+
const extension = extname(normalizeFilePath(file));
|
|
165
|
+
return extension.length > 0 && IMPORT_PROTECTION_PARSEABLE_EXTENSIONS.has(extension);
|
|
166
|
+
}
|
|
167
|
+
function isImportProtectionSourceModule(module) {
|
|
168
|
+
return isImportProtectionSourceFile(getModuleResource(module));
|
|
169
|
+
}
|
|
170
|
+
function addTransformResult(cache, key, result) {
|
|
171
|
+
cache.set(normalizePath(key), result);
|
|
172
|
+
cache.set(normalizeFilePath(key), result);
|
|
173
|
+
}
|
|
174
|
+
function hasTransformResult(cache, key) {
|
|
175
|
+
return cache.has(normalizePath(key)) || cache.has(normalizeFilePath(key));
|
|
176
|
+
}
|
|
177
|
+
function deferFileViolation(envState, violation) {
|
|
178
|
+
const key = `${violation.importer}:${violation.specifier}:${violation.resolved}:${String(violation.pattern)}`;
|
|
179
|
+
if (envState.deferredFileViolationKeys.has(key)) return;
|
|
180
|
+
envState.deferredFileViolationKeys.add(key);
|
|
181
|
+
envState.deferredFileViolations.push(violation);
|
|
182
|
+
}
|
|
183
|
+
function hasOriginalUnsafeUsage(result, source, envType) {
|
|
184
|
+
if (!result) return false;
|
|
185
|
+
const originalResult = getOrCreateOriginalTransformResult(result);
|
|
186
|
+
if (!originalResult) return false;
|
|
187
|
+
return !!findOriginalUnsafeUsagePosFromResult(originalResult, source, envType);
|
|
188
|
+
}
|
|
189
|
+
async function buildTransformResultProvider(opts) {
|
|
190
|
+
const cache = /* @__PURE__ */ new Map();
|
|
191
|
+
if (opts.preloaded) for (const [key, result] of opts.preloaded) cache.set(key, result);
|
|
192
|
+
for (const module of opts.modules) {
|
|
193
|
+
const source = module.originalSource();
|
|
194
|
+
if (!source) continue;
|
|
195
|
+
const sourceAndMap = source.sourceAndMap();
|
|
196
|
+
const code = String(sourceAndMap.source);
|
|
197
|
+
const map = normalizeSourceMap(sourceAndMap.map);
|
|
198
|
+
const file = getModuleFile(module);
|
|
199
|
+
const resource = getModuleResource(module);
|
|
200
|
+
const result = {
|
|
201
|
+
code,
|
|
202
|
+
map,
|
|
203
|
+
originalCode: map?.sourcesContent ? pickOriginalCodeFromSourcesContent(map, resource ?? file, opts.root) ?? (resource ? await opts.loadOriginalCode(resource) : void 0) : resource ? await opts.loadOriginalCode(resource) : void 0,
|
|
204
|
+
lineIndex: buildLineIndex(code)
|
|
205
|
+
};
|
|
206
|
+
if (!hasTransformResult(cache, file)) addTransformResult(cache, file, result);
|
|
207
|
+
if (resource && !hasTransformResult(cache, resource)) addTransformResult(cache, resource, result);
|
|
208
|
+
}
|
|
209
|
+
return { getTransformResult(id) {
|
|
210
|
+
return cache.get(normalizePath(id)) ?? cache.get(normalizeFilePath(id));
|
|
211
|
+
} };
|
|
212
|
+
}
|
|
213
|
+
function getConnectionRequest(dependency) {
|
|
214
|
+
const candidate = dependency;
|
|
215
|
+
return typeof candidate.request === "string" ? candidate.request : void 0;
|
|
216
|
+
}
|
|
217
|
+
function addEntryModulesToGraph(opts) {
|
|
218
|
+
for (const entry of opts.compilation.entries.values()) for (const dependency of entry.dependencies) {
|
|
219
|
+
const module = opts.compilation.moduleGraph.getConnection(dependency)?.module;
|
|
220
|
+
if (!module) continue;
|
|
221
|
+
opts.graph.addEntry(getModuleFile(module));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function buildCompilationGraph(opts) {
|
|
225
|
+
const graph = new ImportGraph();
|
|
226
|
+
const edges = [];
|
|
227
|
+
addEntryModulesToGraph({
|
|
228
|
+
compilation: opts.compilation,
|
|
229
|
+
graph
|
|
230
|
+
});
|
|
231
|
+
for (const module of opts.modules) {
|
|
232
|
+
const importer = getModuleFile(module);
|
|
233
|
+
const connections = opts.compilation.moduleGraph.getOutgoingConnectionsInOrder(module);
|
|
234
|
+
for (const connection of connections) {
|
|
235
|
+
if (!connection.module) continue;
|
|
236
|
+
if (!isActiveConnection(connection)) continue;
|
|
237
|
+
const resolved = getModuleFile(connection.module);
|
|
238
|
+
const specifier = getConnectionRequest(connection.dependency);
|
|
239
|
+
graph.addEdge(resolved, importer, specifier);
|
|
240
|
+
edges.push({
|
|
241
|
+
importer,
|
|
242
|
+
specifier,
|
|
243
|
+
resolved
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
graph,
|
|
249
|
+
edges
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
function isActiveConnection(connection) {
|
|
253
|
+
if (typeof connection.getActiveState !== "function") return true;
|
|
254
|
+
return connection.getActiveState(void 0) === true;
|
|
255
|
+
}
|
|
256
|
+
function findImportLocationInOriginalCode(provider, importer, source) {
|
|
257
|
+
const result = provider.getTransformResult(importer);
|
|
258
|
+
if (!result) return;
|
|
259
|
+
const originalResult = getOrCreateOriginalTransformResult(result);
|
|
260
|
+
if (!originalResult) return;
|
|
261
|
+
const index = importSpecifierLocationIndex.find(originalResult, source);
|
|
262
|
+
if (index === -1) return;
|
|
263
|
+
const loc = indexToLineColumn(originalResult.lineIndex ?? (originalResult.lineIndex = buildLineIndex(originalResult.code)), index);
|
|
264
|
+
return {
|
|
265
|
+
file: normalizeFilePath(importer),
|
|
266
|
+
line: loc.line,
|
|
267
|
+
column: loc.column
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
async function resolveImporterLocation(opts) {
|
|
271
|
+
if (opts.preferOriginalCode) for (const candidate of opts.sourceCandidates) {
|
|
272
|
+
const loc = findOriginalUsageLocation(opts.provider, opts.importer, candidate, opts.envType) ?? findImportLocationInOriginalCode(opts.provider, opts.importer, candidate);
|
|
273
|
+
if (loc) return loc;
|
|
274
|
+
}
|
|
275
|
+
for (const candidate of opts.sourceCandidates) {
|
|
276
|
+
const loc = await findPostCompileUsageLocation(opts.provider, opts.importer, candidate) || await findImportStatementLocationFromTransformed(opts.provider, opts.importer, candidate, opts.importLocCache, importSpecifierLocationIndex.find);
|
|
277
|
+
if (loc) return loc;
|
|
278
|
+
}
|
|
279
|
+
if (!opts.preferOriginalCode) for (const candidate of opts.sourceCandidates) {
|
|
280
|
+
const loc = findImportLocationInOriginalCode(opts.provider, opts.importer, candidate);
|
|
281
|
+
if (loc) return loc;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
async function rebuildAndAnnotateTrace(opts) {
|
|
285
|
+
const trace = buildTrace(opts.graph, opts.importer, opts.maxTraceDepth);
|
|
286
|
+
await addTraceImportLocations(opts.provider, trace, opts.importLocCache, importSpecifierLocationIndex.find);
|
|
287
|
+
if (trace.length > 0) {
|
|
288
|
+
const last = trace[trace.length - 1];
|
|
289
|
+
if (!last.specifier) last.specifier = opts.specifier;
|
|
290
|
+
if (opts.importerLoc && last.line == null) {
|
|
291
|
+
last.line = opts.importerLoc.line;
|
|
292
|
+
last.column = opts.importerLoc.column;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return trace;
|
|
296
|
+
}
|
|
297
|
+
async function buildViolationInfo(opts) {
|
|
298
|
+
const importerLoc = await resolveImporterLocation({
|
|
299
|
+
provider: opts.provider,
|
|
300
|
+
importLocCache: opts.importLocCache,
|
|
301
|
+
importer: opts.importer,
|
|
302
|
+
sourceCandidates: buildSourceCandidates(opts.source, opts.resolved, opts.config.root),
|
|
303
|
+
preferOriginalCode: opts.preferOriginalCode,
|
|
304
|
+
envType: opts.envType
|
|
305
|
+
});
|
|
306
|
+
const trace = await rebuildAndAnnotateTrace({
|
|
307
|
+
provider: opts.provider,
|
|
308
|
+
graph: opts.graph,
|
|
309
|
+
importLocCache: opts.importLocCache,
|
|
310
|
+
importer: opts.importer,
|
|
311
|
+
specifier: opts.source,
|
|
312
|
+
importerLoc,
|
|
313
|
+
maxTraceDepth: opts.config.maxTraceDepth
|
|
314
|
+
});
|
|
315
|
+
const snippet = importerLoc ? buildCodeSnippet(opts.provider, opts.importer, importerLoc) : void 0;
|
|
316
|
+
return {
|
|
317
|
+
env: opts.envName,
|
|
318
|
+
envType: opts.envType,
|
|
319
|
+
behavior: opts.config.effectiveBehavior,
|
|
320
|
+
type: opts.type,
|
|
321
|
+
pattern: opts.pattern,
|
|
322
|
+
specifier: opts.source,
|
|
323
|
+
importer: opts.importer,
|
|
324
|
+
...opts.resolved ? { resolved: opts.resolved } : {},
|
|
325
|
+
...importerLoc ? { importerLoc } : {},
|
|
326
|
+
trace,
|
|
327
|
+
snippet
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
async function getMarkerKindForFile(opts) {
|
|
331
|
+
if (!isImportProtectionSourceFile(opts.file)) return;
|
|
332
|
+
let cached = opts.markerKindCache.get(opts.file);
|
|
333
|
+
if (!cached) {
|
|
334
|
+
cached = (async () => {
|
|
335
|
+
const code = opts.provider.getTransformResult(opts.file)?.originalCode ?? await opts.loadOriginalCode(opts.file);
|
|
336
|
+
if (!code) return;
|
|
337
|
+
const imports = getImportSources(code);
|
|
338
|
+
const hasServerOnly = imports.some((source) => opts.config.markerSpecifiers.serverOnly.has(source));
|
|
339
|
+
const hasClientOnly = imports.some((source) => opts.config.markerSpecifiers.clientOnly.has(source));
|
|
340
|
+
if (hasServerOnly && !hasClientOnly) return "server";
|
|
341
|
+
if (hasClientOnly && !hasServerOnly) return "client";
|
|
342
|
+
})();
|
|
343
|
+
opts.markerKindCache.set(opts.file, cached);
|
|
344
|
+
}
|
|
345
|
+
return cached;
|
|
346
|
+
}
|
|
347
|
+
async function reportViolation(opts) {
|
|
348
|
+
const key = dedupeKey(opts.info);
|
|
349
|
+
if (opts.config.logMode !== "always" && opts.envState.seenViolations.has(key)) return;
|
|
350
|
+
opts.envState.seenViolations.add(key);
|
|
351
|
+
if (opts.config.onViolation) {
|
|
352
|
+
if (await opts.config.onViolation(opts.info) === false) return;
|
|
353
|
+
}
|
|
354
|
+
const message = formatViolation(opts.info, opts.config.root);
|
|
355
|
+
const error = new opts.rspack.WebpackError(message);
|
|
356
|
+
if (opts.config.effectiveBehavior === "error") opts.compilation.errors.push(error);
|
|
357
|
+
else opts.compilation.warnings.push(error);
|
|
358
|
+
}
|
|
359
|
+
function registerImportProtection(api, opts) {
|
|
360
|
+
const extensionlessResolver = new ExtensionlessAbsoluteIdResolver();
|
|
361
|
+
const envStates = /* @__PURE__ */ new Map();
|
|
362
|
+
const fileReadCache = /* @__PURE__ */ new Map();
|
|
363
|
+
const config = {
|
|
364
|
+
enabled: true,
|
|
365
|
+
root: "",
|
|
366
|
+
command: api.context.action === "dev" ? "serve" : "build",
|
|
367
|
+
srcDirectory: "",
|
|
368
|
+
framework: opts.framework,
|
|
369
|
+
effectiveBehavior: "error",
|
|
370
|
+
mockAccess: "error",
|
|
371
|
+
logMode: "once",
|
|
372
|
+
maxTraceDepth: 20,
|
|
373
|
+
compiledRules: {
|
|
374
|
+
client: {
|
|
375
|
+
specifiers: [],
|
|
376
|
+
files: [],
|
|
377
|
+
excludeFiles: []
|
|
378
|
+
},
|
|
379
|
+
server: {
|
|
380
|
+
specifiers: [],
|
|
381
|
+
files: [],
|
|
382
|
+
excludeFiles: []
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
includeMatchers: [],
|
|
386
|
+
excludeMatchers: [],
|
|
387
|
+
ignoreImporterMatchers: [],
|
|
388
|
+
markerSpecifiers: {
|
|
389
|
+
serverOnly: /* @__PURE__ */ new Set(),
|
|
390
|
+
clientOnly: /* @__PURE__ */ new Set()
|
|
391
|
+
},
|
|
392
|
+
envTypeMap: new Map(opts.environments.map((env) => [env.name, env.type])),
|
|
393
|
+
onViolation: void 0
|
|
394
|
+
};
|
|
395
|
+
const shared = {
|
|
396
|
+
root: "",
|
|
397
|
+
virtualModules: /* @__PURE__ */ new Map(),
|
|
398
|
+
vmPlugins: {},
|
|
399
|
+
readyVmPlugins: {},
|
|
400
|
+
inputFileSystems: {},
|
|
401
|
+
pendingWrites: /* @__PURE__ */ new Map()
|
|
402
|
+
};
|
|
403
|
+
function applyUserConfig() {
|
|
404
|
+
const { startConfig, resolvedStartConfig } = opts.getConfig();
|
|
405
|
+
config.root = resolvedStartConfig.root;
|
|
406
|
+
config.srcDirectory = resolvedStartConfig.srcDirectory;
|
|
407
|
+
shared.root = resolvedStartConfig.root;
|
|
408
|
+
const userOpts = startConfig.importProtection;
|
|
409
|
+
if (userOpts?.enabled === false) {
|
|
410
|
+
config.enabled = false;
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
config.enabled = true;
|
|
414
|
+
const behavior = userOpts?.behavior;
|
|
415
|
+
if (typeof behavior === "string") config.effectiveBehavior = behavior;
|
|
416
|
+
else config.effectiveBehavior = config.command === "serve" ? behavior?.dev ?? "mock" : behavior?.build ?? "error";
|
|
417
|
+
config.logMode = userOpts?.log ?? "once";
|
|
418
|
+
config.mockAccess = userOpts?.mockAccess ?? "error";
|
|
419
|
+
config.maxTraceDepth = userOpts?.maxTraceDepth ?? 20;
|
|
420
|
+
config.onViolation = userOpts?.onViolation ? (info) => userOpts.onViolation?.(info) : void 0;
|
|
421
|
+
const defaults = getDefaultImportProtectionRules();
|
|
422
|
+
const pick = (user, fallback) => user ? [...user] : [...fallback];
|
|
423
|
+
const clientSpecifiers = dedupePatterns([...defaults.client.specifiers, ...userOpts?.client?.specifiers ?? []]);
|
|
424
|
+
config.compiledRules.client = {
|
|
425
|
+
specifiers: compileMatchers(clientSpecifiers),
|
|
426
|
+
files: compileMatchers(pick(userOpts?.client?.files, defaults.client.files)),
|
|
427
|
+
excludeFiles: compileMatchers(pick(userOpts?.client?.excludeFiles, defaults.client.excludeFiles))
|
|
428
|
+
};
|
|
429
|
+
config.compiledRules.server = {
|
|
430
|
+
specifiers: compileMatchers(dedupePatterns(pick(userOpts?.server?.specifiers, defaults.server.specifiers))),
|
|
431
|
+
files: compileMatchers(pick(userOpts?.server?.files, defaults.server.files)),
|
|
432
|
+
excludeFiles: compileMatchers(pick(userOpts?.server?.excludeFiles, defaults.server.excludeFiles))
|
|
433
|
+
};
|
|
434
|
+
config.includeMatchers = compileMatchers(userOpts?.include ?? []);
|
|
435
|
+
config.excludeMatchers = compileMatchers(userOpts?.exclude ?? []);
|
|
436
|
+
config.ignoreImporterMatchers = compileMatchers(userOpts?.ignoreImporters ?? []);
|
|
437
|
+
const markers = getMarkerSpecifiers();
|
|
438
|
+
config.markerSpecifiers = {
|
|
439
|
+
serverOnly: new Set(markers.serverOnly),
|
|
440
|
+
clientOnly: new Set(markers.clientOnly)
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
api.onBeforeBuild(() => {
|
|
444
|
+
applyUserConfig();
|
|
445
|
+
clearNormalizeFilePathCache();
|
|
446
|
+
extensionlessResolver.clear();
|
|
447
|
+
fileReadCache.clear();
|
|
448
|
+
envStates.clear();
|
|
449
|
+
});
|
|
450
|
+
api.onBeforeDevCompile(() => {
|
|
451
|
+
applyUserConfig();
|
|
452
|
+
clearNormalizeFilePathCache();
|
|
453
|
+
extensionlessResolver.clear();
|
|
454
|
+
fileReadCache.clear();
|
|
455
|
+
for (const envState of envStates.values()) {
|
|
456
|
+
envState.resolveCache.clear();
|
|
457
|
+
envState.buildTransformResults.clear();
|
|
458
|
+
envState.deferredFileViolations.length = 0;
|
|
459
|
+
envState.deferredFileViolationKeys.clear();
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
api.modifyRspackConfig((rspackConfig, utils) => {
|
|
463
|
+
applyUserConfig();
|
|
464
|
+
const envName = utils.environment.name;
|
|
465
|
+
const VMP = utils.rspack.experiments.VirtualModulesPlugin;
|
|
466
|
+
const vmPlugin = new VMP({});
|
|
467
|
+
shared.vmPlugins[envName] = vmPlugin;
|
|
468
|
+
shared.readyVmPlugins[envName] = false;
|
|
469
|
+
rspackConfig.plugins.push(vmPlugin);
|
|
470
|
+
rspackConfig.plugins.push({ apply(compiler) {
|
|
471
|
+
shared.inputFileSystems[envName] = compiler.inputFileSystem;
|
|
472
|
+
compiler.hooks.thisCompilation.tap("TanStackStartImportProtectionVirtualModulesReady", () => {
|
|
473
|
+
shared.readyVmPlugins[envName] = true;
|
|
474
|
+
flushPendingWrites(shared, envName);
|
|
475
|
+
});
|
|
476
|
+
} });
|
|
477
|
+
});
|
|
478
|
+
for (const environment of opts.environments) api.transform({
|
|
479
|
+
test: /\.[cm]?[tj]sx?$/,
|
|
480
|
+
environments: [environment.name],
|
|
481
|
+
order: "post"
|
|
482
|
+
}, async (ctx) => {
|
|
483
|
+
if (!config.enabled) return ctx.code;
|
|
484
|
+
const envName = environment.name;
|
|
485
|
+
const envType = getImportProtectionEnvType(config, envName);
|
|
486
|
+
const envState = getOrCreateEnvState(envStates, envName);
|
|
487
|
+
const id = ctx.resource;
|
|
488
|
+
const file = normalizeFilePath(ctx.resourcePath);
|
|
489
|
+
if (!shouldCheckImportProtectionImporter(config, file)) return ctx.code;
|
|
490
|
+
const matchers = getRulesForEnvironment(config, envName);
|
|
491
|
+
const relativeFile = getImportProtectionRelativePath(config.root, file);
|
|
492
|
+
const importSources = getImportSources(ctx.code);
|
|
493
|
+
const transformedImportSources = new Set(importSources);
|
|
494
|
+
const transformInputFileSystem = shared.inputFileSystems[envName];
|
|
495
|
+
const loadOriginalCodeForTransform = transformInputFileSystem ? (target) => loadOriginalCodeFromInputFileSystem(transformInputFileSystem, target) : () => Promise.resolve(void 0);
|
|
496
|
+
const originalCode = config.command === "build" ? await loadOriginalCode(fileReadCache, file, loadOriginalCodeForTransform) : void 0;
|
|
497
|
+
const buildImportSources = originalCode ? getImportSources(originalCode) : [];
|
|
498
|
+
const buildTransformResult = config.command === "build" ? {
|
|
499
|
+
code: ctx.code,
|
|
500
|
+
map: void 0,
|
|
501
|
+
originalCode,
|
|
502
|
+
lineIndex: buildLineIndex(ctx.code)
|
|
503
|
+
} : void 0;
|
|
504
|
+
if (config.command === "build") {
|
|
505
|
+
const relativeBuildFile = getImportProtectionRelativePath(config.root, file);
|
|
506
|
+
addTransformResult(envState.buildTransformResults, file, buildTransformResult);
|
|
507
|
+
addTransformResult(envState.buildTransformResults, relativeBuildFile, buildTransformResult);
|
|
508
|
+
if (id !== file) addTransformResult(envState.buildTransformResults, id, buildTransformResult);
|
|
509
|
+
}
|
|
510
|
+
const hasServerOnlyMarker = importSources.some((source) => config.markerSpecifiers.serverOnly.has(source));
|
|
511
|
+
const hasClientOnlyMarker = importSources.some((source) => config.markerSpecifiers.clientOnly.has(source));
|
|
512
|
+
if (hasServerOnlyMarker && hasClientOnlyMarker) throw new Error(`[import-protection] File "${relativeFile}" has both server-only and client-only markers. This is not allowed.`);
|
|
513
|
+
const markerKind = hasServerOnlyMarker ? "server" : hasClientOnlyMarker ? "client" : void 0;
|
|
514
|
+
if (checkFileDenial(relativeFile, matchers) || envType === "client" && markerKind === "server" || envType === "server" && markerKind === "client") {
|
|
515
|
+
let exportNames = [];
|
|
516
|
+
try {
|
|
517
|
+
exportNames = getNamedExports(ctx.code);
|
|
518
|
+
} catch {
|
|
519
|
+
exportNames = [];
|
|
520
|
+
}
|
|
521
|
+
if (config.command === "build") return generateSelfContainedMockModule(exportNames);
|
|
522
|
+
const runtimeId = ensureRuntimeMockModule({
|
|
523
|
+
shared,
|
|
524
|
+
envName,
|
|
525
|
+
mode: config.mockAccess,
|
|
526
|
+
env: envName,
|
|
527
|
+
importer: file,
|
|
528
|
+
specifier: relativeFile
|
|
529
|
+
});
|
|
530
|
+
return generateDevSelfDenialModule(exportNames, runtimeId);
|
|
531
|
+
}
|
|
532
|
+
const deniedSpecifierReplacements = /* @__PURE__ */ new Map();
|
|
533
|
+
const exportsBySource = (() => {
|
|
534
|
+
try {
|
|
535
|
+
return getMockExportNamesBySource(ctx.code);
|
|
536
|
+
} catch {
|
|
537
|
+
return /* @__PURE__ */ new Map();
|
|
538
|
+
}
|
|
539
|
+
})();
|
|
540
|
+
for (const source of importSources) {
|
|
541
|
+
const specifierMatch = matchesAny(source, matchers.specifiers);
|
|
542
|
+
if (!specifierMatch && config.command === "build") {
|
|
543
|
+
const resolved = await resolveAgainstImporter({
|
|
544
|
+
envState,
|
|
545
|
+
config,
|
|
546
|
+
ctx,
|
|
547
|
+
importerId: id,
|
|
548
|
+
source,
|
|
549
|
+
extensionlessResolver
|
|
550
|
+
});
|
|
551
|
+
if (resolved) {
|
|
552
|
+
const relativeResolved = getImportProtectionRelativePath(config.root, resolved);
|
|
553
|
+
const buildFileMatch = checkFileDenial(relativeResolved, matchers);
|
|
554
|
+
if (buildFileMatch && hasOriginalUnsafeUsage(buildTransformResult, source, envType)) deferFileViolation(envState, {
|
|
555
|
+
importer: file,
|
|
556
|
+
specifier: source,
|
|
557
|
+
resolved,
|
|
558
|
+
relativeResolved,
|
|
559
|
+
pattern: buildFileMatch.pattern,
|
|
560
|
+
useOriginalLocation: true
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
if (!specifierMatch) continue;
|
|
566
|
+
const resolved = await resolveAgainstImporter({
|
|
567
|
+
envState,
|
|
568
|
+
config,
|
|
569
|
+
ctx,
|
|
570
|
+
importerId: id,
|
|
571
|
+
source,
|
|
572
|
+
extensionlessResolver
|
|
573
|
+
});
|
|
574
|
+
const runtimeId = config.command === "build" ? ensureSilentMockModule(shared, envName) : ensureRuntimeMockModule({
|
|
575
|
+
shared,
|
|
576
|
+
envName,
|
|
577
|
+
mode: config.mockAccess,
|
|
578
|
+
env: envName,
|
|
579
|
+
importer: file,
|
|
580
|
+
specifier: source
|
|
581
|
+
});
|
|
582
|
+
const replacement = ensureMockEdgeModule({
|
|
583
|
+
shared,
|
|
584
|
+
envName,
|
|
585
|
+
payload: {
|
|
586
|
+
exports: exportsBySource.get(source) ?? [],
|
|
587
|
+
runtimeId,
|
|
588
|
+
violation: {
|
|
589
|
+
env: envName,
|
|
590
|
+
envType,
|
|
591
|
+
importer: file,
|
|
592
|
+
specifier: source,
|
|
593
|
+
...resolved ? { resolved } : {},
|
|
594
|
+
patternText: serializePattern(specifierMatch.pattern)
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
});
|
|
598
|
+
deniedSpecifierReplacements.set(source, replacement);
|
|
599
|
+
}
|
|
600
|
+
if (config.command === "build") for (const source of buildImportSources) {
|
|
601
|
+
if (transformedImportSources.has(source)) continue;
|
|
602
|
+
if (matchesAny(source, matchers.specifiers)) continue;
|
|
603
|
+
const resolved = await resolveAgainstImporter({
|
|
604
|
+
envState,
|
|
605
|
+
config,
|
|
606
|
+
ctx,
|
|
607
|
+
importerId: id,
|
|
608
|
+
source,
|
|
609
|
+
extensionlessResolver
|
|
610
|
+
});
|
|
611
|
+
if (!resolved) continue;
|
|
612
|
+
const relativeResolved = getImportProtectionRelativePath(config.root, resolved);
|
|
613
|
+
const buildFileMatch = checkFileDenial(relativeResolved, matchers);
|
|
614
|
+
if (!buildFileMatch || !hasOriginalUnsafeUsage(buildTransformResult, source, envType)) continue;
|
|
615
|
+
deferFileViolation(envState, {
|
|
616
|
+
importer: file,
|
|
617
|
+
specifier: source,
|
|
618
|
+
resolved,
|
|
619
|
+
relativeResolved,
|
|
620
|
+
pattern: buildFileMatch.pattern,
|
|
621
|
+
useOriginalLocation: true
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
if (deniedSpecifierReplacements.size === 0) return ctx.code;
|
|
625
|
+
const rewritten = rewriteDeniedImports(ctx.code, id, new Set(deniedSpecifierReplacements.keys()), (source) => deniedSpecifierReplacements.get(source) ?? source);
|
|
626
|
+
if (!rewritten) return ctx.code;
|
|
627
|
+
return {
|
|
628
|
+
code: rewritten.code,
|
|
629
|
+
map: normalizeSourceMap(rewritten.map) ?? null
|
|
630
|
+
};
|
|
631
|
+
});
|
|
632
|
+
api.processAssets({
|
|
633
|
+
stage: "report",
|
|
634
|
+
environments: opts.environments.map((environment) => environment.name)
|
|
635
|
+
}, async (context) => {
|
|
636
|
+
if (!config.enabled) return;
|
|
637
|
+
const envName = context.environment.name;
|
|
638
|
+
const envType = getImportProtectionEnvType(config, envName);
|
|
639
|
+
const envState = getOrCreateEnvState(envStates, envName);
|
|
640
|
+
const matchers = getRulesForEnvironment(config, envName);
|
|
641
|
+
const processFileReadCache = /* @__PURE__ */ new Map();
|
|
642
|
+
const loadOriginalCodeFromCompilation = (file) => loadOriginalCode(processFileReadCache, file, context.compilation.inputFileSystem ? (target) => loadOriginalCodeFromInputFileSystem(context.compilation.inputFileSystem, target) : () => Promise.resolve(void 0));
|
|
643
|
+
const relevantModules = Array.from(context.compilation.modules).filter(isImportProtectionSourceModule);
|
|
644
|
+
const provider = await buildTransformResultProvider({
|
|
645
|
+
modules: relevantModules,
|
|
646
|
+
root: config.root,
|
|
647
|
+
loadOriginalCode: loadOriginalCodeFromCompilation,
|
|
648
|
+
preloaded: envState.buildTransformResults
|
|
649
|
+
});
|
|
650
|
+
const importLocCache = new ImportLocCache();
|
|
651
|
+
const markerKindCache = /* @__PURE__ */ new Map();
|
|
652
|
+
const { graph, edges } = buildCompilationGraph({
|
|
653
|
+
compilation: context.compilation,
|
|
654
|
+
modules: relevantModules
|
|
655
|
+
});
|
|
656
|
+
const liveFileEdgeKeys = new Set(edges.filter((edge) => !!edge.specifier).map((edge) => `${normalizeFilePath(edge.importer)}::${edge.specifier}::${normalizeFilePath(edge.resolved)}`));
|
|
657
|
+
const survivingModules = /* @__PURE__ */ new Set();
|
|
658
|
+
for (const module of relevantModules) for (const candidate of buildResolutionCandidates(getModuleFile(module))) survivingModules.add(candidate);
|
|
659
|
+
const didModuleSurvive = (id) => buildResolutionCandidates(id).some((candidate) => survivingModules.has(candidate));
|
|
660
|
+
for (const module of relevantModules) {
|
|
661
|
+
const payload = getMockEdgePayloadFromFile(getModuleFile(module));
|
|
662
|
+
if (!payload) continue;
|
|
663
|
+
if (!shouldCheckImportProtectionImporter(config, payload.violation.importer)) continue;
|
|
664
|
+
const info = await buildViolationInfo({
|
|
665
|
+
config,
|
|
666
|
+
provider,
|
|
667
|
+
graph,
|
|
668
|
+
importLocCache,
|
|
669
|
+
envName,
|
|
670
|
+
envType,
|
|
671
|
+
importer: payload.violation.importer,
|
|
672
|
+
source: payload.violation.specifier,
|
|
673
|
+
resolved: payload.violation.resolved,
|
|
674
|
+
type: "specifier",
|
|
675
|
+
pattern: payload.violation.patternText,
|
|
676
|
+
preferOriginalCode: true
|
|
677
|
+
});
|
|
678
|
+
await reportViolation({
|
|
679
|
+
config,
|
|
680
|
+
envState,
|
|
681
|
+
compilation: context.compilation,
|
|
682
|
+
rspack: context.compiler.rspack,
|
|
683
|
+
info
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
for (const edge of edges) {
|
|
687
|
+
if (!edge.specifier) continue;
|
|
688
|
+
if (!shouldCheckImportProtectionImporter(config, edge.importer)) continue;
|
|
689
|
+
const relativeResolved = getImportProtectionRelativePath(config.root, edge.resolved);
|
|
690
|
+
if (isFileExcluded(relativeResolved, matchers)) continue;
|
|
691
|
+
const fileMatch = checkFileDenial(relativeResolved, matchers);
|
|
692
|
+
if (fileMatch) {
|
|
693
|
+
const info = await buildViolationInfo({
|
|
694
|
+
config,
|
|
695
|
+
provider,
|
|
696
|
+
graph,
|
|
697
|
+
importLocCache,
|
|
698
|
+
envName,
|
|
699
|
+
envType,
|
|
700
|
+
importer: edge.importer,
|
|
701
|
+
source: edge.specifier,
|
|
702
|
+
resolved: edge.resolved,
|
|
703
|
+
type: "file",
|
|
704
|
+
pattern: fileMatch.pattern
|
|
705
|
+
});
|
|
706
|
+
await reportViolation({
|
|
707
|
+
config,
|
|
708
|
+
envState,
|
|
709
|
+
compilation: context.compilation,
|
|
710
|
+
rspack: context.compiler.rspack,
|
|
711
|
+
info
|
|
712
|
+
});
|
|
713
|
+
continue;
|
|
714
|
+
}
|
|
715
|
+
const markerKind = await getMarkerKindForFile({
|
|
716
|
+
config,
|
|
717
|
+
provider,
|
|
718
|
+
loadOriginalCode: loadOriginalCodeFromCompilation,
|
|
719
|
+
markerKindCache,
|
|
720
|
+
file: edge.resolved
|
|
721
|
+
});
|
|
722
|
+
if (!(envType === "client" && markerKind === "server" || envType === "server" && markerKind === "client")) continue;
|
|
723
|
+
const info = await buildViolationInfo({
|
|
724
|
+
config,
|
|
725
|
+
provider,
|
|
726
|
+
graph,
|
|
727
|
+
importLocCache,
|
|
728
|
+
envName,
|
|
729
|
+
envType,
|
|
730
|
+
importer: edge.importer,
|
|
731
|
+
source: edge.specifier,
|
|
732
|
+
resolved: edge.resolved,
|
|
733
|
+
type: "marker"
|
|
734
|
+
});
|
|
735
|
+
await reportViolation({
|
|
736
|
+
config,
|
|
737
|
+
envState,
|
|
738
|
+
compilation: context.compilation,
|
|
739
|
+
rspack: context.compiler.rspack,
|
|
740
|
+
info
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
for (const violation of envState.deferredFileViolations) {
|
|
744
|
+
const liveEdgeKey = `${normalizeFilePath(violation.importer)}::${violation.specifier}::${normalizeFilePath(violation.resolved)}`;
|
|
745
|
+
if (liveFileEdgeKeys.has(liveEdgeKey)) continue;
|
|
746
|
+
if (!didModuleSurvive(violation.resolved)) continue;
|
|
747
|
+
if (!didModuleSurvive(violation.importer)) continue;
|
|
748
|
+
const info = await buildViolationInfo({
|
|
749
|
+
config,
|
|
750
|
+
provider,
|
|
751
|
+
graph,
|
|
752
|
+
importLocCache,
|
|
753
|
+
envName,
|
|
754
|
+
envType,
|
|
755
|
+
importer: violation.importer,
|
|
756
|
+
source: violation.specifier,
|
|
757
|
+
resolved: violation.resolved,
|
|
758
|
+
type: "file",
|
|
759
|
+
pattern: violation.pattern,
|
|
760
|
+
preferOriginalCode: violation.useOriginalLocation
|
|
761
|
+
});
|
|
762
|
+
await reportViolation({
|
|
763
|
+
config,
|
|
764
|
+
envState,
|
|
765
|
+
compilation: context.compilation,
|
|
766
|
+
rspack: context.compiler.rspack,
|
|
767
|
+
info
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
//#endregion
|
|
773
|
+
export { registerImportProtection };
|
|
774
|
+
|
|
775
|
+
//# sourceMappingURL=import-protection.js.map
|