@k8slens/package-build 2.2.3
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/LICENSE +20 -0
- package/bin/package-build +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5781 -0
- package/dist/src/glob-import-plugin.d.ts +3 -0
- package/dist/src/glob-import-plugin.d.ts.map +1 -0
- package/dist/src/package-build.d.ts +2 -0
- package/dist/src/package-build.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/index.js +2 -0
- package/package.json +93 -0
- package/src/glob-import-plugin.js +46 -0
- package/src/glob-import-plugin.ts +63 -0
- package/src/glob-import-transformer.js +171 -0
- package/src/image-transformer.js +9 -0
- package/src/package-build.js +193 -0
- package/src/package-build.ts +231 -0
- package/src/svg-transformer.js +7 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { type Plugin, build } from "esbuild";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { sassPlugin, postcssModules } from "esbuild-sass-plugin";
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { builtinModules as rawBuiltinModules } from "node:module";
|
|
6
|
+
import yargs from "yargs";
|
|
7
|
+
import { globImportPlugin } from "./glob-import-plugin";
|
|
8
|
+
import tailwindcss from "tailwindcss";
|
|
9
|
+
import { partition } from "lodash";
|
|
10
|
+
|
|
11
|
+
const CWD = process.cwd();
|
|
12
|
+
const packageJson = JSON.parse(readFileSync(path.resolve(CWD, "package.json"), "utf-8"));
|
|
13
|
+
|
|
14
|
+
const isExperimentalModule = (module: string) => module.startsWith("_");
|
|
15
|
+
const alsoAddNodeRefs = (module: string) => [module, `node:${module}`];
|
|
16
|
+
|
|
17
|
+
const { builtinModules, disallowedBuiltinModules } = (() => {
|
|
18
|
+
const [disallowed, allowed] = partition(rawBuiltinModules, isExperimentalModule);
|
|
19
|
+
const disallowedBuiltinModules = new Set(disallowed.flatMap(alsoAddNodeRefs));
|
|
20
|
+
const builtinModules = new Set(allowed.flatMap(alsoAddNodeRefs));
|
|
21
|
+
|
|
22
|
+
return { disallowedBuiltinModules, builtinModules };
|
|
23
|
+
})();
|
|
24
|
+
|
|
25
|
+
const getPackageName = (path: string) => {
|
|
26
|
+
const [firstPart, secondPart] = path.split("/");
|
|
27
|
+
|
|
28
|
+
if (path.startsWith("@")) {
|
|
29
|
+
return [firstPart, secondPart].join("/");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return firstPart;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const bundleDependency = null;
|
|
36
|
+
|
|
37
|
+
const ensureCorrectImportsPlugin: Plugin = {
|
|
38
|
+
name: "ensure-correct-imports",
|
|
39
|
+
setup: (build) => {
|
|
40
|
+
build.onResolve({ filter: /^\./ }, (args) => {
|
|
41
|
+
const fullPath = path.resolve(args.resolveDir, args.path);
|
|
42
|
+
const relativePathToImporter = path.relative(CWD, args.importer);
|
|
43
|
+
const relativePath = path.relative(CWD, fullPath);
|
|
44
|
+
|
|
45
|
+
if (relativePath.startsWith("..") && !relativePathToImporter.startsWith("..")) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Tried to import "${args.path}" from "${relativePathToImporter}" but it is outside of the package.`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return bundleDependency;
|
|
52
|
+
});
|
|
53
|
+
build.onResolve({ filter: /^[^.]/ }, async (args) => {
|
|
54
|
+
// Enforce declared imports only for the package's own sources (.ts/.tsx/.js/.jsx).
|
|
55
|
+
// Anything imported from outside the package directory — node_modules files or
|
|
56
|
+
// other workspace packages' dists (whose realpaths live outside node_modules) —
|
|
57
|
+
// belongs to an already-bundled dependency and must keep bundling freely.
|
|
58
|
+
const relativePathToImporter = path.relative(CWD, args.importer);
|
|
59
|
+
const importerIsOwnFile =
|
|
60
|
+
!relativePathToImporter.startsWith("..") && !relativePathToImporter.split(path.sep).includes("node_modules");
|
|
61
|
+
const importerIsSourceFile = /\.(t|j)sx?$/.test(args.importer);
|
|
62
|
+
|
|
63
|
+
if (!importerIsOwnFile || !importerIsSourceFile) {
|
|
64
|
+
return bundleDependency;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (disallowedBuiltinModules.has(args.path)) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Tried to import "${args.path}" from "${args.importer}" but it is a disallowed builtin module.`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (builtinModules.has(args.path)) {
|
|
74
|
+
return {
|
|
75
|
+
external: true,
|
|
76
|
+
path: args.path,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const packageName = getPackageName(args.path);
|
|
81
|
+
|
|
82
|
+
const isExplicitDependency = packageName in (packageJson.dependencies ?? {});
|
|
83
|
+
const isExplicitDevDependency = packageName in (packageJson.devDependencies ?? {});
|
|
84
|
+
|
|
85
|
+
if (isExplicitDependency) {
|
|
86
|
+
// Note: Return the full path since we assume that it is a "exports" entry of the dependency found in package.json
|
|
87
|
+
return {
|
|
88
|
+
external: true,
|
|
89
|
+
path: args.path,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (isExplicitDevDependency) {
|
|
94
|
+
return bundleDependency;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
throw new Error(
|
|
98
|
+
`Tried to import "${args.path}" from "${relativePathToImporter}" but it is not a dependency of the package.`,
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const parsedArgs = yargs()
|
|
105
|
+
.options({
|
|
106
|
+
tsconfig: {
|
|
107
|
+
type: "string",
|
|
108
|
+
default: "tsconfig.json",
|
|
109
|
+
description: 'Custom tsconfig.json -file, defaults to "tsconfig.json"',
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
"entry-point": {
|
|
113
|
+
type: "string",
|
|
114
|
+
default: "index.ts",
|
|
115
|
+
description: 'Custom entry-point, defaults to "index.ts"',
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
outfile: {
|
|
119
|
+
type: "string",
|
|
120
|
+
default: path.resolve(CWD, "dist/index.js"),
|
|
121
|
+
description: `Custom outfile, defaults to "dist/index.js"`,
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
format: {
|
|
125
|
+
choices: ["cjs", "esm"] as const,
|
|
126
|
+
default: "cjs" as const,
|
|
127
|
+
description: `Transpilation output format, defaults to "cjs"`,
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
.parseSync(process.argv);
|
|
131
|
+
|
|
132
|
+
// Copied from https://github.com/evanw/esbuild/issues/1051
|
|
133
|
+
const nativeNodeModulesPlugin: Plugin = {
|
|
134
|
+
name: "native-node-modules",
|
|
135
|
+
setup(build) {
|
|
136
|
+
// If a ".node" file is imported within a module in the "file" namespace, resolve
|
|
137
|
+
// it to an absolute path and put it into the "node-file" virtual namespace.
|
|
138
|
+
build.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
|
|
139
|
+
path: require.resolve(args.path, { paths: [args.resolveDir] }),
|
|
140
|
+
namespace: "node-file",
|
|
141
|
+
}));
|
|
142
|
+
|
|
143
|
+
// Files in the "node-file" virtual namespace call "require()" on the
|
|
144
|
+
// path from esbuild of the ".node" file in the output directory.
|
|
145
|
+
build.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
|
|
146
|
+
contents: `
|
|
147
|
+
import path from ${JSON.stringify(args.path)}
|
|
148
|
+
try { module.exports = require(path) }
|
|
149
|
+
catch {}
|
|
150
|
+
`,
|
|
151
|
+
}));
|
|
152
|
+
|
|
153
|
+
// If a ".node" file is imported within a module in the "node-file" namespace, put
|
|
154
|
+
// it in the "file" namespace where esbuild's default loading behavior will handle
|
|
155
|
+
// it. It is already an absolute path since we resolved it to one above.
|
|
156
|
+
build.onResolve({ filter: /\.node$/, namespace: "node-file" }, (args) => ({
|
|
157
|
+
path: args.path,
|
|
158
|
+
namespace: "file",
|
|
159
|
+
}));
|
|
160
|
+
|
|
161
|
+
// Tell esbuild's default loading behavior to use the "file" loader for
|
|
162
|
+
// these ".node" files.
|
|
163
|
+
const opts = build.initialOptions;
|
|
164
|
+
opts.loader = opts.loader || {};
|
|
165
|
+
opts.loader[".node"] = "file";
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// Keep .png imports as require() calls in the dist bundle instead of inlining
|
|
170
|
+
// them as base64 data urls, so images ship as files loaded on demand rather
|
|
171
|
+
// than megabytes of string literals in every consuming bundle. The import
|
|
172
|
+
// path is rewritten to stay correct relative to the output file, since the
|
|
173
|
+
// application build resolves it from there with its "file" loader.
|
|
174
|
+
const externalImagesPlugin: Plugin = {
|
|
175
|
+
name: "external-images",
|
|
176
|
+
setup(build) {
|
|
177
|
+
build.onResolve({ filter: /\.png$/ }, (args) => {
|
|
178
|
+
const absolutePath = path.resolve(args.resolveDir, args.path);
|
|
179
|
+
const outputDirectory = path.dirname(path.resolve(CWD, parsedArgs.outfile));
|
|
180
|
+
const relativePath = path.relative(outputDirectory, absolutePath).split(path.sep).join("/");
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
path: relativePath.startsWith(".") ? relativePath : `./${relativePath}`,
|
|
184
|
+
external: true,
|
|
185
|
+
};
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export const packageBuild = async () => {
|
|
191
|
+
try {
|
|
192
|
+
await build({
|
|
193
|
+
entryPoints: [parsedArgs.entryPoint],
|
|
194
|
+
tsconfig: parsedArgs.tsconfig,
|
|
195
|
+
outfile: parsedArgs.outfile,
|
|
196
|
+
format: parsedArgs.format,
|
|
197
|
+
bundle: true,
|
|
198
|
+
platform: "node",
|
|
199
|
+
target: "node16",
|
|
200
|
+
sourcemap: "inline",
|
|
201
|
+
plugins: [
|
|
202
|
+
ensureCorrectImportsPlugin,
|
|
203
|
+
externalImagesPlugin,
|
|
204
|
+
globImportPlugin(),
|
|
205
|
+
sassPlugin({
|
|
206
|
+
filter: /\.module\.scss$/,
|
|
207
|
+
transform: postcssModules(
|
|
208
|
+
{
|
|
209
|
+
basedir: __dirname,
|
|
210
|
+
generateScopedName: "[name]__[local]___[hash:base64:5]",
|
|
211
|
+
},
|
|
212
|
+
[tailwindcss],
|
|
213
|
+
),
|
|
214
|
+
}),
|
|
215
|
+
sassPlugin({
|
|
216
|
+
filter: /\.scss$/,
|
|
217
|
+
}),
|
|
218
|
+
nativeNodeModulesPlugin,
|
|
219
|
+
],
|
|
220
|
+
loader: {
|
|
221
|
+
".ttf": "dataurl",
|
|
222
|
+
".svg": "text",
|
|
223
|
+
".yaml": "text",
|
|
224
|
+
".yml": "text",
|
|
225
|
+
".yml.hb": "text",
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
} catch {
|
|
229
|
+
process.exit(1);
|
|
230
|
+
}
|
|
231
|
+
};
|