Skip to content

Commit

Permalink
Merge pull request #1 from bashmish/feat/esbuild-plugins
Browse files Browse the repository at this point in the history
feat: ✨ added support for esbuild plugins
  • Loading branch information
bashmish authored Jan 14, 2022
2 parents 949c847 + ad0255f commit 133faf9
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 172 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
},
"dependencies": {
"source-map-support": "0.5.19",
"temp-write": "^4.0.0",
"tslib": "2.3.1"
}
}
9 changes: 2 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 0 additions & 129 deletions src/esbuild.ts

This file was deleted.

43 changes: 9 additions & 34 deletions src/hook.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import InternalModule from "module"
import { loaders, transpile, supports, TranspileOptions } from "./esbuild"
import { install as installSourceMapSupport } from "source-map-support"
import fs from "fs"
import path from "path"
import { loaders, loadersSupport } from "./loaders"
import { transpile } from "./transpile"
import type { TranspileOptions } from "./types"

type PatchedModule = InternalModule & {
_extensions: Record<string, (mod: PatchedModule, filename: string) => void>
Expand All @@ -11,46 +11,21 @@ type PatchedModule = InternalModule & {

const Module = InternalModule as unknown as PatchedModule

export function findUp(name: string, cwd = process.cwd()): string | undefined {
let up = path.resolve(cwd)
do {
cwd = up
const p = path.resolve(cwd, name)
if (fs.existsSync(p)) return p
up = path.resolve(cwd, "..")
} while (up !== cwd)
}

function loadConfig() {
const configFile = findUp("esbuild-runner.config.js")
if (configFile) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
const ret = require(configFile) as TranspileOptions
return ret
} catch (error) {
// eslint-disable-next-line no-console
console.error(
`[esbuild-runner] could not load "esbuild-runner.config.js"\n`
)
throw error
}
}
}

export function install(options?: Partial<TranspileOptions>) {
options = { ...loadConfig(), ...options }
export function install(transpileOptions?: Partial<TranspileOptions>) {
installSourceMapSupport({ hookRequire: true })
const defaultLoaderJS = Module._extensions[".js"]
for (const ext in loaders) {
const defaultLoader = Module._extensions[ext] || defaultLoaderJS

Module._extensions[ext] = (mod: PatchedModule, filename: string) => {
if (supports(filename)) {
if (loadersSupport(filename)) {
const defaultCompile = mod._compile
mod._compile = (code: string) => {
mod._compile = defaultCompile
return mod._compile(transpile(code, filename, options), filename)
return mod._compile(
transpile(code, filename, transpileOptions),
filename
)
}
}
defaultLoader(mod, filename)
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from "./esbuild"
export * from "./hook"
export * from "./loaders"
export * from "./transpile"
export * from "./types"
2 changes: 1 addition & 1 deletion src/jest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { transpile } from "./esbuild"
import "./register"
import { transpile } from "./transpile"

function process(src: string, filename: string) {
return transpile(src, filename, { type: "transform" })
Expand Down
19 changes: 19 additions & 0 deletions src/loaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Loader } from "esbuild"
import path from "path"

export const loaders: Record<string, Loader> = {
".js": "js",
".mjs": "js",
".cjs": "js",
".jsx": "jsx",
".ts": "ts",
".tsx": "tsx",
// ".css": "css",
".json": "json",
// ".txt": "text",
}

export function loadersSupport(filename: string) {
if (filename.includes("node_modules")) return false
return path.extname(filename) in loaders
}
Loading

0 comments on commit 133faf9

Please sign in to comment.