Skip to content

Commit

Permalink
feat(esbuild): handle .cjs and .mjs extensions
Browse files Browse the repository at this point in the history
shigma committed Jan 27, 2024

Verified

This commit was signed with the committer’s verified signature.
shigma Shigma
1 parent bd2e921 commit c1f67b2
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/esbuild/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "yakumo-esbuild",
"description": "Esbuild support for Yakumo",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.2",
"type": "module",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
@@ -32,7 +32,7 @@
"yakumo": "^1.0.0-beta.2"
},
"dependencies": {
"tsconfig-utils": "^4.0.3",
"tsconfig-utils": "^4.0.4",
"yaml": "^2.3.4"
}
}
18 changes: 13 additions & 5 deletions packages/esbuild/src/index.ts
Original file line number Diff line number Diff line change
@@ -126,6 +126,13 @@ async function compile(relpath: string, meta: PackageJson, yakumo: Yakumo) {
return
}

// transform options by extension
if (pattern.endsWith('.cjs')) {
options = nodeOptions
} else if (pattern.endsWith('.mjs')) {
options = browserOptions
}

// https://nodejs.org/api/packages.html#subpath-patterns
// `*` maps expose nested subpaths as it is a string replacement syntax only
const outExt = extname(pattern)
@@ -167,26 +174,27 @@ async function compile(relpath: string, meta: PackageJson, yakumo: Yakumo) {

for (const key in pattern) {
if (key === 'node' || key === 'require' || key.startsWith('.')) {
addConditionalExport(pattern[key], nodeOptions)
addConditionalExport(pattern[key], options)
} else {
addConditionalExport(pattern[key], browserOptions)
}
}
}

addExport(meta.main, meta.type === 'module' ? browserOptions : nodeOptions)
const defaultOptions = meta.type === 'module' ? browserOptions : nodeOptions
addExport(meta.main, defaultOptions)
addExport(meta.module, browserOptions)
addConditionalExport(meta.exports, nodeOptions)
addConditionalExport(meta.exports, defaultOptions)

if (!meta.exports) {
addExport('package.json', nodeOptions)
}

if (typeof meta.bin === 'string') {
addExport(meta.bin, nodeOptions)
addExport(meta.bin, defaultOptions)
} else if (meta.bin) {
for (const key in meta.bin) {
addExport(meta.bin[key], nodeOptions)
addExport(meta.bin[key], defaultOptions)
}
}

0 comments on commit c1f67b2

Please sign in to comment.