Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app): Polyfill node core modules by default when building apps #11

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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