Skip to content

Commit 03d4dbb

Browse files
committed
feat: remove legal comments
1 parent 0f01fb7 commit 03d4dbb

File tree

8 files changed

+64
-6
lines changed

8 files changed

+64
-6
lines changed

.changeset/weak-penguins-study.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"pkgbld": minor
3+
"pkgbld-internal": minor
4+
---
5+
6+
added remove-legal-comments flag

options/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ export const cliFlags = {
142142
description: 'Do not bundle',
143143
default: false
144144
},
145+
removeLegalComments: {
146+
type: Boolean,
147+
description: 'Remove legal comments',
148+
default: false
149+
}
145150
};
146151

147152
export const packageJsonFieldsOrder = new Set([

pkgbld/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,21 +227,29 @@ If files cannot be copied because of name conflicts the command will fail.
227227
### removeSourcemaps
228228

229229
```
230-
pkgbld prune --removeSourcemaps
230+
pkgbld prune --remove-sourcemaps
231231
```
232232

233233
Removes all sourcemaps from the package. The logic is very simple and removes all files with `.map` extension and references in format `//# sourceMappingURL=<mapFile>`.
234234

235235
### optimizeFiles (default)
236236

237237
```
238-
pkgbld prune --optimizeFiles=false
238+
pkgbld prune --optimize-files=false
239239
```
240240

241241
Optimizes files by removing all files that are not required for pack at the given moment.
242242

243243
You might want to disable this option in some edge cases.
244244

245+
### removeLegalComments
246+
247+
```
248+
pkgbld prune --remove-legal-comments --compress=es,cjs
249+
```
250+
251+
Removes all legal comments from the package. Only works with compress.
252+
245253
## Plugin API
246254

247255
`pkgbld` reads all installed packages named `pkgbld-plugin-*` and assumes they are plugins

pkgbld/src/builtin-plugins/terser.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ export default async function(provider: Provider, config: CliOptions, inputs: st
1414
regex: /_$/
1515
}
1616
}
17-
};
17+
};
18+
19+
if (config.removeLegalComments) {
20+
(options as unknown as { output: { comments: boolean } }).output = {
21+
comments: false,
22+
};
23+
}
1824

1925
if (filteredFormats.length > 0) {
2026
for (const format of filteredFormats as InternalModuleFormat[]) {

pkgbld/src/get-cli-options.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export function getCliOptions(plugins: Partial<PkgbldPlugin>[], pkg: PackageJson
8181
noPack: flags.noPack,
8282
noExports: flags.noExports,
8383
noClean: flags.noClean,
84-
noBundle: flags.noBundle
84+
noBundle: flags.noBundle,
85+
removeLegalComments: flags.removeLegalComments
8586
};
8687

8788
for (const plugin of plugins) {
@@ -110,7 +111,8 @@ export function getCliOptions(plugins: Partial<PkgbldPlugin>[], pkg: PackageJson
110111
noPack: boolean,
111112
noExports: boolean,
112113
noClean: boolean,
113-
noBundle: boolean
114+
noBundle: boolean,
115+
removeLegalComments: boolean
114116
};
115117
}
116118
}

pkgbld/src/get-rollup-configs.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ export async function getRollupConfigs([provider, plugins]: [Provider, PkgbldRol
112112
entryFileNames: fileNamePatterns[format],
113113
plugins: getPlugins([format], inputs, true),
114114
sourcemap: config.sourcemapFormats.includes(format),
115+
// this requires more work
116+
// preserveModules: true,
117+
// preserveModulesRoot: config.sourceDir,
115118
...getExtraOutputSettings(format, inputs)
116119
})),
117120

