├── .github └── CODEOWNERS ├── .gitignore ├── tsconfig.json ├── .eslintrc.js ├── LICENSE ├── README.md ├── src └── plugin.ts ├── package.json └── .cz-config.js /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @a-b-r-o-w-n 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | 3 | dist 4 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "declaration": true, 4 | "outDir": "dist", 5 | "strict": true, 6 | "target": "es2017", 7 | "module": "commonjs" 8 | }, 9 | "files": ["src/plugin.ts"] 10 | } 11 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ 3 | "eslint:recommended", 4 | "plugin:prettier/recommended", 5 | "plugin:import/errors", 6 | "plugin:import/warnings", 7 | "plugin:import/typescript", 8 | "plugin:@typescript-eslint/recommended", 9 | "plugin:@typescript-eslint/eslint-recommended", 10 | "prettier/@typescript-eslint", 11 | ], 12 | plugins: ["import"], 13 | env: { 14 | es6: true, 15 | node: true, 16 | }, 17 | }; 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Andy Brown 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esbuild-plugin-globals 2 | 3 | Provides Webpack's [externals](https://webpack.js.org/configuration/externals/) functionality for [esbuild](https://webpack.js.org/configuration/externals/). 4 | 5 | ## Install 6 | 7 | npm: 8 | 9 | ```bash 10 | npm install --save-dev esbuild-plugin-globals 11 | ``` 12 | 13 | yarn: 14 | 15 | ```bash 16 | yarn add --dev esbuild-plugin-globals 17 | ``` 18 | 19 | ## Usage 20 | 21 | ```js 22 | import esbuild from "esbuild"; 23 | import GlobalsPlugin from "esbuild-plugin-globals"; 24 | 25 | esbuild.build({ 26 | entryPoints: ["src/index.ts"], 27 | bundle: true, 28 | plugins: [ 29 | GlobalsPlugin({ 30 | /** 31 | * Simple string pattern 32 | * Any module matching "react" will be replaced with 33 | * `module.exports = React` 34 | */ 35 | react: "React", 36 | /** 37 | * Regular expression + resolver function 38 | * Invoked with matched module name and returns the module exports (or undefined). 39 | */ 40 | "@some-scope/.*": (moduleName) => { 41 | /** strip the scope */ 42 | const name = name.substring(12); 43 | /** generates module.exports = CamelCasedName */ 44 | return camelCase(name); 45 | }, 46 | }), 47 | ], 48 | }); 49 | ``` 50 | -------------------------------------------------------------------------------- /src/plugin.ts: -------------------------------------------------------------------------------- 1 | import type { Plugin } from "esbuild"; 2 | 3 | type GlobalResolveFunc = (moduleName: string) => string | undefined; 4 | 5 | type PluginGlobalsOptions = { 6 | [key: string]: string | GlobalResolveFunc; 7 | }; 8 | 9 | const generateResolveFilter = (globals: PluginGlobalsOptions): RegExp => { 10 | const moduleNames = Object.keys(globals); 11 | return new RegExp(`^(${moduleNames.join("|")})$`); 12 | }; 13 | 14 | const generateExport = (globals: PluginGlobalsOptions, name: string) => { 15 | const match = Object.entries(globals).find(([pattern]) => { 16 | return new RegExp(`^${pattern}$`).test(name); 17 | }); 18 | 19 | if (match) { 20 | const output = typeof match[1] === "function" ? match[1](name) : match[1]; 21 | 22 | return output ? `module.exports = ${output}` : undefined; 23 | } 24 | }; 25 | 26 | const pluginGlobals = (globals: PluginGlobalsOptions = {}): Plugin => { 27 | const filter = generateResolveFilter(globals); 28 | 29 | return { 30 | name: "globals", 31 | setup(build) { 32 | build.onResolve({ filter }, (args) => { 33 | return { path: args.path, namespace: "globals" }; 34 | }); 35 | 36 | build.onLoad({ filter: /.*/, namespace: "globals" }, (args) => { 37 | const name = args.path; 38 | const contents = generateExport(globals, name); 39 | 40 | if (contents) { 41 | return { contents }; 42 | } 43 | 44 | return null; 45 | }); 46 | }, 47 | }; 48 | }; 49 | 50 | export = pluginGlobals; 51 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "esbuild-plugin-globals", 3 | "version": "0.2.0", 4 | "description": "esbuild plugin that provides Webpack's externals functionality.", 5 | "main": "dist/plugin.js", 6 | "types": "dist/plugin.d.ts", 7 | "engines": { 8 | "node": ">=7" 9 | }, 10 | "files": [ 11 | "dist", 12 | "README.md", 13 | "LICENSE" 14 | ], 15 | "scripts": { 16 | "build": "npm run clean && tsc", 17 | "clean": "rm -rf dist", 18 | "commit": "git-cz", 19 | "prepublishOnly": "npm run build" 20 | }, 21 | "repository": { 22 | "type": "git", 23 | "url": "git+https://github.com/a-b-r-o-w-n/esbuild-plugin-globals.git" 24 | }, 25 | "keywords": [ 26 | "esbuild", 27 | "esbuild-plugin" 28 | ], 29 | "author": { 30 | "name": "Andy Brown", 31 | "email": "abrown.d.ts@gmail.com" 32 | }, 33 | "license": "MIT", 34 | "bugs": { 35 | "url": "https://github.com/a-b-r-o-w-n/esbuild-plugin-globals/issues" 36 | }, 37 | "homepage": "https://github.com/a-b-r-o-w-n/esbuild-plugin-globals#readme", 38 | "devDependencies": { 39 | "@typescript-eslint/eslint-plugin": "^5.54.0", 40 | "@typescript-eslint/parser": "^5.54.0", 41 | "commitizen": "^4.3.0", 42 | "cz-customizable": "^7.0.0", 43 | "esbuild": "^0.17.11", 44 | "eslint": "^8.35.0", 45 | "eslint-config-prettier": "^8.7.0", 46 | "eslint-plugin-import": "^2.27.5", 47 | "eslint-plugin-prettier": "^4.2.1", 48 | "husky": "^8.0.3", 49 | "lint-staged": "^13.1.2", 50 | "prettier": "^2.8.4", 51 | "typescript": "^4.9.5" 52 | }, 53 | "volta": { 54 | "node": "14.15.4", 55 | "npm": "7.5.2" 56 | }, 57 | "config": { 58 | "commitizen": { 59 | "path": "node_modules/cz-customizable" 60 | } 61 | }, 62 | "husky": { 63 | "hooks": { 64 | "pre-commit": "lint-staged" 65 | } 66 | }, 67 | "lint-staged": { 68 | "**/*.{ts,md,json}": [ 69 | "prettier --write" 70 | ] 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /.cz-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | types: [ 3 | { value: "feat", name: "feat: A new feature" }, 4 | { value: "fix", name: "fix: A bug fix" }, 5 | { value: "docs", name: "docs: Documentation only changes" }, 6 | { 7 | value: "style", 8 | name: 9 | "style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)", 10 | }, 11 | { 12 | value: "refactor", 13 | name: 14 | "refactor: A code change that neither fixes a bug nor adds a feature", 15 | }, 16 | { 17 | value: "perf", 18 | name: "perf: A code change that improves performance", 19 | }, 20 | { value: "build", name: "build: Modification to build tooling" }, 21 | { value: "deps", name: "deps: Dependencies" }, 22 | { value: "test", name: "test: Adding missing tests" }, 23 | { 24 | value: "chore", 25 | name: 26 | "chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation", 27 | }, 28 | { value: "revert", name: "revert: Revert to a commit" }, 29 | { value: "WIP", name: "WIP: Work in progress" }, 30 | ], 31 | 32 | scopes: [], 33 | 34 | allowTicketNumber: false, 35 | isTicketNumberRequired: false, 36 | ticketNumberPrefix: "TICKET-", 37 | ticketNumberRegExp: "\\d{1,5}", 38 | 39 | // it needs to match the value for field type. Eg.: 'fix' 40 | /* 41 | scopeOverrides: { 42 | fix: [ 43 | 44 | {name: 'merge'}, 45 | {name: 'style'}, 46 | {name: 'e2eTest'}, 47 | {name: 'unitTest'} 48 | ] 49 | }, 50 | */ 51 | // override the messages, defaults are as follows 52 | messages: { 53 | type: "Select the type of change that you're committing:", 54 | scope: "\nDenote the SCOPE of this change (optional):", 55 | // used if allowCustomScopes is true 56 | customScope: "Denote the SCOPE of this change:", 57 | subject: "Write a SHORT, IMPERATIVE tense description of the change:\n", 58 | body: 59 | 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n', 60 | breaking: "List any BREAKING CHANGES (optional):\n", 61 | footer: 62 | "List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n", 63 | confirmCommit: "Are you sure you want to proceed with the commit above?", 64 | }, 65 | 66 | allowCustomScopes: true, 67 | allowBreakingChanges: ["feat", "fix"], 68 | // skip any questions you want 69 | skipQuestions: ["scope"], 70 | 71 | // limit subject length 72 | subjectLimit: 100, 73 | // breaklineChar: '|', // It is supported for fields body and footer. 74 | // footerPrefix : 'ISSUES CLOSED:' 75 | // askForBreakingChangeFirst : true, // default is false 76 | }; 77 | --------------------------------------------------------------------------------