Skip to content

Commit

Permalink
refactor: lib options
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Apr 8, 2024
1 parent 3ba9214 commit 70731ce
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 82 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/__tests__/dev.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'vitest'
import { resolveConfig } from '..'

describe('resolveBuildOptions in dev', () => {
describe('resolveBuildEnvironmentOptions in dev', () => {
test('build.rollupOptions should not have input in lib', async () => {
const config = await resolveConfig(
{
Expand Down
118 changes: 78 additions & 40 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import { webWorkerPostPlugin } from './plugins/worker'
import { getHookHandler } from './plugins'
import { Environment } from './environment'

export interface BuildOptions {
export interface BuildEnvironmentOptions {
/**
* Compatibility transform target. The transform is performed with esbuild
* and the lowest supported target is es2015/es6. Note this only handles
Expand Down Expand Up @@ -210,13 +210,6 @@ export interface BuildOptions {
* @default false
*/
manifest?: boolean | string
/**
* Build in library mode. The value should be the global name of the lib in
* UMD mode. This will produce esm + cjs + umd bundle formats with default
* configurations that are suitable for distributing libraries.
* @default false
*/
lib?: LibraryOptions | false
/**
* Produce SSR oriented build. Note this requires specifying SSR entry via
* `rollupOptions.input`.
Expand Down Expand Up @@ -265,6 +258,16 @@ export interface BuildOptions {
createEnvironment?: (builder: ViteBuilder, name: string) => BuildEnvironment
}

export interface BuildOptions extends BuildEnvironmentOptions {
/**
* Build in library mode. The value should be the global name of the lib in
* UMD mode. This will produce esm + cjs + umd bundle formats with default
* configurations that are suitable for distributing libraries.
* @default false
*/
lib?: LibraryOptions | false
}

export interface LibraryOptions {
/**
* Path of library entry
Expand Down Expand Up @@ -317,53 +320,72 @@ export type ResolveModulePreloadDependenciesFn = (
},
) => string[]

export interface ResolvedBuildEnvironmentOptions
extends Required<Omit<BuildEnvironmentOptions, 'polyfillModulePreload'>> {
modulePreload: false | ResolvedModulePreloadOptions
}

export interface ResolvedBuildOptions
extends Required<Omit<BuildOptions, 'polyfillModulePreload'>> {
modulePreload: false | ResolvedModulePreloadOptions
}

export function resolveBuildOptions(
raw: BuildOptions | undefined,
raw: BuildOptions,
logger: Logger,
root: string,
environmentName: string | undefined,
): ResolvedBuildOptions {
const libMode = raw.lib ?? false
const buildOptions = resolveBuildEnvironmentOptions(
raw,
logger,
root,
undefined,
libMode,
)
return { ...buildOptions, lib: libMode }
}

export function resolveBuildEnvironmentOptions(
raw: BuildEnvironmentOptions,
logger: Logger,
root: string,
environmentName: string | undefined,
libMode: false | LibraryOptions = false,
): ResolvedBuildEnvironmentOptions {
const deprecatedPolyfillModulePreload = raw?.polyfillModulePreload
if (raw) {
const { polyfillModulePreload, ...rest } = raw
raw = rest
if (deprecatedPolyfillModulePreload !== undefined) {
logger.warn(
'polyfillModulePreload is deprecated. Use modulePreload.polyfill instead.',
)
}
if (
deprecatedPolyfillModulePreload === false &&
raw.modulePreload === undefined
) {
raw.modulePreload = { polyfill: false }
}
const { polyfillModulePreload, ...rest } = raw
raw = rest
if (deprecatedPolyfillModulePreload !== undefined) {
logger.warn(
'polyfillModulePreload is deprecated. Use modulePreload.polyfill instead.',
)
}
if (
deprecatedPolyfillModulePreload === false &&
raw.modulePreload === undefined
) {
raw.modulePreload = { polyfill: false }
}

const modulePreload = raw?.modulePreload
const modulePreload = raw.modulePreload
const defaultModulePreload = {
polyfill: true,
}

const defaultBuildOptions: BuildOptions = {
const defaultBuildEnvironmentOptions: BuildEnvironmentOptions = {
outDir: 'dist',
assetsDir: 'assets',
assetsInlineLimit: DEFAULT_ASSETS_INLINE_LIMIT,
cssCodeSplit: !raw?.lib,
cssCodeSplit: !libMode,
sourcemap: false,
rollupOptions: {},
minify: raw?.ssr ? false : 'esbuild',
minify: raw.ssr ? false : 'esbuild',
terserOptions: {},
write: true,
emptyOutDir: null,
copyPublicDir: true,
manifest: false,
lib: false,
ssr: false,
ssrManifest: false,
ssrEmitAssets: false,
Expand All @@ -373,24 +395,24 @@ export function resolveBuildOptions(
watch: null,
}

const userBuildOptions = raw
? mergeConfig(defaultBuildOptions, raw)
: defaultBuildOptions
const userBuildEnvironmentOptions = raw
? mergeConfig(defaultBuildEnvironmentOptions, raw)
: defaultBuildEnvironmentOptions

// @ts-expect-error Fallback options instead of merging
const resolved: ResolvedBuildOptions = {
const resolved: ResolvedBuildEnvironmentOptions = {
target: 'modules',
cssTarget: false,
...userBuildOptions,
...userBuildEnvironmentOptions,
commonjsOptions: {
include: [/node_modules/],
extensions: ['.js', '.cjs'],
...userBuildOptions.commonjsOptions,
...userBuildEnvironmentOptions.commonjsOptions,
},
dynamicImportVarsOptions: {
warnOnError: true,
exclude: [/node_modules/],
...userBuildOptions.dynamicImportVarsOptions,
...userBuildEnvironmentOptions.dynamicImportVarsOptions,
},
// Resolve to false | object
modulePreload:
Expand Down Expand Up @@ -489,22 +511,37 @@ export async function build(
{},
{ ...inlineConfig, plugins: () => inlineConfig.plugins ?? [] },
)
const ssr = !!builder.config.build.ssr
const environment = builder.environments[ssr ? 'ssr' : 'client']
return builder.build(environment)

if (builder.config.build.lib) {
// TODO: temporal workaround. Should we support `libraries: Record<string, LibraryOptions & EnvironmentOptions>`
// to build multiple libraries and be able to target different environments (for example for a Svelte components
// library generating both client and SSR builds)?
return buildEnvironment(
builder.config,
builder.environments.client,
builder.config.build.lib,
)
} else {
const ssr = !!builder.config.build.ssr
const environment = builder.environments[ssr ? 'ssr' : 'client']
return builder.build(environment)
}
}

function resolveConfigToBuild(inlineConfig: InlineConfig = {}) {
return resolveConfig(inlineConfig, 'build', 'production', 'production')
}

/**
* Build an App environment, or a App library (if librayOptions is provided)
**/
export async function buildEnvironment(
config: ResolvedConfig,
environment: BuildEnvironment,
libOptions: LibraryOptions | false = false,
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
const options = config.build
const ssr = environment.name !== 'client'
const libOptions = options.lib

config.logger.info(
colors.cyan(
Expand Down Expand Up @@ -623,6 +660,7 @@ export async function buildEnvironment(
}

/**
* TODO:
* if (ssr.target === 'webworker') {
* build.rollupOptions.entryFileNames = '[name].js'
* build.rollupOptions.inlineDynamicImports = (typeof input === 'string' || Object.keys(input).length === 1))
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { performance } from 'node:perf_hooks'
import { cac } from 'cac'
import colors from 'picocolors'
import { VERSION } from './constants'
import type { BuildOptions } from './build'
import type { BuildEnvironmentOptions } from './build'
import type { ServerOptions } from './server'
import type { CLIShortcut } from './shortcuts'
import type { LogLevel } from './logger'
Expand Down Expand Up @@ -287,12 +287,12 @@ cli
.action(
async (
root: string,
options: BuildOptions & BuilderCLIOptions & GlobalCLIOptions,
options: BuildEnvironmentOptions & BuilderCLIOptions & GlobalCLIOptions,
) => {
filterDuplicateOptions(options)
const { build, createViteBuilder } = await import('./build')

const buildOptions: BuildOptions = cleanGlobalCLIOptions(
const buildOptions: BuildEnvironmentOptions = cleanGlobalCLIOptions(
cleanBuilderCLIOptions(options),
)

Expand Down
Loading

0 comments on commit 70731ce

Please sign in to comment.