prebundle 1.3.4 → 1.4.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.
- package/README.md +16 -0
- package/dist/index.js +64 -61
- 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,27 +169,27 @@ 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;
|
|
@@ -196,29 +197,29 @@ const getTypes = (json)=>{
|
|
|
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
202
|
if (task.ignoreDts) return void outputDefaultDts();
|
|
202
203
|
if (task.copyDts) {
|
|
203
|
-
const dtsFiles =
|
|
204
|
+
const dtsFiles = fast_glob.sync('**/*.d.ts', {
|
|
204
205
|
cwd: task.depPath,
|
|
205
206
|
absolute: false
|
|
206
207
|
});
|
|
207
|
-
for (const dtsFile of dtsFiles)
|
|
208
|
+
for (const dtsFile of dtsFiles)fs_extra.copySync(join(task.depPath, dtsFile), join(task.distPath, dtsFile));
|
|
208
209
|
return;
|
|
209
210
|
}
|
|
210
211
|
const getInput = ()=>{
|
|
211
|
-
const pkgPath =
|
|
212
|
-
const pkgJson =
|
|
212
|
+
const pkgPath = join(task.depPath, 'package.json');
|
|
213
|
+
const pkgJson = fs_extra.readJsonSync(pkgPath, 'utf-8');
|
|
213
214
|
const types = getTypes(pkgJson);
|
|
214
|
-
if (types) return
|
|
215
|
+
if (types) return join(task.depPath, types);
|
|
215
216
|
const directTypeFile = findDirectTypeFile(task.depEntry);
|
|
216
217
|
if (directTypeFile) return directTypeFile;
|
|
217
218
|
const depTypesPath = findDepPath(`${pkgNameToAtTypes(task.depName)}/package.json`);
|
|
218
219
|
if (!depTypesPath) return null;
|
|
219
|
-
const depTypesPkg =
|
|
220
|
+
const depTypesPkg = fs_extra.readJsonSync(depTypesPath, 'utf-8');
|
|
220
221
|
const depTypes = getTypes(depTypesPkg);
|
|
221
|
-
return depTypes ?
|
|
222
|
+
return depTypes ? join(dirname(depTypesPath), depTypes) : null;
|
|
222
223
|
};
|
|
223
224
|
const input = getInput();
|
|
224
225
|
if (!input) return void outputDefaultDts();
|
|
@@ -231,7 +232,7 @@ async function emitDts(task, externals) {
|
|
|
231
232
|
...NODE_BUILTINS
|
|
232
233
|
],
|
|
233
234
|
plugins: [
|
|
234
|
-
|
|
235
|
+
dts({
|
|
235
236
|
respectExternal: true,
|
|
236
237
|
compilerOptions: {
|
|
237
238
|
skipLibCheck: true,
|
|
@@ -254,7 +255,7 @@ async function emitDts(task, externals) {
|
|
|
254
255
|
exports: 'named',
|
|
255
256
|
entryFileNames: 'index.d.ts'
|
|
256
257
|
};
|
|
257
|
-
const bundle = await
|
|
258
|
+
const bundle = await rollup(inputConfig);
|
|
258
259
|
await bundle.write(outputConfig);
|
|
259
260
|
} catch (error) {
|
|
260
261
|
logger.error(`rollup-plugin-dts failed: ${task.depName}`);
|
|
@@ -262,9 +263,9 @@ async function emitDts(task, externals) {
|
|
|
262
263
|
}
|
|
263
264
|
}
|
|
264
265
|
function emitPackageJson(task, assets) {
|
|
265
|
-
const packageJsonPath =
|
|
266
|
-
const packageJson =
|
|
267
|
-
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');
|
|
268
269
|
const pickedPackageJson = pick(packageJson, [
|
|
269
270
|
'name',
|
|
270
271
|
'author',
|
|
@@ -281,26 +282,26 @@ function emitPackageJson(task, assets) {
|
|
|
281
282
|
'type'
|
|
282
283
|
]));
|
|
283
284
|
} catch {}
|
|
284
|
-
|
|
285
|
+
fs_extra.writeJSONSync(outputPath, pickedPackageJson);
|
|
285
286
|
}
|
|
286
287
|
function emitLicense(task) {
|
|
287
|
-
const licensePath =
|
|
288
|
-
if (
|
|
288
|
+
const licensePath = join(task.depPath, 'LICENSE');
|
|
289
|
+
if (fs_extra.existsSync(licensePath)) fs_extra.copySync(licensePath, join(task.distPath, 'license'));
|
|
289
290
|
}
|
|
290
291
|
function emitExtraFiles(task) {
|
|
291
292
|
const { emitFiles } = task;
|
|
292
293
|
for (const item of emitFiles){
|
|
293
|
-
const path =
|
|
294
|
-
|
|
294
|
+
const path = join(task.distPath, item.path);
|
|
295
|
+
fs_extra.outputFileSync(path, item.content);
|
|
295
296
|
}
|
|
296
297
|
}
|
|
297
298
|
function removeSourceMap(task) {
|
|
298
|
-
const maps =
|
|
299
|
-
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);
|
|
300
301
|
}
|
|
301
302
|
function renameDistFolder(task) {
|
|
302
|
-
const pkgPath =
|
|
303
|
-
const pkgJson =
|
|
303
|
+
const pkgPath = join(task.distPath, 'package.json');
|
|
304
|
+
const pkgJson = fs_extra.readJsonSync(pkgPath, 'utf-8');
|
|
304
305
|
for (const key of [
|
|
305
306
|
'types',
|
|
306
307
|
'typing',
|
|
@@ -309,36 +310,38 @@ function renameDistFolder(task) {
|
|
|
309
310
|
var _pkgJson_key;
|
|
310
311
|
if (null == (_pkgJson_key = pkgJson[key]) ? void 0 : _pkgJson_key.startsWith('dist/')) {
|
|
311
312
|
pkgJson[key] = pkgJson[key].replace('dist/', 'types/');
|
|
312
|
-
const distFolder =
|
|
313
|
-
const typesFolder =
|
|
314
|
-
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);
|
|
315
316
|
}
|
|
316
317
|
}
|
|
317
|
-
|
|
318
|
+
fs_extra.writeJSONSync(pkgPath, pkgJson);
|
|
318
319
|
}
|
|
319
320
|
const pkgName = process.argv[2];
|
|
320
321
|
async function prebundle(task, commonExternals = {}) {
|
|
321
322
|
if (pkgName && task.depName !== pkgName) return;
|
|
322
323
|
logger.start(`prebundle: ${task.depName}`);
|
|
323
|
-
|
|
324
|
+
fs_extra.removeSync(task.distPath);
|
|
324
325
|
if (task.beforeBundle) await task.beforeBundle(task);
|
|
325
326
|
const mergedExternals = {
|
|
326
327
|
...DEFAULT_EXTERNALS,
|
|
327
328
|
...commonExternals,
|
|
328
329
|
...task.externals
|
|
329
330
|
};
|
|
330
|
-
const nodeModulesPath =
|
|
331
|
-
const hasNodeModules =
|
|
331
|
+
const nodeModulesPath = join(process.cwd(), 'node_modules');
|
|
332
|
+
const hasNodeModules = existsSync(nodeModulesPath);
|
|
332
333
|
const enableCache = !process.env.CI && hasNodeModules;
|
|
333
|
-
const { code, assets } = await (
|
|
334
|
+
const { code, assets } = await ncc(task.depEntry, {
|
|
334
335
|
minify: task.minify,
|
|
335
336
|
target: task.target,
|
|
336
337
|
externals: mergedExternals,
|
|
337
338
|
assetBuilds: false,
|
|
338
|
-
cache: enableCache ?
|
|
339
|
+
cache: enableCache ? join(nodeModulesPath, '.cache', 'ncc-cache') : false
|
|
339
340
|
});
|
|
340
|
-
|
|
341
|
-
|
|
341
|
+
if (!task.dtsOnly) {
|
|
342
|
+
await emitIndex(code, task.distPath, task.prettier && !task.minify);
|
|
343
|
+
emitAssets(assets, task.distPath);
|
|
344
|
+
}
|
|
342
345
|
await emitDts(task, mergedExternals);
|
|
343
346
|
emitLicense(task);
|
|
344
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.1",
|
|
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.46.2",
|
|
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.11.0",
|
|
34
34
|
"@types/fs-extra": "^11.0.4",
|
|
35
|
-
"@types/node": "22.
|
|
35
|
+
"@types/node": "22.17.0",
|
|
36
36
|
"fast-glob": "^3.3.3",
|
|
37
37
|
"fs-extra": "^11.3.0",
|
|
38
|
-
"rslog": "^1.2.
|
|
39
|
-
"typescript": "^5.
|
|
38
|
+
"rslog": "^1.2.9",
|
|
39
|
+
"typescript": "^5.9.2"
|
|
40
40
|
},
|
|
41
|
-
"packageManager": "pnpm@10.
|
|
41
|
+
"packageManager": "pnpm@10.14.0",
|
|
42
42
|
"publishConfig": {
|
|
43
43
|
"access": "public",
|
|
44
44
|
"registry": "https://registry.npmjs.org/",
|