Socket
Socket
Sign inDemoInstall

@fal-works/esbuild-plugin-global-externals

Package Overview
Dependencies
1
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 1.0.0

46

lib/index.d.ts
import * as esbuild from "esbuild";
/**
* @param globals
* Mapping from module paths to variable names, e.g. `jquery: "$"`.
* Object that contains a filter and a mapping function for replacing modules
* with corresponding global variables.
*/
export declare const globalExternals: (globals: Record<string, string>) => esbuild.Plugin;
//# sourceMappingURL=index.d.ts.map
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.
*
* @param globals See type declaration.
*/
export declare const globalExternalsWithRegExp: (
globals: GlobalsMapper
) => 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: (
globals: Record<string, string>
) => esbuild.Plugin;
export default globalExternals;
//# sourceMappingURL=index.d.ts.map

50

lib/index.js

@@ -0,25 +1,43 @@

const PLUGIN_NAME = "global-externals";
/**
* @param globals
* Mapping from module paths to variable names, e.g. `jquery: "$"`.
* Create a `Plugin` for replacing modules with corresponding global variables.
*
* @param globals See type declaration.
*/
export const globalExternals = (globals) => {
const pluginName = "global-externals";
const moduleFilter = new RegExp(`^(?:${Object.keys(globals).join("|")})$`);
export const globalExternalsWithRegExp = (globals) => {
const { modulePathFilter, getVariableName } = globals;
return {
name: pluginName,
name: PLUGIN_NAME,
setup(build) {
build.onResolve({ filter: moduleFilter }, (args) => ({
build.onResolve({ filter: modulePathFilter }, (args) => ({
path: args.path,
namespace: pluginName,
namespace: PLUGIN_NAME,
}));
build.onLoad({ filter: /.*/, namespace: pluginName }, (args) => {
const variableName = globals[args.path];
if (!variableName) {
// Shouldn't happen
throw new Error(`Unknown module path: ${args.path}`);
}
return { contents: `export default ${variableName};` };
});
build.onLoad({ filter: /.*/, namespace: PLUGIN_NAME }, (args) => ({
contents: `export default ${getVariableName(args.path)};`,
}));
},
};
};
/**
* Creates a mapper object from an object-form table.
*/
const mapperFromTable = (globals) => ({
modulePathFilter: new RegExp(`^(?:${Object.keys(globals).join("|")})$`),
getVariableName: (modulePath) => globals[modulePath],
});
/**
* 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) =>
globalExternalsWithRegExp(mapperFromTable(globals));
export default globalExternals;
{
"name": "@fal-works/esbuild-plugin-global-externals",
"description": "esbuild plugin for replacing imports with global variables.",
"version": "0.1.0",
"version": "1.0.0",
"license": "MIT",

@@ -26,18 +26,19 @@ "author": "FAL",

"dependencies": {
"esbuild": "^0.8.34",
"fast-glob": "^3.2.5"
"esbuild": "^0.8.36"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "4.14.0",
"@typescript-eslint/parser": "^4.14.0",
"@typescript-eslint/eslint-plugin": "4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"eslint": "7.18.0",
"eslint-config-prettier": "^7.2.0",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"typescript": "^4.1.3"
},
"scripts": {
"prebuild": "rimraf lib",
"build": "tsc && npm run format:lib",
"preformat:lib": "eslint --fix --no-ignore lib/**/*.js",
"format:lib": "prettier --write lib/**/*.js"
"preformat:lib": "eslint --fix --no-ignore lib/**/*.{js,ts}",
"format:lib": "prettier --write lib/**/*.{js,ts}"
}
}
import * as esbuild from "esbuild";
const PLUGIN_NAME = "global-externals";
/**
* @param globals
* Mapping from module paths to variable names, e.g. `jquery: "$"`.
* Object that contains a filter and a mapping function for replacing modules
* with corresponding global variables.
*/
export const globalExternals = (
globals: Record<string, string>
export 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.
*
* @param globals See type declaration.
*/
export const globalExternalsWithRegExp = (
globals: GlobalsMapper
): esbuild.Plugin => {
const pluginName = "global-externals";
const moduleFilter = new RegExp(`^(?:${Object.keys(globals).join("|")})$`);
const { modulePathFilter, getVariableName } = globals;
return {
name: pluginName,
name: PLUGIN_NAME,
setup(build) {
build.onResolve({ filter: moduleFilter }, (args) => ({
build.onResolve({ filter: modulePathFilter }, (args) => ({
path: args.path,
namespace: pluginName,
namespace: PLUGIN_NAME,
}));
build.onLoad({ filter: /.*/, namespace: pluginName }, (args) => {
const variableName = globals[args.path];
if (!variableName) {
// Shouldn't happen
throw new Error(`Unknown module path: ${args.path}`);
}
return { contents: `export default ${variableName};` };
});
build.onLoad({ filter: /.*/, namespace: PLUGIN_NAME }, (args) => ({
contents: `export default ${getVariableName(args.path)};`,
}));
},
};
};
/**
* Creates a mapper object from an object-form table.
*/
const mapperFromTable = (globals: Record<string, string>): GlobalsMapper => ({
modulePathFilter: new RegExp(`^(?:${Object.keys(globals).join("|")})$`),
getVariableName: (modulePath: string) => globals[modulePath] as string,
});
/**
* 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: Record<string, string>
): esbuild.Plugin => globalExternalsWithRegExp(mapperFromTable(globals));
export default globalExternals;

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc