From 51e3d13e45d45dc0eaf6e6125de00a56c63345d8 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 1 Nov 2024 15:21:08 +0800 Subject: [PATCH 1/5] refactor: set defaults --- packages/vite/src/node/__tests__/plugins/esbuild.spec.ts | 7 ------- packages/vite/src/node/config.ts | 3 +++ packages/vite/src/node/optimizer/index.ts | 1 + packages/vite/src/node/plugins/css.ts | 2 +- packages/vite/src/node/plugins/define.ts | 2 +- packages/vite/src/node/plugins/esbuild.ts | 2 -- 6 files changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts index 936415f9c33826..19fb3c4e1787ba 100644 --- a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts @@ -20,7 +20,6 @@ describe('resolveEsbuildTranspileOptions', () => { 'es', ) expect(options).toEqual({ - charset: 'utf8', loader: 'js', target: 'es2020', format: 'esm', @@ -64,7 +63,6 @@ describe('resolveEsbuildTranspileOptions', () => { 'es', ) expect(options).toEqual({ - charset: 'utf8', loader: 'js', target: undefined, format: 'esm', @@ -95,7 +93,6 @@ describe('resolveEsbuildTranspileOptions', () => { 'es', ) expect(options).toEqual({ - charset: 'utf8', loader: 'js', target: 'es2020', format: 'esm', @@ -128,7 +125,6 @@ describe('resolveEsbuildTranspileOptions', () => { 'es', ) expect(options).toEqual({ - charset: 'utf8', loader: 'js', target: undefined, format: 'esm', @@ -161,7 +157,6 @@ describe('resolveEsbuildTranspileOptions', () => { 'cjs', ) expect(options).toEqual({ - charset: 'utf8', loader: 'js', target: undefined, format: 'cjs', @@ -193,7 +188,6 @@ describe('resolveEsbuildTranspileOptions', () => { 'es', ) expect(options).toEqual({ - charset: 'utf8', loader: 'js', target: undefined, format: 'esm', @@ -229,7 +223,6 @@ describe('resolveEsbuildTranspileOptions', () => { 'cjs', ) expect(options).toEqual({ - charset: 'utf8', loader: 'js', target: undefined, format: 'cjs', diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index d9f8c4083c65c1..39c0570e0a16f7 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -1243,6 +1243,9 @@ export async function resolveConfig( ? false : { jsxDev: !isProduction, + // change defaults that fit better for vite + charset: 'utf8', + legalComments: 'none', ...config.esbuild, }, server, diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 921326bdda0adb..3406a32e6fb422 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -798,6 +798,7 @@ async function prepareEsbuildOptimizerRun( metafile: true, plugins, charset: 'utf8', + legalComments: 'none', ...esbuildOptions, supported: { ...defaultEsbuildSupported, diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 9677d3c2439d24..4c6352020bb40f 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -1926,7 +1926,7 @@ function resolveMinifyCssEsbuildOptions( options: ESBuildOptions, ): TransformOptions { const base: TransformOptions = { - charset: options.charset ?? 'utf8', + charset: options.charset, logLevel: options.logLevel, logLimit: options.logLimit, logOverride: options.logOverride, diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index 36c9b57dbfeb6c..0a133ca7457529 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -191,7 +191,7 @@ export async function replaceDefine( const result = await transform(code, { loader: 'js', - charset: esbuildOptions.charset ?? 'utf8', + charset: esbuildOptions.charset, platform: 'neutral', define, sourcefile: id, diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 6d3ffc86f33b06..c461bf9fb68a5c 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -248,7 +248,6 @@ export function esbuildPlugin(config: ResolvedConfig): Plugin { // and for build as the final optimization is in `buildEsbuildPlugin` const transformOptions: TransformOptions = { target: 'esnext', - charset: 'utf8', ...esbuildTransformOptions, minify: false, minifyIdentifiers: false, @@ -390,7 +389,6 @@ export function resolveEsbuildTranspileOptions( const esbuildOptions = config.esbuild || {} const options: TransformOptions = { - charset: 'utf8', ...esbuildOptions, loader: 'js', target: target || undefined, From e96e888109ead9ac4057d739d0c95b92c86a4392 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 1 Nov 2024 15:38:46 +0800 Subject: [PATCH 2/5] feat: initial implementation --- packages/vite/src/node/build.ts | 15 ++- packages/vite/src/node/plugins/license.ts | 150 ++++++++++++++++++++++ playground/optimize-deps/vite.config.js | 1 + 3 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 packages/vite/src/node/plugins/license.ts diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 23dc250ce36f6a..4564beead5eb54 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -73,6 +73,7 @@ import { } from './baseEnvironment' import type { MinimalPluginContext, Plugin, PluginContext } from './plugin' import type { RollupPluginHooks } from './typeUtils' +import { licensePlugin } from './plugins/license' export interface BuildEnvironmentOptions { /** @@ -199,6 +200,12 @@ export interface BuildEnvironmentOptions { * @default true */ copyPublicDir?: boolean + /** + * Whether to emit a .vite/license.md that includes all bundled dependencies' + * licenses. Specify a path that ends with `.json` to generate a raw JSON.entry. + * @default false + */ + license?: boolean | string /** * Whether to emit a .vite/manifest.json under assets dir to map hash-less filenames * to their hashed versions. Useful when you want to generate your own HTML @@ -383,6 +390,7 @@ export function resolveBuildEnvironmentOptions( write: true, emptyOutDir: null, copyPublicDir: true, + license: false, manifest: false, lib: false, ssr: consumer === 'server', @@ -501,7 +509,12 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{ ...(config.esbuild !== false ? [buildEsbuildPlugin(config)] : []), terserPlugin(config), ...(!config.isWorker - ? [manifestPlugin(), ssrManifestPlugin(), buildReporterPlugin(config)] + ? [ + licensePlugin(), + manifestPlugin(), + ssrManifestPlugin(), + buildReporterPlugin(config), + ] : []), buildLoadFallbackPlugin(), ], diff --git a/packages/vite/src/node/plugins/license.ts b/packages/vite/src/node/plugins/license.ts new file mode 100644 index 00000000000000..b4d8a2cfaa6b99 --- /dev/null +++ b/packages/vite/src/node/plugins/license.ts @@ -0,0 +1,150 @@ +import fs from 'node:fs' +import path from 'node:path' +import type { Plugin } from '../plugin' +import { isInNodeModules, sortObjectKeys } from '../utils' +import type { PackageCache } from '../packages' +import { findNearestMainPackageData } from '../packages' + +export interface LicenseEntry { + /** + * Package name + */ + name: string + /** + * Package version + */ + version: string + /** + * SPDX license identifier (from package.json "license" field) + */ + identifier?: string + /** + * License file text + */ + text?: string +} + +// https://github.com/npm/npm-packlist/blob/53b2a4f42b7fef0f63e8f26a3ea4692e23a58fed/lib/index.js#L284-L286 +const licenseFiles = [/^license/i, /^licence/i, /^copying/i] + +export interface ManifestChunk { + src?: string + file: string + css?: string[] + assets?: string[] + isEntry?: boolean + name?: string + isDynamicEntry?: boolean + imports?: string[] + dynamicImports?: string[] +} + +export function licensePlugin(): Plugin { + return { + name: 'vite:manifest', + + async generateBundle(_, bundle) { + const licenseOption = this.environment.config.build.license + if (licenseOption === false) return + + const packageCache: PackageCache = new Map() + // Track license via a key to its license entry. + // A key consists of "name@version" of a package. + const licenses: Record = {} + + for (const file in bundle) { + const chunk = bundle[file] + if (chunk.type === 'asset') continue + + for (const moduleId of chunk.moduleIds) { + if (moduleId.startsWith('\0') || !isInNodeModules(moduleId)) continue + + // Find the dependency package.json + const pkgData = findNearestMainPackageData( + path.dirname(moduleId), + packageCache, + ) + if (!pkgData) continue + + // Grab the package.json keys and check if already exists in the licenses + const { name, version = '0.0.0', license } = pkgData.data + const key = `${name}@${version}` + if (licenses[key]) continue + + // If not, create a new license entry + const entry: LicenseEntry = { name, version } + if (license) { + entry.identifier = license.trim() + } + const licenseFile = findLicenseFile(pkgData.dir) + if (licenseFile) { + entry.text = fs.readFileSync(licenseFile, 'utf-8').trim() + } + licenses[key] = entry + } + } + + const licenseEntries = Object.values(sortObjectKeys(licenses)) + + // Emit as a JSON file + if ( + typeof licenseOption === 'string' && + licenseOption.endsWith('.json') + ) { + this.emitFile({ + fileName: licenseOption, + type: 'asset', + source: JSON.stringify(licenseEntries, null, 2), + }) + return + } + + // Emit a license file as markdown + const markdown = licenseEntryToMarkdown(licenseEntries) + this.emitFile({ + fileName: + typeof licenseOption === 'string' + ? licenseOption + : '.vite/license.md', + type: 'asset', + source: markdown, + }) + }, + } +} + +function licenseEntryToMarkdown(licenses: LicenseEntry[]) { + if (licenses.length === 0) { + return `\ +# Licenses + +The app does not bundle any dependencies with licenses. +` + } + + let text = `\ +# Licenses + +The app bundles dependencies which contains the following licenses: +` + for (const license of licenses) { + const nameAndVersionText = `${license.name} - ${license.version}` + const identifierText = license.identifier ? ` (${license.identifier})` : '' + + text += `\n## ${nameAndVersionText}${identifierText}\n` + if (license.text) { + text += `\n${license.text}\n` + } + } + return text +} + +function findLicenseFile(pkgDir: string) { + const files = fs.readdirSync(pkgDir) + const matchedFile = files.find((file) => + licenseFiles.some((re) => re.test(file)), + ) + if (matchedFile) { + return path.join(pkgDir, matchedFile) + } +} diff --git a/playground/optimize-deps/vite.config.js b/playground/optimize-deps/vite.config.js index 6ef09488556cc1..e3ca7e62b5e18a 100644 --- a/playground/optimize-deps/vite.config.js +++ b/playground/optimize-deps/vite.config.js @@ -47,6 +47,7 @@ export default defineConfig({ build: { // to make tests faster minify: false, + license: true, rollupOptions: { onwarn(msg, warn) { // filter `"Buffer" is not exported by "__vite-browser-external"` warning From 5e435fdd2efea83816b3730326a20ca56a32c5fa Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 4 Nov 2024 14:41:11 +0800 Subject: [PATCH 3/5] chore: add docs and tests --- docs/config/build-options.md | 28 +++++++++++ .../__snapshots__/license.spec.ts.snap | 47 +++++++++++++++++++ .../fixtures/license/dep-licence-cc0/index.js | 1 + .../fixtures/license/dep-licence-cc0/licence | 3 ++ .../license/dep-licence-cc0/package.json | 7 +++ .../fixtures/license/dep-license-mit/index.js | 3 ++ .../fixtures/license/dep-license-mit/license | 3 ++ .../license/dep-license-mit/package.json | 10 ++++ .../license/dep-nested-license-isc/LICENSE | 1 + .../license/dep-nested-license-isc/index.js | 1 + .../dep-nested-license-isc/package.json | 7 +++ .../plugins/fixtures/license/index.html | 5 ++ .../plugins/fixtures/license/package.json | 10 ++++ .../node/__tests__/plugins/license.spec.ts | 36 ++++++++++++++ packages/vite/src/node/build.ts | 4 +- pnpm-lock.yaml | 36 ++++++++++++++ 16 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 packages/vite/src/node/__tests__/plugins/__snapshots__/license.spec.ts.snap create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/index.js create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/licence create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/package.json create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/index.js create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/license create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/package.json create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/LICENSE create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/index.js create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/package.json create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/index.html create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/license/package.json create mode 100644 packages/vite/src/node/__tests__/plugins/license.spec.ts diff --git a/docs/config/build-options.md b/docs/config/build-options.md index 3419070bd474a6..b7f2f6c8cc616c 100644 --- a/docs/config/build-options.md +++ b/docs/config/build-options.md @@ -185,6 +185,34 @@ export default defineConfig({ }) ``` +## build.license + +- **Type:** `boolean | string` +- **Default:** `false` + +When set to `true`, the build will also generate a `.vite/license.md` file that includes all bundled dependencies' licenses. It can be hosted to display and acknowledge the dependencies used by the app. When the value is a string, it will be used as the license file name. + +If a string is passed that ends with `.json`, a raw JSON file will be generated instead. For example: + +```json +[ + { + "name": "dep-1", + "version": "0.0.0", + "identifier": "CC0-1.0", + "text": "CC0 1.0 Universal ..." + }, + { + "name": "dep-2", + "version": "0.0.0", + "identifier": "MIT", + "text": "MIT License ..." + } +] +``` + +It can be used for further processing to output as a different format. + ## build.manifest - **Type:** `boolean | string` diff --git a/packages/vite/src/node/__tests__/plugins/__snapshots__/license.spec.ts.snap b/packages/vite/src/node/__tests__/plugins/__snapshots__/license.spec.ts.snap new file mode 100644 index 00000000000000..149e15653f6dc3 --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/__snapshots__/license.spec.ts.snap @@ -0,0 +1,47 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`json 1`] = ` +"[ + { + "name": "@vitejs/test-dep-licence-cc0", + "version": "0.0.0", + "identifier": "CC0-1.0", + "text": "CC0 1.0 Universal\\n\\n..." + }, + { + "name": "@vitejs/test-dep-license-mit", + "version": "0.0.0", + "identifier": "MIT", + "text": "MIT License\\n\\nCopyright (c) ..." + }, + { + "name": "@vitejs/test-dep-nested-license-isc", + "version": "0.0.0", + "identifier": "ISC", + "text": "Copyright (c) ..." + } +]" +`; + +exports[`markdown 1`] = ` +"# Licenses + +The app bundles dependencies which contains the following licenses: + +## @vitejs/test-dep-licence-cc0 - 0.0.0 (CC0-1.0) + +CC0 1.0 Universal + +... + +## @vitejs/test-dep-license-mit - 0.0.0 (MIT) + +MIT License + +Copyright (c) ... + +## @vitejs/test-dep-nested-license-isc - 0.0.0 (ISC) + +Copyright (c) ... +" +`; diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/index.js b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/index.js new file mode 100644 index 00000000000000..60c71f346d9a3e --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/index.js @@ -0,0 +1 @@ +export default 'ok' diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/licence b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/licence new file mode 100644 index 00000000000000..7837407e70efb5 --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/licence @@ -0,0 +1,3 @@ +CC0 1.0 Universal + +... diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/package.json b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/package.json new file mode 100644 index 00000000000000..9e4c8b22a55c6f --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0/package.json @@ -0,0 +1,7 @@ +{ + "name": "@vitejs/test-dep-licence-cc0", + "private": true, + "version": "0.0.0", + "type": "module", + "license": "CC0-1.0" +} diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/index.js b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/index.js new file mode 100644 index 00000000000000..9da0ff4a917379 --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/index.js @@ -0,0 +1,3 @@ +import nestedDep from '@vitejs/test-dep-nested-licence-isc' + +export default 'ok' + nestedDep diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/license b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/license new file mode 100644 index 00000000000000..1732da241e5252 --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/license @@ -0,0 +1,3 @@ +MIT License + +Copyright (c) ... diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/package.json b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/package.json new file mode 100644 index 00000000000000..73594817cfead0 --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit/package.json @@ -0,0 +1,10 @@ +{ + "name": "@vitejs/test-dep-license-mit", + "private": true, + "version": "0.0.0", + "type": "module", + "license": "MIT", + "dependencies": { + "@vitejs/test-dep-nested-licence-isc": "file:../dep-nested-license-isc" + } +} diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/LICENSE b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/LICENSE new file mode 100644 index 00000000000000..40ce705e530b07 --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/LICENSE @@ -0,0 +1 @@ +Copyright (c) ... diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/index.js b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/index.js new file mode 100644 index 00000000000000..60c71f346d9a3e --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/index.js @@ -0,0 +1 @@ +export default 'ok' diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/package.json b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/package.json new file mode 100644 index 00000000000000..70b3745d3dc0ef --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc/package.json @@ -0,0 +1,7 @@ +{ + "name": "@vitejs/test-dep-nested-license-isc", + "private": true, + "version": "0.0.0", + "type": "module", + "license": "ISC" +} diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/index.html b/packages/vite/src/node/__tests__/plugins/fixtures/license/index.html new file mode 100644 index 00000000000000..b0825ecb300d5b --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/index.html @@ -0,0 +1,5 @@ + diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/license/package.json b/packages/vite/src/node/__tests__/plugins/fixtures/license/package.json new file mode 100644 index 00000000000000..4e06638e94f0cc --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/license/package.json @@ -0,0 +1,10 @@ +{ + "name": "@vitejs/test-license", + "private": true, + "version": "0.0.0", + "type": "module", + "dependencies": { + "@vitejs/test-dep-license-mit": "file:./dep-license-mit", + "@vitejs/test-dep-licence-cc0": "file:./dep-licence-cc0" + } +} diff --git a/packages/vite/src/node/__tests__/plugins/license.spec.ts b/packages/vite/src/node/__tests__/plugins/license.spec.ts new file mode 100644 index 00000000000000..0b410c29f66341 --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/license.spec.ts @@ -0,0 +1,36 @@ +import { fileURLToPath } from 'node:url' +import type { OutputAsset, RollupOutput } from 'rollup' +import { expect, test } from 'vitest' +import { build } from '../../build' + +test('markdown', async () => { + const result = (await build({ + root: fileURLToPath(new URL('./fixtures/license', import.meta.url)), + logLevel: 'silent', + build: { + write: false, + license: true, + }, + })) as RollupOutput + const licenseAsset = result.output.find( + (asset) => asset.fileName === '.vite/license.md', + ) as OutputAsset | undefined + expect(licenseAsset).toBeDefined() + expect(licenseAsset?.source).toMatchSnapshot() +}) + +test('json', async () => { + const result = (await build({ + root: fileURLToPath(new URL('./fixtures/license', import.meta.url)), + logLevel: 'silent', + build: { + write: false, + license: '.vite/license.json', + }, + })) as RollupOutput + const licenseAsset = result.output.find( + (asset) => asset.fileName === '.vite/license.json', + ) as OutputAsset | undefined + expect(licenseAsset).toBeDefined() + expect(licenseAsset?.source).toMatchSnapshot() +}) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 4564beead5eb54..cd47300678d3dc 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -201,8 +201,8 @@ export interface BuildEnvironmentOptions { */ copyPublicDir?: boolean /** - * Whether to emit a .vite/license.md that includes all bundled dependencies' - * licenses. Specify a path that ends with `.json` to generate a raw JSON.entry. + * Whether to emit a `.vite/license.md` file that includes all bundled dependencies' + * licenses. Specify a path that ends with `.json` to generate a raw JSON entry. * @default false */ license?: boolean | string diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a8236b1c7258f6..b7175d02ac84aa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -429,6 +429,25 @@ importers: packages/vite/src/node/__tests__/packages/noname: {} + packages/vite/src/node/__tests__/plugins/fixtures/license: + dependencies: + '@vitejs/test-dep-licence-cc0': + specifier: file:./dep-licence-cc0 + version: file:packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0 + '@vitejs/test-dep-license-mit': + specifier: file:./dep-license-mit + version: file:packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit + + packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0: {} + + packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit: + dependencies: + '@vitejs/test-dep-nested-licence-isc': + specifier: file:../dep-nested-license-isc + version: '@vitejs/test-dep-nested-license-isc@file:packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc' + + packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc: {} + packages/vite/src/node/server/__tests__/fixtures/lerna/nested: {} packages/vite/src/node/server/__tests__/fixtures/none/nested: {} @@ -3242,6 +3261,15 @@ packages: '@vitejs/test-dep-incompatible@file:playground/optimize-deps/dep-incompatible': resolution: {directory: playground/optimize-deps/dep-incompatible, type: directory} + '@vitejs/test-dep-licence-cc0@file:packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0': + resolution: {directory: packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0, type: directory} + + '@vitejs/test-dep-license-mit@file:packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit': + resolution: {directory: packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit, type: directory} + + '@vitejs/test-dep-nested-license-isc@file:packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc': + resolution: {directory: packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc, type: directory} + '@vitejs/test-dep-no-discovery@file:playground/optimize-deps-no-discovery/dep-no-discovery': resolution: {directory: playground/optimize-deps-no-discovery/dep-no-discovery, type: directory} @@ -8706,6 +8734,14 @@ snapshots: '@vitejs/test-dep-incompatible@file:playground/optimize-deps/dep-incompatible': {} + '@vitejs/test-dep-licence-cc0@file:packages/vite/src/node/__tests__/plugins/fixtures/license/dep-licence-cc0': {} + + '@vitejs/test-dep-license-mit@file:packages/vite/src/node/__tests__/plugins/fixtures/license/dep-license-mit': + dependencies: + '@vitejs/test-dep-nested-licence-isc': '@vitejs/test-dep-nested-license-isc@file:packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc' + + '@vitejs/test-dep-nested-license-isc@file:packages/vite/src/node/__tests__/plugins/fixtures/license/dep-nested-license-isc': {} + '@vitejs/test-dep-no-discovery@file:playground/optimize-deps-no-discovery/dep-no-discovery': {} '@vitejs/test-dep-node-env@file:playground/optimize-deps/dep-node-env': {} From 744eea4d77e70039bb6e6bb98a3c1c70a2d24407 Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 4 Nov 2024 14:46:19 +0800 Subject: [PATCH 4/5] chore: remove --- playground/optimize-deps/vite.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/playground/optimize-deps/vite.config.js b/playground/optimize-deps/vite.config.js index e3ca7e62b5e18a..6ef09488556cc1 100644 --- a/playground/optimize-deps/vite.config.js +++ b/playground/optimize-deps/vite.config.js @@ -47,7 +47,6 @@ export default defineConfig({ build: { // to make tests faster minify: false, - license: true, rollupOptions: { onwarn(msg, warn) { // filter `"Buffer" is not exported by "__vite-browser-external"` warning From ac5b6ea02e63fb470c4e64f669121f3e85a55fdf Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 4 Nov 2024 14:50:22 +0800 Subject: [PATCH 5/5] chore: update docs --- docs/config/build-options.md | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/docs/config/build-options.md b/docs/config/build-options.md index b7f2f6c8cc616c..c14a2dedec3f40 100644 --- a/docs/config/build-options.md +++ b/docs/config/build-options.md @@ -190,7 +190,25 @@ export default defineConfig({ - **Type:** `boolean | string` - **Default:** `false` -When set to `true`, the build will also generate a `.vite/license.md` file that includes all bundled dependencies' licenses. It can be hosted to display and acknowledge the dependencies used by the app. When the value is a string, it will be used as the license file name. +When set to `true`, the build will also generate a `.vite/license.md` file that includes all bundled dependencies' licenses. It can be hosted to display and acknowledge the dependencies used by the app. When the value is a string, it will be used as the license file name. An example output may look like this: + +```md +# Licenses + +The app bundles dependencies which contains the following licenses: + +## dep-1 - 1.2.3 (CC0-1.0) + +CC0 1.0 Universal + +... + +## dep-2 - 4.5.6 (MIT) + +MIT License + +... +``` If a string is passed that ends with `.json`, a raw JSON file will be generated instead. For example: @@ -198,15 +216,15 @@ If a string is passed that ends with `.json`, a raw JSON file will be generated [ { "name": "dep-1", - "version": "0.0.0", + "version": "1.2.3", "identifier": "CC0-1.0", - "text": "CC0 1.0 Universal ..." + "text": "CC0 1.0 Universal\n\n..." }, { "name": "dep-2", - "version": "0.0.0", + "version": "4.5.6", "identifier": "MIT", - "text": "MIT License ..." + "text": "MIT License\n\n..." } ] ```