prebundle 1.3.3 → 1.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.
- package/README.md +16 -0
- package/dist/index.js +68 -71
- package/dist/types.d.ts +3 -0
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -213,6 +213,22 @@ export default {
|
|
|
213
213
|
};
|
|
214
214
|
```
|
|
215
215
|
|
|
216
|
+
### dtsOnly
|
|
217
|
+
|
|
218
|
+
Only bundle the dts files, default `false`.
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
// prebundle.config.mjs
|
|
222
|
+
export default {
|
|
223
|
+
dependencies: [
|
|
224
|
+
{
|
|
225
|
+
name: 'foo',
|
|
226
|
+
dtsOnly: 'true',
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
};
|
|
230
|
+
```
|
|
231
|
+
|
|
216
232
|
### prettier
|
|
217
233
|
|
|
218
234
|
Whether to prettier the code and strip comments, default `false`.
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import
|
|
1
|
+
import { dirname, extname, join } from "node:path";
|
|
2
|
+
import fs_extra from "../compiled/fs-extra/index.js";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import ncc from "@vercel/ncc";
|
|
6
|
+
import fast_glob from "../compiled/fast-glob/index.js";
|
|
7
|
+
import rslog from "../compiled/rslog/index.js";
|
|
8
|
+
import { dts } from "rollup-plugin-dts";
|
|
9
|
+
import { rollup } from "rollup";
|
|
10
|
+
import { minify } from "terser";
|
|
11
|
+
import { format } from "prettier";
|
|
12
|
+
import { existsSync } from "node:fs";
|
|
13
13
|
const DIST_DIR = 'compiled';
|
|
14
14
|
const DEFAULT_EXTERNALS = {
|
|
15
15
|
'./package.json': './package.json',
|
|
@@ -57,32 +57,32 @@ const NODE_BUILTINS = [
|
|
|
57
57
|
'zlib'
|
|
58
58
|
];
|
|
59
59
|
const cwd = process.cwd();
|
|
60
|
-
const helper_require =
|
|
60
|
+
const helper_require = createRequire(import.meta.url);
|
|
61
61
|
function findDepPath(name) {
|
|
62
62
|
try {
|
|
63
|
-
let entry =
|
|
63
|
+
let entry = dirname(helper_require.resolve(join(name), {
|
|
64
64
|
paths: [
|
|
65
65
|
cwd
|
|
66
66
|
]
|
|
67
67
|
}));
|
|
68
|
-
while(!
|
|
69
|
-
if (name.includes('/')) return
|
|
68
|
+
while(!dirname(entry).endsWith('node_modules'))entry = dirname(entry);
|
|
69
|
+
if (name.includes('/')) return join(dirname(entry), name);
|
|
70
70
|
return entry;
|
|
71
71
|
} catch (err) {
|
|
72
72
|
return null;
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
const resolveConfig = async ()=>{
|
|
76
|
-
const configPath =
|
|
77
|
-
const config = await import(
|
|
76
|
+
const configPath = join(cwd, 'prebundle.config.mjs');
|
|
77
|
+
const config = await import(pathToFileURL(configPath).href);
|
|
78
78
|
return config.default;
|
|
79
79
|
};
|
|
80
80
|
function parseTasks(dependencies, globalPrettier) {
|
|
81
81
|
const result = [];
|
|
82
82
|
for (const dep of dependencies){
|
|
83
83
|
const depName = 'string' == typeof dep ? dep : dep.name;
|
|
84
|
-
const importPath =
|
|
85
|
-
const distPath =
|
|
84
|
+
const importPath = join(cwd, DIST_DIR, depName);
|
|
85
|
+
const distPath = join(cwd, DIST_DIR, depName);
|
|
86
86
|
const depPath = findDepPath(depName);
|
|
87
87
|
if (!depPath) throw new Error(`Failed to resolve dependency: ${depName}`);
|
|
88
88
|
const depEntry = helper_require.resolve(depName, {
|
|
@@ -113,6 +113,7 @@ function parseTasks(dependencies, globalPrettier) {
|
|
|
113
113
|
ignoreDts: dep.ignoreDts,
|
|
114
114
|
copyDts: dep.copyDts,
|
|
115
115
|
externals: dep.externals ?? {},
|
|
116
|
+
dtsOnly: dep.dtsOnly ?? false,
|
|
116
117
|
dtsExternals: dep.dtsExternals ?? [],
|
|
117
118
|
emitFiles: dep.emitFiles ?? [],
|
|
118
119
|
prettier: dep.prettier ?? globalPrettier,
|
|
@@ -136,7 +137,7 @@ function pkgNameToAtTypes(name) {
|
|
|
136
137
|
}
|
|
137
138
|
function findDirectTypeFile(filepath) {
|
|
138
139
|
if (/\.d\.[cm]?ts/.test(filepath)) return filepath;
|
|
139
|
-
const ext =
|
|
140
|
+
const ext = extname(filepath);
|
|
140
141
|
const base = filepath.slice(0, -ext.length);
|
|
141
142
|
const _find = (list)=>{
|
|
142
143
|
for (const f of list)try {
|
|
@@ -168,66 +169,60 @@ function findDirectTypeFile(filepath) {
|
|
|
168
169
|
default:
|
|
169
170
|
}
|
|
170
171
|
}
|
|
171
|
-
const { logger } =
|
|
172
|
+
const { logger } = rslog;
|
|
172
173
|
function emitAssets(assets, distPath) {
|
|
173
174
|
for (const key of Object.keys(assets)){
|
|
174
175
|
const asset = assets[key];
|
|
175
|
-
|
|
176
|
+
fs_extra.outputFileSync(join(distPath, key), asset.source);
|
|
176
177
|
}
|
|
177
178
|
}
|
|
178
179
|
async function emitIndex(code, distPath, prettier) {
|
|
179
|
-
const distIndex =
|
|
180
|
+
const distIndex = join(distPath, 'index.js');
|
|
180
181
|
if (prettier) {
|
|
181
|
-
const minimized = await
|
|
182
|
+
const minimized = await minify(code, {
|
|
182
183
|
compress: false,
|
|
183
184
|
mangle: false,
|
|
184
185
|
ecma: 2019
|
|
185
186
|
});
|
|
186
187
|
if (!minimized.code) throw new Error('terser minify failed');
|
|
187
|
-
const formatted = await
|
|
188
|
+
const formatted = await format(minimized.code, {
|
|
188
189
|
filepath: distIndex
|
|
189
190
|
});
|
|
190
|
-
await
|
|
191
|
-
} else await
|
|
191
|
+
await fs_extra.outputFile(distIndex, formatted);
|
|
192
|
+
} else await fs_extra.outputFile(distIndex, code);
|
|
192
193
|
}
|
|
193
194
|
const getTypes = (json)=>{
|
|
194
195
|
var _json_exports;
|
|
195
|
-
return json && (json.types || json.typing || json.typings || getTypes(null
|
|
196
|
+
return json && (json.types || json.typing || json.typings || getTypes(null == (_json_exports = json.exports) ? void 0 : _json_exports['.'])) || null;
|
|
196
197
|
};
|
|
197
198
|
async function emitDts(task, externals) {
|
|
198
199
|
const outputDefaultDts = ()=>{
|
|
199
|
-
|
|
200
|
+
fs_extra.outputFileSync(join(task.distPath, 'index.d.ts'), 'export = any;\n');
|
|
200
201
|
};
|
|
201
|
-
if (task.ignoreDts)
|
|
202
|
-
outputDefaultDts();
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
202
|
+
if (task.ignoreDts) return void outputDefaultDts();
|
|
205
203
|
if (task.copyDts) {
|
|
206
|
-
const dtsFiles =
|
|
204
|
+
const dtsFiles = fast_glob.sync('**/*.d.ts', {
|
|
207
205
|
cwd: task.depPath,
|
|
208
206
|
absolute: false
|
|
209
207
|
});
|
|
210
|
-
for (const dtsFile of dtsFiles)
|
|
208
|
+
for (const dtsFile of dtsFiles)fs_extra.copySync(join(task.depPath, dtsFile), join(task.distPath, dtsFile));
|
|
211
209
|
return;
|
|
212
210
|
}
|
|
213
211
|
const getInput = ()=>{
|
|
214
|
-
const pkgPath =
|
|
215
|
-
const pkgJson =
|
|
212
|
+
const pkgPath = join(task.depPath, 'package.json');
|
|
213
|
+
const pkgJson = fs_extra.readJsonSync(pkgPath, 'utf-8');
|
|
216
214
|
const types = getTypes(pkgJson);
|
|
217
|
-
if (types) return
|
|
215
|
+
if (types) return join(task.depPath, types);
|
|
218
216
|
const directTypeFile = findDirectTypeFile(task.depEntry);
|
|
219
217
|
if (directTypeFile) return directTypeFile;
|
|
220
218
|
const depTypesPath = findDepPath(`${pkgNameToAtTypes(task.depName)}/package.json`);
|
|
221
219
|
if (!depTypesPath) return null;
|
|
222
|
-
const depTypesPkg =
|
|
220
|
+
const depTypesPkg = fs_extra.readJsonSync(depTypesPath, 'utf-8');
|
|
223
221
|
const depTypes = getTypes(depTypesPkg);
|
|
224
|
-
return depTypes ?
|
|
222
|
+
return depTypes ? join(dirname(depTypesPath), depTypes) : null;
|
|
225
223
|
};
|
|
226
224
|
const input = getInput();
|
|
227
|
-
if (!input)
|
|
228
|
-
outputDefaultDts();
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
225
|
+
if (!input) return void outputDefaultDts();
|
|
231
226
|
try {
|
|
232
227
|
const inputConfig = {
|
|
233
228
|
input,
|
|
@@ -237,7 +232,7 @@ async function emitDts(task, externals) {
|
|
|
237
232
|
...NODE_BUILTINS
|
|
238
233
|
],
|
|
239
234
|
plugins: [
|
|
240
|
-
|
|
235
|
+
dts({
|
|
241
236
|
respectExternal: true,
|
|
242
237
|
compilerOptions: {
|
|
243
238
|
skipLibCheck: true,
|
|
@@ -260,7 +255,7 @@ async function emitDts(task, externals) {
|
|
|
260
255
|
exports: 'named',
|
|
261
256
|
entryFileNames: 'index.d.ts'
|
|
262
257
|
};
|
|
263
|
-
const bundle = await
|
|
258
|
+
const bundle = await rollup(inputConfig);
|
|
264
259
|
await bundle.write(outputConfig);
|
|
265
260
|
} catch (error) {
|
|
266
261
|
logger.error(`rollup-plugin-dts failed: ${task.depName}`);
|
|
@@ -268,9 +263,9 @@ async function emitDts(task, externals) {
|
|
|
268
263
|
}
|
|
269
264
|
}
|
|
270
265
|
function emitPackageJson(task, assets) {
|
|
271
|
-
const packageJsonPath =
|
|
272
|
-
const packageJson =
|
|
273
|
-
const outputPath =
|
|
266
|
+
const packageJsonPath = join(task.depPath, 'package.json');
|
|
267
|
+
const packageJson = fs_extra.readJsonSync(packageJsonPath, 'utf-8');
|
|
268
|
+
const outputPath = join(task.distPath, 'package.json');
|
|
274
269
|
const pickedPackageJson = pick(packageJson, [
|
|
275
270
|
'name',
|
|
276
271
|
'author',
|
|
@@ -287,64 +282,66 @@ function emitPackageJson(task, assets) {
|
|
|
287
282
|
'type'
|
|
288
283
|
]));
|
|
289
284
|
} catch {}
|
|
290
|
-
|
|
285
|
+
fs_extra.writeJSONSync(outputPath, pickedPackageJson);
|
|
291
286
|
}
|
|
292
287
|
function emitLicense(task) {
|
|
293
|
-
const licensePath =
|
|
294
|
-
if (
|
|
288
|
+
const licensePath = join(task.depPath, 'LICENSE');
|
|
289
|
+
if (fs_extra.existsSync(licensePath)) fs_extra.copySync(licensePath, join(task.distPath, 'license'));
|
|
295
290
|
}
|
|
296
291
|
function emitExtraFiles(task) {
|
|
297
292
|
const { emitFiles } = task;
|
|
298
293
|
for (const item of emitFiles){
|
|
299
|
-
const path =
|
|
300
|
-
|
|
294
|
+
const path = join(task.distPath, item.path);
|
|
295
|
+
fs_extra.outputFileSync(path, item.content);
|
|
301
296
|
}
|
|
302
297
|
}
|
|
303
298
|
function removeSourceMap(task) {
|
|
304
|
-
const maps =
|
|
305
|
-
for (const mapPath of maps)
|
|
299
|
+
const maps = fast_glob.sync(join(task.distPath, '**/*.map'));
|
|
300
|
+
for (const mapPath of maps)fs_extra.removeSync(mapPath);
|
|
306
301
|
}
|
|
307
302
|
function renameDistFolder(task) {
|
|
308
|
-
const pkgPath =
|
|
309
|
-
const pkgJson =
|
|
303
|
+
const pkgPath = join(task.distPath, 'package.json');
|
|
304
|
+
const pkgJson = fs_extra.readJsonSync(pkgPath, 'utf-8');
|
|
310
305
|
for (const key of [
|
|
311
306
|
'types',
|
|
312
307
|
'typing',
|
|
313
308
|
'typings'
|
|
314
309
|
]){
|
|
315
310
|
var _pkgJson_key;
|
|
316
|
-
if (null
|
|
311
|
+
if (null == (_pkgJson_key = pkgJson[key]) ? void 0 : _pkgJson_key.startsWith('dist/')) {
|
|
317
312
|
pkgJson[key] = pkgJson[key].replace('dist/', 'types/');
|
|
318
|
-
const distFolder =
|
|
319
|
-
const typesFolder =
|
|
320
|
-
if (
|
|
313
|
+
const distFolder = join(task.distPath, 'dist');
|
|
314
|
+
const typesFolder = join(task.distPath, 'types');
|
|
315
|
+
if (fs_extra.existsSync(distFolder)) fs_extra.renameSync(distFolder, typesFolder);
|
|
321
316
|
}
|
|
322
317
|
}
|
|
323
|
-
|
|
318
|
+
fs_extra.writeJSONSync(pkgPath, pkgJson);
|
|
324
319
|
}
|
|
325
320
|
const pkgName = process.argv[2];
|
|
326
321
|
async function prebundle(task, commonExternals = {}) {
|
|
327
322
|
if (pkgName && task.depName !== pkgName) return;
|
|
328
323
|
logger.start(`prebundle: ${task.depName}`);
|
|
329
|
-
|
|
324
|
+
fs_extra.removeSync(task.distPath);
|
|
330
325
|
if (task.beforeBundle) await task.beforeBundle(task);
|
|
331
326
|
const mergedExternals = {
|
|
332
327
|
...DEFAULT_EXTERNALS,
|
|
333
328
|
...commonExternals,
|
|
334
329
|
...task.externals
|
|
335
330
|
};
|
|
336
|
-
const nodeModulesPath =
|
|
337
|
-
const hasNodeModules =
|
|
331
|
+
const nodeModulesPath = join(process.cwd(), 'node_modules');
|
|
332
|
+
const hasNodeModules = existsSync(nodeModulesPath);
|
|
338
333
|
const enableCache = !process.env.CI && hasNodeModules;
|
|
339
|
-
const { code, assets } = await (
|
|
334
|
+
const { code, assets } = await ncc(task.depEntry, {
|
|
340
335
|
minify: task.minify,
|
|
341
336
|
target: task.target,
|
|
342
337
|
externals: mergedExternals,
|
|
343
338
|
assetBuilds: false,
|
|
344
|
-
cache: enableCache ?
|
|
339
|
+
cache: enableCache ? join(nodeModulesPath, '.cache', 'ncc-cache') : false
|
|
345
340
|
});
|
|
346
|
-
|
|
347
|
-
|
|
341
|
+
if (!task.dtsOnly) {
|
|
342
|
+
await emitIndex(code, task.distPath, task.prettier && !task.minify);
|
|
343
|
+
emitAssets(assets, task.distPath);
|
|
344
|
+
}
|
|
348
345
|
await emitDts(task, mergedExternals);
|
|
349
346
|
emitLicense(task);
|
|
350
347
|
emitPackageJson(task, assets);
|
package/dist/types.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ export type DependencyConfig = {
|
|
|
9
9
|
minify?: boolean;
|
|
10
10
|
/** Externals to leave as requires of the build. */
|
|
11
11
|
externals?: Record<string, string>;
|
|
12
|
+
/** Only bundle the dts files */
|
|
13
|
+
dtsOnly?: boolean;
|
|
12
14
|
/** Externals types */
|
|
13
15
|
dtsExternals?: Array<string | RegExp>;
|
|
14
16
|
/** Whether to prettier the code and strip comments */
|
|
@@ -42,6 +44,7 @@ export type ParsedTask = {
|
|
|
42
44
|
distPath: string;
|
|
43
45
|
importPath: string;
|
|
44
46
|
ignoreDts?: boolean;
|
|
47
|
+
dtsOnly?: boolean;
|
|
45
48
|
copyDts?: boolean;
|
|
46
49
|
prettier?: boolean;
|
|
47
50
|
target: NonNullable<DependencyConfig['target']>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prebundle",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/rspack-contrib/prebundle"
|
|
@@ -24,21 +24,21 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@vercel/ncc": "0.38.3",
|
|
27
|
-
"prettier": "^3.
|
|
28
|
-
"rollup": "^4.
|
|
27
|
+
"prettier": "^3.6.2",
|
|
28
|
+
"rollup": "^4.44.1",
|
|
29
29
|
"rollup-plugin-dts": "^6.2.1",
|
|
30
|
-
"terser": "^5.
|
|
30
|
+
"terser": "^5.43.1"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@rslib/core": "0.
|
|
33
|
+
"@rslib/core": "0.10.4",
|
|
34
34
|
"@types/fs-extra": "^11.0.4",
|
|
35
|
-
"@types/node": "22.
|
|
35
|
+
"@types/node": "22.15.34",
|
|
36
36
|
"fast-glob": "^3.3.3",
|
|
37
37
|
"fs-extra": "^11.3.0",
|
|
38
|
-
"rslog": "^1.2.
|
|
39
|
-
"typescript": "^5.8.
|
|
38
|
+
"rslog": "^1.2.9",
|
|
39
|
+
"typescript": "^5.8.3"
|
|
40
40
|
},
|
|
41
|
-
"packageManager": "pnpm@10.
|
|
41
|
+
"packageManager": "pnpm@10.12.4",
|
|
42
42
|
"publishConfig": {
|
|
43
43
|
"access": "public",
|
|
44
44
|
"registry": "https://registry.npmjs.org/",
|