From 54565c5be80cf170e813d5e861585ae35a030838 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 1 Aug 2023 16:29:41 +0200 Subject: [PATCH] fix(app): Polyfill node core modules by default when building apps Signed-off-by: Ferdinand Thiessen --- lib/appConfig.ts | 24 ++++++++++++++++-------- lib/baseConfig.ts | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/appConfig.ts b/lib/appConfig.ts index bbc015c..dca6074 100644 --- a/lib/appConfig.ts +++ b/lib/appConfig.ts @@ -5,7 +5,7 @@ */ import type { UserConfig, UserConfigFn } from 'vite' -import type { BaseOptions } from './baseConfig.js' +import type { BaseOptions, NodePolyfillsOptions } from './baseConfig.js' import { mergeConfig } from 'vite' import { createBaseConfig } from './baseConfig.js' @@ -17,12 +17,20 @@ export const appName = process.env.npm_package_name export const appVersion = process.env.npm_package_version export const appNameSanitized = appName.replace(/[/\\]/, '-') -export interface AppOptions extends BaseOptions { - /** - * Whether to empty the output directory (`js/`) - * @default true - */ - emptyOutputDirectory?: boolean +export interface AppOptions extends Omit { + /** + * Whether to empty the output directory (`js/`) + * @default true + */ + emptyOutputDirectory?: boolean + + /** + * Inject polyfills for node packages + * By default all node core modules are polyfilled, including prefixed with `node:` protocol + * + * @default {protocolImports: true} + */ + nodePolyfills?: boolean | NodePolyfillsOptions } /** @@ -39,7 +47,7 @@ export interface AppOptions extends BaseOptions { */ export const createAppConfig = (entries: { [entryAlias: string]: string }, options: AppOptions = {}): UserConfigFn => { // Add default options - options = { config: {}, ...options } + options = { config: {}, nodePolyfills: { protocolImports: true }, ...options } return createBaseConfig({ ...options, diff --git a/lib/baseConfig.ts b/lib/baseConfig.ts index 62f693f..667da4f 100644 --- a/lib/baseConfig.ts +++ b/lib/baseConfig.ts @@ -17,7 +17,7 @@ import browserslistToEsbuild from 'browserslist-to-esbuild' import license from 'rollup-plugin-license' import injectCSSPlugin from 'vite-plugin-css-injected-by-js' -type NodePolyfillsOptions = Parameters[0] +export type NodePolyfillsOptions = Parameters[0] export interface BaseOptions { /** Strings to replace within your code */