@fal-works/esbuild-plugin-global-externals
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -1,30 +0,8 @@ | ||
import type * as esbuild from "esbuild"; | ||
import type { GlobalsMapper, ModuleType, Options } from "./types"; | ||
export type { GlobalsMapper, ModuleType, Options }; | ||
/** | ||
* Create a `Plugin` for replacing modules with corresponding global variables. | ||
* | ||
* @param globals See type declaration. | ||
*/ | ||
export declare const globalExternalsWithRegExp: <T extends string>( | ||
globals: GlobalsMapper<T>, | ||
options?: Options<T> | undefined | ||
) => esbuild.Plugin; | ||
/** | ||
* Create a `Plugin` for replacing modules with corresponding global variables. | ||
* | ||
* @param globals Object that maps module paths to variable names, e.g.: | ||
* ``` | ||
* const globals = { jquery: "$" }; | ||
* const plugins = [globalExternals(globals)]; | ||
* ``` | ||
*/ | ||
export declare const globalExternals: <T extends string>( | ||
globals: Record<T, string>, | ||
options?: Options<T> | undefined | ||
) => esbuild.Plugin; | ||
export type { ModuleType, ModuleInfo } from "./module-info"; | ||
import { globalExternals } from "./with-object.js"; | ||
export { globalExternals }; | ||
export default globalExternals; | ||
export { globalExternalsWithRegExp } from "./with-reg-exp.js"; | ||
export type { GlobalsMapper } from "./with-reg-exp"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,50 +0,4 @@ | ||
import { normalizeOptions } from "./options.js"; | ||
import { createContents } from "./on-load.js"; | ||
const PLUGIN_NAME = "global-externals"; | ||
/** | ||
* Create a `Plugin` for replacing modules with corresponding global variables. | ||
* | ||
* @param globals See type declaration. | ||
*/ | ||
export const globalExternalsWithRegExp = (globals, options) => { | ||
const { modulePathFilter, getVariableName } = globals; | ||
const { getModuleType, getNamedExports } = normalizeOptions(options); | ||
return { | ||
name: PLUGIN_NAME, | ||
setup(build) { | ||
build.onResolve({ filter: modulePathFilter }, (args) => ({ | ||
path: args.path, | ||
namespace: PLUGIN_NAME, | ||
})); | ||
build.onLoad({ filter: /.*/, namespace: PLUGIN_NAME }, (args) => { | ||
// eslint-disable-next-line total-functions/no-unsafe-type-assertion | ||
const modulePath = args.path; // type T since already filtered | ||
const variableName = getVariableName(modulePath); | ||
const moduleType = getModuleType(modulePath); | ||
const namedExports = getNamedExports(modulePath); | ||
return { | ||
contents: createContents(moduleType, variableName, namedExports), | ||
}; | ||
}); | ||
}, | ||
}; | ||
}; | ||
/** | ||
* Create a `Plugin` for replacing modules with corresponding global variables. | ||
* | ||
* @param globals Object that maps module paths to variable names, e.g.: | ||
* ``` | ||
* const globals = { jquery: "$" }; | ||
* const plugins = [globalExternals(globals)]; | ||
* ``` | ||
*/ | ||
export const globalExternals = (globals, options) => { | ||
const normalizedGlobals = { | ||
modulePathFilter: new RegExp(`^(?:${Object.keys(globals).join("|")})$`), | ||
getVariableName: (modulePath) => globals[modulePath], | ||
}; | ||
return globalExternalsWithRegExp(normalizedGlobals, options); | ||
}; | ||
import { globalExternals } from "./with-object.js"; | ||
export { globalExternals }; | ||
export default globalExternals; | ||
export { globalExternalsWithRegExp } from "./with-reg-exp.js"; |
@@ -1,2 +0,2 @@ | ||
import type { ModuleType } from "./types"; | ||
import type { NormalizedModuleInfo } from "./module-info"; | ||
@@ -7,7 +7,5 @@ /** | ||
export declare const createContents: ( | ||
moduleType: ModuleType, | ||
variableName: string, | ||
namedExports: readonly string[] | null | ||
moduleInfo: NormalizedModuleInfo | ||
) => string; | ||
//# sourceMappingURL=on-load.d.ts.map |
const createCjsContents = (variableName) => `module.exports = ${variableName};`; | ||
const convertNamedExport = (variableName) => (exportName) => | ||
`const ${exportName} = ${variableName}.${exportName}; export { ${exportName} };`; | ||
const createEsmContents = (variableName, namedExports) => { | ||
return [`export default ${variableName};`] | ||
.concat([...new Set(namedExports)].map(convertNamedExport(variableName))) | ||
.join("\n"); | ||
const createEsmContents = (variableName, namedExports, defaultExport) => { | ||
const codeElements = defaultExport ? [`export default ${variableName};`] : []; | ||
if (namedExports && namedExports.length) { | ||
const exportNames = [...new Set(namedExports)].join(", "); | ||
codeElements.push(`const { ${exportNames} } = ${variableName};`); | ||
codeElements.push(`export { ${exportNames} };`); | ||
} | ||
return codeElements.join("\n"); | ||
}; | ||
@@ -13,9 +15,10 @@ | ||
*/ | ||
export const createContents = (moduleType, variableName, namedExports) => { | ||
switch (moduleType) { | ||
export const createContents = (moduleInfo) => { | ||
const { type, varName, namedExports, defaultExport } = moduleInfo; | ||
switch (type) { | ||
case "esm": | ||
return createEsmContents(variableName, namedExports); | ||
return createEsmContents(varName, namedExports, defaultExport); | ||
case "cjs": | ||
return createCjsContents(variableName); | ||
return createCjsContents(varName); | ||
} | ||
}; |
{ | ||
"name": "@fal-works/esbuild-plugin-global-externals", | ||
"description": "esbuild plugin for replacing imports with global variables.", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "author": "FAL", |
@@ -5,7 +5,25 @@ # esbuild-plugin-global-externals | ||
Similar to `output.globals` option of [Rollup](https://rollupjs.org/). | ||
Developed with: esbuild v0.8.39 | ||
See also: [evanw/esbuild#337](https://github.com/evanw/esbuild/issues/337) | ||
Similar to (but not the same as) `output.globals` option of [Rollup](https://rollupjs.org/). See also: [evanw/esbuild#337](https://github.com/evanw/esbuild/issues/337) | ||
## TL;DR | ||
This: | ||
```js | ||
import p5 from "p5"; | ||
new p5(); | ||
``` | ||
transpiles to: | ||
```js | ||
// Here a variable `p5` should be defined globally (typically via UMD) | ||
var p5_default = p5; | ||
new p5_default(); | ||
``` | ||
## Usage example | ||
@@ -32,4 +50,6 @@ | ||
## Options | ||
## Configuration | ||
Instead of simply providing variable names only, you can pass `ModuleInfo` objects to configure the loading behavior for each module. | ||
### Module type | ||
@@ -39,24 +59,41 @@ | ||
You can also provide an object or a function for specifying the type for each module individually. | ||
This determines the internal behavior of this plugin when loading modules (which affects the code after bundling as well). | ||
```js | ||
globalExternals(globals, { | ||
moduleType: "cjs" | ||
globalExternals({ | ||
"someModulePath": { | ||
varName: "someGlobalVar", | ||
type: "cjs" | ||
} | ||
}) | ||
``` | ||
### Named exports | ||
### Named/default export | ||
An object or a function that specifies names of variables exported from each module. | ||
If `type: "esm"` and also if you're doing named import with some modules in question, you have to tell which variables from each module should be importable. | ||
Without this option, the module type "esm" works only with modules that are imported with default import. | ||
Additionally, if you don't do default import/export, you can prevent emitting code for this with `defaultExport: false`. | ||
No effect (and no need to use this option) if the module type is `"cjs"`. | ||
Both have no effects if `type: "cjs"` (which should work fine with named imports as well). | ||
```js | ||
globalExternals(globals, { | ||
namedExports: { | ||
someModule: ["someExportedVariableName"] | ||
globalExternals({ | ||
"someModulePath": { | ||
varName: "someGlobalVar", | ||
namedExports: ["someExportedVar"], | ||
defaultExport: false | ||
} | ||
}) | ||
``` | ||
The example above enables the following even if `type: "esm"`: | ||
```js | ||
import { someExportedVar } from "someModulePath"; | ||
``` | ||
which transpiles to: | ||
```js | ||
var someExportedVar = someGlobalVar.someExportedVar; | ||
``` |
@@ -1,67 +0,8 @@ | ||
import type * as esbuild from "esbuild"; | ||
import type { GlobalsMapper, ModuleType, Options } from "./types"; | ||
import { normalizeOptions } from "./options.js"; | ||
import { createContents } from "./on-load.js"; | ||
export type { ModuleType, ModuleInfo } from "./module-info"; | ||
export type { GlobalsMapper, ModuleType, Options }; | ||
import { globalExternals } from "./with-object.js"; | ||
export { globalExternals }; | ||
export default globalExternals; | ||
const PLUGIN_NAME = "global-externals"; | ||
/** | ||
* Create a `Plugin` for replacing modules with corresponding global variables. | ||
* | ||
* @param globals See type declaration. | ||
*/ | ||
export const globalExternalsWithRegExp = <T extends string>( | ||
globals: GlobalsMapper<T>, | ||
options?: Options<T> | ||
): esbuild.Plugin => { | ||
const { modulePathFilter, getVariableName } = globals; | ||
const { getModuleType, getNamedExports } = normalizeOptions(options); | ||
return { | ||
name: PLUGIN_NAME, | ||
setup(build) { | ||
build.onResolve({ filter: modulePathFilter }, (args) => ({ | ||
path: args.path, | ||
namespace: PLUGIN_NAME, | ||
})); | ||
build.onLoad({ filter: /.*/, namespace: PLUGIN_NAME }, (args) => { | ||
// eslint-disable-next-line total-functions/no-unsafe-type-assertion | ||
const modulePath = args.path as T; // type T since already filtered | ||
const variableName = getVariableName(modulePath); | ||
const moduleType = getModuleType(modulePath); | ||
const namedExports = getNamedExports(modulePath); | ||
return { | ||
contents: createContents(moduleType, variableName, namedExports), | ||
}; | ||
}); | ||
}, | ||
}; | ||
}; | ||
/** | ||
* Create a `Plugin` for replacing modules with corresponding global variables. | ||
* | ||
* @param globals Object that maps module paths to variable names, e.g.: | ||
* ``` | ||
* const globals = { jquery: "$" }; | ||
* const plugins = [globalExternals(globals)]; | ||
* ``` | ||
*/ | ||
export const globalExternals = <T extends string>( | ||
globals: Record<T, string>, | ||
options?: Options<T> | ||
): esbuild.Plugin => { | ||
const normalizedGlobals: GlobalsMapper<T> = { | ||
modulePathFilter: new RegExp(`^(?:${Object.keys(globals).join("|")})$`), | ||
getVariableName: (modulePath: T) => globals[modulePath], | ||
}; | ||
return globalExternalsWithRegExp(normalizedGlobals, options); | ||
}; | ||
export default globalExternals; | ||
export { globalExternalsWithRegExp } from "./with-reg-exp.js"; | ||
export type { GlobalsMapper } from "./with-reg-exp"; |
@@ -1,2 +0,2 @@ | ||
import type { ModuleType } from "./types"; | ||
import type { NormalizedModuleInfo } from "./module-info"; | ||
@@ -6,12 +6,16 @@ const createCjsContents = (variableName: string) => | ||
const convertNamedExport = (variableName: string) => (exportName: string) => | ||
`const ${exportName} = ${variableName}.${exportName}; export { ${exportName} };`; | ||
const createEsmContents = ( | ||
variableName: string, | ||
namedExports: readonly string[] | null | ||
namedExports: readonly string[] | null, | ||
defaultExport: boolean | ||
) => { | ||
return [`export default ${variableName};`] | ||
.concat([...new Set(namedExports)].map(convertNamedExport(variableName))) | ||
.join("\n"); | ||
const codeElements = defaultExport ? [`export default ${variableName};`] : []; | ||
if (namedExports && namedExports.length) { | ||
const exportNames = [...new Set(namedExports)].join(", "); | ||
codeElements.push(`const { ${exportNames} } = ${variableName};`); | ||
codeElements.push(`export { ${exportNames} };`); | ||
} | ||
return codeElements.join("\n"); | ||
}; | ||
@@ -22,13 +26,11 @@ | ||
*/ | ||
export const createContents = ( | ||
moduleType: ModuleType, | ||
variableName: string, | ||
namedExports: readonly string[] | null | ||
): string => { | ||
switch (moduleType) { | ||
export const createContents = (moduleInfo: NormalizedModuleInfo): string => { | ||
const { type, varName, namedExports, defaultExport } = moduleInfo; | ||
switch (type) { | ||
case "esm": | ||
return createEsmContents(variableName, namedExports); | ||
return createEsmContents(varName, namedExports, defaultExport); | ||
case "cjs": | ||
return createCjsContents(variableName); | ||
return createCjsContents(varName); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
17953
23
97
335