@cubing/dev-config 0.7.0 → 0.7.1
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/bin/package.json/index.js +580 -0
- package/bin/package.json/index.js.map +7 -0
- package/chunks/chunk-NX2753TH.js +28 -0
- package/chunks/chunk-NX2753TH.js.map +7 -0
- package/chunks/chunk-OZDRWEHO.js +51 -0
- package/chunks/chunk-OZDRWEHO.js.map +7 -0
- package/esbuild/es2022/index.js +5 -22
- package/esbuild/es2022/index.js.map +3 -3
- package/lib/check-allowed-imports/checkAllowedImports.d.ts +20 -0
- package/lib/check-allowed-imports/index.d.ts +1 -0
- package/lib/check-allowed-imports/index.js +110 -0
- package/lib/check-allowed-imports/index.js.map +7 -0
- package/package.json +6 -3
- package/bin/package.json.ts +0 -700
- package/lib/check-allowed-imports/checkAllowedImports.ts +0 -152
- package/lib/check-allowed-imports/index.ts +0 -4
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type BuildOptions,
|
|
3
|
-
build,
|
|
4
|
-
type ImportKind,
|
|
5
|
-
type Metafile,
|
|
6
|
-
type Plugin,
|
|
7
|
-
} from "esbuild";
|
|
8
|
-
import { es2022Lib } from "../../src/esbuild/es2022";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Note:
|
|
12
|
-
* - A file may be matched by any parent path scope key.
|
|
13
|
-
* - Files in a given scope key are allowed to import any other within the same scope.
|
|
14
|
-
*/
|
|
15
|
-
export type AllowedImports = {
|
|
16
|
-
[scope: string]: { static?: string[]; dynamic?: string[] };
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const plugin = {
|
|
20
|
-
name: "mark-bare-imports-as-external",
|
|
21
|
-
setup(build) {
|
|
22
|
-
const filter = /^[^./]|^\.[^./]|^\.\.[^/]/; // Must not start with "/" or "./" or "../"
|
|
23
|
-
build.onResolve({ filter }, (args) => ({
|
|
24
|
-
path: args.path,
|
|
25
|
-
external: true,
|
|
26
|
-
}));
|
|
27
|
-
},
|
|
28
|
-
} satisfies Plugin;
|
|
29
|
-
|
|
30
|
-
export async function checkAllowedImports(
|
|
31
|
-
groups: {
|
|
32
|
-
[description: string]: {
|
|
33
|
-
entryPoints: string[];
|
|
34
|
-
allowedImports: AllowedImports;
|
|
35
|
-
};
|
|
36
|
-
},
|
|
37
|
-
options?: { overrideEsbuildOptions: BuildOptions },
|
|
38
|
-
): Promise<void> {
|
|
39
|
-
let failure = false;
|
|
40
|
-
|
|
41
|
-
console.log("+ means a new file");
|
|
42
|
-
console.log("- means a valid import for that file");
|
|
43
|
-
|
|
44
|
-
for (const [description, { entryPoints, allowedImports }] of Object.entries(
|
|
45
|
-
groups,
|
|
46
|
-
)) {
|
|
47
|
-
console.log(`# ${description}`);
|
|
48
|
-
// From https://github.com/evanw/esbuild/issues/619#issuecomment-1504100390
|
|
49
|
-
|
|
50
|
-
const { metafile } = await build({
|
|
51
|
-
...es2022Lib(),
|
|
52
|
-
entryPoints,
|
|
53
|
-
plugins: [plugin],
|
|
54
|
-
...options?.overrideEsbuildOptions,
|
|
55
|
-
write: false,
|
|
56
|
-
metafile: true,
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// Starts with the path and then keeps chopping off from the right.
|
|
60
|
-
function* pathPrefixes(path: string) {
|
|
61
|
-
const pathParts = path.split("/");
|
|
62
|
-
for (let n = pathParts.length; n > 0; n--) {
|
|
63
|
-
yield pathParts.slice(0, n).join("/");
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function matchingPathPrefix(matchPrefixes: string[], path: string) {
|
|
68
|
-
for (const pathPrefix of pathPrefixes(path)) {
|
|
69
|
-
if (matchPrefixes.includes(pathPrefix)) {
|
|
70
|
-
return pathPrefix;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const importKindMap: Partial<Record<ImportKind, "static" | "dynamic">> = {
|
|
77
|
-
"import-statement": "static",
|
|
78
|
-
"dynamic-import": "dynamic",
|
|
79
|
-
} as const;
|
|
80
|
-
|
|
81
|
-
function checkImport(
|
|
82
|
-
sourcePath: string,
|
|
83
|
-
importInfo: {
|
|
84
|
-
path: string;
|
|
85
|
-
kind: ImportKind;
|
|
86
|
-
external?: boolean;
|
|
87
|
-
original?: string;
|
|
88
|
-
},
|
|
89
|
-
allowedImports: AllowedImports,
|
|
90
|
-
) {
|
|
91
|
-
const importKind = importKindMap[importInfo.kind];
|
|
92
|
-
if (!importKind) {
|
|
93
|
-
throw new Error("Unexpected import kind!");
|
|
94
|
-
}
|
|
95
|
-
for (const sourcePathPrefix of pathPrefixes(sourcePath)) {
|
|
96
|
-
const matchingSourcePathPrefix = matchingPathPrefix(
|
|
97
|
-
Object.keys(allowedImports),
|
|
98
|
-
sourcePathPrefix,
|
|
99
|
-
);
|
|
100
|
-
if (matchingSourcePathPrefix) {
|
|
101
|
-
const allowedImportsForKind =
|
|
102
|
-
allowedImports[matchingSourcePathPrefix][importKind];
|
|
103
|
-
if (
|
|
104
|
-
typeof allowedImportsForKind !== "undefined" &&
|
|
105
|
-
!Array.isArray(allowedImportsForKind)
|
|
106
|
-
) {
|
|
107
|
-
throw new Error(
|
|
108
|
-
`Expected a string list for ${importKind} imports under the scope "${matchingSourcePathPrefix}"`,
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
if (
|
|
112
|
-
matchingPathPrefix(
|
|
113
|
-
[
|
|
114
|
-
matchingSourcePathPrefix, // allow importing from any source group to itself.
|
|
115
|
-
...(allowedImportsForKind ?? []),
|
|
116
|
-
],
|
|
117
|
-
importInfo.path,
|
|
118
|
-
)
|
|
119
|
-
) {
|
|
120
|
-
process.stdout.write("-");
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
failure = true;
|
|
126
|
-
console.error(`\n❌ File has disallowed ${importKind} import:`);
|
|
127
|
-
console.error(`From file: ${sourcePath}`);
|
|
128
|
-
console.error(`Importing: ${importInfo.path}`);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
async function checkImports(
|
|
132
|
-
metafile: Metafile,
|
|
133
|
-
allowedImports: AllowedImports,
|
|
134
|
-
) {
|
|
135
|
-
for (const [filePath, importInfoList] of Object.entries(
|
|
136
|
-
metafile.inputs,
|
|
137
|
-
)) {
|
|
138
|
-
process.stdout.write("+");
|
|
139
|
-
for (const importInfo of importInfoList.imports) {
|
|
140
|
-
checkImport(filePath, importInfo, allowedImports);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
console.log();
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
await checkImports(metafile, allowedImports);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (failure) {
|
|
150
|
-
throw new Error("Failure");
|
|
151
|
-
}
|
|
152
|
-
}
|