@sse-ui/builder 1.2.0 → 1.3.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.
@@ -0,0 +1,286 @@
1
+ import {
2
+ getOutExtension,
3
+ mapConcurrently
4
+ } from "./chunk-N46AJ2OI.js";
5
+
6
+ // src/utils/typescript.ts
7
+ import * as babel from "@babel/core";
8
+ import pluginTypescriptSyntax from "@ssets/babel/plugin/syntax-typescript";
9
+ import pluginResolveImports from "@ssets/babel/plugin/resolve-imports";
10
+ import { findWorkspacesRoot } from "find-workspaces";
11
+ import pluginRemoveImports from "@ssets/babel/plugin/transform-remove-imports";
12
+ import { $ } from "execa";
13
+ import { globby } from "globby";
14
+ import { generateDtsBundle } from "@ssets/dts";
15
+ import * as fs from "fs/promises";
16
+ import * as os from "os";
17
+ import * as path from "path";
18
+ import chalk from "chalk";
19
+ var $$ = $({ stdio: "inherit" });
20
+ async function findTsgo(cwd) {
21
+ const workspaceDir = await findWorkspacesRoot(cwd);
22
+ if (!workspaceDir) {
23
+ return null;
24
+ }
25
+ const tsgoPath = path.join(
26
+ workspaceDir.location,
27
+ "node_modules",
28
+ ".bin",
29
+ "tsgo"
30
+ );
31
+ const exists = await fs.stat(tsgoPath).then(
32
+ (stat2) => stat2.isFile(),
33
+ () => false
34
+ );
35
+ return exists ? tsgoPath : null;
36
+ }
37
+ async function emitDeclarations(tsconfig, outDir, options = {}) {
38
+ const { useTsgo = false } = options ?? {};
39
+ const tsconfigDir = path.dirname(tsconfig);
40
+ const rootDir = path.resolve(tsconfigDir, "./src");
41
+ const tsgoPath = useTsgo ? await findTsgo(tsconfigDir) : null;
42
+ if (useTsgo && !tsgoPath) {
43
+ throw new Error(
44
+ '--tsgo flag was passed or SSE_USE_TSGO environment was set but no tsgo cli was found. Either remove the flag to use tsc or install the native package "@typescript/native-preview" at the workspace level to use tsgo.'
45
+ );
46
+ }
47
+ if (tsgoPath) {
48
+ console.log(chalk.cyan("Using tsgo for declaration emit"));
49
+ await $$`${tsgoPath}
50
+ -p ${tsconfig}
51
+ --rootDir ${rootDir}
52
+ --outDir ${outDir}
53
+ --declaration
54
+ --emitDeclarationOnly
55
+ --noEmit false
56
+ --composite false
57
+ --incremental false
58
+ --declarationMap false`;
59
+ } else {
60
+ await $$`tsc
61
+ -p ${tsconfig}
62
+ --rootDir ${rootDir}
63
+ --outDir ${outDir}
64
+ --declaration
65
+ --emitDeclarationOnly
66
+ --noEmit false
67
+ --composite false
68
+ --incremental false
69
+ --declarationMap false`;
70
+ }
71
+ }
72
+ async function copyDeclarations(sourceDirectory, destinationDirectory, options = {}) {
73
+ const fullSourceDirectory = path.resolve(sourceDirectory);
74
+ const fullDestinationDirectory = path.resolve(destinationDirectory);
75
+ if (options.verbose) {
76
+ console.log(
77
+ chalk.gray(
78
+ `Copying declarations from ${fullSourceDirectory} to ${fullDestinationDirectory}`
79
+ )
80
+ );
81
+ }
82
+ await fs.cp(fullSourceDirectory, fullDestinationDirectory, {
83
+ recursive: true,
84
+ filter: async (src) => {
85
+ if (path.basename(src).startsWith(".")) {
86
+ return false;
87
+ }
88
+ const stats = await fs.stat(src);
89
+ if (stats.isDirectory()) {
90
+ return true;
91
+ }
92
+ return src.endsWith(".d.ts") || src.endsWith(".d.mts") || src.endsWith(".d.cts");
93
+ }
94
+ });
95
+ }
96
+ async function moveAndTransformDeclarations({
97
+ inputDir,
98
+ buildDir,
99
+ bundles,
100
+ isFlat,
101
+ packageType
102
+ }) {
103
+ const toCopyDir = bundles.length === 1 ? path.join(buildDir, bundles[0].dir) : buildDir;
104
+ await fs.cp(inputDir, toCopyDir, {
105
+ recursive: true,
106
+ force: false
107
+ });
108
+ const dtsFiles = await globby("**/*.d.ts", {
109
+ absolute: true,
110
+ cwd: toCopyDir
111
+ });
112
+ if (dtsFiles.length === 0) {
113
+ console.log(
114
+ chalk.yellow(
115
+ `No d.ts files found in ${toCopyDir}. Skipping transformation.`
116
+ )
117
+ );
118
+ return;
119
+ }
120
+ await mapConcurrently(
121
+ dtsFiles,
122
+ async (dtsFile) => {
123
+ const nativeDtsFile = path.normalize(dtsFile);
124
+ const content = await fs.readFile(nativeDtsFile, "utf8");
125
+ const relativePath = path.relative(toCopyDir, nativeDtsFile);
126
+ const writesToOriginalPath = isFlat && bundles.some((bundle) => {
127
+ const newFileExtension = getOutExtension(bundle.type, {
128
+ isFlat,
129
+ isType: true,
130
+ packageType
131
+ });
132
+ const outFileRelative = relativePath.replace(
133
+ /\.d\.ts$/,
134
+ newFileExtension
135
+ );
136
+ const outFilePath = path.join(buildDir, bundle.dir, outFileRelative);
137
+ return path.resolve(outFilePath) === path.resolve(nativeDtsFile);
138
+ });
139
+ await Promise.all(
140
+ bundles.map(async (bundle) => {
141
+ const importExtension = getOutExtension(bundle.type, {
142
+ isFlat,
143
+ packageType
144
+ });
145
+ const newFileExtension = getOutExtension(bundle.type, {
146
+ isFlat,
147
+ isType: true,
148
+ packageType
149
+ });
150
+ const outFileRelative = isFlat ? relativePath.replace(/\.d\.ts$/, newFileExtension) : relativePath;
151
+ const outFilePath = path.join(buildDir, bundle.dir, outFileRelative);
152
+ const babelPlugins = [
153
+ [pluginTypescriptSyntax, { dts: true }],
154
+ [pluginResolveImports, { outExtension: importExtension }],
155
+ [pluginRemoveImports, { test: /\.css$/ }]
156
+ ];
157
+ const result = await babel.transformAsync(content, {
158
+ configFile: false,
159
+ plugins: babelPlugins,
160
+ filename: nativeDtsFile
161
+ });
162
+ if (typeof result?.code === "string") {
163
+ await fs.mkdir(path.dirname(outFilePath), { recursive: true });
164
+ await fs.writeFile(outFilePath, result.code);
165
+ } else {
166
+ console.error(
167
+ chalk.red("failed to transform"),
168
+ chalk.gray(dtsFile)
169
+ );
170
+ }
171
+ })
172
+ );
173
+ if (isFlat && !writesToOriginalPath) {
174
+ await fs.unlink(nativeDtsFile);
175
+ }
176
+ },
177
+ 30
178
+ );
179
+ }
180
+ async function createTypes({
181
+ bundles,
182
+ srcDir,
183
+ buildDir,
184
+ cwd,
185
+ skipTsc,
186
+ useTsgo = false,
187
+ isFlat = false,
188
+ packageType,
189
+ verbose,
190
+ builder,
191
+ entryPoints
192
+ }) {
193
+ if (builder === "esbuild" && entryPoints && !skipTsc) {
194
+ if (verbose) console.log(chalk.blue("\u{1F4E6} Bundling TypeScript declarations"));
195
+ const tmpDir2 = await fs.mkdtemp(path.join(os.tmpdir(), "sse-dts-bundle-"));
196
+ try {
197
+ const tsconfigPath = path.join(cwd, "tsconfig.build.json");
198
+ await emitDeclarations(tsconfigPath, tmpDir2, { useTsgo });
199
+ await Promise.all(
200
+ entryPoints.map(async (entry) => {
201
+ const absoluteEntryPath = path.resolve(cwd, entry);
202
+ const entryName = path.basename(entry, path.extname(entry));
203
+ const [bundledTypes] = generateDtsBundle(
204
+ [
205
+ {
206
+ filePath: absoluteEntryPath,
207
+ output: {
208
+ noBanner: false,
209
+ inlineDeclareGlobals: true
210
+ }
211
+ }
212
+ ],
213
+ {
214
+ preferredConfigPath: tsconfigPath
215
+ }
216
+ );
217
+ await Promise.all(
218
+ bundles.map(async (bundleItem) => {
219
+ const outExt = getOutExtension(bundleItem.type, {
220
+ isFlat,
221
+ isType: true,
222
+ packageType
223
+ });
224
+ const outFilePath = path.join(
225
+ buildDir,
226
+ bundleItem.dir,
227
+ `${entryName}${outExt}`
228
+ );
229
+ await fs.mkdir(path.dirname(outFilePath), { recursive: true });
230
+ await fs.writeFile(outFilePath, bundledTypes, "utf-8");
231
+ if (verbose) {
232
+ console.log(
233
+ chalk.green(
234
+ `\u2705 Generated bundled types for ${bundleItem.type}: ${outFilePath}`
235
+ )
236
+ );
237
+ }
238
+ })
239
+ );
240
+ })
241
+ );
242
+ } catch (err) {
243
+ console.error(
244
+ chalk.red(`\u274C Failed to bundle types for ${entryPoints.join(", ")}`)
245
+ );
246
+ console.error(chalk.red(err.message));
247
+ }
248
+ return;
249
+ }
250
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "sse-build-tsc-"));
251
+ try {
252
+ await copyDeclarations(srcDir, tmpDir, { verbose });
253
+ const tsconfigPath = path.join(cwd, "tsconfig.build.json");
254
+ const tsconfigExists = await fs.stat(tsconfigPath).then(
255
+ (file) => file.isFile(),
256
+ () => false
257
+ );
258
+ if (!skipTsc) {
259
+ if (!tsconfigExists) {
260
+ throw new Error(
261
+ `Unable to find a tsconfig to build this project. The package root needs to contain a 'tsconfig.build.json'. The package root is '${cwd}'`
262
+ );
263
+ }
264
+ if (verbose)
265
+ console.log(
266
+ chalk.cyan(`Building types for ${tsconfigPath} in ${tmpDir}`)
267
+ );
268
+ await emitDeclarations(tsconfigPath, tmpDir, { useTsgo });
269
+ }
270
+ await moveAndTransformDeclarations({
271
+ inputDir: tmpDir,
272
+ buildDir,
273
+ bundles,
274
+ isFlat,
275
+ packageType
276
+ });
277
+ } finally {
278
+ await fs.rm(tmpDir, { recursive: true, force: true });
279
+ }
280
+ }
281
+ export {
282
+ copyDeclarations,
283
+ createTypes,
284
+ emitDeclarations,
285
+ moveAndTransformDeclarations
286
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sse-ui/builder",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "Builder By SSE",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -30,8 +30,6 @@
30
30
  "@types/node": "^25.4.0",
31
31
  "@types/resolve": "^1.20.6",
32
32
  "@types/semver": "^7.7.1",
33
- "prettier": "^3.8.1",
34
- "tsup": "^8.5.1",
35
33
  "typescript": "^5.9.3"
36
34
  },
37
35
  "dependencies": {
@@ -40,6 +38,7 @@
40
38
  "@babel/preset-react": "^7.28.5",
41
39
  "@babel/preset-typescript": "^7.28.5",
42
40
  "@ssets/babel": "*",
41
+ "@ssets/dts": "*",
43
42
  "babel-plugin-optimize-clsx": "^2.6.2",
44
43
  "babel-plugin-react-compiler": "^1.0.0",
45
44
  "babel-plugin-transform-import-meta": "^2.3.3",