pkgbld/src/process-pkg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export async function processPackage(pkg: JsonObject, config: CliOptions, plugin
129129
for (const id in pkg.exports as object) {
130130
if (id === './package.json') continue;
131131

132-
const basename = id == '.' ? indexId : path.basename(id);
132+
const basename = id == '.' ? indexId : path.join(path.dirname(id), path.basename(id));
133133

134134
if (typeof (pkg.exports as Record<string, JsonValue>)[id] !== 'object') {
135135
(pkg.exports as Record<string, JsonValue>)[id] = {};

pkgbld/tests/tests.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,34 @@
269269
"output": "dist\n index.mjs\n|const o=0;export{o as test};\n|\npackage.json\n|{}\n|\nsrc\n index.js\n|export const test = 0;",
270270
"stdout": "preparing..\n\npreparing...\nsrc → dist 0 / 1\n✓ index [es]\nsrc → dist 1 / 1\n✓ src → dist 1 / 1 in XXX\n",
271271
"stderr": ""
272+
},
273+
{
274+
"id": 29,
275+
"name": "remove legal comments",
276+
"args": "--remove-legal-comments --compress=es,cjs",
277+
"input": "package.json\n|{\n| \"exports\": {\n| \".\": {\n| \"import\": \"./dist/index.mjs\"\n| }\n| }\n|}\nsrc\n index.ts\n|//! Copyright (c) 2024 Konstantin Shutkin MIT License\n|export const test = 0;",
278+
"output": "dist\n index.cjs\n|\"use strict\";exports.test=0;\n|\n index.d.ts\n|export declare const test = 0;\n|\n index.mjs\n|const o=0;export{o as test};\n|\npackage.json\n|{\n| \"exports\": {\n| \".\": {\n| \"types\": \"./dist/index.d.ts\",\n| \"import\": \"./dist/index.mjs\",\n| \"require\": \"./dist/index.cjs\",\n| \"default\": \"./dist/index.cjs\"\n| },\n| \"./package.json\": \"./package.json\"\n| },\n| \"files\": [\n| \"dist\"\n| ],\n| \"scripts\": {\n| \"prepack\": \"pkgbld prune\"\n| },\n| \"types\": \"./dist/index.d.ts\",\n| \"main\": \"./dist/index.cjs\",\n| \"module\": \"./dist/index.mjs\",\n| \"typesVersions\": {\n| \"*\": {\n| \".\": [\n| \"dist/index.d.ts\"\n| ],\n| \"*\": [\n| \"dist/index.d.ts\",\n| \"dist/*\"\n| ]\n| }\n| }\n|}\n|\nsrc\n index.ts\n|//! Copyright (c) 2024 Konstantin Shutkin MIT License\n|export const test = 0;\ntsconfig.json\n|{\n| \"include\": [\n| \"src\",\n| \"types\"\n| ],\n| \"compilerOptions\": {\n| \"lib\": [\n| \"dom\",\n| \"esnext\"\n| ],\n| \"target\": \"esnext\",\n| \"module\": \"esnext\",\n| \"esModuleInterop\": true,\n| \"allowJs\": true,\n| \"skipLibCheck\": true,\n| \"strict\": true,\n| \"sourceMap\": true,\n| \"noUncheckedIndexedAccess\": true,\n| \"declaration\": true,\n| \"moduleResolution\": \"node\"\n| }\n|}\n|",
279+
"stdout": "preparing..\n\npreparing...\nno tsconfig.json or jsconfig.json and --no-ts-config not specified, writing tsconfig...\ndone\nsrc → dist 0 / 1\n✓ index [es, cjs]\nsrc → dist 1 / 1\n✓ src → dist 1 / 1 in XXX\n",
280+
"stderr": ""
281+
},
282+
{
283+
"id": 30,
284+
"name": "legal comments",
285+
"args": "--compress=es,cjs",
286+
"input": "package.json\n|{\n| \"exports\": {\n| \".\": {\n| \"import\": \"./dist/index.mjs\"\n| }\n| }\n|}\nsrc\n index.ts\n|//! Copyright (c) 2024 Konstantin Shutkin MIT License\n|export const test = 0;",
287+
"output": "dist\n index.cjs\n|\"use strict\";\n|//! Copyright (c) 2024 Konstantin Shutkin MIT License\n|exports.test=0;\n|\n index.d.ts\n|export declare const test = 0;\n|\n index.mjs\n|//! Copyright (c) 2024 Konstantin Shutkin MIT License\n|const o=0;export{o as test};\n|\npackage.json\n|{\n| \"exports\": {\n| \".\": {\n| \"types\": \"./dist/index.d.ts\",\n| \"import\": \"./dist/index.mjs\",\n| \"require\": \"./dist/index.cjs\",\n| \"default\": \"./dist/index.cjs\"\n| },\n| \"./package.json\": \"./package.json\"\n| },\n| \"files\": [\n| \"dist\"\n| ],\n| \"scripts\": {\n| \"prepack\": \"pkgbld prune\"\n| },\n| \"types\": \"./dist/index.d.ts\",\n| \"main\": \"./dist/index.cjs\",\n| \"module\": \"./dist/index.mjs\",\n| \"typesVersions\": {\n| \"*\": {\n| \".\": [\n| \"dist/index.d.ts\"\n| ],\n| \"*\": [\n| \"dist/index.d.ts\",\n| \"dist/*\"\n| ]\n| }\n| }\n|}\n|\nsrc\n index.ts\n|//! Copyright (c) 2024 Konstantin Shutkin MIT License\n|export const test = 0;\ntsconfig.json\n|{\n| \"include\": [\n| \"src\",\n| \"types\"\n| ],\n| \"compilerOptions\": {\n| \"lib\": [\n| \"dom\",\n| \"esnext\"\n| ],\n| \"target\": \"esnext\",\n| \"module\": \"esnext\",\n| \"esModuleInterop\": true,\n| \"allowJs\": true,\n| \"skipLibCheck\": true,\n| \"strict\": true,\n| \"sourceMap\": true,\n| \"noUncheckedIndexedAccess\": true,\n| \"declaration\": true,\n| \"moduleResolution\": \"node\"\n| }\n|}\n|",
288+
"stdout": "preparing..\n\npreparing...\nno tsconfig.json or jsconfig.json and --no-ts-config not specified, writing tsconfig...\ndone\nsrc → dist 0 / 1\n✓ index [es, cjs]\nsrc → dist 1 / 1\n✓ src → dist 1 / 1 in XXX\n",
289+
"stderr": ""
290+
}
291+
],
292+
"capture": [
293+
{
294+
"id": 31,
295+
"name": "",
296+
"input": "package.json\n|{\n| \"exports\": {\n| \".\": {\n| \"import\": \"./dist/index.mjs\"\n| },\n| \"./folder/second\": { \n| \"import\": \"./dist/folder/second.mjs\"\n| }\n| }\n|}\nsrc\n folder\n second.ts\n|export const t = 0;\n index.ts\n|export const test = 0;",
297+
"output": "dist\n folder\n second.d.ts\n|export declare const t = 0;\n|\n index.cjs\n|'use strict';\n|\n|const test = 0;\n|\n|exports.test = test;\n|\n index.d.ts\n|export declare const test = 0;\n|\n index.mjs\n|const test = 0;\n|\n|export { test };\n|\n second.cjs\n|'use strict';\n|\n|const t = 0;\n|\n|exports.t = t;\n|\n second.mjs\n|const t = 0;\n|\n|export { t };\n|\npackage.json\n|{\n| \"exports\": {\n| \".\": {\n| \"types\": \"./dist/index.d.ts\",\n| \"import\": \"./dist/index.mjs\",\n| \"require\": \"./dist/index.cjs\",\n| \"default\": \"./dist/index.cjs\"\n| },\n| \"./folder/second\": {\n| \"types\": \"./dist/folder/second.d.ts\",\n| \"import\": \"./dist/folder/second.mjs\",\n| \"default\": \"./dist/folder/second.cjs\"\n| },\n| \"./package.json\": \"./package.json\"\n| },\n| \"files\": [\n| \"dist\",\n| \"folder/second\"\n| ],\n| \"scripts\": {\n| \"prepack\": \"pkgbld prune\"\n| },\n| \"types\": \"./dist/index.d.ts\",\n| \"main\": \"./dist/index.cjs\",\n| \"module\": \"./dist/index.mjs\",\n| \"typesVersions\": {\n| \"*\": {\n| \".\": [\n| \"dist/index.d.ts\"\n| ],\n| \"./folder/second\": [\n| \"dist/folder/second.d.ts\"\n| ],\n| \"*\": [\n| \"dist/index.d.ts\",\n| \"dist/*\"\n| ]\n| }\n| }\n|}\n|\nsecond\n package.json\n|{\n| \"type\": \"module\",\n| \"types\": \"../dist/second.d.ts\",\n| \"main\": \"../dist/second.mjs\"\n|}\nsrc\n folder\n second.ts\n|export const t = 0;\n index.ts\n|export const test = 0;\ntsconfig.json\n|{\n| \"include\": [\n| \"src\",\n| \"types\"\n| ],\n| \"compilerOptions\": {\n| \"lib\": [\n| \"dom\",\n| \"esnext\"\n| ],\n| \"target\": \"esnext\",\n| \"module\": \"esnext\",\n| \"esModuleInterop\": true,\n| \"allowJs\": true,\n| \"skipLibCheck\": true,\n| \"strict\": true,\n| \"sourceMap\": true,\n| \"noUncheckedIndexedAccess\": true,\n| \"declaration\": true,\n| \"moduleResolution\": \"node\"\n| }\n|}\n|",
298+
"stdout": "preparing..\n\npreparing...\nno tsconfig.json or jsconfig.json and --no-ts-config not specified, writing tsconfig...\ndone\nsrc → dist 0 / 1\n✓ index, second [es, cjs]\nsrc → dist 1 / 1\n✓ src → dist 1 / 1 in XXX\n",
299+
"stderr": ""
272300
}
273301
]
274302
}

0 commit comments

Comments
 (0)