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