pkgbld 1.29.4 → 1.30.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 +32 -4
- package/dist/index.d.ts +6 -1
- package/dist/index.mjs +228 -54
- package/package.json +2 -1
- package/dist/index.d.ts.map +0 -26
package/README.md
CHANGED
|
@@ -186,13 +186,31 @@ Do not setup pack script in package.json
|
|
|
186
186
|
pkgbld --no-pack
|
|
187
187
|
```
|
|
188
188
|
|
|
189
|
+
### no-exports
|
|
190
|
+
|
|
191
|
+
```
|
|
192
|
+
pkgbld --no-exports
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Do not add exports field in package.json.
|
|
196
|
+
|
|
189
197
|
### prune (command)
|
|
190
198
|
|
|
191
199
|
```
|
|
192
200
|
pkgbld prune
|
|
193
201
|
```
|
|
194
202
|
|
|
195
|
-
prune devDependencies and
|
|
203
|
+
prune devDependencies and redundant scripts from package.json
|
|
204
|
+
|
|
205
|
+
### prune --profile=<profile>
|
|
206
|
+
|
|
207
|
+
There are two profiles: `library` and `app`. `library` is default.
|
|
208
|
+
|
|
209
|
+
Right now it only affects how `prune` command removes entries in the `scripts` field.
|
|
210
|
+
|
|
211
|
+
For `library` profile it retains: 'preinstall', 'install', 'postinstall', 'prepublish', 'preprepare', 'prepare', 'postprepare'.
|
|
212
|
+
|
|
213
|
+
For `app` profile it retains in addition: 'prestart', 'start', 'poststart', 'prerestart', 'restart', 'postrestart', 'prestop', 'stop', 'poststop', 'pretest', 'test', 'posttest'.
|
|
196
214
|
|
|
197
215
|
### flatten
|
|
198
216
|
|
|
@@ -206,13 +224,23 @@ If the directory is not specified it is guessed from package.json.
|
|
|
206
224
|
|
|
207
225
|
If files cannot be copied because of name conflicts the command will fail.
|
|
208
226
|
|
|
209
|
-
###
|
|
227
|
+
### removeSourcemaps
|
|
210
228
|
|
|
211
229
|
```
|
|
212
|
-
pkgbld --
|
|
230
|
+
pkgbld prune --removeSourcemaps
|
|
213
231
|
```
|
|
214
232
|
|
|
215
|
-
|
|
233
|
+
Removes all sourcemaps from the package. The logic is very simple and removes all files with `.map` extension and references in format `//# sourceMappingURL=<mapFile>`.
|
|
234
|
+
|
|
235
|
+
### optimizeFiles (default)
|
|
236
|
+
|
|
237
|
+
```
|
|
238
|
+
pkgbld prune --optimizeFiles=false
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Optimizes files by removing all files that are not required for pack at the given moment.
|
|
242
|
+
|
|
243
|
+
You might want to disable this option in some edge cases.
|
|
216
244
|
|
|
217
245
|
## Plugin API
|
|
218
246
|
|
package/dist/index.d.ts
CHANGED
|
@@ -40,6 +40,8 @@ declare module 'pkgbld' {
|
|
|
40
40
|
private?: boolean;
|
|
41
41
|
version?: string;
|
|
42
42
|
name?: string;
|
|
43
|
+
bin?: string | Record<string, string>;
|
|
44
|
+
main?: string;
|
|
43
45
|
license?: string;
|
|
44
46
|
readme?: string;
|
|
45
47
|
author?: string | {
|
|
@@ -84,11 +86,14 @@ declare module 'pkgbld' {
|
|
|
84
86
|
formatPackageJson: boolean;
|
|
85
87
|
noPack: boolean;
|
|
86
88
|
noExports: boolean;
|
|
89
|
+
noClean: boolean;
|
|
90
|
+
noBundle: boolean;
|
|
87
91
|
} | {
|
|
88
92
|
readonly kind: "prune";
|
|
89
93
|
readonly profile: string;
|
|
90
94
|
readonly flatten: string | boolean;
|
|
95
|
+
readonly removeSourcemaps: boolean;
|
|
96
|
+
readonly optimizeFiles: boolean;
|
|
91
97
|
};
|
|
92
98
|
}
|
|
93
99
|
|
|
94
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import '@niceties/draftlog-appender';
|
|
2
2
|
import { createLogger } from '@niceties/logger';
|
|
3
3
|
import { rollup } from 'rollup';
|
|
4
|
-
import fs, { access,
|
|
4
|
+
import fs, { access, readFile, writeFile, rm, readdir, mkdir, rename, stat } from 'fs/promises';
|
|
5
5
|
import path from 'path';
|
|
6
6
|
import { cli, command } from 'cleye';
|
|
7
7
|
import refiner from '@slimlib/refine-partition';
|
|
@@ -148,6 +148,16 @@ const cliFlags = {
|
|
|
148
148
|
description: 'Do not add exports field in package.json',
|
|
149
149
|
default: false
|
|
150
150
|
},
|
|
151
|
+
noClean: {
|
|
152
|
+
type: Boolean,
|
|
153
|
+
description: 'Do not clean output directory',
|
|
154
|
+
default: false
|
|
155
|
+
},
|
|
156
|
+
noBundle: {
|
|
157
|
+
type: Boolean,
|
|
158
|
+
description: 'Do not bundle',
|
|
159
|
+
default: false
|
|
160
|
+
},
|
|
151
161
|
};
|
|
152
162
|
const packageJsonFieldsOrder = new Set([
|
|
153
163
|
'private',
|
|
@@ -232,7 +242,7 @@ function getCliOptions(plugins, pkg) {
|
|
|
232
242
|
commands: [
|
|
233
243
|
command({
|
|
234
244
|
name: 'prune',
|
|
235
|
-
description: 'prune devDependencies and
|
|
245
|
+
description: 'prune devDependencies and redundant scripts from package.json',
|
|
236
246
|
flags: {
|
|
237
247
|
profile: {
|
|
238
248
|
type: String,
|
|
@@ -243,6 +253,16 @@ function getCliOptions(plugins, pkg) {
|
|
|
243
253
|
type: FlattenParam,
|
|
244
254
|
description: 'flatten package files',
|
|
245
255
|
default: false
|
|
256
|
+
},
|
|
257
|
+
removeSourcemaps: {
|
|
258
|
+
type: Boolean,
|
|
259
|
+
description: 'remove sourcemaps',
|
|
260
|
+
default: false
|
|
261
|
+
},
|
|
262
|
+
optimizeFiles: {
|
|
263
|
+
type: Boolean,
|
|
264
|
+
description: 'optimize files array',
|
|
265
|
+
default: true
|
|
246
266
|
}
|
|
247
267
|
}
|
|
248
268
|
})
|
|
@@ -252,7 +272,9 @@ function getCliOptions(plugins, pkg) {
|
|
|
252
272
|
return {
|
|
253
273
|
kind: 'prune',
|
|
254
274
|
profile: cliOptions.flags.profile,
|
|
255
|
-
flatten: cliOptions.flags.flatten
|
|
275
|
+
flatten: cliOptions.flags.flatten,
|
|
276
|
+
removeSourcemaps: cliOptions.flags.removeSourcemaps,
|
|
277
|
+
optimizeFiles: cliOptions.flags.optimizeFiles
|
|
256
278
|
};
|
|
257
279
|
}
|
|
258
280
|
else {
|
|
@@ -277,7 +299,9 @@ function getCliOptions(plugins, pkg) {
|
|
|
277
299
|
umdPattern: flags.umdPattern,
|
|
278
300
|
formatPackageJson: flags.formatPackageJson,
|
|
279
301
|
noPack: flags.noPack,
|
|
280
|
-
noExports: flags.noExports
|
|
302
|
+
noExports: flags.noExports,
|
|
303
|
+
noClean: flags.noClean,
|
|
304
|
+
noBundle: flags.noBundle
|
|
281
305
|
};
|
|
282
306
|
for (const plugin of plugins) {
|
|
283
307
|
plugin.options?.(flags, options);
|
|
@@ -303,7 +327,10 @@ const Priority = {
|
|
|
303
327
|
finalize: 20000
|
|
304
328
|
};
|
|
305
329
|
|
|
306
|
-
async function clean (provider) {
|
|
330
|
+
async function clean (provider, config) {
|
|
331
|
+
if (config.noClean) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
307
334
|
const pluginClean = await provider.import('@rollup-extras/plugin-clean');
|
|
308
335
|
const pluginInstance = pluginClean();
|
|
309
336
|
provider.provide(pluginFactory, Priority.cleanup, { outputPlugin: true });
|
|
@@ -508,11 +535,8 @@ function formatOutput(output, field) {
|
|
|
508
535
|
return (Array.isArray(output) ? output : [output ?? '']).map(item => kleur.cyan(item[field])).join(', ');
|
|
509
536
|
}
|
|
510
537
|
function getTimeDiff(starting) {
|
|
511
|
-
const
|
|
512
|
-
|
|
513
|
-
return `${((now - starting) / 1000).toFixed(1)}s`;
|
|
514
|
-
}
|
|
515
|
-
return `${now - starting}ms`;
|
|
538
|
+
const diff = Date.now() - starting;
|
|
539
|
+
return diff >= 1000 ? `${(diff / 1000).toFixed(1)}s` : `${diff}ms`;
|
|
516
540
|
}
|
|
517
541
|
const areSetsEqual = (a, b) => a.size === b.size ? [...a].every(value => b.has(value)) : false;
|
|
518
542
|
function formatPackageJson(pkg) {
|
|
@@ -647,7 +671,7 @@ async function getRollupConfigs([provider, plugins$1], inputs, inputsExt, config
|
|
|
647
671
|
break;
|
|
648
672
|
}
|
|
649
673
|
if (inputs.length > 1) {
|
|
650
|
-
throw new Error(`Cannot produce global name for
|
|
674
|
+
throw new Error(`Cannot produce global name for multiple umd inputs in one output: ${inputs}`);
|
|
651
675
|
}
|
|
652
676
|
result = {
|
|
653
677
|
name: helpers.getGlobalName(inputs.join('_')),
|
|
@@ -721,15 +745,15 @@ async function processPackage(pkg, config, plugins, tsConfig) {
|
|
|
721
745
|
const allowEsm = (config.formatsOverridden && config.formats.includes('es') || !config.formatsOverridden);
|
|
722
746
|
const allowCjs = (config.formatsOverridden && config.formats.includes('cjs') || !config.formatsOverridden);
|
|
723
747
|
const allowUmd = (config.formatsOverridden && config.formats.includes('umd') || !config.formatsOverridden || config.umdInputs);
|
|
724
|
-
if (typeof pkg !== 'object' || Array.isArray(pkg)) {
|
|
748
|
+
if (typeof pkg !== 'object' || Array.isArray(pkg) || pkg == null) {
|
|
725
749
|
logger.finish('expecting object on top level of package.json', 3 /* LogLevel.error */);
|
|
726
750
|
process.exit(-1);
|
|
727
751
|
}
|
|
728
|
-
if (typeof pkg
|
|
752
|
+
if (typeof pkg.name !== 'string' && config.umdInputs.length > 0) {
|
|
729
753
|
logger.finish('expecting name to be a string in package.json', 3 /* LogLevel.error */);
|
|
730
754
|
process.exit(-1);
|
|
731
755
|
}
|
|
732
|
-
if (!Array.isArray(pkg
|
|
756
|
+
if (!Array.isArray(pkg.files)) {
|
|
733
757
|
pkg.files = [];
|
|
734
758
|
}
|
|
735
759
|
if (!pkg.files.includes(config.dir)) {
|
|
@@ -912,11 +936,12 @@ async function writeJson(path, json) {
|
|
|
912
936
|
await fs.writeFile(path, toFormattedJson(json));
|
|
913
937
|
}
|
|
914
938
|
|
|
915
|
-
var version = "1.
|
|
939
|
+
var version = "1.30.1";
|
|
916
940
|
var name = "pkgbld";
|
|
917
941
|
var license = "MIT";
|
|
918
942
|
var author = "Konstantin Shutkin";
|
|
919
943
|
var bin = "./index.js";
|
|
944
|
+
var type = "module";
|
|
920
945
|
var main = "./dist/index.mjs";
|
|
921
946
|
var types = "./dist/index.d.ts";
|
|
922
947
|
var files = [
|
|
@@ -942,7 +967,8 @@ var keywords = [
|
|
|
942
967
|
var scripts = {
|
|
943
968
|
build: "xc6 rm src/options-types.ts && xc6 ln ../options/src/types.ts src/options-types.ts && rollup -c && dts-buddy dist/index.d.ts -m pkgbld:dist/src/index.d.ts && xc6 rm dist/src",
|
|
944
969
|
lint: "eslint ./src",
|
|
945
|
-
prepack: "node ./index.js prune"
|
|
970
|
+
prepack: "node ./index.js prune --removeSourcemaps",
|
|
971
|
+
test: "c8 --src=. --all -r=html node --env-file=ci.env tests/test.js"
|
|
946
972
|
};
|
|
947
973
|
var dependencies = {
|
|
948
974
|
"@niceties/logger": "^1.1.12",
|
|
@@ -968,11 +994,12 @@ var dependencies = {
|
|
|
968
994
|
};
|
|
969
995
|
var devDependencies = {
|
|
970
996
|
"@types/lodash": "^4.14.202",
|
|
971
|
-
"rollup-plugin-dts": "^6.1.0",
|
|
972
997
|
"@total-typescript/ts-reset": "^0.5.1",
|
|
973
|
-
"dts-buddy": "^0.4.
|
|
998
|
+
"dts-buddy": "^0.4.5",
|
|
974
999
|
options: "workspace:*",
|
|
975
|
-
xc6: "workspace:*"
|
|
1000
|
+
xc6: "workspace:*",
|
|
1001
|
+
c8: "^9.1.0",
|
|
1002
|
+
"cli-test-helper": "workspace:*"
|
|
976
1003
|
};
|
|
977
1004
|
var peerDependencies = {
|
|
978
1005
|
typescript: ">=5.3.3"
|
|
@@ -983,6 +1010,7 @@ var pkgbldPkg = {
|
|
|
983
1010
|
license: license,
|
|
984
1011
|
author: author,
|
|
985
1012
|
bin: bin,
|
|
1013
|
+
type: type,
|
|
986
1014
|
main: main,
|
|
987
1015
|
types: types,
|
|
988
1016
|
files: files,
|
|
@@ -1107,6 +1135,7 @@ const defaultTsConfig = {
|
|
|
1107
1135
|
allowJs: true,
|
|
1108
1136
|
skipLibCheck: true,
|
|
1109
1137
|
strict: true,
|
|
1138
|
+
sourceMap: true,
|
|
1110
1139
|
noUncheckedIndexedAccess: true,
|
|
1111
1140
|
declaration: true,
|
|
1112
1141
|
moduleResolution: 'node'
|
|
@@ -1160,9 +1189,6 @@ async function loadPlugins(pkg) {
|
|
|
1160
1189
|
}
|
|
1161
1190
|
|
|
1162
1191
|
async function prunePkg(pkg, options, logger) {
|
|
1163
|
-
if (options.kind !== 'prune') {
|
|
1164
|
-
throw new Error('prunePkg should only be called in prune mode');
|
|
1165
|
-
}
|
|
1166
1192
|
const scriptsToKeep = getScriptsData();
|
|
1167
1193
|
const keys = scriptsToKeep[options.profile];
|
|
1168
1194
|
if (!keys) {
|
|
@@ -1170,24 +1196,128 @@ async function prunePkg(pkg, options, logger) {
|
|
|
1170
1196
|
}
|
|
1171
1197
|
delete pkg.devDependencies;
|
|
1172
1198
|
delete pkg['packageManager'];
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1199
|
+
if (pkg.scripts) {
|
|
1200
|
+
for (const key of Object.keys(pkg.scripts)) {
|
|
1201
|
+
if (!keys.has(key)) {
|
|
1202
|
+
delete pkg.scripts[key];
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
if (Object.keys(pkg.scripts).length === 0) {
|
|
1206
|
+
delete pkg.scripts;
|
|
1176
1207
|
}
|
|
1177
|
-
}
|
|
1178
|
-
if (Object.keys(pkg.scripts).length === 0) {
|
|
1179
|
-
delete pkg.scripts;
|
|
1180
1208
|
}
|
|
1181
1209
|
if (options.flatten) {
|
|
1182
1210
|
await flatten(pkg, options.flatten, logger);
|
|
1183
1211
|
}
|
|
1212
|
+
if (options.removeSourcemaps) {
|
|
1213
|
+
const sourceMaps = await walkDir('.').then(files => files.filter(file => file.endsWith('.map')));
|
|
1214
|
+
for (const sourceMap of sourceMaps) {
|
|
1215
|
+
// find corresponding file
|
|
1216
|
+
const sourceFile = sourceMap.slice(0, -4);
|
|
1217
|
+
// load file
|
|
1218
|
+
const sourceFileContent = await readFile(sourceFile, 'utf8');
|
|
1219
|
+
// find sourceMappingURL
|
|
1220
|
+
const sourceMappingUrl = `//# sourceMappingURL=${path.basename(sourceMap)}`;
|
|
1221
|
+
// remove sourceMappingURL
|
|
1222
|
+
const newContent = sourceFileContent.replace(sourceMappingUrl, '');
|
|
1223
|
+
// write file
|
|
1224
|
+
await writeFile(sourceFile, newContent, 'utf8');
|
|
1225
|
+
// remove sourceMap
|
|
1226
|
+
await rm(sourceMap);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
if (pkg.files && Array.isArray(pkg.files) && options.optimizeFiles) {
|
|
1230
|
+
const filterFiles = ['package.json'];
|
|
1231
|
+
const specialFiles = ['README', 'LICENSE', 'LICENCE'];
|
|
1232
|
+
if (pkg.main && typeof pkg.main === 'string') {
|
|
1233
|
+
filterFiles.push(normalizePath(pkg.main));
|
|
1234
|
+
}
|
|
1235
|
+
if (pkg.bin) {
|
|
1236
|
+
if (typeof pkg.bin === 'string') {
|
|
1237
|
+
filterFiles.push(normalizePath(pkg.bin));
|
|
1238
|
+
}
|
|
1239
|
+
if (typeof pkg.bin === 'object' && pkg.bin !== null) {
|
|
1240
|
+
filterFiles.push(...Object.values(pkg.bin).map(normalizePath));
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
const depthToFiles = new Map();
|
|
1244
|
+
for (const file of pkg.files.concat(filterFiles)) {
|
|
1245
|
+
const dirname = path.dirname(file);
|
|
1246
|
+
const depth = dirname.split('/').length;
|
|
1247
|
+
if (!depthToFiles.has(depth)) {
|
|
1248
|
+
depthToFiles.set(depth, [file]);
|
|
1249
|
+
}
|
|
1250
|
+
else {
|
|
1251
|
+
depthToFiles.get(depth)?.push(file);
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
// walk depth keys from the highest to the lowest
|
|
1255
|
+
const maxDepth = Math.max(...depthToFiles.keys());
|
|
1256
|
+
for (let depth = maxDepth; depth > 0; --depth) {
|
|
1257
|
+
const files = depthToFiles.get(depth);
|
|
1258
|
+
const mapDirToFiles = new Map();
|
|
1259
|
+
for (const file of files) {
|
|
1260
|
+
const dirname = path.dirname(file);
|
|
1261
|
+
const basename = normalizePath(path.basename(file));
|
|
1262
|
+
if (!mapDirToFiles.has(dirname)) {
|
|
1263
|
+
mapDirToFiles.set(dirname, [basename]);
|
|
1264
|
+
}
|
|
1265
|
+
else {
|
|
1266
|
+
mapDirToFiles.get(dirname)?.push(basename);
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
for (const [dirname, filesInDir] of mapDirToFiles) {
|
|
1270
|
+
// find out real content of the directory
|
|
1271
|
+
const realFiles = await readdir(dirname);
|
|
1272
|
+
// check if all files in the directory are in the filesInDir
|
|
1273
|
+
const allFilesInDir = realFiles.every(file => filesInDir.includes(file)) || realFiles.length === 0;
|
|
1274
|
+
if (allFilesInDir && dirname !== '.') {
|
|
1275
|
+
if (!depthToFiles.has(depth - 1)) {
|
|
1276
|
+
depthToFiles.set(depth - 1, [dirname]);
|
|
1277
|
+
}
|
|
1278
|
+
else {
|
|
1279
|
+
depthToFiles.get(depth - 1).push(dirname);
|
|
1280
|
+
}
|
|
1281
|
+
const thisDepth = depthToFiles.get(depth);
|
|
1282
|
+
depthToFiles.set(depth, thisDepth.filter(file => filesInDir.every(fileInDir => path.join(dirname, fileInDir) !== file)));
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
pkg.files = [...new Set(Array.from(depthToFiles.values()).flat())];
|
|
1287
|
+
pkg.files = pkg.files.filter(file => {
|
|
1288
|
+
const fileNormalized = normalizePath(file);
|
|
1289
|
+
const dirname = path.dirname(fileNormalized);
|
|
1290
|
+
const basenameWithoutExtension = path.basename(fileNormalized, path.extname(fileNormalized)).toUpperCase();
|
|
1291
|
+
return !filterFiles.includes(fileNormalized) && (dirname !== '' && dirname !== '.' || !specialFiles.includes(basenameWithoutExtension));
|
|
1292
|
+
});
|
|
1293
|
+
const ignoreDirs = [];
|
|
1294
|
+
for (const fileOrDir of pkg.files) {
|
|
1295
|
+
if (await isDirectory(fileOrDir)) {
|
|
1296
|
+
const allFiles = await walkDir(fileOrDir);
|
|
1297
|
+
if (allFiles.every(file => {
|
|
1298
|
+
const fileNormalized = normalizePath(file);
|
|
1299
|
+
return filterFiles.includes(fileNormalized);
|
|
1300
|
+
})) {
|
|
1301
|
+
ignoreDirs.push(fileOrDir);
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
pkg.files = pkg.files.filter(dir => !ignoreDirs.includes(dir));
|
|
1306
|
+
if (pkg.files.length === 0) {
|
|
1307
|
+
delete pkg.files;
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1184
1310
|
}
|
|
1185
1311
|
async function flatten(pkg, flatten, logger) {
|
|
1186
1312
|
const { default: jsonata } = await import('jsonata');
|
|
1187
1313
|
// find out where is the dist folder
|
|
1188
|
-
const expression = jsonata('[bin, main, module, unpkg, umd, types, typings, exports[].*.*, typesVersions
|
|
1314
|
+
const expression = jsonata('[bin, bin.*, main, module, unpkg, umd, types, typings, exports[].*.*, typesVersions.*.*, directories.bin]');
|
|
1189
1315
|
const allReferences = (await expression.evaluate(pkg));
|
|
1190
1316
|
let distDir;
|
|
1317
|
+
// at this point we requested directories.bin, but it is the only one that is directory and not a file
|
|
1318
|
+
// later when we get dirname we can't flatten directories.bin completely
|
|
1319
|
+
// it is easy to fix by checking element is a directory but it is kind of good
|
|
1320
|
+
// to have it as a separate directory, but user still can flatten it by specifying the directory
|
|
1191
1321
|
if (flatten === true) {
|
|
1192
1322
|
let commonSegments;
|
|
1193
1323
|
for (const entry of allReferences) {
|
|
@@ -1211,7 +1341,7 @@ async function flatten(pkg, flatten, logger) {
|
|
|
1211
1341
|
distDir = commonSegments?.join('/');
|
|
1212
1342
|
}
|
|
1213
1343
|
else {
|
|
1214
|
-
distDir = flatten;
|
|
1344
|
+
distDir = normalizePath(flatten);
|
|
1215
1345
|
}
|
|
1216
1346
|
if (!distDir) {
|
|
1217
1347
|
throw new Error('could not find dist folder');
|
|
@@ -1231,6 +1361,33 @@ async function flatten(pkg, flatten, logger) {
|
|
|
1231
1361
|
if (filesAlreadyExist.length) {
|
|
1232
1362
|
throw new Error(`dist folder cannot be flattened because files already exist: ${filesAlreadyExist.join(', ')}`);
|
|
1233
1363
|
}
|
|
1364
|
+
if (typeof flatten === 'string' && 'directories' in pkg && pkg.directories != null
|
|
1365
|
+
&& typeof pkg.directories === 'object' && 'bin' in pkg.directories
|
|
1366
|
+
&& typeof pkg.directories.bin === 'string' && normalizePath(pkg.directories.bin) === normalizePath(flatten)) {
|
|
1367
|
+
delete pkg.directories.bin;
|
|
1368
|
+
if (Object.keys(pkg.directories).length === 0) {
|
|
1369
|
+
delete pkg.directories;
|
|
1370
|
+
}
|
|
1371
|
+
const files = await readdir(flatten);
|
|
1372
|
+
if (files.length === 1) {
|
|
1373
|
+
const file = files[0];
|
|
1374
|
+
pkg.bin = file;
|
|
1375
|
+
}
|
|
1376
|
+
else {
|
|
1377
|
+
pkg.bin = {};
|
|
1378
|
+
for (const file of files) {
|
|
1379
|
+
pkg.bin[path.basename(file, path.extname(file))] = file;
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
// create new directory structure
|
|
1384
|
+
const mkdirPromises = [];
|
|
1385
|
+
for (const file of filesInDist) {
|
|
1386
|
+
// check file is not in root dir
|
|
1387
|
+
const relativePath = path.relative(relativeDistDir, file);
|
|
1388
|
+
mkdirPromises.push(mkdir(path.dirname(relativePath), { recursive: true }));
|
|
1389
|
+
}
|
|
1390
|
+
await Promise.all(mkdirPromises);
|
|
1234
1391
|
// move files to root dir (rename)
|
|
1235
1392
|
const renamePromises = [];
|
|
1236
1393
|
const newFiles = [];
|
|
@@ -1241,30 +1398,31 @@ async function flatten(pkg, flatten, logger) {
|
|
|
1241
1398
|
renamePromises.push(rename(file, relativePath));
|
|
1242
1399
|
}
|
|
1243
1400
|
await Promise.all(renamePromises);
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
await rm(
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1401
|
+
let cleanedDir = relativeDistDir;
|
|
1402
|
+
while (isEmptyDir(cleanedDir)) {
|
|
1403
|
+
await rm(cleanedDir, { recursive: true, force: true });
|
|
1404
|
+
const parentDir = path.dirname(cleanedDir);
|
|
1405
|
+
if (parentDir === '.') {
|
|
1406
|
+
break;
|
|
1407
|
+
}
|
|
1408
|
+
cleanedDir = parentDir;
|
|
1250
1409
|
}
|
|
1410
|
+
const normalizedCleanDir = normalizePath(cleanedDir);
|
|
1251
1411
|
const allReferencesSet = new Set(allReferences);
|
|
1252
1412
|
// update package.json
|
|
1253
|
-
const stringToReplace = distDir + '/';
|
|
1413
|
+
const stringToReplace = distDir + '/'; // we append / to remove in from the middle of the string
|
|
1254
1414
|
const pkgClone = cloneAndUpdate(pkg, value => allReferencesSet.has(value) ? value.replace(stringToReplace, '') : value);
|
|
1255
1415
|
Object.assign(pkg, pkgClone);
|
|
1256
1416
|
// update files
|
|
1257
|
-
let files = pkg.files
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
}
|
|
1266
|
-
files.push(...newFiles);
|
|
1267
|
-
pkg.files = [...files];
|
|
1417
|
+
let files = pkg.files;
|
|
1418
|
+
if (files) {
|
|
1419
|
+
files = files.filter(file => {
|
|
1420
|
+
const fileNormalized = normalizePath(file);
|
|
1421
|
+
return !isSubDirectory(cleanedDir, fileNormalized) && fileNormalized !== normalizedCleanDir;
|
|
1422
|
+
});
|
|
1423
|
+
files.push(...newFiles);
|
|
1424
|
+
pkg.files = [...files];
|
|
1425
|
+
}
|
|
1268
1426
|
// remove extra directories with package.json
|
|
1269
1427
|
const exports = pkg.exports ? Object.keys(pkg.exports) : [];
|
|
1270
1428
|
for (const key of exports) {
|
|
@@ -1283,6 +1441,14 @@ async function flatten(pkg, flatten, logger) {
|
|
|
1283
1441
|
}
|
|
1284
1442
|
}
|
|
1285
1443
|
}
|
|
1444
|
+
function normalizePath(file) {
|
|
1445
|
+
let fileNormalized = path.normalize(file);
|
|
1446
|
+
if (fileNormalized.endsWith('/') || fileNormalized.endsWith('\\')) {
|
|
1447
|
+
// remove trailing slash
|
|
1448
|
+
fileNormalized = fileNormalized.slice(0, -1);
|
|
1449
|
+
}
|
|
1450
|
+
return fileNormalized;
|
|
1451
|
+
}
|
|
1286
1452
|
function cloneAndUpdate(pkg, updater) {
|
|
1287
1453
|
if (typeof pkg === 'string') {
|
|
1288
1454
|
return updater(pkg);
|
|
@@ -1299,12 +1465,16 @@ function cloneAndUpdate(pkg, updater) {
|
|
|
1299
1465
|
}
|
|
1300
1466
|
return pkg;
|
|
1301
1467
|
}
|
|
1468
|
+
function isSubDirectory(parent, child) {
|
|
1469
|
+
return path.relative(child, parent).startsWith('..');
|
|
1470
|
+
}
|
|
1471
|
+
async function isEmptyDir(dir) {
|
|
1472
|
+
const entries = await readdir(dir);
|
|
1473
|
+
return entries.length === 0;
|
|
1474
|
+
}
|
|
1302
1475
|
async function isDirectory(file) {
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
return fileStat.isDirectory();
|
|
1306
|
-
}
|
|
1307
|
-
catch (e) { /**/ }
|
|
1476
|
+
const fileStat = await stat(file);
|
|
1477
|
+
return fileStat.isDirectory();
|
|
1308
1478
|
}
|
|
1309
1479
|
async function walkDir(dir) {
|
|
1310
1480
|
const entries = await readdir(dir, { withFileTypes: true });
|
|
@@ -1384,6 +1554,9 @@ async function execute() {
|
|
|
1384
1554
|
const preimportMap = preimport();
|
|
1385
1555
|
const provider = options.eject ? await createEjectProvider(preimportMap) : createProvider(preimportMap);
|
|
1386
1556
|
const rollupConfigs = await getRollupConfigs(provider, inputs, inputsExt, options, helpers, plugins);
|
|
1557
|
+
if (options.noBundle) {
|
|
1558
|
+
rollupConfigs.length = 0;
|
|
1559
|
+
}
|
|
1387
1560
|
if (options.eject) {
|
|
1388
1561
|
await ejectConfig(rollupConfigs, pkgPath, options, inputs, inputsExt, helpers, pkg);
|
|
1389
1562
|
mainLogger.finish(`ejected config in ${getTimeDiff(time)}`);
|
|
@@ -1406,7 +1579,7 @@ async function execute() {
|
|
|
1406
1579
|
}
|
|
1407
1580
|
}
|
|
1408
1581
|
catch (e) {
|
|
1409
|
-
mainLogger.finish(
|
|
1582
|
+
mainLogger.finish(String(e), 3 /* LogLevel.error */);
|
|
1410
1583
|
process.exit(-1);
|
|
1411
1584
|
}
|
|
1412
1585
|
async function buildConfig(config, updater) {
|
|
@@ -1424,3 +1597,4 @@ function preimport() {
|
|
|
1424
1597
|
['@rollup-extras/plugin-externals', import('@rollup-extras/plugin-externals')]
|
|
1425
1598
|
]) : new Map);
|
|
1426
1599
|
}
|
|
1600
|
+
|
package/package.json
CHANGED
package/dist/index.d.ts.map
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"file": "index.d.ts",
|
|
4
|
-
"names": [
|
|
5
|
-
"Json",
|
|
6
|
-
"ProvideFunction",
|
|
7
|
-
"Provider",
|
|
8
|
-
"PkgbldRollupPlugin",
|
|
9
|
-
"CliOptions",
|
|
10
|
-
"ParsedOptions",
|
|
11
|
-
"PkgbldPlugin",
|
|
12
|
-
"PackageJson",
|
|
13
|
-
"getCliOptions"
|
|
14
|
-
],
|
|
15
|
-
"sources": [
|
|
16
|
-
"src/types.d.ts",
|
|
17
|
-
"src/options-types.d.ts",
|
|
18
|
-
"src/get-cli-options.d.ts"
|
|
19
|
-
],
|
|
20
|
-
"sourcesContent": [
|
|
21
|
-
null,
|
|
22
|
-
null,
|
|
23
|
-
null
|
|
24
|
-
],
|
|
25
|
-
"mappings": ";;;;;aAIYA,IAAIA;;;aAGJC,eAAeA;;;;;aAKfC,QAAQA;;;;;;aAMRC,kBAAkBA;;;;;;;aAOlBC,UAAUA;;;aAGVC,aAAaA;kBACRC,YAAYA;;;;;;;;aC7BjBC,WAAWA;;;;;;;;;;;;;;;;;;;;;;;;;;;UCECC,aAAaA"
|
|
26
|
-
}
|