prebundle 1.2.1 → 1.2.3

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,197 +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 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
- }
@@ -1,50 +0,0 @@
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
- };
package/dist/src/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
package/dist/types.d.ts DELETED
@@ -1,53 +0,0 @@
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
- /** Externals to leave as requires of the build. */
11
- externals?: Record<string, string>;
12
- /** Externals types */
13
- dtsExternals?: Array<string | RegExp>;
14
- /** Whether to prettier the code and strip comments */
15
- prettier?: boolean;
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
- /** Whether to prettier the code and strip comments */
35
- prettier?: boolean;
36
- };
37
- export type ParsedTask = {
38
- depPath: string;
39
- depEntry: string;
40
- distPath: string;
41
- importPath: string;
42
- ignoreDts?: boolean;
43
- prettier?: boolean;
44
- target: NonNullable<DependencyConfig['target']>;
45
- minify: NonNullable<DependencyConfig['minify']>;
46
- depName: NonNullable<DependencyConfig['name']>;
47
- externals: NonNullable<DependencyConfig['externals']>;
48
- dtsExternals: NonNullable<DependencyConfig['dtsExternals']>;
49
- emitFiles: NonNullable<DependencyConfig['emitFiles']>;
50
- afterBundle?: NonNullable<DependencyConfig['afterBundle']>;
51
- beforeBundle?: NonNullable<DependencyConfig['beforeBundle']>;
52
- packageJsonField: NonNullable<DependencyConfig['packageJsonField']>;
53
- };
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};