-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite-plugin-licenses.ts
94 lines (89 loc) · 2.83 KB
/
vite-plugin-licenses.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import fs from 'node:fs';
import rollupPluginLicense from 'rollup-plugin-license';
import type { PluginOption } from 'vite';
import packageJson from './package.json';
type License = {
name: string;
version: string;
license: string;
licenseText: string;
url: string;
};
const registryUrl = 'https://www.npmjs.com/package';
const manualLicenses: License[] = [
{
name: '@tailwindcss/typography',
version: packageJson.devDependencies['@tailwindcss/typography'].replace('^', ''),
license: 'MIT',
licenseText: fs.readFileSync('./node_modules/@tailwindcss/typography/LICENSE').toString(),
url: 'https://github.com/tailwindlabs/tailwindcss-typography'
},
{
name: 'daisyui',
version: packageJson.devDependencies['daisyui'].replace('^', ''),
license: 'MIT',
licenseText: fs.readFileSync('./node_modules/daisyui/LICENSE').toString(),
url: 'https://daisyui.com'
},
{
name: 'modern-normalize',
version: packageJson.devDependencies['modern-normalize'].replace('^', ''),
license: 'MIT',
licenseText: fs.readFileSync('./node_modules/modern-normalize/license').toString(),
url: 'https://github.com/sindresorhus/modern-normalize'
},
{
name: 'tailwindcss',
version: packageJson.devDependencies['tailwindcss'].replace('^', ''),
license: 'MIT',
licenseText: fs.readFileSync('./node_modules/tailwindcss/LICENSE').toString(),
url: 'https://tailwindcss.com'
},
{
name: 'Noto Emoji',
version: '15.0',
license: 'Apache-2.0',
licenseText: fs.readFileSync('./third-party-licenses/noto-emoji/license.txt').toString(),
url: 'https://github.com/googlefonts/noto-emoji'
}
];
export const licenses = () => {
// "Falsy plugins will be ignored, which can be used to easily activate or deactivate plugins."
// See https://vitejs.dev/guide/api-plugin.html#plugins-config.
if (process.env.GENERATE_LICENSES !== 'true') {
return false;
}
// See https://github.com/vitejs/vite/discussions/7722 and
// https://github.com/mjeanroy/rollup-plugin-license and
// https://github.com/mjeanroy/rollup-plugin-license/issues/1362.
return {
...rollupPluginLicense({
thirdParty: {
includePrivate: false,
allow: {
test: 'MIT OR ISC OR 0BSD OR Apache-2.0',
failOnUnlicensed: true,
failOnViolation: true
},
output: {
file: 'src/licenses.json',
template: (dependencies) => {
const foundLicenses = dependencies.map(
(dep): License => ({
name: dep.name ?? '',
version: dep.version ?? '',
license: dep.license ?? '',
licenseText: dep.licenseText ?? '',
url: dep.homepage || (dep.name ? `${registryUrl}/${dep.name}` : '')
})
);
const allLicenses: License[] = [...manualLicenses, ...foundLicenses].sort((a, b) =>
a.name.localeCompare(b.name)
);
return JSON.stringify(allLicenses, null, 2);
}
}
}
})
} as unknown as PluginOption;
};