Skip to content

Commit 0e878ec

Browse files
committed
build: enhance 3rd party licenses digest
1 parent 9779deb commit 0e878ec

File tree

5 files changed

+63
-7
lines changed

5 files changed

+63
-7
lines changed

.size-limit.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
"name": "zx-lite",
44
"path": [
5+
"build/3rd-party-licenses",
56
"build/core.cjs",
67
"build/core.js",
78
"build/core.d.ts",
@@ -16,14 +17,14 @@
1617
"README.md",
1718
"LICENSE"
1819
],
19-
"limit": "111 kB",
20+
"limit": "114.5 kB",
2021
"brotli": false,
2122
"gzip": false
2223
},
2324
{
2425
"name": "js parts",
2526
"path": "build/*.{js,cjs}",
26-
"limit": "813.5 kB",
27+
"limit": "812.6 kB",
2728
"brotli": false,
2829
"gzip": false
2930
},
@@ -37,14 +38,14 @@
3738
{
3839
"name": "vendor",
3940
"path": "build/vendor-*",
40-
"limit": "767.12 kB",
41+
"limit": "765.5 kB",
4142
"brotli": false,
4243
"gzip": false
4344
},
4445
{
4546
"name": "all",
4647
"path": ["build/*", "man/*", "README.md", "LICENSE"],
47-
"limit": "866.5 kB",
48+
"limit": "868.1 kB",
4849
"brotli": false,
4950
"gzip": false
5051
}

scripts/build-js.mjs

+53-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const argv = minimist(process.argv.slice(2), {
3434
entry: './src/index.ts',
3535
external: 'node:*',
3636
bundle: 'src', // 'all' | 'none'
37-
license: 'eof',
37+
license: 'none', // see digestLicenses below // 'eof',
3838
minify: false,
3939
sourcemap: false,
4040
format: 'cjs,esm',
@@ -72,6 +72,8 @@ const plugins = [
7272
}),
7373
]
7474

75+
const thirdPartyModules = new Set()
76+
7577
if (_bundle && entryPoints.length > 1) {
7678
plugins.push(entryChunksPlugin())
7779
}
@@ -93,6 +95,14 @@ if (hybrid) {
9395
}
9496

9597
plugins.push(
98+
{
99+
name: 'get-3rd-party-modules',
100+
setup: (build) => {
101+
build.onResolve({ filter: /./, namespace: 'file' }, async (args) => {
102+
thirdPartyModules.add(args.resolveDir)
103+
})
104+
},
105+
},
96106
transformHookPlugin({
97107
hooks: [
98108
{
@@ -157,13 +167,53 @@ plugins.push(
157167
{
158168
name: 'deno',
159169
setup(build) {
160-
build.onEnd(() =>
170+
build.onEnd(() => {
161171
fs.copyFileSync('./scripts/deno.polyfill.js', './build/deno.js')
162-
)
172+
fs.writeFileSync(
173+
'./build/3rd-party-licenses',
174+
digestLicenses(thirdPartyModules)
175+
)
176+
})
163177
},
164178
}
165179
)
166180

181+
// prettier-ignore
182+
function digestLicenses(dirs) {
183+
const digest = [...[...dirs]
184+
.reduce((m, d) => {
185+
const chunks = d.split('/')
186+
const i = chunks.lastIndexOf('node_modules')
187+
const name = chunks[i + 1]
188+
const shift = i + 1 + (name.startsWith('@') ? 2 : 1)
189+
const root = chunks.slice(0, shift).join('/')
190+
if (name !== 'zx') m.add(root)
191+
return m
192+
}, new Set())]
193+
.map(d => {
194+
const extractName = (entry) => entry?.name ? `${entry.name} <${entry.email}>` : entry
195+
const pkg = path.join(d, 'package.json')
196+
const pkgJson = JSON.parse(fs.readFileSync(pkg, 'utf-8'))
197+
const author = extractName(pkgJson.author)
198+
const contributors = (pkgJson.contributors || pkgJson.maintainers || []).map(extractName).join(', ')
199+
const by = author || contributors || '<unknown>'
200+
const repository = pkgJson.repository?.url || pkgJson.repository || ''
201+
const license = pkgJson.license || '<unknown>'
202+
203+
return `${pkgJson.name}@${pkgJson.version}
204+
by ${by}
205+
licensed under ${license}
206+
${repository}`
207+
})
208+
.sort()
209+
.join('\n\n')
210+
211+
return `THIRD PARTY LICENSES
212+
213+
${digest}
214+
`
215+
}
216+
167217
function entryPointsToRegexp(entryPoints) {
168218
return new RegExp(
169219
'(' +

scripts/prepublish-lite.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const pkgJson = {
4343
},
4444
man: undefined,
4545
files: [
46+
'build/3rd-party-licenses',
4647
'build/core.cjs',
4748
'build/core.js',
4849
'build/core.d.ts',

test/export.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ describe('index', () => {
375375
assert.equal(typeof index.glob.isDynamicPattern, 'function', 'index.glob.isDynamicPattern')
376376
assert.equal(typeof index.glob.isGitIgnored, 'function', 'index.glob.isGitIgnored')
377377
assert.equal(typeof index.glob.isGitIgnoredSync, 'function', 'index.glob.isGitIgnoredSync')
378+
assert.equal(typeof index.glob.sync, 'function', 'index.glob.sync')
378379
assert.equal(typeof index.globby, 'function', 'index.globby')
379380
assert.equal(typeof index.globby.convertPathToPattern, 'function', 'index.globby.convertPathToPattern')
380381
assert.equal(typeof index.globby.generateGlobTasks, 'function', 'index.globby.generateGlobTasks')
@@ -385,6 +386,7 @@ describe('index', () => {
385386
assert.equal(typeof index.globby.isDynamicPattern, 'function', 'index.globby.isDynamicPattern')
386387
assert.equal(typeof index.globby.isGitIgnored, 'function', 'index.globby.isGitIgnored')
387388
assert.equal(typeof index.globby.isGitIgnoredSync, 'function', 'index.globby.isGitIgnoredSync')
389+
assert.equal(typeof index.globby.sync, 'function', 'index.globby.sync')
388390
assert.equal(typeof index.kill, 'function', 'index.kill')
389391
assert.equal(typeof index.log, 'function', 'index.log')
390392
assert.equal(typeof index.minimist, 'function', 'index.minimist')
@@ -639,6 +641,7 @@ describe('vendor', () => {
639641
assert.equal(typeof vendor.glob.isDynamicPattern, 'function', 'vendor.glob.isDynamicPattern')
640642
assert.equal(typeof vendor.glob.isGitIgnored, 'function', 'vendor.glob.isGitIgnored')
641643
assert.equal(typeof vendor.glob.isGitIgnoredSync, 'function', 'vendor.glob.isGitIgnoredSync')
644+
assert.equal(typeof vendor.glob.sync, 'function', 'vendor.glob.sync')
642645
assert.equal(typeof vendor.minimist, 'function', 'vendor.minimist')
643646
assert.equal(typeof vendor.ps, 'object', 'vendor.ps')
644647
assert.equal(typeof vendor.ps.kill, 'function', 'vendor.ps.kill')

test/package.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe('package', () => {
7676
'build/vendor.cjs',
7777
'build/vendor.d.ts',
7878
'build/vendor.js',
79+
'build/3rd-party-licenses',
7980
].sort()
8081
)
8182
})

0 commit comments

Comments
 (0)