Skip to content

Commit

Permalink
Merge pull request #1565 from nickgros/SWC-7242
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgros authored Feb 14, 2025
2 parents ed11a14 + 229a580 commit d3fcd0b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
9 changes: 7 additions & 2 deletions packages/synapse-react-client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import { ConfigBuilder } from 'vite-config'
*/
const config = new ConfigBuilder()
.setIncludeReactConfig(true)
.setIncludeLibraryConfig(true)
.setBuildLibEntry(resolve(__dirname, 'src/index.ts'))
.setIncludeLibraryConfig(true, {
except: [
// Include @sage-bionetworks/synapse-types because the local version may drift from the version released on NPM
'@sage-bionetworks/synapse-types',
],
})
.setBuildLibEntry(resolve(__dirname, 'src/umd.index.ts'))
.setConfigOverrides({
root: '.',
build: {
Expand Down
20 changes: 9 additions & 11 deletions packages/vite-config/src/ConfigBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import viteLibraryConfig from './vite-library-config.js'
import vitestConfig from './vitest-config.js'

export class ConfigBuilder {
private includeReactConfig = false
private includeLibraryConfig = false
private buildLibEntry: string | string[] | undefined = undefined
private includeVitestConfig = false
private pluginConfigOptions: PluginConfigOptions = {}
private configOverrides: Record<string, any> | null = null

setIncludeReactConfig(includeReactConfig: boolean): ConfigBuilder {
this.includeReactConfig = includeReactConfig
this.pluginConfigOptions.includeReactPlugins = includeReactConfig
return this
}

Expand All @@ -27,15 +26,14 @@ export class ConfigBuilder {
return this
}

setIncludeLibraryConfig(includeLibraryConfig: boolean): ConfigBuilder {
this.includeLibraryConfig = includeLibraryConfig
return this
}

setPluginConfigOptions(
pluginConfigOptions: PluginConfigOptions,
setIncludeLibraryConfig(
includeLibraryConfig: boolean,
externalizeDepsOptions?: Parameters<
typeof getPluginConfig
>[0]['externalizeDepsOptions'],
): ConfigBuilder {
this.pluginConfigOptions = pluginConfigOptions
this.includeLibraryConfig = includeLibraryConfig
this.pluginConfigOptions.externalizeDepsOptions = externalizeDepsOptions
return this
}

Expand Down Expand Up @@ -63,7 +61,7 @@ export class ConfigBuilder {
if (this.pluginConfigOptions) {
config = mergeConfig(config, {
plugins: getPluginConfig({
includeReactPlugins: this.includeReactConfig,
...this.pluginConfigOptions,
includeLibraryPlugins: this.includeLibraryConfig,
}),
})
Expand Down
23 changes: 14 additions & 9 deletions packages/vite-config/src/pluginConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dts from 'vite-plugin-dts'
export type PluginConfigOptions = {
includeReactPlugins?: boolean
includeLibraryPlugins?: boolean
externalizeDepsOptions?: Parameters<typeof externalizeDeps>[0]
}

/**
Expand All @@ -34,14 +35,18 @@ const REACT_PLUGINS: PluginOption[] = [
/**
* Plugins that libraries that should emit ESM and CJS bundles will use
*/
const LIBRARY_PLUGINS: PluginOption[] = [
// Do not bundle any dependencies; the consumer's bundler will resolve and link them.
externalizeDeps(),
// Generate a single type definition file for distribution.
dts({
rollupTypes: true,
}),
]
function getLibraryPlugins(
externalizeDepsOptions?: Parameters<typeof externalizeDeps>[0],
): PluginOption[] {
return [
// Do not bundle any dependencies; the consumer's bundler will resolve and link them.
externalizeDeps(externalizeDepsOptions),
// Generate a single type definition file for distribution.
dts({
rollupTypes: true,
}),
]
}

/**
* Get a shared configuration of Vite plugins to use based on the provided options. Note that Vite does not deeply merge
Expand All @@ -53,7 +58,7 @@ export function getPluginConfig(options: PluginConfigOptions): PluginOption[] {
plugins.push(...REACT_PLUGINS)
}
if (options.includeLibraryPlugins) {
plugins.push(...LIBRARY_PLUGINS)
plugins.push(...getLibraryPlugins(options.externalizeDepsOptions))
}
return plugins
}

0 comments on commit d3fcd0b

Please sign in to comment.