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: add es.min format for lib mode #6585

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 26 additions & 7 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ export interface LibraryOptions {
entry: string
name?: string
formats?: LibraryFormats[]
fileName?: string | ((format: ModuleFormat) => string)
fileName?: string | ((format: ModuleFormat | LibraryFormats) => string)
}

export type LibraryFormats = 'es' | 'cjs' | 'umd' | 'iife'
export type LibraryFormats = 'es' | 'es.min' | 'cjs' | 'umd' | 'iife'

export type ResolvedBuildOptions = Required<
Omit<
Expand Down Expand Up @@ -471,7 +471,13 @@ async function doBuild(
entryFileNames: ssr
? `[name].js`
: libOptions
? resolveLibFilename(libOptions, output.format || 'es', config.root)
? resolveLibFilename(
libOptions,
output.format || 'es',
config.root,
// @ts-ignore
output.__vite_lib_minify__
)
: path.posix.join(options.assetsDir, `[name].[hash].js`),
chunkFileNames: libOptions
? `[name].js`
Expand Down Expand Up @@ -668,7 +674,8 @@ function staticImportedByEntry(
export function resolveLibFilename(
libOptions: LibraryOptions,
format: ModuleFormat,
root: string
root: string,
esMinify?: boolean
): string {
if (typeof libOptions.fileName === 'function') {
return libOptions.fileName(format)
Expand All @@ -681,7 +688,19 @@ export function resolveLibFilename(
'Name in package.json is required if option "build.lib.fileName" is not provided.'
)

return `${name}.${format}.js`
return `${name}.${esMinify ? 'es.min' : format}.js`
}

function resolveLibFormat(
format: LibraryFormats,
output?: OutputOptions
): OutputOptions {
return {
...output,
...(format === 'es.min'
? { format: 'es', __vite_lib_minify__: true }
: { format })
}
}

function resolveBuildOutputs(
Expand All @@ -701,9 +720,9 @@ function resolveBuildOutputs(
)
}
if (!outputs) {
return formats.map((format) => ({ format }))
return formats.map((format) => resolveLibFormat(format))
} else if (!Array.isArray(outputs)) {
return formats.map((format) => ({ ...outputs, format }))
return formats.map((format) => resolveLibFormat(format, outputs))
} else if (libOptions.formats) {
// user explicitly specifying own output array
logger.warn(
Expand Down
14 changes: 13 additions & 1 deletion packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ const rollupToEsbuildFormatMap: Record<
export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
return {
name: 'vite:esbuild-transpile',
outputOptions(opts) {
// @ts-ignore inject by resolveLibFormat call
if (opts.__vite_lib_minify__) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the __vite_lib_minify__ can save into config: ResolvedConfig ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResolvedConfig will include mutiple format,maybe both es and es.min.And save into config: ResolvedConfig making this case not work.

Copy link
Member

@poyoho poyoho Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Record<string, boolean>?

This comment was marked as abuse.

// @ts-ignore
delete opts.__vite_lib_minify__
// @ts-ignore
this.meta.__vite_lib_minify__ = true
}
return null
},
async renderChunk(code, chunk, opts) {
// @ts-ignore injected by @vitejs/plugin-legacy
if (opts.__vite_skip_esbuild__) {
Expand All @@ -236,7 +246,9 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
// Do not minify ES lib output since that would remove pure annotations
// and break tree-shaking
// https://github.com/vuejs/core/issues/2860#issuecomment-926882793
!(config.build.lib && opts.format === 'es')
(!(config.build.lib && opts.format === 'es') ||
// @ts-ignore
this.meta.__vite_lib_minify__)

if ((!target || target === 'esnext') && !minify) {
return null
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/plugins/terser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ export function terserPlugin(config: ResolvedConfig): Plugin {

// Do not minify ES lib output since that would remove pure annotations
// and break tree-shaking.
if (config.build.lib && outputOptions.format === 'es') {
if (
config.build.lib &&
outputOptions.format === 'es' &&
// @ts-ignore inject by buildEsbuildPlugin
!this.meta.__vite_lib_minify__
) {
return null
}

Expand Down