@fal-works/esbuild-plugin-global-externals
Advanced tools
Comparing version 1.0.0 to 1.1.0
import * as esbuild from "esbuild"; | ||
import type { GlobalsMapper, ModuleType, Options } from "./types"; | ||
export type { GlobalsMapper, ModuleType, Options }; | ||
/** | ||
* Object that contains a filter and a mapping function for replacing modules | ||
* with corresponding global variables. | ||
*/ | ||
export declare type GlobalsMapper = { | ||
/** | ||
* Regular expression that match module paths to be processed by this plugin. | ||
* | ||
* @see <https://esbuild.github.io/plugins/> | ||
*/ | ||
modulePathFilter: RegExp; | ||
/** | ||
* Function that returns a global variable name with which the import | ||
* statements of `modulePath` should be replaced. | ||
*/ | ||
getVariableName: (modulePath: string) => string; | ||
}; | ||
/** | ||
* Create a `Plugin` for replacing modules with corresponding global variables. | ||
@@ -27,4 +10,5 @@ * | ||
*/ | ||
export declare const globalExternalsWithRegExp: ( | ||
globals: GlobalsMapper | ||
export declare const globalExternalsWithRegExp: <T extends string>( | ||
globals: GlobalsMapper<T>, | ||
options?: Options<T> | undefined | ||
) => esbuild.Plugin; | ||
@@ -41,4 +25,5 @@ | ||
*/ | ||
export declare const globalExternals: ( | ||
globals: Record<string, string> | ||
export declare const globalExternals: <T extends string>( | ||
globals: Record<T, string>, | ||
options?: Options<T> | undefined | ||
) => esbuild.Plugin; | ||
@@ -45,0 +30,0 @@ export default globalExternals; |
const PLUGIN_NAME = "global-externals"; | ||
/** | ||
* Returns a function that determines module type for any specific module path. | ||
*/ | ||
const createGetModuleType = (options) => { | ||
const moduleType = | ||
options === null || options === void 0 ? void 0 : options.moduleType; | ||
if (!moduleType) return () => "esm"; | ||
if (typeof moduleType === "string") return () => moduleType; | ||
return (modulePath) => moduleType(modulePath) || "esm"; | ||
}; | ||
/** | ||
* Returns a function that creates string for `OnLoadResult.contents`. | ||
*/ | ||
const createGetContents = (getModuleType, getVariableName) => (modulePath) => { | ||
const variableName = getVariableName(modulePath); | ||
switch (getModuleType(modulePath)) { | ||
case "esm": | ||
return `export default ${variableName};`; | ||
case "cjs": | ||
return `module.exports = ${variableName};`; | ||
} | ||
}; | ||
/** | ||
* Create a `Plugin` for replacing modules with corresponding global variables. | ||
@@ -8,4 +32,6 @@ * | ||
*/ | ||
export const globalExternalsWithRegExp = (globals) => { | ||
export const globalExternalsWithRegExp = (globals, options) => { | ||
const { modulePathFilter, getVariableName } = globals; | ||
const getModuleType = createGetModuleType(options); | ||
const getContents = createGetContents(getModuleType, getVariableName); | ||
return { | ||
@@ -19,3 +45,4 @@ name: PLUGIN_NAME, | ||
build.onLoad({ filter: /.*/, namespace: PLUGIN_NAME }, (args) => ({ | ||
contents: `export default ${getVariableName(args.path)};`, | ||
// eslint-disable-next-line total-functions/no-unsafe-type-assertion | ||
contents: getContents(args.path), | ||
})); | ||
@@ -43,4 +70,4 @@ }, | ||
*/ | ||
export const globalExternals = (globals) => | ||
globalExternalsWithRegExp(mapperFromTable(globals)); | ||
export const globalExternals = (globals, options) => | ||
globalExternalsWithRegExp(mapperFromTable(globals), options); | ||
export default globalExternals; |
{ | ||
"name": "@fal-works/esbuild-plugin-global-externals", | ||
"description": "esbuild plugin for replacing imports with global variables.", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -33,2 +33,3 @@ "author": "FAL", | ||
"eslint-config-prettier": "^7.2.0", | ||
"eslint-plugin-total-functions": "^4.7.2", | ||
"prettier": "^2.2.1", | ||
@@ -35,0 +36,0 @@ "rimraf": "^3.0.2", |
@@ -27,1 +27,16 @@ # esbuild-plugin-global-externals | ||
``` | ||
If you prefer `RegExp` use `globalExternalsWithRegExp()` instead, however note that the `RegExp` you'll pass should be valid in Go language as well. | ||
## Options | ||
### Module type | ||
Default value is "esm", however this only works with default exports. You can also specify "cjs" instead, which might resolve this problem (though the emitted code may be a little redundant). | ||
```js | ||
globalExternals(globals, { | ||
moduleType: "cjs" | ||
}) | ||
``` |
import * as esbuild from "esbuild"; | ||
import type { GlobalsMapper, ModuleType, Options } from "./types"; | ||
export type { GlobalsMapper, ModuleType, Options }; | ||
const PLUGIN_NAME = "global-externals"; | ||
/** | ||
* Object that contains a filter and a mapping function for replacing modules | ||
* with corresponding global variables. | ||
* Returns a function that determines module type for any specific module path. | ||
*/ | ||
export type GlobalsMapper = { | ||
/** | ||
* Regular expression that match module paths to be processed by this plugin. | ||
* | ||
* @see <https://esbuild.github.io/plugins/> | ||
*/ | ||
modulePathFilter: RegExp; | ||
const createGetModuleType = <T extends string>(options?: Options<T>) => { | ||
const moduleType = options?.moduleType; | ||
/** | ||
* Function that returns a global variable name with which the import | ||
* statements of `modulePath` should be replaced. | ||
*/ | ||
getVariableName: (modulePath: string) => string; | ||
if (!moduleType) return (): ModuleType => "esm"; | ||
if (typeof moduleType === "string") return () => moduleType; | ||
return (modulePath: T) => moduleType(modulePath) || "esm"; | ||
}; | ||
/** | ||
* Returns a function that creates string for `OnLoadResult.contents`. | ||
*/ | ||
const createGetContents = <T extends string>( | ||
getModuleType: (modulePath: T) => ModuleType, | ||
getVariableName: (modulePath: T) => string | ||
) => (modulePath: T): string => { | ||
const variableName = getVariableName(modulePath); | ||
switch (getModuleType(modulePath)) { | ||
case "esm": | ||
return `export default ${variableName};`; | ||
case "cjs": | ||
return `module.exports = ${variableName};`; | ||
} | ||
}; | ||
/** | ||
* Create a `Plugin` for replacing modules with corresponding global variables. | ||
@@ -29,6 +40,9 @@ * | ||
*/ | ||
export const globalExternalsWithRegExp = ( | ||
globals: GlobalsMapper | ||
export const globalExternalsWithRegExp = <T extends string>( | ||
globals: GlobalsMapper<T>, | ||
options?: Options<T> | ||
): esbuild.Plugin => { | ||
const { modulePathFilter, getVariableName } = globals; | ||
const getModuleType = createGetModuleType(options); | ||
const getContents = createGetContents(getModuleType, getVariableName); | ||
@@ -44,3 +58,4 @@ return { | ||
build.onLoad({ filter: /.*/, namespace: PLUGIN_NAME }, (args) => ({ | ||
contents: `export default ${getVariableName(args.path)};`, | ||
// eslint-disable-next-line total-functions/no-unsafe-type-assertion | ||
contents: getContents(args.path as T), | ||
})); | ||
@@ -54,5 +69,7 @@ }, | ||
*/ | ||
const mapperFromTable = (globals: Record<string, string>): GlobalsMapper => ({ | ||
const mapperFromTable = <T extends string>( | ||
globals: Record<T, string> | ||
): GlobalsMapper<T> => ({ | ||
modulePathFilter: new RegExp(`^(?:${Object.keys(globals).join("|")})$`), | ||
getVariableName: (modulePath: string) => globals[modulePath] as string, | ||
getVariableName: (modulePath: T) => globals[modulePath], | ||
}); | ||
@@ -69,6 +86,8 @@ | ||
*/ | ||
export const globalExternals = ( | ||
globals: Record<string, string> | ||
): esbuild.Plugin => globalExternalsWithRegExp(mapperFromTable(globals)); | ||
export const globalExternals = <T extends string>( | ||
globals: Record<T, string>, | ||
options?: Options<T> | ||
): esbuild.Plugin => | ||
globalExternalsWithRegExp(mapperFromTable(globals), options); | ||
export default globalExternals; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11506
11
214
42
8