prebundle 1.1.0 → 1.2.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 +11 -0
- package/compiled/fast-glob/index.js +5496 -7063
- package/compiled/fs-extra/index.d.ts +1063 -1
- package/compiled/fs-extra/index.js +2114 -2780
- package/compiled/rslog/index.js +375 -404
- package/dist/compiled/fast-glob/index.d.ts +2 -0
- package/dist/compiled/fast-glob/index.js +5332 -0
- package/dist/helper.d.ts +1 -1
- package/dist/helper.js +3 -1
- package/dist/index.js +1 -1
- package/dist/prebundle.js +19 -4
- package/dist/src/constant.d.ts +8 -0
- package/dist/src/constant.js +48 -0
- package/dist/src/helper.d.ts +6 -0
- package/dist/src/helper.js +87 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +9 -0
- package/dist/src/prebundle.d.ts +2 -0
- package/dist/src/prebundle.js +197 -0
- package/dist/src/types.d.ts +50 -0
- package/dist/src/types.js +1 -0
- package/dist/types.d.ts +5 -0
- package/package.json +9 -8
package/dist/helper.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Config, DependencyConfig, ParsedTask } from './types.js';
|
|
2
2
|
export declare function findDepPath(name: string): string | null;
|
|
3
3
|
export declare const resolveConfig: () => Promise<Config>;
|
|
4
|
-
export declare function parseTasks(dependencies: Array<string | DependencyConfig
|
|
4
|
+
export declare function parseTasks(dependencies: Array<string | DependencyConfig>, globalPrettier?: boolean): ParsedTask[];
|
|
5
5
|
export declare function pick<T, U extends keyof T>(obj: T, keys: ReadonlyArray<U>): Pick<T, U>;
|
|
6
6
|
export declare function replaceFileContent(filePath: string, replaceFn: (content: string) => string): void;
|
package/dist/helper.js
CHANGED
|
@@ -24,7 +24,7 @@ export const resolveConfig = async () => {
|
|
|
24
24
|
const config = await import(pathToFileURL(configPath).href);
|
|
25
25
|
return config.default;
|
|
26
26
|
};
|
|
27
|
-
export function parseTasks(dependencies) {
|
|
27
|
+
export function parseTasks(dependencies, globalPrettier) {
|
|
28
28
|
const result = [];
|
|
29
29
|
for (const dep of dependencies) {
|
|
30
30
|
const depName = typeof dep === 'string' ? dep : dep.name;
|
|
@@ -50,6 +50,7 @@ export function parseTasks(dependencies) {
|
|
|
50
50
|
dtsExternals: [],
|
|
51
51
|
emitFiles: [],
|
|
52
52
|
packageJsonField: [],
|
|
53
|
+
prettier: globalPrettier,
|
|
53
54
|
...info,
|
|
54
55
|
});
|
|
55
56
|
}
|
|
@@ -61,6 +62,7 @@ export function parseTasks(dependencies) {
|
|
|
61
62
|
externals: dep.externals ?? {},
|
|
62
63
|
dtsExternals: dep.dtsExternals ?? [],
|
|
63
64
|
emitFiles: dep.emitFiles ?? [],
|
|
65
|
+
prettier: dep.prettier ?? globalPrettier,
|
|
64
66
|
afterBundle: dep.afterBundle,
|
|
65
67
|
beforeBundle: dep.beforeBundle,
|
|
66
68
|
packageJsonField: dep.packageJsonField ?? [],
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { parseTasks, resolveConfig } from './helper.js';
|
|
|
2
2
|
import { prebundle } from './prebundle.js';
|
|
3
3
|
export async function run() {
|
|
4
4
|
const config = await resolveConfig();
|
|
5
|
-
const parsedTasks = parseTasks(config.dependencies);
|
|
5
|
+
const parsedTasks = parseTasks(config.dependencies, config.prettier);
|
|
6
6
|
for (const task of parsedTasks) {
|
|
7
7
|
await prebundle(task, config.externals);
|
|
8
8
|
}
|
package/dist/prebundle.js
CHANGED
|
@@ -7,6 +7,8 @@ import { DEFAULT_EXTERNALS, NODE_BUILTINS } from './constant.js';
|
|
|
7
7
|
import { findDepPath, pick } from './helper.js';
|
|
8
8
|
import { dts } from 'rollup-plugin-dts';
|
|
9
9
|
import { rollup } from 'rollup';
|
|
10
|
+
import swc from '@swc/core';
|
|
11
|
+
import { format } from 'prettier';
|
|
10
12
|
const { logger } = rslog;
|
|
11
13
|
function emitAssets(assets, distPath) {
|
|
12
14
|
for (const key of Object.keys(assets)) {
|
|
@@ -14,13 +16,26 @@ function emitAssets(assets, distPath) {
|
|
|
14
16
|
fs.outputFileSync(join(distPath, key), asset.source);
|
|
15
17
|
}
|
|
16
18
|
}
|
|
17
|
-
function emitIndex(code, distPath) {
|
|
19
|
+
async function emitIndex(code, distPath, prettier) {
|
|
18
20
|
const distIndex = join(distPath, 'index.js');
|
|
19
|
-
|
|
21
|
+
if (prettier) {
|
|
22
|
+
const minimized = await swc.minify(code, {
|
|
23
|
+
compress: false,
|
|
24
|
+
mangle: false,
|
|
25
|
+
ecma: 2019,
|
|
26
|
+
});
|
|
27
|
+
const formatted = await format(minimized.code, {
|
|
28
|
+
filepath: distIndex,
|
|
29
|
+
});
|
|
30
|
+
await fs.outputFile(distIndex, formatted);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
await fs.outputFile(distIndex, code);
|
|
34
|
+
}
|
|
20
35
|
}
|
|
21
36
|
async function emitDts(task, externals) {
|
|
22
37
|
const outputDefaultDts = () => {
|
|
23
|
-
fs.
|
|
38
|
+
fs.outputFileSync(join(task.distPath, 'index.d.ts'), 'export = any;\n');
|
|
24
39
|
};
|
|
25
40
|
if (task.ignoreDts) {
|
|
26
41
|
outputDefaultDts();
|
|
@@ -172,7 +187,7 @@ export async function prebundle(task, commonExternals = {}) {
|
|
|
172
187
|
externals: mergedExternals,
|
|
173
188
|
assetBuilds: false,
|
|
174
189
|
});
|
|
175
|
-
emitIndex(code, task.distPath);
|
|
190
|
+
await emitIndex(code, task.distPath, task.prettier);
|
|
176
191
|
emitAssets(assets, task.distPath);
|
|
177
192
|
await emitDts(task, mergedExternals);
|
|
178
193
|
emitLicense(task);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export const DIST_DIR = 'compiled';
|
|
2
|
+
export const DEFAULT_EXTERNALS = {
|
|
3
|
+
// ncc bundled wrong package.json, using external to avoid this problem
|
|
4
|
+
'./package.json': './package.json',
|
|
5
|
+
'../package.json': './package.json',
|
|
6
|
+
'../../package.json': './package.json',
|
|
7
|
+
};
|
|
8
|
+
export const NODE_BUILTINS = [
|
|
9
|
+
'_stream_duplex',
|
|
10
|
+
'_stream_passthrough',
|
|
11
|
+
'_stream_readable',
|
|
12
|
+
'_stream_transform',
|
|
13
|
+
'_stream_writable',
|
|
14
|
+
'assert',
|
|
15
|
+
'buffer',
|
|
16
|
+
'child_process',
|
|
17
|
+
'cluster',
|
|
18
|
+
'console',
|
|
19
|
+
'constants',
|
|
20
|
+
'crypto',
|
|
21
|
+
'dgram',
|
|
22
|
+
'dns',
|
|
23
|
+
'domain',
|
|
24
|
+
'events',
|
|
25
|
+
'fs',
|
|
26
|
+
'http',
|
|
27
|
+
'https',
|
|
28
|
+
'module',
|
|
29
|
+
'net',
|
|
30
|
+
'os',
|
|
31
|
+
'path',
|
|
32
|
+
'process',
|
|
33
|
+
'punycode',
|
|
34
|
+
'querystring',
|
|
35
|
+
'readline',
|
|
36
|
+
'repl',
|
|
37
|
+
'stream',
|
|
38
|
+
'string_decoder',
|
|
39
|
+
'sys',
|
|
40
|
+
'timers',
|
|
41
|
+
'tls',
|
|
42
|
+
'tty',
|
|
43
|
+
'url',
|
|
44
|
+
'util',
|
|
45
|
+
'vm',
|
|
46
|
+
'zlib',
|
|
47
|
+
];
|
|
48
|
+
export const cwd = process.cwd();
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Config, DependencyConfig, ParsedTask } from './types.js';
|
|
2
|
+
export declare function findDepPath(name: string): string | null;
|
|
3
|
+
export declare const resolveConfig: () => Promise<Config>;
|
|
4
|
+
export declare function parseTasks(dependencies: Array<string | DependencyConfig>): ParsedTask[];
|
|
5
|
+
export declare function pick<T, U extends keyof T>(obj: T, keys: ReadonlyArray<U>): Pick<T, U>;
|
|
6
|
+
export declare function replaceFileContent(filePath: string, replaceFn: (content: string) => string): void;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { dirname, join } from 'node:path';
|
|
2
|
+
import fs from '../compiled/fs-extra/index.js';
|
|
3
|
+
import { cwd, DIST_DIR } from './constant.js';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
export function findDepPath(name) {
|
|
8
|
+
try {
|
|
9
|
+
let entry = dirname(require.resolve(join(name), { paths: [cwd] }));
|
|
10
|
+
while (!dirname(entry).endsWith('node_modules')) {
|
|
11
|
+
entry = dirname(entry);
|
|
12
|
+
}
|
|
13
|
+
if (name.includes('/')) {
|
|
14
|
+
return join(dirname(entry), name);
|
|
15
|
+
}
|
|
16
|
+
return entry;
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export const resolveConfig = async () => {
|
|
23
|
+
const configPath = join(cwd, 'prebundle.config.mjs');
|
|
24
|
+
const config = await import(pathToFileURL(configPath).href);
|
|
25
|
+
return config.default;
|
|
26
|
+
};
|
|
27
|
+
export function parseTasks(dependencies) {
|
|
28
|
+
const result = [];
|
|
29
|
+
for (const dep of dependencies) {
|
|
30
|
+
const depName = typeof dep === 'string' ? dep : dep.name;
|
|
31
|
+
const importPath = join(cwd, DIST_DIR, depName);
|
|
32
|
+
const distPath = join(cwd, DIST_DIR, depName);
|
|
33
|
+
const depPath = findDepPath(depName);
|
|
34
|
+
if (!depPath) {
|
|
35
|
+
throw new Error(`Failed to resolve dependency: ${depName}`);
|
|
36
|
+
}
|
|
37
|
+
const depEntry = require.resolve(depName, { paths: [cwd] });
|
|
38
|
+
const info = {
|
|
39
|
+
depName,
|
|
40
|
+
depPath,
|
|
41
|
+
depEntry,
|
|
42
|
+
distPath,
|
|
43
|
+
importPath,
|
|
44
|
+
};
|
|
45
|
+
if (typeof dep === 'string') {
|
|
46
|
+
result.push({
|
|
47
|
+
minify: false,
|
|
48
|
+
target: 'es2019',
|
|
49
|
+
externals: {},
|
|
50
|
+
dtsExternals: [],
|
|
51
|
+
emitFiles: [],
|
|
52
|
+
packageJsonField: [],
|
|
53
|
+
...info,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
result.push({
|
|
58
|
+
minify: dep.minify ?? false,
|
|
59
|
+
target: dep.target ?? 'es2019',
|
|
60
|
+
ignoreDts: dep.ignoreDts,
|
|
61
|
+
externals: dep.externals ?? {},
|
|
62
|
+
dtsExternals: dep.dtsExternals ?? [],
|
|
63
|
+
emitFiles: dep.emitFiles ?? [],
|
|
64
|
+
afterBundle: dep.afterBundle,
|
|
65
|
+
beforeBundle: dep.beforeBundle,
|
|
66
|
+
packageJsonField: dep.packageJsonField ?? [],
|
|
67
|
+
...info,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
export function pick(obj, keys) {
|
|
74
|
+
return keys.reduce((ret, key) => {
|
|
75
|
+
if (obj[key] !== undefined) {
|
|
76
|
+
ret[key] = obj[key];
|
|
77
|
+
}
|
|
78
|
+
return ret;
|
|
79
|
+
}, {});
|
|
80
|
+
}
|
|
81
|
+
export function replaceFileContent(filePath, replaceFn) {
|
|
82
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
83
|
+
const newContent = replaceFn(content);
|
|
84
|
+
if (newContent !== content) {
|
|
85
|
+
fs.writeFileSync(filePath, newContent);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { parseTasks, resolveConfig } from './helper.js';
|
|
2
|
+
import { prebundle } from './prebundle.js';
|
|
3
|
+
export async function run() {
|
|
4
|
+
const config = await resolveConfig();
|
|
5
|
+
const parsedTasks = parseTasks(config.dependencies);
|
|
6
|
+
for (const task of parsedTasks) {
|
|
7
|
+
await prebundle(task, config.externals);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { dirname, join } from 'node:path';
|
|
2
|
+
import ncc from '@vercel/ncc';
|
|
3
|
+
import fastGlob from '../compiled/fast-glob/index.js';
|
|
4
|
+
import fs from '../compiled/fs-extra/index.js';
|
|
5
|
+
import rslog from '../compiled/rslog/index.js';
|
|
6
|
+
import { DEFAULT_EXTERNALS, NODE_BUILTINS } from './constant.js';
|
|
7
|
+
import { findDepPath, pick } from './helper.js';
|
|
8
|
+
import { dts } from 'rollup-plugin-dts';
|
|
9
|
+
import { rollup } from 'rollup';
|
|
10
|
+
import swc from '@swc/core';
|
|
11
|
+
import { format } from 'prettier';
|
|
12
|
+
const { logger } = rslog;
|
|
13
|
+
function emitAssets(assets, distPath) {
|
|
14
|
+
for (const key of Object.keys(assets)) {
|
|
15
|
+
const asset = assets[key];
|
|
16
|
+
fs.outputFileSync(join(distPath, key), asset.source);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function emitIndex(code, distPath) {
|
|
20
|
+
const distIndex = join(distPath, 'index.js');
|
|
21
|
+
const minimized = swc.minifySync(code, {
|
|
22
|
+
compress: false,
|
|
23
|
+
mangle: false,
|
|
24
|
+
ecma: 2019,
|
|
25
|
+
});
|
|
26
|
+
const formatted = await format(minimized.code, {
|
|
27
|
+
filepath: distIndex,
|
|
28
|
+
});
|
|
29
|
+
fs.outputFileSync(distIndex, formatted);
|
|
30
|
+
}
|
|
31
|
+
async function emitDts(task, externals) {
|
|
32
|
+
const outputDefaultDts = () => {
|
|
33
|
+
fs.writeFileSync(join(task.distPath, 'index.d.ts'), 'export = any;\n');
|
|
34
|
+
};
|
|
35
|
+
if (task.ignoreDts) {
|
|
36
|
+
outputDefaultDts();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const getTypes = (json) => json.types || json.typing || json.typings || null;
|
|
40
|
+
const getInput = () => {
|
|
41
|
+
const pkgPath = join(task.depPath, 'package.json');
|
|
42
|
+
const pkgJson = fs.readJsonSync(pkgPath, 'utf-8');
|
|
43
|
+
const types = getTypes(pkgJson);
|
|
44
|
+
if (types) {
|
|
45
|
+
return join(task.depPath, types);
|
|
46
|
+
}
|
|
47
|
+
const depTypesPath = findDepPath(`@types/${task.depName}/package.json`);
|
|
48
|
+
if (!depTypesPath) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
const depTypesPkg = fs.readJsonSync(depTypesPath, 'utf-8');
|
|
52
|
+
const depTypes = getTypes(depTypesPkg);
|
|
53
|
+
return depTypes ? join(dirname(depTypesPath), depTypes) : null;
|
|
54
|
+
};
|
|
55
|
+
const input = getInput();
|
|
56
|
+
if (!input) {
|
|
57
|
+
outputDefaultDts();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const inputConfig = {
|
|
62
|
+
input,
|
|
63
|
+
external: [
|
|
64
|
+
...Object.keys(externals),
|
|
65
|
+
...task.dtsExternals,
|
|
66
|
+
...NODE_BUILTINS,
|
|
67
|
+
],
|
|
68
|
+
plugins: [
|
|
69
|
+
dts({
|
|
70
|
+
respectExternal: true,
|
|
71
|
+
compilerOptions: {
|
|
72
|
+
skipLibCheck: true,
|
|
73
|
+
// https://github.com/Swatinem/rollup-plugin-dts/issues/143,
|
|
74
|
+
// but it will cause error when bundle ts which import another ts file.
|
|
75
|
+
preserveSymlinks: false,
|
|
76
|
+
// https://github.com/Swatinem/rollup-plugin-dts/issues/127
|
|
77
|
+
composite: false,
|
|
78
|
+
// https://github.com/Swatinem/rollup-plugin-dts/issues/113
|
|
79
|
+
declarationMap: false,
|
|
80
|
+
// Ensure ".d.ts" modules are generated
|
|
81
|
+
declaration: true,
|
|
82
|
+
// Skip ".js" generation
|
|
83
|
+
noEmit: false,
|
|
84
|
+
emitDeclarationOnly: true,
|
|
85
|
+
// Skip code generation when error occurs
|
|
86
|
+
noEmitOnError: true,
|
|
87
|
+
// Avoid extra work
|
|
88
|
+
checkJs: false,
|
|
89
|
+
// Ensure we can parse the latest code
|
|
90
|
+
// @ts-expect-error
|
|
91
|
+
target: task.target,
|
|
92
|
+
},
|
|
93
|
+
}),
|
|
94
|
+
],
|
|
95
|
+
};
|
|
96
|
+
const outputConfig = {
|
|
97
|
+
dir: task.distPath,
|
|
98
|
+
format: 'esm',
|
|
99
|
+
exports: 'named',
|
|
100
|
+
entryFileNames: 'index.d.ts',
|
|
101
|
+
};
|
|
102
|
+
const bundle = await rollup(inputConfig);
|
|
103
|
+
await bundle.write(outputConfig);
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
logger.error(`rollup-plugin-dts failed: ${task.depName}`);
|
|
107
|
+
logger.error(error);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function emitPackageJson(task) {
|
|
111
|
+
const packageJsonPath = join(task.depPath, 'package.json');
|
|
112
|
+
const packageJson = fs.readJsonSync(packageJsonPath, 'utf-8');
|
|
113
|
+
const outputPath = join(task.distPath, 'package.json');
|
|
114
|
+
const pickedPackageJson = pick(packageJson, [
|
|
115
|
+
'name',
|
|
116
|
+
'author',
|
|
117
|
+
'version',
|
|
118
|
+
'funding',
|
|
119
|
+
'license',
|
|
120
|
+
...task.packageJsonField,
|
|
121
|
+
]);
|
|
122
|
+
if (task.depName !== pickedPackageJson.name) {
|
|
123
|
+
pickedPackageJson.name = task.depName;
|
|
124
|
+
}
|
|
125
|
+
pickedPackageJson.types = 'index.d.ts';
|
|
126
|
+
fs.writeJSONSync(outputPath, pickedPackageJson);
|
|
127
|
+
}
|
|
128
|
+
function emitLicense(task) {
|
|
129
|
+
const licensePath = join(task.depPath, 'LICENSE');
|
|
130
|
+
if (fs.existsSync(licensePath)) {
|
|
131
|
+
fs.copySync(licensePath, join(task.distPath, 'license'));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function emitExtraFiles(task) {
|
|
135
|
+
const { emitFiles } = task;
|
|
136
|
+
for (const item of emitFiles) {
|
|
137
|
+
const path = join(task.distPath, item.path);
|
|
138
|
+
fs.outputFileSync(path, item.content);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function removeSourceMap(task) {
|
|
142
|
+
const maps = fastGlob.sync(join(task.distPath, '**/*.map'));
|
|
143
|
+
for (const mapPath of maps) {
|
|
144
|
+
fs.removeSync(mapPath);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function renameDistFolder(task) {
|
|
148
|
+
const pkgPath = join(task.distPath, 'package.json');
|
|
149
|
+
const pkgJson = fs.readJsonSync(pkgPath, 'utf-8');
|
|
150
|
+
for (const key of ['types', 'typing', 'typings']) {
|
|
151
|
+
if (pkgJson[key]?.startsWith('dist/')) {
|
|
152
|
+
pkgJson[key] = pkgJson[key].replace('dist/', 'types/');
|
|
153
|
+
const distFolder = join(task.distPath, 'dist');
|
|
154
|
+
const typesFolder = join(task.distPath, 'types');
|
|
155
|
+
if (fs.existsSync(distFolder)) {
|
|
156
|
+
fs.renameSync(distFolder, typesFolder);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// compiled packages are always use commonjs
|
|
161
|
+
pkgJson.type = 'commonjs';
|
|
162
|
+
fs.writeJSONSync(pkgPath, pkgJson);
|
|
163
|
+
}
|
|
164
|
+
const pkgName = process.argv[2];
|
|
165
|
+
export async function prebundle(task, commonExternals = {}) {
|
|
166
|
+
if (pkgName && task.depName !== pkgName) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
logger.start(`prebundle: ${task.depName}`);
|
|
170
|
+
fs.removeSync(task.distPath);
|
|
171
|
+
if (task.beforeBundle) {
|
|
172
|
+
await task.beforeBundle(task);
|
|
173
|
+
}
|
|
174
|
+
const mergedExternals = {
|
|
175
|
+
...DEFAULT_EXTERNALS,
|
|
176
|
+
...commonExternals,
|
|
177
|
+
...task.externals,
|
|
178
|
+
};
|
|
179
|
+
const { code, assets } = await ncc(task.depEntry, {
|
|
180
|
+
minify: task.minify,
|
|
181
|
+
target: task.target,
|
|
182
|
+
externals: mergedExternals,
|
|
183
|
+
assetBuilds: false,
|
|
184
|
+
});
|
|
185
|
+
emitIndex(code, task.distPath);
|
|
186
|
+
emitAssets(assets, task.distPath);
|
|
187
|
+
await emitDts(task, mergedExternals);
|
|
188
|
+
emitLicense(task);
|
|
189
|
+
emitPackageJson(task);
|
|
190
|
+
removeSourceMap(task);
|
|
191
|
+
renameDistFolder(task);
|
|
192
|
+
emitExtraFiles(task);
|
|
193
|
+
if (task.afterBundle) {
|
|
194
|
+
await task.afterBundle(task);
|
|
195
|
+
}
|
|
196
|
+
logger.success(`prebundle: ${task.depName}\n\n`);
|
|
197
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type ImportMap = {
|
|
2
|
+
path: string;
|
|
3
|
+
content: string;
|
|
4
|
+
};
|
|
5
|
+
export type DependencyConfig = {
|
|
6
|
+
/** Name of dependency */
|
|
7
|
+
name: string;
|
|
8
|
+
/** Whether to minify the code. */
|
|
9
|
+
minify?: boolean;
|
|
10
|
+
/** Whether to strip comments when minify is false. */
|
|
11
|
+
stripComments?: boolean;
|
|
12
|
+
/** Externals to leave as requires of the build. */
|
|
13
|
+
externals?: Record<string, string>;
|
|
14
|
+
/** Externals types */
|
|
15
|
+
dtsExternals?: Array<string | RegExp>;
|
|
16
|
+
/** Emit extra entry files to map imports. */
|
|
17
|
+
emitFiles?: ImportMap[];
|
|
18
|
+
/** Copy extra fields from original package.json to target package.json. */
|
|
19
|
+
packageJsonField?: string[];
|
|
20
|
+
/** Whether to ignore type definitions */
|
|
21
|
+
ignoreDts?: boolean;
|
|
22
|
+
/** Target ECMA version */
|
|
23
|
+
target?: string;
|
|
24
|
+
beforeBundle?: (task: ParsedTask) => void | Promise<void>;
|
|
25
|
+
afterBundle?: (task: ParsedTask) => void | Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
export type Config = {
|
|
28
|
+
/**
|
|
29
|
+
* Configure externals for all packages,
|
|
30
|
+
* will be merged with dependencies[i].externals.
|
|
31
|
+
*/
|
|
32
|
+
externals?: Record<string, string>;
|
|
33
|
+
dependencies: Array<string | DependencyConfig>;
|
|
34
|
+
};
|
|
35
|
+
export type ParsedTask = {
|
|
36
|
+
depPath: string;
|
|
37
|
+
depEntry: string;
|
|
38
|
+
distPath: string;
|
|
39
|
+
importPath: string;
|
|
40
|
+
ignoreDts?: boolean;
|
|
41
|
+
target: NonNullable<DependencyConfig['target']>;
|
|
42
|
+
minify: NonNullable<DependencyConfig['minify']>;
|
|
43
|
+
depName: NonNullable<DependencyConfig['name']>;
|
|
44
|
+
externals: NonNullable<DependencyConfig['externals']>;
|
|
45
|
+
dtsExternals: NonNullable<DependencyConfig['dtsExternals']>;
|
|
46
|
+
emitFiles: NonNullable<DependencyConfig['emitFiles']>;
|
|
47
|
+
afterBundle?: NonNullable<DependencyConfig['afterBundle']>;
|
|
48
|
+
beforeBundle?: NonNullable<DependencyConfig['beforeBundle']>;
|
|
49
|
+
packageJsonField: NonNullable<DependencyConfig['packageJsonField']>;
|
|
50
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export type DependencyConfig = {
|
|
|
11
11
|
externals?: Record<string, string>;
|
|
12
12
|
/** Externals types */
|
|
13
13
|
dtsExternals?: Array<string | RegExp>;
|
|
14
|
+
/** Whether to prettier the code and strip comments */
|
|
15
|
+
prettier?: boolean;
|
|
14
16
|
/** Emit extra entry files to map imports. */
|
|
15
17
|
emitFiles?: ImportMap[];
|
|
16
18
|
/** Copy extra fields from original package.json to target package.json. */
|
|
@@ -29,6 +31,8 @@ export type Config = {
|
|
|
29
31
|
*/
|
|
30
32
|
externals?: Record<string, string>;
|
|
31
33
|
dependencies: Array<string | DependencyConfig>;
|
|
34
|
+
/** Whether to prettier the code and strip comments */
|
|
35
|
+
prettier?: boolean;
|
|
32
36
|
};
|
|
33
37
|
export type ParsedTask = {
|
|
34
38
|
depPath: string;
|
|
@@ -36,6 +40,7 @@ export type ParsedTask = {
|
|
|
36
40
|
distPath: string;
|
|
37
41
|
importPath: string;
|
|
38
42
|
ignoreDts?: boolean;
|
|
43
|
+
prettier?: boolean;
|
|
39
44
|
target: NonNullable<DependencyConfig['target']>;
|
|
40
45
|
minify: NonNullable<DependencyConfig['minify']>;
|
|
41
46
|
depName: NonNullable<DependencyConfig['name']>;
|
package/package.json
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prebundle",
|
|
3
|
-
"version": "1.1
|
|
4
|
-
"main": "./dist/index.js",
|
|
5
|
-
"type": "module",
|
|
3
|
+
"version": "1.2.1",
|
|
6
4
|
"repository": {
|
|
7
5
|
"type": "git",
|
|
8
6
|
"url": "https://github.com/rspack-contrib/prebundle"
|
|
9
7
|
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"bin": {
|
|
11
|
+
"prebundle": "./bin.js"
|
|
12
|
+
},
|
|
10
13
|
"files": [
|
|
11
14
|
"dist",
|
|
12
15
|
"bin.js",
|
|
13
16
|
"compiled"
|
|
14
17
|
],
|
|
15
|
-
"bin": {
|
|
16
|
-
"prebundle": "./bin.js"
|
|
17
|
-
},
|
|
18
18
|
"dependencies": {
|
|
19
|
+
"@swc/core": "^1.6.13",
|
|
19
20
|
"@vercel/ncc": "0.38.1",
|
|
21
|
+
"prettier": "^3.2.5",
|
|
20
22
|
"rollup": "^4.17.1",
|
|
21
23
|
"rollup-plugin-dts": "^6.1.0"
|
|
22
24
|
},
|
|
@@ -25,7 +27,6 @@
|
|
|
25
27
|
"@types/node": "18.x",
|
|
26
28
|
"fast-glob": "^3.3.2",
|
|
27
29
|
"fs-extra": "^11.2.0",
|
|
28
|
-
"prettier": "^3.2.5",
|
|
29
30
|
"rslog": "^1.2.2",
|
|
30
31
|
"typescript": "^5.4.2"
|
|
31
32
|
},
|
|
@@ -33,8 +34,8 @@
|
|
|
33
34
|
"registry": "https://registry.npmjs.org/"
|
|
34
35
|
},
|
|
35
36
|
"scripts": {
|
|
36
|
-
"dev": "tsc --watch",
|
|
37
37
|
"build": "tsc",
|
|
38
|
+
"dev": "tsc --watch",
|
|
38
39
|
"prebundle": "node ./bin.js"
|
|
39
40
|
}
|
|
40
41
|
}
|