Skip to content

Commit

Permalink
fix: use bundle-require to bundle tsup.config.ts
Browse files Browse the repository at this point in the history
skip-release
  • Loading branch information
egoist committed Nov 24, 2021
1 parent 87d34be commit 3aba009
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 69 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -25,6 +25,7 @@
"build:simple": "tsup src/cli-*.ts src/index.ts src/rollup.ts --clean --splitting"
},
"dependencies": {
"bundle-require": "^2.1.2",
"cac": "^6.7.2",
"chalk": "^4.1.0",
"chokidar": "^3.5.1",
Expand Down
77 changes: 8 additions & 69 deletions src/load.ts
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import { parse as parseJson } from 'jju/lib/parse'
import JoyCon from 'joycon'
import path from 'path'
import { Loader } from 'esbuild'
import { bundleRequire } from 'bundle-require'
import stripJsonComments from 'strip-json-comments'
import { defineConfig } from './'

Expand Down Expand Up @@ -65,79 +65,18 @@ export async function loadTsupConfig(
return {}
}

const config = await bundleConfig(configPath)
return { path: configPath, data: config }
const config = await bundleRequire({
filepath: configPath,
})
return {
path: configPath,
data: config.mod.tsup || config.mod.default || config.mod,
}
}

return {}
}

function removeFile(filepath: string) {
if (fs.existsSync(filepath)) {
fs.unlinkSync(filepath)
}
}

async function bundleConfig(configFile: string) {
const { build } = await import('esbuild')
const outFile = configFile.replace(/\.[a-z]+$/, '.bundled.cjs')
const readConfig = () => {
delete require.cache[outFile]
const result = require(outFile)
removeFile(outFile)
return result.tsup || result.default || result
}
try {
await build({
entryPoints: [configFile],
format: 'cjs',
outfile: outFile,
platform: 'node',
bundle: true,
plugins: [
{
name: 'ignore',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (!path.isAbsolute(args.path) && !/^[\.\/]/.test(args.path)) {
return { external: true }
}
})
build.onLoad(
{ filter: /\.(js|ts|mjs|cjs|jsx|tsx)$/ },
async (args) => {
const contents = await fs.promises.readFile(args.path, 'utf8')
const ext = path.extname(args.path)
return {
contents: contents
.replace(
/\b__dirname\b/g,
JSON.stringify(path.dirname(args.path))
)
.replace(/\b__filename\b/g, JSON.stringify(args.path))
.replace(
/\bimport\.meta\.url\b/g,
JSON.stringify(`file://${args.path}`)
),
loader:
ext === '.mjs' || ext === '.cjs'
? 'js'
: (ext.slice(1) as Loader),
}
}
)
},
},
],
})
const config = readConfig()
return config
} catch (error) {
removeFile(outFile)
throw error
}
}

export async function loadPkg(cwd: string) {
const { data } = await joycon.load(['package.json'], cwd, path.dirname(cwd))
return data || {}
Expand Down

1 comment on commit 3aba009

@vercel
Copy link

@vercel vercel bot commented on 3aba009 Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.