Skip to content

Commit

Permalink
feat(esbuild-hybrid): provide reexport flow
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Nov 27, 2024
1 parent d2ed917 commit c8b3193
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
13 changes: 9 additions & 4 deletions packages/esbuild/plugin-hybrid-export/src/main/ts/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type TOpts = {
to: string
toExt: string
entryPoints: string[]
loader: string
loader: 'import' | 'require' | 'reexport'
}

type TPluginOpts = Partial<TOpts>
Expand Down Expand Up @@ -74,11 +74,16 @@ const onEnd = async (result: OnEndResult, opts: TOpts) => {
const formatRefs = (link: string, refs: string[], loader = 'require'): string => {
const hasDefault = refs.includes('default')
const _refs = refs.filter(r => r !== 'default')
const load = loader === 'require' ? 'require' : 'await import'
const module = ({
'require': `const __module__ = require("${link}")`,
'import': `const __module__ = await import("${link}")`,
'reexport': `import __module__ from "${link}"`
})[loader]

return `const {
return `${module}
const {
${renderList([..._refs, hasDefault ? 'default: __default__' : '',].filter(Boolean))}
} = ${load}('${link}')
} = __module__
export {
${renderList(_refs)}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"extends": ["eslint-config-qiwi"],
"rules": {}
"rules": {
"unicorn/import-style": "off"
}
}
50 changes: 48 additions & 2 deletions packages/esbuild/plugin-hybrid-export/src/test/ts/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ describe('plugin()', () => {
await build(config)

assert.equal(await fs.readFile(path.resolve(temp, 'mixed/index.mjs'), 'utf8'), `"use strict";
const __module__ = require("./index.js")
const {
bar,
foo,
qux,
default: __default__
} = require('./index.js')
} = __module__
export {
bar,
foo,
Expand All @@ -54,6 +55,50 @@ export default __default__
)
})

it('generates esm files via reexport', async () => {
const cwd = path.resolve(fixtures, 'mixed-exports')
const plugin = hybridExportPlugin({
to: '../../temp/mixed',
toExt: '.mjs',
loader: 'reexport'
})
const config: BuildOptions = {
entryPoints: [
'index.ts',
],
plugins: [plugin],
platform: 'node',
external: ['node:*'],
bundle: true,
minify: false,
sourcemap: false,
format: 'cjs',
legalComments: 'none',
absWorkingDir: cwd,
outdir: path.resolve(temp, 'mixed'),
allowOverwrite: true,
}

await build(config)

assert.equal(await fs.readFile(path.resolve(temp, 'mixed/index.mjs'), 'utf8'), `"use strict";
import __module__ from "./index.js"
const {
bar,
foo,
qux,
default: __default__
} = __module__
export {
bar,
foo,
qux
}
export default __default__
`
)
})

it('generates esm files (full reexport case)', async () => {
const cwd = path.resolve(fixtures, 'full-reexport')
const plugin = hybridExportPlugin({
Expand Down Expand Up @@ -83,13 +128,14 @@ export default __default__

assert.equal(await fs.readFile(path.resolve(temp, 'reexport/index.mjs'), 'utf8'), `#!/usr/bin/env node
"use strict";
const __module__ = await import("./index.js")
const {
a,
baz,
bar,
foo,
default: __default__
} = await import('./index.js')
} = __module__
export {
a,
baz,
Expand Down

0 comments on commit c8b3193

Please sign in to comment.