pkgbld 1.14.5 → 1.15.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 +27 -14
- package/dist/builtin-plugins/binify.d.ts +3 -3
- package/dist/builtin-plugins/clean.d.ts +2 -2
- package/dist/builtin-plugins/commonjs.d.ts +2 -2
- package/dist/builtin-plugins/externals.d.ts +3 -3
- package/dist/builtin-plugins/preprocess.d.ts +3 -3
- package/dist/builtin-plugins/resolve.d.ts +2 -2
- package/dist/builtin-plugins/terser.d.ts +3 -3
- package/dist/builtin-plugins/typescript.d.ts +2 -2
- package/dist/check-ts-config.d.ts +3 -0
- package/dist/create-subpackages.d.ts +2 -2
- package/dist/eject.d.ts +6 -0
- package/dist/get-cli-options.d.ts +15 -12
- package/dist/get-pkg.d.ts +2 -2
- package/dist/get-plugins.d.ts +4 -11
- package/dist/get-rollup-configs.d.ts +57 -56
- package/dist/helpers.d.ts +10 -13
- package/dist/index.d.ts +1 -1
- package/dist/index.js +642 -490
- package/dist/messages.d.ts +1 -1
- package/dist/process-pkg.d.ts +3 -3
- package/dist/types.d.ts +43 -24
- package/dist/write-json.d.ts +2 -0
- package/package.json +22 -19
- package/dist/write-pkg.d.ts +0 -2
package/dist/index.js
CHANGED
|
@@ -10,529 +10,681 @@ var minimist = require('minimist');
|
|
|
10
10
|
var refiner = require('@slimlib/refine-partition');
|
|
11
11
|
var camelCase = require('lodash/camelCase');
|
|
12
12
|
var kleur = require('kleur');
|
|
13
|
+
var lodash = require('lodash');
|
|
14
|
+
var fs$1 = require('fs');
|
|
13
15
|
|
|
14
|
-
async function createSubpackages(inputs, config) {
|
|
15
|
-
for (const input of inputs) {
|
|
16
|
-
const basename = path.basename(input, '.ts');
|
|
17
|
-
if (basename !== 'index') {
|
|
18
|
-
const pkg = {
|
|
19
|
-
type: 'module',
|
|
20
|
-
types: `../${config.dir}/${basename}.d.ts`,
|
|
21
|
-
main: `../${config.dir}/${basename}.mjs`
|
|
22
|
-
};
|
|
23
|
-
await fs.mkdir(basename, { recursive: true });
|
|
24
|
-
await fs.writeFile(`${basename}/package.json`, JSON.stringify(pkg, null, 2));
|
|
25
|
-
}
|
|
26
|
-
}
|
|
16
|
+
async function createSubpackages(inputs, config) {
|
|
17
|
+
for (const input of inputs) {
|
|
18
|
+
const basename = path.basename(input, '.ts');
|
|
19
|
+
if (basename !== 'index') {
|
|
20
|
+
const pkg = {
|
|
21
|
+
type: 'module',
|
|
22
|
+
types: `../${config.dir}/${basename}.d.ts`,
|
|
23
|
+
main: `../${config.dir}/${basename}.mjs`
|
|
24
|
+
};
|
|
25
|
+
await fs.mkdir(basename, { recursive: true });
|
|
26
|
+
await fs.writeFile(`${basename}/package.json`, JSON.stringify(pkg, null, 2));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
const defaults = {
|
|
30
|
-
formats: ['es', 'cjs'],
|
|
31
|
-
umdInputs: [],
|
|
32
|
-
compressFormats: ['umd'],
|
|
33
|
-
sourcemapFormats: ['umd'],
|
|
34
|
-
preprocess: [],
|
|
35
|
-
dir: 'dist',
|
|
36
|
-
sourceDir: 'src',
|
|
37
|
-
includeExternals: false
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
31
|
+
const defaults = {
|
|
32
|
+
formats: ['es', 'cjs'],
|
|
33
|
+
umdInputs: [],
|
|
34
|
+
compressFormats: ['umd'],
|
|
35
|
+
sourcemapFormats: ['umd'],
|
|
36
|
+
preprocess: [],
|
|
37
|
+
dir: 'dist',
|
|
38
|
+
sourceDir: 'src',
|
|
39
|
+
includeExternals: false,
|
|
40
|
+
eject: false,
|
|
41
|
+
noTsConfig: false,
|
|
42
|
+
noUpdatePackageJson: false
|
|
43
|
+
};
|
|
44
|
+
function getCliOptions() {
|
|
45
|
+
const parsedArgs = minimist(process.argv.slice(2));
|
|
46
|
+
const umdInputs = parsedArgs.umd?.split(',').map((arg) => arg.trim()) ?? defaults.umdInputs;
|
|
47
|
+
const compressFormats = parsedArgs.compress?.split(',').map((arg) => arg.trim()) ?? defaults.compressFormats;
|
|
48
|
+
const sourcemapFormats = parsedArgs.sourcemaps?.split(',').map((arg) => arg.trim()) ?? defaults.sourcemapFormats;
|
|
49
|
+
const formats = parsedArgs.formats?.split(',').map((arg) => arg.trim()) ?? defaults.formats;
|
|
50
|
+
const preprocess = parsedArgs.preprocess?.split(',').map((arg) => arg.trim()) ?? defaults.preprocess;
|
|
51
|
+
const dir = parsedArgs.dir ?? defaults.dir;
|
|
52
|
+
const sourceDir = parsedArgs.sourceDir ?? defaults.sourceDir;
|
|
53
|
+
const bin = parsedArgs.bin?.split(',').map((arg) => arg.trim());
|
|
54
|
+
const includeExternals = parsedArgs['include-externals'] ?? defaults.includeExternals;
|
|
55
|
+
const eject = !!parsedArgs.eject ?? defaults.eject;
|
|
56
|
+
const noTsConfig = !!parsedArgs.noTsConfig ?? defaults.noTsConfig;
|
|
57
|
+
const noUpdatePackageJson = !!parsedArgs.noUpdatePackageJson ?? defaults.noUpdatePackageJson;
|
|
58
|
+
return {
|
|
59
|
+
umdInputs,
|
|
60
|
+
compressFormats,
|
|
61
|
+
sourcemapFormats,
|
|
62
|
+
formats,
|
|
63
|
+
formatsOverriden: 'formats' in parsedArgs,
|
|
64
|
+
preprocess,
|
|
65
|
+
dir,
|
|
66
|
+
sourceDir,
|
|
67
|
+
bin,
|
|
68
|
+
includeExternals,
|
|
69
|
+
eject,
|
|
70
|
+
noTsConfig,
|
|
71
|
+
noUpdatePackageJson
|
|
72
|
+
};
|
|
63
73
|
}
|
|
64
74
|
|
|
65
|
-
async function getPackage() {
|
|
66
|
-
const pkgPath = path.resolve('package.json');
|
|
67
|
-
const buffer = await fs.readFile(pkgPath);
|
|
68
|
-
return [pkgPath, JSON.parse(buffer.toString())];
|
|
75
|
+
async function getPackage() {
|
|
76
|
+
const pkgPath = path.resolve('package.json');
|
|
77
|
+
const buffer = await fs.readFile(pkgPath);
|
|
78
|
+
return [pkgPath, JSON.parse(buffer.toString())];
|
|
69
79
|
}
|
|
70
80
|
|
|
71
|
-
async function clean (
|
|
72
|
-
const
|
|
73
|
-
const pluginInstance = pluginClean();
|
|
74
|
-
provide(pluginFactory, 1000 /* Priotiry.cleanup */, { outputPlugin: true });
|
|
75
|
-
let firstPluginInstance = true;
|
|
76
|
-
function pluginFactory() {
|
|
77
|
-
const result = firstPluginInstance ? pluginInstance : pluginInstance.api.addInstance();
|
|
78
|
-
firstPluginInstance = false;
|
|
79
|
-
return result;
|
|
80
|
-
}
|
|
81
|
+
async function clean (provider) {
|
|
82
|
+
const pluginClean = await provider.import('@rollup-extras/plugin-clean');
|
|
83
|
+
const pluginInstance = pluginClean();
|
|
84
|
+
provider.provide(pluginFactory, 1000 /* Priotiry.cleanup */, { outputPlugin: true });
|
|
85
|
+
let firstPluginInstance = true;
|
|
86
|
+
function pluginFactory() {
|
|
87
|
+
const result = firstPluginInstance ? pluginInstance : pluginInstance.api.addInstance();
|
|
88
|
+
firstPluginInstance = false;
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
81
91
|
}
|
|
82
92
|
|
|
83
|
-
async function commonjs (
|
|
84
|
-
const
|
|
85
|
-
provide(() => pluginCommonjs(), 4000 /* Priotiry.commonjs */);
|
|
93
|
+
async function commonjs (provider) {
|
|
94
|
+
const pluginCommonjs = await provider.import('@rollup/plugin-commonjs');
|
|
95
|
+
provider.provide(() => pluginCommonjs(), 4000 /* Priotiry.commonjs */);
|
|
86
96
|
}
|
|
87
97
|
|
|
88
|
-
async function externals (
|
|
89
|
-
if (config.includeExternals) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
const
|
|
93
|
-
const allowGenericUmd = config.umdInputs.length === 1 && inputs.length === 1;
|
|
94
|
-
if (config.formats.length > 0) {
|
|
95
|
-
const format = (allowGenericUmd ? undefined : config.formats.filter(format => format !== 'umd'));
|
|
96
|
-
provide(() => pluginExternals(), 2000 /* Priotiry.externals */, { format });
|
|
97
|
-
}
|
|
98
|
-
if (!allowGenericUmd && config.umdInputs.length > 0) {
|
|
99
|
-
for (const currentInput of config.umdInputs) {
|
|
100
|
-
provide(() => pluginExternals({
|
|
101
|
-
external: (id, external) => external || isExternalInput(currentInput, inputs, id, config)
|
|
102
|
-
}), 2000 /* Priotiry.externals */, { format: 'umd', inputs: [`./${config.sourceDir}/${currentInput}.ts`] });
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
normalizedPath = './' + path.
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
|
|
98
|
+
async function externals (provider, config, inputs) {
|
|
99
|
+
if (config.includeExternals) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const pluginExternals = await provider.import('@rollup-extras/plugin-externals');
|
|
103
|
+
const allowGenericUmd = config.umdInputs.length === 1 && inputs.length === 1;
|
|
104
|
+
if (config.formats.length > 0) {
|
|
105
|
+
const format = (allowGenericUmd ? undefined : config.formats.filter(format => format !== 'umd'));
|
|
106
|
+
provider.provide(() => pluginExternals(), 2000 /* Priotiry.externals */, { format });
|
|
107
|
+
}
|
|
108
|
+
if (!allowGenericUmd && config.umdInputs.length > 0) {
|
|
109
|
+
for (const currentInput of config.umdInputs) {
|
|
110
|
+
provider.provide(() => pluginExternals({
|
|
111
|
+
external: (id, external) => external || isExternalInput(currentInput, inputs, id, config)
|
|
112
|
+
}), 2000 /* Priotiry.externals */, { format: 'umd', inputs: [`./${config.sourceDir}/${currentInput}.ts`] });
|
|
113
|
+
}
|
|
114
|
+
// for eject config
|
|
115
|
+
provider.globalImport('path', 'path');
|
|
116
|
+
provider.globalSetupt(isExternalInput);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function isExternalInput(currentInput, inputs, id, config) {
|
|
120
|
+
let normalizedPath;
|
|
121
|
+
if (path.isAbsolute(currentInput)) {
|
|
122
|
+
normalizedPath = './' + path.relative(process.cwd(), currentInput);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
normalizedPath = './' + path.join(config.sourceDir, currentInput + '.ts');
|
|
126
|
+
}
|
|
127
|
+
if (path.isAbsolute(id)) {
|
|
128
|
+
id = './' + path.relative(process.cwd(), id);
|
|
129
|
+
}
|
|
130
|
+
return normalizedPath !== id && inputs.includes(normalizedPath);
|
|
118
131
|
}
|
|
119
132
|
|
|
120
|
-
async function preprocess (
|
|
121
|
-
if (config.preprocess.length > 0) {
|
|
122
|
-
const
|
|
123
|
-
const include = config.preprocess.map(name => `${config.sourceDir}/${name}.ts`);
|
|
124
|
-
for (const format of config.formats) {
|
|
125
|
-
if (format !== 'umd') {
|
|
126
|
-
provide(() => pluginPreprocess({ include, context: { [format]: true } }), 1000 /* Priotiry.preprocess */, { format });
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
for (const currentInput of config.umdInputs) {
|
|
130
|
-
provide(() => pluginPreprocess({ include, context: { umd: true } }), 1000 /* Priotiry.preprocess */, { format, inputs: [`./${config.sourceDir}/${currentInput}.ts`] });
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
133
|
+
async function preprocess (provider, config) {
|
|
134
|
+
if (config.preprocess.length > 0) {
|
|
135
|
+
const pluginPreprocess = await provider.import('rollup-plugin-preprocess');
|
|
136
|
+
const include = config.preprocess.map(name => `${config.sourceDir}/${name}.ts`);
|
|
137
|
+
for (const format of config.formats) {
|
|
138
|
+
if (format !== 'umd') {
|
|
139
|
+
provider.provide(() => pluginPreprocess({ include, context: { [format]: true } }), 1000 /* Priotiry.preprocess */, { format });
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
for (const currentInput of config.umdInputs) {
|
|
143
|
+
provider.provide(() => pluginPreprocess({ include, context: { umd: true } }), 1000 /* Priotiry.preprocess */, { format, inputs: [`./${config.sourceDir}/${currentInput}.ts`] });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
135
148
|
}
|
|
136
149
|
|
|
137
|
-
async function resolve (
|
|
138
|
-
const
|
|
139
|
-
provide(() => pluginResolve(), 3000 /* Priotiry.resolve */);
|
|
150
|
+
async function resolve (provider) {
|
|
151
|
+
const pluginResolve = await provider.import('@rollup/plugin-node-resolve');
|
|
152
|
+
provider.provide(() => pluginResolve(), 3000 /* Priotiry.resolve */);
|
|
140
153
|
}
|
|
141
154
|
|
|
142
|
-
async function terser (
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (filteredFormats.length > 0) {
|
|
154
|
-
for (const format of filteredFormats) {
|
|
155
|
-
if (format !== 'umd') {
|
|
156
|
-
provide(() => pluginTerser(options), 10000 /* Priotiry.compress */, { format, outputPlugin: true });
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
for (const currentInput of config.umdInputs) {
|
|
160
|
-
provide(() => pluginTerser(options), 10000 /* Priotiry.compress */, { format, outputPlugin: true, inputs: [`./${config.sourceDir}/${currentInput}.ts`] });
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
155
|
+
async function terser (provider, config) {
|
|
156
|
+
const filteredFormats = config.compressFormats.filter(format => config.formats.includes(format));
|
|
157
|
+
if (filteredFormats.length > 0) {
|
|
158
|
+
const pluginTerser = await provider.import('@rollup/plugin-terser');
|
|
159
|
+
const options = {
|
|
160
|
+
mangle: {
|
|
161
|
+
properties: {
|
|
162
|
+
regex: /_$/
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
if (filteredFormats.length > 0) {
|
|
167
|
+
for (const format of filteredFormats) {
|
|
168
|
+
if (format !== 'umd') {
|
|
169
|
+
provider.provide(() => pluginTerser(options), 10000 /* Priotiry.compress */, { format, outputPlugin: true });
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
for (const currentInput of config.umdInputs) {
|
|
173
|
+
provider.provide(() => pluginTerser(options), 10000 /* Priotiry.compress */, { format, outputPlugin: true, inputs: [`./${config.sourceDir}/${currentInput}.ts`] });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
166
179
|
}
|
|
167
180
|
|
|
168
|
-
async function typescript (
|
|
169
|
-
const
|
|
170
|
-
provide(() => pluginTypescript(), 6000 /* Priotiry.transpile */);
|
|
181
|
+
async function typescript (provider) {
|
|
182
|
+
const pluginTypescript = await provider.import('rollup-plugin-typescript2');
|
|
183
|
+
provider.provide(() => pluginTypescript(), 6000 /* Priotiry.transpile */);
|
|
171
184
|
}
|
|
172
185
|
|
|
173
|
-
async function binify (
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
'
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
186
|
+
async function binify (provider, config) {
|
|
187
|
+
if (config.bin != null) {
|
|
188
|
+
const pluginBinify = await provider.import('@rollup-extras/plugin-binify');
|
|
189
|
+
const format = {
|
|
190
|
+
'cjs': 'cjs',
|
|
191
|
+
'mjs': 'es'
|
|
192
|
+
}[path.extname(config.bin[0])] ?? 'cjs';
|
|
193
|
+
provider.provide(() => pluginBinify({
|
|
194
|
+
filter: (item) => item.type === 'chunk' && item.isEntry && config.bin.some(input => input === `./${config.dir}/${item.fileName}`)
|
|
195
|
+
}), 20000 /* Priotiry.finalize */, { outputPlugin: true, format });
|
|
196
|
+
}
|
|
185
197
|
}
|
|
186
198
|
|
|
187
|
-
const plugins = [
|
|
188
|
-
clean,
|
|
189
|
-
commonjs,
|
|
190
|
-
externals,
|
|
191
|
-
preprocess,
|
|
192
|
-
resolve,
|
|
193
|
-
terser,
|
|
194
|
-
typescript,
|
|
195
|
-
binify
|
|
196
|
-
];
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
199
|
+
const plugins = [
|
|
200
|
+
clean,
|
|
201
|
+
commonjs,
|
|
202
|
+
externals,
|
|
203
|
+
preprocess,
|
|
204
|
+
resolve,
|
|
205
|
+
terser,
|
|
206
|
+
typescript,
|
|
207
|
+
binify
|
|
208
|
+
];
|
|
209
|
+
const noop = () => undefined;
|
|
210
|
+
function createProvider() {
|
|
211
|
+
const plugins = [];
|
|
212
|
+
return [{
|
|
213
|
+
provide: (plugin, priority, options) => {
|
|
214
|
+
plugins.push({ priority, plugin, format: options?.format, inputs: options?.inputs, outputPlugin: options?.outputPlugin });
|
|
215
|
+
},
|
|
216
|
+
import: async (name, exportName) => {
|
|
217
|
+
const result = await import(name);
|
|
218
|
+
return result[exportName ?? 'default'];
|
|
219
|
+
},
|
|
220
|
+
globalImport: noop,
|
|
221
|
+
globalSetupt: noop
|
|
222
|
+
}, plugins];
|
|
202
223
|
}
|
|
203
224
|
|
|
204
|
-
function getHelpers(
|
|
205
|
-
function getGlobalName(anInput) {
|
|
206
|
-
return camelCase(path.join(
|
|
207
|
-
}
|
|
208
|
-
function getExternalGlobalName(id) {
|
|
209
|
-
if (path.isAbsolute(id)) {
|
|
210
|
-
return getGlobalName(path.relative(__dirname, id));
|
|
211
|
-
}
|
|
212
|
-
return camelCase(id);
|
|
213
|
-
}
|
|
214
|
-
return {
|
|
215
|
-
getGlobalName,
|
|
216
|
-
getExternalGlobalName
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
function toArray(object) {
|
|
220
|
-
if (Array.isArray(object)) {
|
|
221
|
-
return object;
|
|
222
|
-
}
|
|
223
|
-
if (object == null) {
|
|
224
|
-
return [];
|
|
225
|
-
}
|
|
226
|
-
return [object];
|
|
227
|
-
}
|
|
228
|
-
function formatInput(input) {
|
|
229
|
-
return (Array.isArray(input) ? input : [input
|
|
230
|
-
}
|
|
231
|
-
function formatOutput(output, field) {
|
|
232
|
-
//? can we avoid it
|
|
233
|
-
if (output == null) {
|
|
234
|
-
return '';
|
|
235
|
-
}
|
|
236
|
-
return (Array.isArray(output) ? output : [output
|
|
237
|
-
}
|
|
238
|
-
function getTimeDiff(starting) {
|
|
239
|
-
const now = Date.now();
|
|
240
|
-
if (now - starting > 1000) {
|
|
241
|
-
return `${((now - starting) / 1000).toFixed(1)}s`;
|
|
242
|
-
}
|
|
243
|
-
return `${now - starting}ms`;
|
|
244
|
-
}
|
|
225
|
+
function getHelpers(pkgName) {
|
|
226
|
+
function getGlobalName(anInput) {
|
|
227
|
+
return camelCase(path.join(pkgName, path.basename(anInput, '.ts') !== 'index' ? path.basename(anInput, '.ts') : ''));
|
|
228
|
+
}
|
|
229
|
+
function getExternalGlobalName(id) {
|
|
230
|
+
if (path.isAbsolute(id)) {
|
|
231
|
+
return getGlobalName(path.relative(__dirname, id));
|
|
232
|
+
}
|
|
233
|
+
return camelCase(id);
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
getGlobalName,
|
|
237
|
+
getExternalGlobalName
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function toArray(object) {
|
|
241
|
+
if (Array.isArray(object)) {
|
|
242
|
+
return object;
|
|
243
|
+
}
|
|
244
|
+
if (object == null) {
|
|
245
|
+
return [];
|
|
246
|
+
}
|
|
247
|
+
return [object];
|
|
248
|
+
}
|
|
249
|
+
function formatInput(input) {
|
|
250
|
+
return (Array.isArray(input) ? input : [input ?? '']).map(item => kleur.magenta(path.basename(item, '.ts'))).join(', ');
|
|
251
|
+
}
|
|
252
|
+
function formatOutput(output, field) {
|
|
253
|
+
//? can we avoid it
|
|
254
|
+
if (output == null) {
|
|
255
|
+
return '';
|
|
256
|
+
}
|
|
257
|
+
return (Array.isArray(output) ? output : [output ?? '']).map(item => kleur.cyan(item[field])).join(', ');
|
|
258
|
+
}
|
|
259
|
+
function getTimeDiff(starting) {
|
|
260
|
+
const now = Date.now();
|
|
261
|
+
if (now - starting > 1000) {
|
|
262
|
+
return `${((now - starting) / 1000).toFixed(1)}s`;
|
|
263
|
+
}
|
|
264
|
+
return `${now - starting}ms`;
|
|
265
|
+
}
|
|
245
266
|
const areSetsEqual = (a, b) => a.size === b.size ? [...a].every(value => b.has(value)) : false;
|
|
246
267
|
|
|
247
|
-
const fileNamePatterns = {
|
|
248
|
-
'es': '[name].mjs',
|
|
249
|
-
'cjs': '[name].cjs',
|
|
250
|
-
'umd': '[name].umd.js',
|
|
251
|
-
};
|
|
252
|
-
async function getRollupConfigs(inputs, config, helpers) {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
const
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
for (const input of
|
|
392
|
-
expanded.push(`${format}
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
268
|
+
const fileNamePatterns = {
|
|
269
|
+
'es': '[name].mjs',
|
|
270
|
+
'cjs': '[name].cjs',
|
|
271
|
+
'umd': '[name].umd.js',
|
|
272
|
+
};
|
|
273
|
+
async function getRollupConfigs([provider, plugins$1], inputs, config, helpers) {
|
|
274
|
+
for (const factory of plugins) {
|
|
275
|
+
await factory(provider, config, inputs);
|
|
276
|
+
}
|
|
277
|
+
const expandInputs = new Set;
|
|
278
|
+
for (const plugin of plugins$1) {
|
|
279
|
+
if (plugin.format && plugin.inputs?.length && !plugin.outputPlugin) {
|
|
280
|
+
for (const format of toArray(plugin.format)) {
|
|
281
|
+
expandInputs.add(format);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const refineNext = refiner();
|
|
286
|
+
refineNext(doExpandInputs(toArray(config.formats)));
|
|
287
|
+
for (const plugin of plugins$1) {
|
|
288
|
+
if (plugin.format && !plugin.outputPlugin) {
|
|
289
|
+
const formats = toArray(plugin.format);
|
|
290
|
+
if (!plugin.inputs || plugin.inputs.length === 0) {
|
|
291
|
+
refineNext(doExpandInputs(formats));
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
if (inputs.length === 1) {
|
|
295
|
+
refineNext(formats);
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
const expanded = [];
|
|
299
|
+
for (const format of formats) {
|
|
300
|
+
for (const input of plugin.inputs) {
|
|
301
|
+
expanded.push(`${format}.${input}`);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
refineNext(expanded);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const refined = refineNext();
|
|
310
|
+
const partitions = [];
|
|
311
|
+
for (const partition of refined) {
|
|
312
|
+
const result = [];
|
|
313
|
+
for (const format of partition) {
|
|
314
|
+
if (format.includes('.')) {
|
|
315
|
+
const [, realFormat, input] = format.split(/(.*?)\.(.*)/gm);
|
|
316
|
+
result.push({ format: realFormat, input });
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
result.push({ format });
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const mapFormatInputs = new Map;
|
|
323
|
+
const formatsWithoutInputs = new Set;
|
|
324
|
+
for (const { format, input } of result) {
|
|
325
|
+
if (input) {
|
|
326
|
+
if (mapFormatInputs.has(format)) {
|
|
327
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
328
|
+
mapFormatInputs.get(format).add(input);
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
mapFormatInputs.set(format, new Set([input]));
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
formatsWithoutInputs.add(format);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
for (const format of formatsWithoutInputs) {
|
|
339
|
+
if (mapFormatInputs.has(format)) {
|
|
340
|
+
throw new Error(`${format} is both used with inputs and without in plugins configuration and was not expanded / handled correctly. Please file an issue for pkgbld.`);
|
|
341
|
+
}
|
|
342
|
+
mapFormatInputs.set(format, new Set(inputs));
|
|
343
|
+
}
|
|
344
|
+
let prevInputs;
|
|
345
|
+
for (const inputs of mapFormatInputs.values()) {
|
|
346
|
+
if (prevInputs) {
|
|
347
|
+
if (!areSetsEqual(inputs, prevInputs)) {
|
|
348
|
+
throw new Error(`unbalanced inputs for partition: ${JSON.stringify(partition)}`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
prevInputs = inputs;
|
|
352
|
+
}
|
|
353
|
+
partitions.push({ formats: [...mapFormatInputs.keys()], inputs: [...prevInputs] });
|
|
354
|
+
}
|
|
355
|
+
return partitions.map(({ formats, inputs }) => {
|
|
356
|
+
return {
|
|
357
|
+
input: inputs,
|
|
358
|
+
output: formats.map(format => ({
|
|
359
|
+
format,
|
|
360
|
+
dir: config.dir,
|
|
361
|
+
entryFileNames: fileNamePatterns[format],
|
|
362
|
+
plugins: getPlugins([format], inputs, true),
|
|
363
|
+
sourcemap: config.sourcemapFormats.includes(format),
|
|
364
|
+
...getExtraOutputSettings(format, inputs)
|
|
365
|
+
})),
|
|
366
|
+
plugins: getPlugins(formats, inputs, false)
|
|
367
|
+
};
|
|
368
|
+
});
|
|
369
|
+
function getExtraOutputSettings(format, inputs) {
|
|
370
|
+
switch (format) {
|
|
371
|
+
case 'cjs':
|
|
372
|
+
case 'es':
|
|
373
|
+
return { chunkFileNames: fileNamePatterns[format] };
|
|
374
|
+
case 'umd':
|
|
375
|
+
if (inputs.length <= 0) {
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
if (inputs.length > 1) {
|
|
379
|
+
throw new Error(`Cannot produce global name for mutliple umd inputs in one output: ${inputs}`);
|
|
380
|
+
}
|
|
381
|
+
return {
|
|
382
|
+
name: helpers.getGlobalName(inputs[0]),
|
|
383
|
+
globals: helpers.getExternalGlobalName,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
return {};
|
|
387
|
+
}
|
|
388
|
+
function getPlugins(formats, inputs, outputPlugin) {
|
|
389
|
+
const filteredPlugins = [];
|
|
390
|
+
for (const plugin of plugins$1) {
|
|
391
|
+
if ((!!plugin.outputPlugin) === outputPlugin) {
|
|
392
|
+
if ((!plugin.format || toArray(plugin.format).some(format => formats.includes(format)))
|
|
393
|
+
&& (!plugin.inputs || plugin.inputs.every(input => inputs.includes(input)))) {
|
|
394
|
+
filteredPlugins.push({
|
|
395
|
+
instance: plugin.plugin(),
|
|
396
|
+
priority: plugin.priority
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
filteredPlugins.sort((a, b) => a.priority - b.priority);
|
|
402
|
+
return filteredPlugins.map(plugin => plugin.instance);
|
|
403
|
+
}
|
|
404
|
+
function doExpandInputs(formats) {
|
|
405
|
+
if (inputs.length === 1) {
|
|
406
|
+
return formats;
|
|
407
|
+
}
|
|
408
|
+
const expanded = [];
|
|
409
|
+
for (const format of formats) {
|
|
410
|
+
if (expandInputs.has(format)) {
|
|
411
|
+
if (format !== 'umd') {
|
|
412
|
+
for (const input of inputs) {
|
|
413
|
+
expanded.push(`${format}.${input}`);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
for (const input of config.umdInputs) {
|
|
418
|
+
expanded.push(`${format}../${config.sourceDir}/${input}.ts`);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
expanded.push(format);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return expanded;
|
|
427
|
+
}
|
|
402
428
|
}
|
|
403
429
|
|
|
404
430
|
const mainLoggerText = (sourceDir, dir, configsCount, startingTime, finishedCount = 0) => (final = false) => `${sourceDir} → ${dir} ${final ? configsCount : finishedCount++} / ${configsCount}${final ? (' in ' + getTimeDiff(startingTime)) : ''}`;
|
|
405
431
|
|
|
406
|
-
function processPackage(pkg, config) {
|
|
407
|
-
|
|
408
|
-
const
|
|
409
|
-
const
|
|
410
|
-
const
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
pkg.
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
config.bin
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
return input;
|
|
432
|
+
function processPackage(pkg, config) {
|
|
433
|
+
const input = [];
|
|
434
|
+
const logger$1 = logger.createLogger();
|
|
435
|
+
const allowEsm = (config.formatsOverriden && config.formats.includes('es') || !config.formatsOverriden);
|
|
436
|
+
const allowCjs = (config.formatsOverriden && config.formats.includes('cjs') || !config.formatsOverriden);
|
|
437
|
+
const allowUmd = (config.formatsOverriden && config.formats.includes('umd') || !config.formatsOverriden || config.umdInputs);
|
|
438
|
+
if (typeof pkg !== 'object' || Array.isArray(pkg)) {
|
|
439
|
+
logger$1.finish('expecting object on top level of package.json', 3 /* LogLevel.error */);
|
|
440
|
+
process.exit(-1);
|
|
441
|
+
}
|
|
442
|
+
if (typeof pkg?.name !== 'string') {
|
|
443
|
+
logger$1.finish('expecting name to be a string in package.json', 3 /* LogLevel.error */);
|
|
444
|
+
process.exit(-1);
|
|
445
|
+
}
|
|
446
|
+
if (!Array.isArray(pkg?.files)) {
|
|
447
|
+
pkg.files = [];
|
|
448
|
+
}
|
|
449
|
+
if (!pkg.files.includes(config.dir)) {
|
|
450
|
+
pkg.files.push(config.dir);
|
|
451
|
+
}
|
|
452
|
+
if (typeof pkg.exports !== 'object' && pkg.exports !== null) {
|
|
453
|
+
pkg.exports = {};
|
|
454
|
+
}
|
|
455
|
+
if (pkg.exports['.'] == null) {
|
|
456
|
+
pkg.exports['.'] = {};
|
|
457
|
+
}
|
|
458
|
+
pkg.exports['./package.json'] = './package.json';
|
|
459
|
+
if (allowUmd && typeof pkg.umd === 'string') {
|
|
460
|
+
pkg.umd = `./${config.dir}/index.umd.js`;
|
|
461
|
+
if (!config.umdInputs.includes('index')) {
|
|
462
|
+
config.umdInputs.push('index');
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (config.bin) {
|
|
466
|
+
if (config.bin.length > 0) {
|
|
467
|
+
if (config.bin[0] === '') {
|
|
468
|
+
delete pkg.bin;
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
pkg.bin = config.bin[0];
|
|
472
|
+
}
|
|
473
|
+
config.bin = config.bin.filter(Boolean);
|
|
474
|
+
if (config.bin.length === 0) {
|
|
475
|
+
delete config.bin;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
if (typeof pkg.bin === 'string') {
|
|
481
|
+
config.bin = [pkg.bin];
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
delete pkg.bin;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
if (allowCjs && typeof pkg.main !== 'string') {
|
|
488
|
+
pkg.main = `./${config.dir}/index.cjs`;
|
|
489
|
+
}
|
|
490
|
+
if (allowCjs && pkg.main !== pkg.exports['.'].require) {
|
|
491
|
+
pkg.exports['.'].require = pkg.main;
|
|
492
|
+
}
|
|
493
|
+
if (allowEsm && typeof pkg.module !== 'string') {
|
|
494
|
+
pkg.module = `./${config.dir}/index.mjs`;
|
|
495
|
+
}
|
|
496
|
+
if (allowEsm && pkg.module !== pkg.exports['.']?.default) {
|
|
497
|
+
pkg.exports['.'].default = pkg.module;
|
|
498
|
+
}
|
|
499
|
+
if (allowUmd && config.umdInputs.includes('index')) {
|
|
500
|
+
pkg.unpkg = `./${config.dir}/index.umd.js`;
|
|
501
|
+
}
|
|
502
|
+
for (const id in pkg.exports) {
|
|
503
|
+
if (id === './package.json')
|
|
504
|
+
continue;
|
|
505
|
+
const basename = id == '.' ? 'index' : path.basename(id);
|
|
506
|
+
if (typeof pkg.exports[id] !== 'object') {
|
|
507
|
+
pkg.exports[id] = {};
|
|
508
|
+
}
|
|
509
|
+
if (allowCjs) {
|
|
510
|
+
pkg.exports[id].require = `./${config.dir}/${basename}.cjs`;
|
|
511
|
+
}
|
|
512
|
+
if (allowEsm) {
|
|
513
|
+
pkg.exports[id].default = `./${config.dir}/${basename}.mjs`;
|
|
514
|
+
}
|
|
515
|
+
if (basename !== 'index') {
|
|
516
|
+
if (!pkg.files.includes(basename)) {
|
|
517
|
+
pkg.files.push(basename);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
input.push(`./${config.sourceDir}/${basename}.ts`);
|
|
521
|
+
}
|
|
522
|
+
if (allowUmd && config.umdInputs.length > 0 && !config.formats.includes('umd')) {
|
|
523
|
+
config.formats.push('umd');
|
|
524
|
+
}
|
|
525
|
+
return input;
|
|
501
526
|
}
|
|
502
527
|
|
|
503
|
-
async function
|
|
504
|
-
await fs.writeFile(
|
|
528
|
+
async function writeJson(path, json) {
|
|
529
|
+
await fs.writeFile(path, JSON.stringify(json, null, 2) + '\n');
|
|
505
530
|
}
|
|
506
531
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
const
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
}
|
|
532
|
+
const imports = new Map;
|
|
533
|
+
const setup = new Set;
|
|
534
|
+
let generate, generateGlobals;
|
|
535
|
+
async function createEjectProvider() {
|
|
536
|
+
const createMockProvider = (await import('@slimlib/smart-mock')).default;
|
|
537
|
+
const provider = createMockProvider();
|
|
538
|
+
const createMock = provider.createMock;
|
|
539
|
+
generate = provider.generate;
|
|
540
|
+
generateGlobals = provider.generateGlobals;
|
|
541
|
+
const plugins = [];
|
|
542
|
+
return [{
|
|
543
|
+
provide: (plugin, priority, options) => {
|
|
544
|
+
plugins.push({ priority, plugin, format: options?.format, inputs: options?.inputs, outputPlugin: options?.outputPlugin });
|
|
545
|
+
},
|
|
546
|
+
import: async (name, exportName) => {
|
|
547
|
+
const result = await import(name);
|
|
548
|
+
const exports = result[exportName ?? 'default'];
|
|
549
|
+
const mangledName = lodash.camelCase(name);
|
|
550
|
+
imports.set(name, mangledName);
|
|
551
|
+
return createMock(exports, mangledName);
|
|
552
|
+
},
|
|
553
|
+
globalImport: (module, exportName) => {
|
|
554
|
+
imports.set(module, exportName ?? 'default');
|
|
555
|
+
},
|
|
556
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
557
|
+
globalSetupt: (code) => {
|
|
558
|
+
if (typeof code === 'function') {
|
|
559
|
+
setup.add(code.toString());
|
|
560
|
+
}
|
|
561
|
+
setup.add(String(code));
|
|
562
|
+
}
|
|
563
|
+
}, plugins];
|
|
564
|
+
}
|
|
565
|
+
async function ejectConfig(config, pkgPath, options, inputs, helpers, pkg) {
|
|
566
|
+
const pkgName = pkg.name;
|
|
567
|
+
// generate from config
|
|
568
|
+
const text = generate(config);
|
|
569
|
+
setup.add(generateGlobals());
|
|
570
|
+
// generate globals for config functions
|
|
571
|
+
setup.add(`const config = ${generate(options)}`);
|
|
572
|
+
setup.add(`const inputs = ${generate(inputs)}`);
|
|
573
|
+
// generate helpers code
|
|
574
|
+
if (options.formats.includes('umd')) {
|
|
575
|
+
imports.set('path', 'path');
|
|
576
|
+
imports.set('lodash/camelCase', 'camelCase');
|
|
577
|
+
setup.add(`const pkgName = ${generate(pkgName)}`);
|
|
578
|
+
setup.add(helpers.getGlobalName.toString());
|
|
579
|
+
}
|
|
580
|
+
// imports
|
|
581
|
+
const importsString = Array.from(imports)
|
|
582
|
+
.map((value) => `import ${value[1]} from '${value[0]}';`)
|
|
583
|
+
.join('\n');
|
|
584
|
+
const setupString = Array.from(setup)
|
|
585
|
+
.join('\n');
|
|
586
|
+
const { minify } = await import('terser');
|
|
587
|
+
const result = await minify(`${importsString}\n${setupString}\nexport default ${text};`, {
|
|
588
|
+
module: true,
|
|
589
|
+
compress: {
|
|
590
|
+
booleans: false,
|
|
591
|
+
ecma: 2020,
|
|
592
|
+
module: true,
|
|
593
|
+
passes: 3,
|
|
594
|
+
unsafe: true
|
|
595
|
+
},
|
|
596
|
+
mangle: false,
|
|
597
|
+
output: {
|
|
598
|
+
beautify: true,
|
|
599
|
+
ecma: 2020,
|
|
600
|
+
quote_style: 1
|
|
601
|
+
}
|
|
602
|
+
});
|
|
603
|
+
await fs.writeFile(path.join(path.dirname(pkgPath), 'rollup.config.mjs'), result.code);
|
|
604
|
+
// this can be improved a lot
|
|
605
|
+
if (typeof pkg.devDependencies !== 'object') {
|
|
606
|
+
pkg.devDependencies = {};
|
|
607
|
+
}
|
|
608
|
+
const devDependencies = pkg.devDependencies;
|
|
609
|
+
if ('pkgbld' in devDependencies) {
|
|
610
|
+
delete devDependencies.pkgbld;
|
|
611
|
+
}
|
|
612
|
+
devDependencies['rollup'] = '*';
|
|
613
|
+
const isBuiltin = (await import('is-builtin-module')).default;
|
|
614
|
+
for (const key of imports.keys()) {
|
|
615
|
+
if (!isBuiltin(key)) {
|
|
616
|
+
devDependencies[key] = '*';
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
const defaultTsConfig = {
|
|
622
|
+
include: ['src', 'types'],
|
|
623
|
+
compilerOptions: {
|
|
624
|
+
lib: ['dom', 'esnext'],
|
|
625
|
+
target: 'esnext',
|
|
626
|
+
module: 'esnext',
|
|
627
|
+
esModuleInterop: true,
|
|
628
|
+
strict: true,
|
|
629
|
+
declaration: true,
|
|
630
|
+
moduleResolution: 'node'
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
async function checkTsConfig(options, mainLogger) {
|
|
634
|
+
if (options.noTsConfig) {
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
const tsConfigPath = path.resolve('tsconfig.json');
|
|
638
|
+
if (fs$1.existsSync(tsConfigPath)) {
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
mainLogger('no tsconfig.json and --no-ts-config not specified, writing tsconfig...');
|
|
642
|
+
await writeJson(tsConfigPath, defaultTsConfig);
|
|
643
|
+
mainLogger('done');
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
async function execute() {
|
|
647
|
+
const time = Date.now();
|
|
648
|
+
logger.createLogger().update(' '); // blank line
|
|
649
|
+
const mainLogger = logger.createLogger();
|
|
650
|
+
mainLogger.update('preparing...');
|
|
651
|
+
try {
|
|
652
|
+
const [pkgPath, pkg] = await getPackage();
|
|
653
|
+
const options = getCliOptions();
|
|
654
|
+
checkTsConfig(options, mainLogger);
|
|
655
|
+
const inputs = processPackage(pkg, options);
|
|
656
|
+
const helpers = getHelpers(pkg.name);
|
|
657
|
+
const provider = options.eject ? await createEjectProvider() : createProvider();
|
|
658
|
+
const rollupConfigs = await getRollupConfigs(provider, inputs, options, helpers);
|
|
659
|
+
if (options.eject) {
|
|
660
|
+
await ejectConfig(rollupConfigs, pkgPath, options, inputs, helpers, pkg);
|
|
661
|
+
mainLogger.finish(`ejected config in ${getTimeDiff(time)}`);
|
|
662
|
+
if (!options.noUpdatePackageJson) {
|
|
663
|
+
await writeJson(pkgPath, pkg);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
else {
|
|
667
|
+
const updater = mainLoggerText(options.sourceDir, options.dir, rollupConfigs.length, time);
|
|
668
|
+
mainLogger.start(updater());
|
|
669
|
+
await Promise.all(rollupConfigs.map(config => buildConfig(config, updater)));
|
|
670
|
+
if (!options.noUpdatePackageJson) {
|
|
671
|
+
await writeJson(pkgPath, pkg);
|
|
672
|
+
}
|
|
673
|
+
await createSubpackages(inputs, options);
|
|
674
|
+
mainLogger.finish(updater(true));
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
catch (e) {
|
|
678
|
+
console.log(e);
|
|
679
|
+
mainLogger.finish(`${e}}`, 3 /* LogLevel.error */);
|
|
680
|
+
process.exit(-1);
|
|
681
|
+
}
|
|
682
|
+
async function buildConfig(config, updater) {
|
|
683
|
+
const bundle = await rollup.rollup(config);
|
|
684
|
+
await Promise.all(toArray(config.output).map(config => bundle.write(config)));
|
|
685
|
+
await bundle.close();
|
|
686
|
+
mainLogger(`${kleur.green('✓')} ${formatInput(config.input)} [${formatOutput(config.output, 'format')}]`);
|
|
687
|
+
mainLogger.update(updater());
|
|
688
|
+
}
|
|
689
|
+
}
|
|
538
690
|
execute();
|