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

fix: esbuild support #53

Merged
merged 4 commits into from
Oct 27, 2024
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@sxzz/prettier-config": "^2.0.2",
"@types/node": "^22.8.1",
"bumpp": "^9.7.1",
"esbuild": "^0.24.0",
"eslint": "^9.13.0",
"prettier": "^3.3.3",
"rollup": "^4.24.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

93 changes: 63 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'
import path, { dirname } from 'node:path'
import { createFilter, normalizePath } from '@rollup/pluginutils'
import glob from 'fast-glob'
import { createUnplugin } from 'unplugin'
import { createUnplugin, type UnpluginOptions } from 'unplugin'
import { ID_PREFIX } from './core/constants'
import { writeTypeDeclaration } from './core/dts'
import { resolveOption, type Options } from './core/options'
Expand All @@ -17,48 +17,81 @@ export default createUnplugin<Options>((options = {}) => {
const filter = createFilter(opt.include, opt.exclude)
const map: GlobMap = {}

return {
name,
const resolveId = (id: string, src: string | undefined) => {
if (!src || !filter(src)) return

resolveId(id, src) {
if (!id.startsWith(ID_PREFIX)) return
if (!src || !filter(src)) return
const [name, pattern] = id.replace(ID_PREFIX, '').split(':', 2)
return `${ID_PREFIX}${name}:${src.replaceAll(
':',
DRIVER_DIVIDER,
)}:${pattern}`
}

const [name, pattern] = id.replace(ID_PREFIX, '').split(':', 2)
return `${ID_PREFIX}${name}:${src.replaceAll(
':',
DRIVER_DIVIDER,
)}:${pattern}`
},
const load = async (id: string) => {
const [name, src, pattern] = id.replace(ID_PREFIX, '').split(':', 3)
const filename = src.replaceAll(DRIVER_DIVIDER_REGEXP, ':')

async load(id) {
if (!id.startsWith(ID_PREFIX)) return
const files = (
await glob(pattern, {
cwd: filename ? path.dirname(filename) : opt.root,
absolute: true,
})
)
.map((file) => normalizePath(file))
.filter((file) => file !== normalizePath(filename))
.sort()
map[`${name}:${pattern}`] = files

const [name, src, pattern] = id.replace(ID_PREFIX, '').split(':', 3)
const filename = src.replaceAll(DRIVER_DIVIDER_REGEXP, ':')
const contents = files.map((file) => `export * from '${file}'`).join('\n')

const files = (
await glob(pattern, {
cwd: filename ? path.dirname(filename) : opt.root,
absolute: true,
})
)
.map((file) => normalizePath(file))
.filter((file) => file !== normalizePath(filename))
.sort()
map[`${name}:${pattern}`] = files
if (opt.dts) await writeTypeDeclaration(map, opt.dts)

const contents = files.map((file) => `export * from '${file}'`).join('\n')
return `${contents}\n`
}

if (opt.dts) await writeTypeDeclaration(map, opt.dts)
const context: UnpluginOptions = {
name,

return `${contents}\n`
resolveId(id, importer) {
if (!id.startsWith(ID_PREFIX)) return
return resolveId(id, importer)
},
load(id) {
if (!id.startsWith(ID_PREFIX)) return
return load(id)
},

vite: {
configResolved(config) {
opt.root = config.root
},
},

esbuild: {
setup(build) {
build.onResolve(
{ filter: new RegExp(`^${ID_PREFIX}`) },
({ path, importer }) => {
const id = resolveId(path, importer)
if (id) return { path: id, namespace: name }
},
)

build.onLoad(
{ filter: new RegExp(`^${ID_PREFIX}`), namespace: name },
async ({ path }) => {
let [, src] = path.replace(ID_PREFIX, '').split(':', 2)
src = src.replaceAll(DRIVER_DIVIDER_REGEXP, ':')

return {
contents: await load(path),
resolveDir: dirname(src),
}
},
)
},
},
}

return context
})
56 changes: 52 additions & 4 deletions tests/__snapshots__/resolve.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`resolve > basic 1`] = `
exports[`resolve > Rollup: basic 1`] = `
"const a = "a";

const foo = "bar";
Expand All @@ -11,7 +11,7 @@ export { a, foo };
"
`;

exports[`resolve > exclude-self 1`] = `
exports[`resolve > Rollup: exclude-self 1`] = `
"const a = "a";

const foo = "bar";
Expand All @@ -20,7 +20,7 @@ export { a, foo };
"
`;

exports[`resolve > import-all 1`] = `
exports[`resolve > Rollup: import-all 1`] = `
"const a = "a";

const foo = "bar";
Expand All @@ -32,11 +32,59 @@ export { a, foo };
"
`;

exports[`resolve > ts 1`] = `
exports[`resolve > Rollup: ts 1`] = `
"const a = "a";

const foo = "bar";

export { a, foo };
"
`;

exports[`resolve > esbuild: basic 1`] = `
"(() => {
// tests/fixtures/mod/a.ts
var a = "a";

// tests/fixtures/mod/b.ts
var foo = "bar";
})();
"
`;

exports[`resolve > esbuild: exclude-self 1`] = `
"(() => {
// tests/fixtures/mod/a.ts
var a = "a";

// tests/fixtures/mod/b.ts
var foo = "bar";
})();
"
`;

exports[`resolve > esbuild: import-all 1`] = `
"(() => {
// tests/fixtures/mod/a.ts
var a = "a";

// tests/fixtures/mod/b.ts
var foo = "bar";

// tests/fixtures/import-all.ts
console.log(a);
console.log(foo);
})();
"
`;

exports[`resolve > esbuild: ts 1`] = `
"(() => {
// tests/fixtures/mod/a.ts
var a = "a";

// tests/fixtures/mod/b.ts
var foo = "bar";
})();
"
`;
24 changes: 21 additions & 3 deletions tests/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import path from 'node:path'
import { build } from 'esbuild'
import glob from 'fast-glob'
import { rollup } from 'rollup'
import Esbuild from 'rollup-plugin-esbuild'
import { describe, expect, test } from 'vitest'
import Plugin from '../src/rollup'
import EsBuildPlugin from '../src/esbuild'
import RollupPlugin from '../src/rollup'

describe('resolve', async () => {
const fixtures = await glob(['./fixtures/*.{js,ts}', '!**/*.d.ts'], {
Expand All @@ -14,12 +16,12 @@ describe('resolve', async () => {
const ext = path.extname(fixture)
const filename = path.basename(fixture, ext)

test(filename, async () => {
test(`Rollup: ${filename}`, async () => {
const bundle = await rollup({
input: fixture,
treeshake: false,
plugins: [
Plugin({
RollupPlugin({
dts: path.resolve(path.dirname(fixture), `${filename}-glob`),
}),
Esbuild(),
Expand All @@ -28,5 +30,21 @@ describe('resolve', async () => {
const { output } = await bundle.generate({})
expect(output[0].code).toMatchSnapshot()
})

test(`esbuild: ${filename}`, async () => {
const bundle = await build({
entryPoints: [fixture],
treeShaking: false,
bundle: true,
write: false,
plugins: [
EsBuildPlugin({
dts: path.resolve(path.dirname(fixture), `${filename}-glob`),
}),
],
})
const { outputFiles } = bundle
expect(outputFiles[0].text).toMatchSnapshot()
})
}
})