-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create @kablamo/rollup-plugin-resolve-externals to fix resolving Comm…
…onJS from .mjs
- Loading branch information
Showing
11 changed files
with
190 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"name": "@kablamo/rollup-plugin-resolve-externals", | ||
"version": "0.0.1", | ||
"repository": "https://github.com/KablamoOSS/kerosene/tree/master/packages/rollup-plugin-resolve-externals", | ||
"bugs": { | ||
"url": "https://github.com/KablamoOSS/kerosene/issues" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Kablamo", | ||
"url": "https://www.kablamo.com.au" | ||
}, | ||
{ | ||
"name": "Nathan Hardy", | ||
"email": "[email protected]", | ||
"url": "https://nhardy.id.au" | ||
} | ||
], | ||
"homepage": "https://github.com/KablamoOSS/kerosene", | ||
"private": false, | ||
"license": "MIT", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"directories": { | ||
"doc": "readme.md" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"engines": { | ||
"node": ">=18.12.0" | ||
}, | ||
"keywords": [ | ||
"kablamo", | ||
"kerosene", | ||
"rollup", | ||
"plugin", | ||
"externals", | ||
"resolve" | ||
], | ||
"dependencies": { | ||
"lodash": "^4.17.21" | ||
}, | ||
"devDependencies": { | ||
"rollup": "^4.5.1" | ||
}, | ||
"peerDependencies": { | ||
"rollup": "^4.5.1" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"clean": "rimraf .dist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# @kablamo/rollup-plugin-resolve-externals | ||
|
||
``` | ||
npm install --save-dev @kablamo/rollup-plugin-resolve-externals | ||
``` | ||
|
||
This rollup plugin forces subpath imports from packages without [package `exports`](https://nodejs.org/api/packages.html#subpath-exports) to resolve to a filename. This allows such imports to occur in `.mjs` files which would otherwise complain. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { escapeRegExp } from "lodash"; | ||
import path from "path"; | ||
import type { ExternalOption, Plugin } from "rollup"; | ||
|
||
export interface ResolveExternalsPluginOptions { | ||
/** | ||
* List of external modules | ||
*/ | ||
externals: readonly string[]; | ||
/** | ||
* `paths` used by `require.resolve(name, { paths })` to resolve dependencies | ||
*/ | ||
paths?: string[]; | ||
} | ||
|
||
export default function resolveExternals({ | ||
externals, | ||
paths = require.main?.paths, | ||
}: ResolveExternalsPluginOptions): Plugin { | ||
const external = ((source) => | ||
externals.includes(source) || | ||
externals.some((mod) => | ||
source.startsWith(`${mod}/`), | ||
)) satisfies ExternalOption; | ||
const deepExternalRegex = new RegExp( | ||
`^(${externals.map((e) => escapeRegExp(e)).join("|")})/(.+)$`, | ||
); | ||
|
||
return { | ||
name: "@kablamo/rollup-plugin-resolve-externals", | ||
|
||
resolveId(source) { | ||
// Ignore non-externals | ||
if (!external(source)) return null; | ||
|
||
// Match deep imports from a package | ||
const match = deepExternalRegex.exec(source); | ||
if (match) { | ||
const [, mod, importPath] = match; | ||
// eslint-disable-next-line import/no-dynamic-require, global-require, @typescript-eslint/no-var-requires | ||
const pkgJson = require(`${mod}/package.json`) as Record< | ||
string, | ||
unknown | ||
>; | ||
|
||
// If package exports are not defined, we need to resolve the path so that CommonJS files | ||
// can be resolved properly from `.mjs` files | ||
if (!pkgJson.exports) { | ||
// Resolve the module to a path on disk | ||
const resolvedPath = require.resolve(`${mod}/${importPath}`, { | ||
paths, | ||
}); | ||
// Rewrite to a relative path | ||
const rewritten = `${mod}/${path | ||
.relative( | ||
path.dirname( | ||
require.resolve(`${mod}/package.json`, { | ||
paths, | ||
}), | ||
), | ||
resolvedPath, | ||
) | ||
// Handle win32 path separators | ||
.split(path.sep) | ||
.join("/")}`; | ||
return { | ||
id: rewritten, | ||
external: true, | ||
}; | ||
} | ||
} | ||
|
||
return { | ||
id: source, | ||
external: true, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"noEmit": false, | ||
"allowJs": false, | ||
"declaration": true, | ||
"declarationDir": "dist" | ||
}, | ||
"include": ["src/**/*.ts"] | ||
} |