-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
78 additions
and
21 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')!, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` | ||
}, | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; | ||
" | ||
`; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './mod/*.ts?glob' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const a = 'a' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const b = 'b' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
} | ||
}) |