@fal-works/esbuild-plugin-global-externals
Advanced tools
Comparing version 0.1.0 to 1.0.0
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 |
@@ -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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7734
1
141
0
7
- Removedfast-glob@^3.2.5
- Removed@nodelib/fs.scandir@2.1.5(transitive)
- Removed@nodelib/fs.stat@2.0.5(transitive)
- Removed@nodelib/fs.walk@1.2.8(transitive)
- Removedbraces@3.0.3(transitive)
- Removedfast-glob@3.3.2(transitive)
- Removedfastq@1.17.1(transitive)
- Removedfill-range@7.1.1(transitive)
- Removedglob-parent@5.1.2(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedis-number@7.0.0(transitive)
- Removedmerge2@1.4.1(transitive)
- Removedmicromatch@4.0.7(transitive)
- Removedpicomatch@2.3.1(transitive)
- Removedqueue-microtask@1.2.3(transitive)
- Removedreusify@1.0.4(transitive)
- Removedrun-parallel@1.2.0(transitive)
- Removedto-regex-range@5.0.1(transitive)
Updatedesbuild@^0.8.36