Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): emit license #18546

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/config/build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,52 @@ 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. 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:

```json
[
{
"name": "dep-1",
"version": "1.2.3",
"identifier": "CC0-1.0",
"text": "CC0 1.0 Universal\n\n..."
},
{
"name": "dep-2",
"version": "4.5.6",
"identifier": "MIT",
"text": "MIT License\n\n..."
}
]
```

It can be used for further processing to output as a different format.

## build.manifest

- **Type:** `boolean | string`
Expand Down
Original file line number Diff line number Diff line change
@@ -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) ...
"
`;
7 changes: 0 additions & 7 deletions packages/vite/src/node/__tests__/plugins/esbuild.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'es',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: 'es2020',
format: 'esm',
Expand Down Expand Up @@ -64,7 +63,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'es',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: undefined,
format: 'esm',
Expand Down Expand Up @@ -95,7 +93,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'es',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: 'es2020',
format: 'esm',
Expand Down Expand Up @@ -128,7 +125,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'es',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: undefined,
format: 'esm',
Expand Down Expand Up @@ -161,7 +157,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'cjs',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: undefined,
format: 'cjs',
Expand Down Expand Up @@ -193,7 +188,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'es',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: undefined,
format: 'esm',
Expand Down Expand Up @@ -229,7 +223,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'cjs',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: undefined,
format: 'cjs',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'ok'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CC0 1.0 Universal

...
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@vitejs/test-dep-licence-cc0",
"private": true,
"version": "0.0.0",
"type": "module",
"license": "CC0-1.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import nestedDep from '@vitejs/test-dep-nested-licence-isc'

export default 'ok' + nestedDep
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MIT License

Copyright (c) ...
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Copyright (c) ...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'ok'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@vitejs/test-dep-nested-license-isc",
"private": true,
"version": "0.0.0",
"type": "module",
"license": "ISC"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script type="module">
import dep1 from '@vitejs/test-dep-licence-cc0'
import dep2 from '@vitejs/test-dep-license-mit'
console.log(dep1, dep2)
</script>
Original file line number Diff line number Diff line change
@@ -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"
}
}
36 changes: 36 additions & 0 deletions packages/vite/src/node/__tests__/plugins/license.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
15 changes: 14 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -199,6 +200,12 @@ export interface BuildEnvironmentOptions {
* @default true
*/
copyPublicDir?: boolean
/**
* 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
/**
* 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
Expand Down Expand Up @@ -383,6 +390,7 @@ export function resolveBuildEnvironmentOptions(
write: true,
emptyOutDir: null,
copyPublicDir: true,
license: false,
manifest: false,
lib: false,
ssr: consumer === 'server',
Expand Down Expand Up @@ -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(),
],
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ async function prepareEsbuildOptimizerRun(
metafile: true,
plugins,
charset: 'utf8',
legalComments: 'none',
...esbuildOptions,
supported: {
...defaultEsbuildSupported,
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -390,7 +389,6 @@ export function resolveEsbuildTranspileOptions(
const esbuildOptions = config.esbuild || {}

const options: TransformOptions = {
charset: 'utf8',
...esbuildOptions,
loader: 'js',
target: target || undefined,
Expand Down
Loading