@k8slens/package-build 3.2.0 → 3.4.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.
@@ -1,105 +1,14 @@
1
1
  import { type Plugin, build } from "esbuild";
2
2
  import path from "node:path";
3
3
  import { readFileSync } from "node:fs";
4
- import { builtinModules as rawBuiltinModules } from "node:module";
5
4
  import yargs from "yargs";
5
+ import { declaredImportsPlugin } from "./declared-imports-plugin";
6
6
  import { globImportPlugin } from "./glob-import-plugin";
7
7
  import { stylesheetPlugin } from "./stylesheet-plugin";
8
- import { partition } from "lodash";
9
8
 
10
9
  const CWD = process.cwd();
11
10
  const packageJson = JSON.parse(readFileSync(path.resolve(CWD, "package.json"), "utf-8"));
12
11
 
13
- const isExperimentalModule = (module: string) => module.startsWith("_");
14
- const alsoAddNodeRefs = (module: string) => [module, `node:${module}`];
15
-
16
- const { builtinModules, disallowedBuiltinModules } = (() => {
17
- const [disallowed, allowed] = partition(rawBuiltinModules, isExperimentalModule);
18
- const disallowedBuiltinModules = new Set(disallowed.flatMap(alsoAddNodeRefs));
19
- const builtinModules = new Set(allowed.flatMap(alsoAddNodeRefs));
20
-
21
- return { disallowedBuiltinModules, builtinModules };
22
- })();
23
-
24
- const getPackageName = (path: string) => {
25
- const [firstPart, secondPart] = path.split("/");
26
-
27
- if (path.startsWith("@")) {
28
- return [firstPart, secondPart].join("/");
29
- }
30
-
31
- return firstPart;
32
- };
33
-
34
- const bundleDependency = null;
35
-
36
- const ensureCorrectImportsPlugin: Plugin = {
37
- name: "ensure-correct-imports",
38
- setup: (build) => {
39
- build.onResolve({ filter: /^\./ }, (args) => {
40
- const fullPath = path.resolve(args.resolveDir, args.path);
41
- const relativePathToImporter = path.relative(CWD, args.importer);
42
- const relativePath = path.relative(CWD, fullPath);
43
-
44
- if (relativePath.startsWith("..") && !relativePathToImporter.startsWith("..")) {
45
- throw new Error(
46
- `Tried to import "${args.path}" from "${relativePathToImporter}" but it is outside of the package.`,
47
- );
48
- }
49
-
50
- return bundleDependency;
51
- });
52
- build.onResolve({ filter: /^[^.]/ }, async (args) => {
53
- // Enforce declared imports only for the package's own sources (.ts/.tsx/.js/.jsx).
54
- // Anything imported from outside the package directory — node_modules files or
55
- // other workspace packages' dists (whose realpaths live outside node_modules) —
56
- // belongs to an already-bundled dependency and must keep bundling freely.
57
- const relativePathToImporter = path.relative(CWD, args.importer);
58
- const importerIsOwnFile =
59
- !relativePathToImporter.startsWith("..") && !relativePathToImporter.split(path.sep).includes("node_modules");
60
- const importerIsSourceFile = /\.(t|j)sx?$/.test(args.importer);
61
-
62
- if (!importerIsOwnFile || !importerIsSourceFile) {
63
- return bundleDependency;
64
- }
65
-
66
- if (disallowedBuiltinModules.has(args.path)) {
67
- throw new Error(
68
- `Tried to import "${args.path}" from "${args.importer}" but it is a disallowed builtin module.`,
69
- );
70
- }
71
-
72
- if (builtinModules.has(args.path)) {
73
- return {
74
- external: true,
75
- path: args.path,
76
- };
77
- }
78
-
79
- const packageName = getPackageName(args.path);
80
-
81
- const isExplicitDependency = packageName in (packageJson.dependencies ?? {});
82
- const isExplicitDevDependency = packageName in (packageJson.devDependencies ?? {});
83
-
84
- if (isExplicitDependency) {
85
- // Note: Return the full path since we assume that it is a "exports" entry of the dependency found in package.json
86
- return {
87
- external: true,
88
- path: args.path,
89
- };
90
- }
91
-
92
- if (isExplicitDevDependency) {
93
- return bundleDependency;
94
- }
95
-
96
- throw new Error(
97
- `Tried to import "${args.path}" from "${relativePathToImporter}" but it is not a dependency of the package.`,
98
- );
99
- });
100
- },
101
- };
102
-
103
12
  const parsedArgs = yargs()
104
13
  .options({
105
14
  tsconfig: {
@@ -204,7 +113,7 @@ export const packageBuild = async () => {
204
113
  packageDirectory: CWD,
205
114
  declaredDependencies: new Set(Object.keys(packageJson.dependencies ?? {})),
206
115
  }),
207
- ensureCorrectImportsPlugin,
116
+ declaredImportsPlugin({ packageDirectory: CWD, packageJson }),
208
117
  externalImagesPlugin,
209
118
  globImportPlugin(),
210
119
  nativeNodeModulesPlugin,