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

add include/exclude polyfills functionality #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions node-modules-polyfill/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ function removeEndingSlash(importee) {

export interface NodePolyfillsOptions {
name?: string
namespace?: string
namespace?: string,
include?: string[],
exclude?: string[]
}

export function NodeModulesPolyfillPlugin(
options: NodePolyfillsOptions = {},
): Plugin {
const { namespace = NAMESPACE, name = NAME } = options
const { namespace = NAMESPACE, name = NAME, include = [], exclude = [] } = options
if (namespace.endsWith('commonjs')) {
throw new Error(`namespace ${namespace} must not end with commonjs`)
}
Expand Down Expand Up @@ -80,11 +82,13 @@ export function NodeModulesPolyfillPlugin(
}
}
}
onLoad({ filter: /.*/, namespace }, loader)
onLoad({ filter: /.*/, namespace: commonjsNamespace }, loader)
const filter = new RegExp(
polyfilledBuiltinsNames.map(escapeStringRegexp).join('|'), // TODO builtins could end with slash, keep in mind in regex
(polyfilledBuiltinsNames.map(escapeStringRegexp) as string[])
.filter((value: string) => include.length ? include.includes(value) : exclude.length ? !exclude.includes(value) : true)
.join('|'), // TODO builtins could end with slash, keep in mind in regex
)
onLoad({ filter, namespace }, loader)
onLoad({ filter, namespace: commonjsNamespace }, loader)
async function resolver(args: OnResolveArgs) {
const ignoreRequire = args.namespace === commonjsNamespace

Expand Down