diff --git a/shared/shared-vite-config.ts b/shared/shared-vite-config.ts new file mode 100644 index 00000000000..e4e2880443b --- /dev/null +++ b/shared/shared-vite-config.ts @@ -0,0 +1,19 @@ +import { UserConfigFn, mergeConfig } from 'vite'; +import { useLocalWebComponents, useLitWebComponents } from './web-components-vite-plugin'; + +export const mergeConfigs = (...configs: UserConfigFn[]) => { + return configs.reduce((acc, config) => mergeConfig(acc, config)); +}; + +export const sharedConfig: UserConfigFn = (env) => ({ + plugins: [ + useLitWebComponents() + + // Use local version of web-components, disabled by default. + // To use this, uncomment the lines below and change the path + // to your local web-components folder if needed (absolute or + // relative to this shared config). + // DO NOT COMMIT THESE CHANGES! + // useLocalWebComponents('../../web-components') + ] +}); diff --git a/shared/web-components-vite-plugin.ts b/shared/web-components-vite-plugin.ts index 1c27a6fec5f..bbee9f21639 100644 --- a/shared/web-components-vite-plugin.ts +++ b/shared/web-components-vite-plugin.ts @@ -4,23 +4,30 @@ import { PluginOption } from 'vite'; /** * Vite plugin that resolves Vaadin component JS modules to a * local checkout of the web-components repository - * @param webComponentsNodeModulesPath + * + * @param webComponentsRepoPath */ -export function useLocalWebComponents(webComponentsNodeModulesPath: string): PluginOption { +export function useLocalWebComponents(webComponentsRepoPath: string): PluginOption { + const nodeModulesPath = path.resolve(__dirname, `${webComponentsRepoPath}/node_modules`); + return { name: 'use-local-web-components', enforce: 'pre', - config(config) { - config.server = config.server ?? {}; - config.server.fs = config.server.fs ?? {}; - config.server.fs.allow = config.server.fs.allow ?? []; - config.server.fs.allow.push(webComponentsNodeModulesPath); - config.server.watch = config.server.watch ?? {}; - config.server.watch.ignored = [`!${webComponentsNodeModulesPath}/**`]; + config() { + return { + server: { + fs: { + allow: [nodeModulesPath] + }, + watch: { + ignored: [`!${nodeModulesPath}/**`] + } + } + }; }, resolveId(id) { if (/^(@polymer|@vaadin)/.test(id)) { - return this.resolve(path.join(webComponentsNodeModulesPath, id)); + return this.resolve(path.join(nodeModulesPath, id)); } } };