prebundle 1.2.2 → 1.2.4

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.
@@ -1,8 +0,0 @@
1
- export declare const DIST_DIR = "compiled";
2
- export declare const DEFAULT_EXTERNALS: {
3
- './package.json': string;
4
- '../package.json': string;
5
- '../../package.json': string;
6
- };
7
- export declare const NODE_BUILTINS: string[];
8
- export declare const cwd: string;
package/dist/constant.js DELETED
@@ -1,48 +0,0 @@
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();
package/dist/helper.d.ts DELETED
@@ -1,6 +0,0 @@
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>, globalPrettier?: boolean): 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;
package/dist/helper.js DELETED
@@ -1,89 +0,0 @@
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, globalPrettier) {
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
- prettier: globalPrettier,
54
- ...info,
55
- });
56
- }
57
- else {
58
- result.push({
59
- minify: dep.minify ?? false,
60
- target: dep.target ?? 'es2019',
61
- ignoreDts: dep.ignoreDts,
62
- externals: dep.externals ?? {},
63
- dtsExternals: dep.dtsExternals ?? [],
64
- emitFiles: dep.emitFiles ?? [],
65
- prettier: dep.prettier ?? globalPrettier,
66
- afterBundle: dep.afterBundle,
67
- beforeBundle: dep.beforeBundle,
68
- packageJsonField: dep.packageJsonField ?? [],
69
- ...info,
70
- });
71
- }
72
- }
73
- return result;
74
- }
75
- export function pick(obj, keys) {
76
- return keys.reduce((ret, key) => {
77
- if (obj[key] !== undefined) {
78
- ret[key] = obj[key];
79
- }
80
- return ret;
81
- }, {});
82
- }
83
- export function replaceFileContent(filePath, replaceFn) {
84
- const content = fs.readFileSync(filePath, 'utf-8');
85
- const newContent = replaceFn(content);
86
- if (newContent !== content) {
87
- fs.writeFileSync(filePath, newContent);
88
- }
89
- }
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare function run(): Promise<void>;
2
- export type { Config } from './types.js';
package/dist/index.js DELETED
@@ -1,9 +0,0 @@
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, config.prettier);
6
- for (const task of parsedTasks) {
7
- await prebundle(task, config.externals);
8
- }
9
- }
@@ -1,2 +0,0 @@
1
- import type { ParsedTask } from './types.js';
2
- export declare function prebundle(task: ParsedTask, commonExternals?: Record<string, string>): Promise<void>;
package/dist/prebundle.js DELETED
@@ -1,206 +0,0 @@
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 { minify } from 'terser';
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, prettier) {
20
- const distIndex = join(distPath, 'index.js');
21
- // use terser to strip comments and use prettier to format the code
22
- if (prettier) {
23
- const minimized = await minify(code, {
24
- compress: false,
25
- mangle: false,
26
- ecma: 2019,
27
- });
28
- if (!minimized.code) {
29
- throw new Error('terser minify failed');
30
- }
31
- const formatted = await format(minimized.code, {
32
- filepath: distIndex,
33
- });
34
- await fs.outputFile(distIndex, formatted);
35
- }
36
- else {
37
- await fs.outputFile(distIndex, code);
38
- }
39
- }
40
- async function emitDts(task, externals) {
41
- const outputDefaultDts = () => {
42
- fs.outputFileSync(join(task.distPath, 'index.d.ts'), 'export = any;\n');
43
- };
44
- if (task.ignoreDts) {
45
- outputDefaultDts();
46
- return;
47
- }
48
- const getTypes = (json) => json.types || json.typing || json.typings || null;
49
- const getInput = () => {
50
- const pkgPath = join(task.depPath, 'package.json');
51
- const pkgJson = fs.readJsonSync(pkgPath, 'utf-8');
52
- const types = getTypes(pkgJson);
53
- if (types) {
54
- return join(task.depPath, types);
55
- }
56
- const depTypesPath = findDepPath(`@types/${task.depName}/package.json`);
57
- if (!depTypesPath) {
58
- return null;
59
- }
60
- const depTypesPkg = fs.readJsonSync(depTypesPath, 'utf-8');
61
- const depTypes = getTypes(depTypesPkg);
62
- return depTypes ? join(dirname(depTypesPath), depTypes) : null;
63
- };
64
- const input = getInput();
65
- if (!input) {
66
- outputDefaultDts();
67
- return;
68
- }
69
- try {
70
- const inputConfig = {
71
- input,
72
- external: [
73
- ...Object.keys(externals),
74
- ...task.dtsExternals,
75
- ...NODE_BUILTINS,
76
- ],
77
- plugins: [
78
- dts({
79
- respectExternal: true,
80
- compilerOptions: {
81
- skipLibCheck: true,
82
- // https://github.com/Swatinem/rollup-plugin-dts/issues/143,
83
- // but it will cause error when bundle ts which import another ts file.
84
- preserveSymlinks: false,
85
- // https://github.com/Swatinem/rollup-plugin-dts/issues/127
86
- composite: false,
87
- // https://github.com/Swatinem/rollup-plugin-dts/issues/113
88
- declarationMap: false,
89
- // Ensure ".d.ts" modules are generated
90
- declaration: true,
91
- // Skip ".js" generation
92
- noEmit: false,
93
- emitDeclarationOnly: true,
94
- // Skip code generation when error occurs
95
- noEmitOnError: true,
96
- // Avoid extra work
97
- checkJs: false,
98
- // Ensure we can parse the latest code
99
- // @ts-expect-error
100
- target: task.target,
101
- },
102
- }),
103
- ],
104
- };
105
- const outputConfig = {
106
- dir: task.distPath,
107
- format: 'esm',
108
- exports: 'named',
109
- entryFileNames: 'index.d.ts',
110
- };
111
- const bundle = await rollup(inputConfig);
112
- await bundle.write(outputConfig);
113
- }
114
- catch (error) {
115
- logger.error(`rollup-plugin-dts failed: ${task.depName}`);
116
- logger.error(error);
117
- }
118
- }
119
- function emitPackageJson(task) {
120
- const packageJsonPath = join(task.depPath, 'package.json');
121
- const packageJson = fs.readJsonSync(packageJsonPath, 'utf-8');
122
- const outputPath = join(task.distPath, 'package.json');
123
- const pickedPackageJson = pick(packageJson, [
124
- 'name',
125
- 'author',
126
- 'version',
127
- 'funding',
128
- 'license',
129
- ...task.packageJsonField,
130
- ]);
131
- if (task.depName !== pickedPackageJson.name) {
132
- pickedPackageJson.name = task.depName;
133
- }
134
- pickedPackageJson.types = 'index.d.ts';
135
- fs.writeJSONSync(outputPath, pickedPackageJson);
136
- }
137
- function emitLicense(task) {
138
- const licensePath = join(task.depPath, 'LICENSE');
139
- if (fs.existsSync(licensePath)) {
140
- fs.copySync(licensePath, join(task.distPath, 'license'));
141
- }
142
- }
143
- function emitExtraFiles(task) {
144
- const { emitFiles } = task;
145
- for (const item of emitFiles) {
146
- const path = join(task.distPath, item.path);
147
- fs.outputFileSync(path, item.content);
148
- }
149
- }
150
- function removeSourceMap(task) {
151
- const maps = fastGlob.sync(join(task.distPath, '**/*.map'));
152
- for (const mapPath of maps) {
153
- fs.removeSync(mapPath);
154
- }
155
- }
156
- function renameDistFolder(task) {
157
- const pkgPath = join(task.distPath, 'package.json');
158
- const pkgJson = fs.readJsonSync(pkgPath, 'utf-8');
159
- for (const key of ['types', 'typing', 'typings']) {
160
- if (pkgJson[key]?.startsWith('dist/')) {
161
- pkgJson[key] = pkgJson[key].replace('dist/', 'types/');
162
- const distFolder = join(task.distPath, 'dist');
163
- const typesFolder = join(task.distPath, 'types');
164
- if (fs.existsSync(distFolder)) {
165
- fs.renameSync(distFolder, typesFolder);
166
- }
167
- }
168
- }
169
- // compiled packages are always use commonjs
170
- pkgJson.type = 'commonjs';
171
- fs.writeJSONSync(pkgPath, pkgJson);
172
- }
173
- const pkgName = process.argv[2];
174
- export async function prebundle(task, commonExternals = {}) {
175
- if (pkgName && task.depName !== pkgName) {
176
- return;
177
- }
178
- logger.start(`prebundle: ${task.depName}`);
179
- fs.removeSync(task.distPath);
180
- if (task.beforeBundle) {
181
- await task.beforeBundle(task);
182
- }
183
- const mergedExternals = {
184
- ...DEFAULT_EXTERNALS,
185
- ...commonExternals,
186
- ...task.externals,
187
- };
188
- const { code, assets } = await ncc(task.depEntry, {
189
- minify: task.minify,
190
- target: task.target,
191
- externals: mergedExternals,
192
- assetBuilds: false,
193
- });
194
- await emitIndex(code, task.distPath, task.prettier);
195
- emitAssets(assets, task.distPath);
196
- await emitDts(task, mergedExternals);
197
- emitLicense(task);
198
- emitPackageJson(task);
199
- removeSourceMap(task);
200
- renameDistFolder(task);
201
- emitExtraFiles(task);
202
- if (task.afterBundle) {
203
- await task.afterBundle(task);
204
- }
205
- logger.success(`prebundle: ${task.depName}\n\n`);
206
- }
@@ -1,8 +0,0 @@
1
- export declare const DIST_DIR = "compiled";
2
- export declare const DEFAULT_EXTERNALS: {
3
- './package.json': string;
4
- '../package.json': string;
5
- '../../package.json': string;
6
- };
7
- export declare const NODE_BUILTINS: string[];
8
- export declare const cwd: string;
@@ -1,48 +0,0 @@
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();
@@ -1,6 +0,0 @@
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;
@@ -1,87 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- export declare function run(): Promise<void>;
2
- export type { Config } from './types.js';
package/dist/src/index.js DELETED
@@ -1,9 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- import type { ParsedTask } from './types.js';
2
- export declare function prebundle(task: ParsedTask, commonExternals?: Record<string, string>): Promise<void>;