Skip to content

Commit

Permalink
feat: glob export
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jul 31, 2022
1 parent 2c8a532 commit 44de2ec
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 21 deletions.
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

3 changes: 0 additions & 3 deletions .eslintrc.js
Expand Up @@ -3,7 +3,4 @@ const { defineConfig } = require('eslint-define-config')
module.exports = defineConfig({
root: true,
extends: ['@sxzz'],
rules: {
'unicorn/prefer-string-replace-all': 'off',
},
})
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -85,6 +85,7 @@
"eslint-define-config": "^1.5.1",
"fast-glob": "^3.2.11",
"prettier": "^2.7.1",
"rollup": "^2.77.2",
"tsup": "^6.2.1",
"tsx": "^3.8.0",
"typescript": "^4.7.4",
Expand Down
7 changes: 5 additions & 2 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion src/core/options.ts
Expand Up @@ -9,7 +9,7 @@ export type OptionsResolved = Required<Options>

export function resolveOption(options: Options): OptionsResolved {
return {
include: options.include || [/\.[jt]sx?$/],
include: options.include || [/\.m?[jt]sx?$/],
exclude: options.exclude || undefined,
}
}
11 changes: 11 additions & 0 deletions src/core/utils.ts
@@ -0,0 +1,11 @@
export function parsePattern(id: string): {
src: string
pattern: string
} {
const [src, rawQuery] = id.split(`?`, 2)
const params = new URLSearchParams(rawQuery)
return {
src,
pattern: params.get('pattern')!,
}
}
32 changes: 26 additions & 6 deletions src/index.ts
@@ -1,25 +1,45 @@
import path from 'node:path'
import { createUnplugin } from 'unplugin'
import { createFilter } from '@rollup/pluginutils'
import glob from 'fast-glob'
import { resolveOption } from './core/options'
import { parsePattern } from './core/utils'
import type { Options } from './core/options'

export default createUnplugin<Options>((options = {}) => {
const opt = resolveOption(options)
const filter = createFilter(opt.include, opt.exclude)
const root = process.cwd()

const name = 'unplugin-export'
return {
name,
enforce: undefined,

transformInclude(id) {
return filter(id)
resolveId(id, src) {
if (!src || !filter(src)) return
const [pattern, rawQuery] = id.split(`?`, 2)
if (rawQuery !== 'glob') return
return `/export-glob${src}?pattern=${pattern}`
},

transform(code, id) {
// eslint-disable-next-line no-console
console.log(code, id)
return undefined
async load(id) {
if (!id.startsWith('/export-glob')) return

const { src, pattern } = parsePattern(id.replace('/export-glob', ''))

const files = await glob(pattern, {
cwd: src ? path.dirname(src) : root,
absolute: true,
})

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

return `${contents}\n`
},
}
})
10 changes: 10 additions & 0 deletions tests/__snapshots__/resolve.test.ts.snap
@@ -0,0 +1,10 @@
// Vitest Snapshot v1

exports[`resolve > /Users/kevin/Developer/open-source/unplugin-export/tests/fixtures/basic.js 1`] = `
"const a = 'a';
const b = 'b';
export { a, b };
"
`;
5 changes: 0 additions & 5 deletions tests/basic.test.ts

This file was deleted.

1 change: 1 addition & 0 deletions tests/fixtures/basic.js
@@ -0,0 +1 @@
export * from './mod/*.ts?glob'
1 change: 1 addition & 0 deletions tests/fixtures/mod/a.ts
@@ -0,0 +1 @@
export const a = 'a'
1 change: 1 addition & 0 deletions tests/fixtures/mod/b.ts
@@ -0,0 +1 @@
export const b = 'b'
21 changes: 21 additions & 0 deletions tests/resolve.test.ts
@@ -0,0 +1,21 @@
import { describe, expect, test } from 'vitest'
import { rollup } from 'rollup'
import glob from 'fast-glob'
import Plugin from '../src/rollup'

describe('resolve', async () => {
const fixtures = await glob('./fixtures/*', {
absolute: true,
cwd: __dirname,
})
for (const fixture of fixtures) {
test(fixture, async () => {
const bundle = await rollup({
input: fixture,
plugins: [Plugin()],
})
const { output } = await bundle.generate({})
expect(output[0].code).toMatchSnapshot()
})
}
})

0 comments on commit 44de2ec

Please sign in to comment.