Skip to content
Merged
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
65 changes: 65 additions & 0 deletions docs/config/build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,71 @@ export default defineConfig({
})
```

## build.license

- **Type:** `boolean | { fileName?: string }`
- **Default:** `false`

When set to `true`, the build will 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 `fileName` is passed, it will be used as the license file name relative to the `outDir`. An example output may look like this:

```md
# Licenses

The app bundles dependencies which contain the following licenses:

## dep-1 - 1.2.3 (CC0-1.0)

CC0 1.0 Universal

...

## dep-2 - 4.5.6 (MIT)

MIT License

...
```

If the `fileName` ends with `.json`, the raw JSON metadata will be generated instead and can be used for further processing. 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..."
}
]
```

::: tip
If you'd like to reference the license file in the built code, you can use `build.rollupOptions.output.banner` to inject a comment at the top of the files. For example:

```js twoslash [vite.config.js]
import { defineConfig } from 'vite'

export default defineConfig({
build: {
license: true,
rollupOptions: {
output: {
banner:
'/* See licenses of bundled dependencies at https://example.com/license.md */',
},
},
},
})
```

:::

## 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 contain 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 @@ -23,7 +23,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'es',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: 'es2020',
format: 'esm',
Expand Down Expand Up @@ -67,7 +66,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'es',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: undefined,
format: 'esm',
Expand Down Expand Up @@ -98,7 +96,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'es',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: 'es2020',
format: 'esm',
Expand Down Expand Up @@ -131,7 +128,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'es',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: undefined,
format: 'esm',
Expand Down Expand Up @@ -164,7 +160,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'cjs',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: undefined,
format: 'cjs',
Expand Down Expand Up @@ -196,7 +191,6 @@ describe('resolveEsbuildTranspileOptions', () => {
'es',
)
expect(options).toEqual({
charset: 'utf8',
loader: 'js',
target: undefined,
format: 'esm',
Expand Down Expand Up @@ -232,7 +226,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-license-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-license-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"
}
}
38 changes: 38 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,38 @@
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: {
fileName: '.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 @@ -71,6 +71,7 @@ import { getHookHandler } from './plugins'
import { BaseEnvironment } from './baseEnvironment'
import type { MinimalPluginContextWithoutEnvironment, Plugin } from './plugin'
import type { RollupPluginHooks } from './typeUtils'
import { type LicenseOptions, licensePlugin } from './plugins/license'
import {
BasicMinimalPluginContext,
basePluginContextMeta,
Expand Down Expand Up @@ -209,6 +210,12 @@ export interface BuildEnvironmentOptions {
* @default true
*/
copyPublicDir?: boolean
/**
* Whether to emit a `.vite/license.md` file that includes all bundled dependencies'
* licenses. Pass an object to customize the output file name.
* @default false
*/
license?: boolean | LicenseOptions
/**
* Whether to emit a .vite/manifest.json in the output dir to map hash-less filenames
* to their hashed versions. Useful when you want to generate your own HTML
Expand Down Expand Up @@ -380,6 +387,7 @@ const _buildEnvironmentOptionsDefaults = Object.freeze({
write: true,
emptyOutDir: null,
copyPublicDir: true,
license: false,
manifest: false,
lib: false,
// ssr
Expand Down Expand Up @@ -495,7 +503,12 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
buildEsbuildPlugin(),
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 @@ -1518,6 +1518,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 @@ -858,6 +858,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 @@ -2194,7 +2194,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 @@ -199,7 +199,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 @@ -249,7 +249,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 @@ -403,7 +402,6 @@ export function resolveEsbuildTranspileOptions(
const esbuildOptions = config.esbuild || {}

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