Skip to content

Commit

Permalink
fix(app): Polyfill node core modules by default when building apps
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Aug 1, 2023
1 parent 59b430e commit 54565c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 16 additions & 8 deletions lib/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<BaseOptions, 'nodePolyfills'> {
/**
* 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
}

/**
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/baseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof nodePolyfills>[0]
export type NodePolyfillsOptions = Parameters<typeof nodePolyfills>[0]

export interface BaseOptions {
/** Strings to replace within your code */
Expand Down

0 comments on commit 54565c5

Please sign in to comment.