Skip to content
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
4 changes: 3 additions & 1 deletion packages/markdown-exit/src/index.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export * from './token'
// for correct d.ts generation
type MarkdownExitConstructor = InstanceType<typeof MarkdownExit>

// Marked pure so bundlers can drop `MarkdownExit` with its renderer and presets
// when an app imports only `Parser` or other parser exports.
// eslint-disable-next-line ts/no-redeclare
const MarkdownExitConstructor = callable(MarkdownExit) as unknown as (typeof createMarkdownExit & typeof MarkdownExit)
const MarkdownExitConstructor = /* @__PURE__ */ callable(MarkdownExit) as unknown as (typeof createMarkdownExit & typeof MarkdownExit)

export default MarkdownExitConstructor
35 changes: 35 additions & 0 deletions packages/markdown-exit/tests/core-entry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,47 @@ async function bundleImports(entry: string): Promise<string[]> {
return result[0].output.flatMap(chunk => chunk.type === 'chunk' ? chunk.imports : [])
}

const ENTRY = 'virtual:entry'

// Maps each bundled module id to the length of its code left after tree-shaking.
async function bundleRenderedLengths(code: string): Promise<Map<string, number>> {
const result = await build({
configFile: false,
logLevel: 'silent',
plugins: [{
name: 'virtual-entry',
resolveId: id => id === ENTRY ? id : null,
load: id => id === ENTRY ? code : null,
}],
build: {
write: false,
minify: false,
rollupOptions: {
input: ENTRY,
external: id => id !== ENTRY && !id.startsWith('.') && !id.startsWith('/'),
},
},
}) as Rollup.RollupOutput | Rollup.RollupOutput[]
const chunks = [result].flat()[0].output.filter(chunk => chunk.type === 'chunk')
return new Map(chunks.flatMap(chunk => Object.entries(chunk.modules).map(([id, module]) => [id, module.renderedLength])))
}

describe('markdown-exit/core', () => {
it('does not bundle linkify-it', async () => {
assert.include(await bundleImports('../src/index.ts'), 'linkify-it')
assert.notInclude(await bundleImports('../src/index.core.ts'), 'linkify-it')
})

it('tree-shakes the renderer and presets when only `Parser` is imported', async () => {
const core = fileURLToPath(new URL('../src/index.core.ts', import.meta.url))
const lengths = await bundleRenderedLengths(`import { Parser } from ${JSON.stringify(core)}\nconsole.log(new Parser().parse('# a'))`)
const rendered = (path: string) => [...lengths].reduce((sum, [id, length]) => id.includes(path) ? sum + length : sum, 0)

assert.isAbove(rendered('/src/parser/parser.ts'), 0)
assert.strictEqual(rendered('/src/renderer.ts'), 0)
assert.strictEqual(rendered('/src/presets/'), 0)
})

it('renders without linkify-it', () => {
const md = MarkdownExit()

Expand Down
Loading