-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor vite-configs to simplify sharing code, expose a config builder
- Loading branch information
Showing
15 changed files
with
780 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import { vitestConfig } from 'vite-config' | ||
import { mergeConfig } from 'vitest/config' | ||
import { ConfigBuilder } from 'vite-config' | ||
|
||
export default mergeConfig(vitestConfig, { | ||
test: { | ||
include: ['src/**/*.test.[jt]s?(x)'], | ||
setupFiles: ['src/tests/setupTests.ts'], | ||
environment: 'jsdom', // introduced due to random "ReferenceError: window is not defined" during tests | ||
}, | ||
}) | ||
const config = new ConfigBuilder() | ||
.setIncludeReactConfig(true) | ||
.setIncludeVitestConfig(true) | ||
.setConfigOverrides({ | ||
test: { | ||
include: ['src/**/*.test.[jt]s?(x)'], | ||
setupFiles: ['src/tests/setupTests.ts'], | ||
environment: 'jsdom', // introduced due to random "ReferenceError: window is not defined" during tests | ||
}, | ||
}) | ||
.build() | ||
|
||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import { vitestConfig } from 'vite-config' | ||
import { mergeConfig } from 'vitest/config' | ||
import { ConfigBuilder } from 'vite-config' | ||
|
||
export default mergeConfig(vitestConfig, { | ||
test: { | ||
include: ['src/test/**/*.test.[jt]s?(x)'], | ||
setupFiles: ['src/test/setupTests.ts'], | ||
environment: 'jsdom', // introduced due to random "ReferenceError: window is not defined" during tests | ||
}, | ||
}) | ||
const config = new ConfigBuilder() | ||
.setIncludeReactConfig(true) | ||
.setIncludeVitestConfig(true) | ||
.setConfigOverrides({ | ||
test: { | ||
include: ['src/test/**/*.test.[jt]s?(x)'], | ||
setupFiles: ['src/test/setupTests.ts'], | ||
environment: 'jsdom', // introduced due to random "ReferenceError: window is not defined" during tests | ||
}, | ||
}) | ||
.build() | ||
|
||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,18 @@ | ||
import { vitestConfig } from 'vite-config' | ||
import { mergeConfig } from 'vitest/config' | ||
import { ConfigBuilder } from 'vite-config' | ||
import { resolve } from 'path' | ||
export default mergeConfig(vitestConfig, { | ||
build: { | ||
outDir: 'dist', | ||
lib: { | ||
// Could also be a dictionary or array of multiple entry points | ||
entry: resolve(__dirname, 'src/index.ts'), | ||
name: 'SynapsePortalFramework', | ||
// the proper extensions will be added | ||
fileName: 'synapse-portal-framework', | ||
|
||
const config = new ConfigBuilder() | ||
.setIncludeReactConfig(true) | ||
.setIncludeLibraryConfig(true) | ||
.setBuildLibEntry(resolve(__dirname, 'src/index.ts')) | ||
.setIncludeVitestConfig(true) | ||
.setConfigOverrides({ | ||
test: { | ||
include: ['src/**/*.test.[jt]s?(x)'], | ||
setupFiles: ['src/tests/setupTests.ts'], | ||
environment: 'jsdom', | ||
}, | ||
rollupOptions: { | ||
// make sure to externalize deps that shouldn't be bundled | ||
// into your library | ||
external: ['react'], | ||
output: { | ||
// Provide global variables to use in the UMD build | ||
// for externalized deps | ||
globals: { | ||
react: 'React', | ||
}, | ||
}, | ||
}, | ||
}, | ||
test: { | ||
include: ['src/**/*.test.[jt]s?(x)'], | ||
setupFiles: ['src/tests/setupTests.ts'], | ||
environment: 'jsdom', | ||
}, | ||
}) | ||
}) | ||
.build() | ||
|
||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,20 @@ | ||
import { vitestConfig } from 'vite-config' | ||
import { mergeConfig } from 'vitest/config' | ||
import { resolve } from 'path' | ||
import { externalizeDeps } from 'vite-plugin-externalize-deps' | ||
import dts from 'vite-plugin-dts' | ||
import { ConfigBuilder } from 'vite-config' | ||
|
||
export default mergeConfig(vitestConfig, { | ||
build: { | ||
sourcemap: true, | ||
emptyOutDir: true, | ||
outDir: './dist', | ||
lib: { | ||
entry: resolve(__dirname, 'src/index.ts'), | ||
name: 'markdownitSynapseTable', | ||
fileName: 'index', | ||
formats: ['es', 'cjs', 'umd'], | ||
export default new ConfigBuilder() | ||
.setIncludeLibraryConfig(true) | ||
.setBuildLibEntry(resolve(__dirname, 'src/index.ts')) | ||
.setIncludeVitestConfig(true) | ||
.setConfigOverrides({ | ||
build: { | ||
lib: { | ||
name: 'markdownitSynapseTable', | ||
formats: ['es', 'cjs', 'umd'], | ||
}, | ||
}, | ||
}, | ||
test: { | ||
globals: true, | ||
include: ['test/**/*.test.[jt]s?(x)'], | ||
}, | ||
plugins: [ | ||
// 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, | ||
}), | ||
], | ||
}) | ||
test: { | ||
globals: true, | ||
include: ['test/**/*.test.[jt]s?(x)'], | ||
}, | ||
}) | ||
.build() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,20 @@ | ||
import { resolve } from 'path' | ||
import { mergeConfig } from 'vite' | ||
import viteConfig from 'vite-config' | ||
import { externalizeDeps } from 'vite-plugin-externalize-deps' | ||
import dts from 'vite-plugin-dts' | ||
import { ConfigBuilder } from 'vite-config' | ||
|
||
/** | ||
* Vite config to generate the ESM & CJS bundles for Synapse React Client. | ||
*/ | ||
export default mergeConfig(viteConfig, { | ||
root: '.', | ||
build: { | ||
sourcemap: true, | ||
emptyOutDir: false, | ||
outDir: './dist', | ||
lib: { | ||
entry: resolve(__dirname, 'src/index.ts'), | ||
name: 'SRC', | ||
fileName: 'index', | ||
formats: ['es', 'cjs'], | ||
const config = new ConfigBuilder() | ||
.setIncludeReactConfig(true) | ||
.setIncludeLibraryConfig(true) | ||
.setBuildLibEntry(resolve(__dirname, 'src/index.ts')) | ||
.setConfigOverrides({ | ||
root: '.', | ||
build: { | ||
// Do not clean the output directory before building, since we build ESM/CJS and UMD separately. | ||
emptyOutDir: false, | ||
}, | ||
}, | ||
plugins: [ | ||
// 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, | ||
}), | ||
], | ||
}) | ||
}) | ||
.build() | ||
|
||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { getPluginConfig, PluginConfigOptions } from './pluginConfig.js' | ||
import viteConfig from './vite-config.js' | ||
import { mergeConfig } from 'vitest/config' | ||
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 | ||
return this | ||
} | ||
|
||
setIncludeVitestConfig(includeVitestConfig: boolean): ConfigBuilder { | ||
this.includeVitestConfig = includeVitestConfig | ||
return this | ||
} | ||
|
||
setBuildLibEntry(buildLibEntry: string | string[]): ConfigBuilder { | ||
this.buildLibEntry = buildLibEntry | ||
return this | ||
} | ||
|
||
setIncludeLibraryConfig(includeLibraryConfig: boolean): ConfigBuilder { | ||
this.includeLibraryConfig = includeLibraryConfig | ||
return this | ||
} | ||
|
||
setPluginConfigOptions( | ||
pluginConfigOptions: PluginConfigOptions, | ||
): ConfigBuilder { | ||
this.pluginConfigOptions = pluginConfigOptions | ||
return this | ||
} | ||
|
||
setConfigOverrides(configOverrides: Record<string, any>): ConfigBuilder { | ||
this.configOverrides = configOverrides | ||
return this | ||
} | ||
|
||
build() { | ||
let config = viteConfig | ||
if (this.includeLibraryConfig) { | ||
if (!this.buildLibEntry) { | ||
throw new Error( | ||
'buildLibEntry must be provided when includeLibraryConfig is true', | ||
) | ||
} | ||
config = mergeConfig(config, viteLibraryConfig) | ||
config = mergeConfig(config, { | ||
build: { lib: { entry: this.buildLibEntry } }, | ||
}) | ||
} | ||
if (this.includeVitestConfig) { | ||
config = mergeConfig(config, vitestConfig) | ||
} | ||
if (this.pluginConfigOptions) { | ||
config = mergeConfig(config, { | ||
plugins: getPluginConfig({ | ||
includeReactPlugins: this.includeReactConfig, | ||
includeLibraryPlugins: this.includeLibraryConfig, | ||
}), | ||
}) | ||
} | ||
if (this.configOverrides) { | ||
config = mergeConfig(config, this.configOverrides) | ||
} | ||
|
||
return config | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import viteConfig from './vite-config.js' | ||
import vitestConfig from './vitest-config.js' | ||
import portalsViteConfig from './portals-vite-config.js' | ||
import viteLibraryConfig from './vite-library-config.js' | ||
import { ConfigBuilder } from './ConfigBuilder.js' | ||
|
||
export { viteConfig, vitestConfig, portalsViteConfig } | ||
export { | ||
viteConfig, | ||
vitestConfig, | ||
portalsViteConfig, | ||
viteLibraryConfig, | ||
ConfigBuilder, | ||
} | ||
|
||
export default viteConfig |
Oops, something went wrong.