Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds support for workspace protocol #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
TsconfigRaw,
} from "esbuild"
import { loadTsConfig } from "load-tsconfig"
import { dynamicImport, getRandomId, guessFormat } from "./utils"
import {
dynamicImport,
getPkgDependencies,
getRandomId,
guessFormat,
} from "./utils"

const DIRNAME_VAR_NAME = "__injected_dirname__"
const FILENAME_VAR_NAME = "__injected_filename__"
Expand Down Expand Up @@ -135,6 +140,14 @@ export const tsconfigPathsToRegExp = (paths: Record<string, any>) => {
})
}

export const filterWorkspaceDependencyPaths = (
pkgDependencies: Record<string, any>,
) => {
return Object.keys(pkgDependencies).filter((dep) =>
pkgDependencies[dep].startsWith("workspace:"),
)
}

export const match = (id: string, patterns?: (string | RegExp)[]) => {
if (!patterns) return false
return patterns.some((p) => {
Expand Down Expand Up @@ -256,6 +269,9 @@ export function bundleRequire<T = any>(
tsconfig?.data.compilerOptions?.paths || {},
)

const pkgDeps = getPkgDependencies()
const workspaceDepPaths = filterWorkspaceDependencyPaths(pkgDeps)

const extractResult = async (result: BuildResult) => {
if (!result.outputFiles) {
throw new Error(`[bundle-require] no output files`)
Expand Down Expand Up @@ -311,7 +327,11 @@ export function bundleRequire<T = any>(
...(options.esbuildOptions?.plugins || []),
externalPlugin({
external: options.external,
notExternal: [...(options.notExternal || []), ...resolvePaths],
notExternal: [
...(options.notExternal || []),
...resolvePaths,
...workspaceDepPaths,
],
// When `filepath` is in node_modules, this is default to false
externalNodeModules:
options.externalNodeModules ??
Expand Down
21 changes: 16 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import path from "path"
import { createRequire } from "module"
import { RequireFunction } from "."

const getPkgType = (): string | undefined => {
const getPkg = (): Record<string, any> | undefined => {
try {
const pkg = JSON.parse(
fs.readFileSync(path.resolve("package.json"), "utf-8"),
)
return pkg.type
return JSON.parse(fs.readFileSync(path.resolve("package.json"), "utf-8"))
} catch (error) {}
}

const getPkgType = (): string | undefined => {
return getPkg()?.type
}

export function guessFormat(inputFile: string): "esm" | "cjs" {
if (!usingDynamicImport) return "cjs"

Expand All @@ -27,6 +28,16 @@ export function guessFormat(inputFile: string): "esm" | "cjs" {
return "cjs"
}

export const getPkgDependencies = (): Record<string, string> => {
const pkg = getPkg()
return {
...pkg?.dependencies,
...pkg?.devDependencies,
...pkg?.peerDependencies,
...pkg?.optionalDependencies,
}
}

// Injected by TSUP
declare const TSUP_FORMAT: "esm" | "cjs"

Expand Down