-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add rollup entry-points scenario (#26)
- Loading branch information
1 parent
c196bb9
commit 469879b
Showing
14 changed files
with
165 additions
and
33 deletions.
There are no files selected for viewing
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 @@ | ||
import React from "react"; | ||
|
||
interface ComponentAProps { | ||
test: string; | ||
} | ||
|
||
export function ComponentA(props: ComponentAProps): React.JSX.Element { | ||
const { test } = props; | ||
return <>ComponentA: {test}</>; | ||
} |
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 { ComponentA } from "./ComponentA/ComponentA"; |
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 @@ | ||
import React from "react"; | ||
|
||
interface ComponentBProps { | ||
test: string; | ||
} | ||
|
||
export function ComponentB(props: ComponentBProps): React.JSX.Element { | ||
const { test } = props; | ||
return <>ComponentB: {test}</>; | ||
} |
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 { ComponentB } from "./ComponentB/ComponentB"; |
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 |
---|---|---|
@@ -1,31 +1,88 @@ | ||
import fs from 'node:fs/promises' | ||
import path from 'node:path' | ||
import { rollup } from 'rollup' | ||
import esbuild from 'rollup-plugin-esbuild' | ||
import { expect, test } from 'vitest' | ||
import { describe, expect, test } from 'vitest' | ||
import UnpluginIsolatedDecl from '../src/rollup' | ||
|
||
test('rollup', async () => { | ||
const input = path.resolve(__dirname, 'fixtures/main.ts') | ||
const dist = path.resolve(__dirname, 'temp') | ||
|
||
const bundle = await rollup({ | ||
input, | ||
plugins: [ | ||
UnpluginIsolatedDecl({ | ||
extraOutdir: 'temp', | ||
}), | ||
esbuild(), | ||
], | ||
logLevel: 'silent', | ||
describe('rollup', () => { | ||
const TEST_SANDBOX_FOLDER = 'temp/rollup' | ||
|
||
test('generate basic', async () => { | ||
const input = path.resolve(__dirname, 'fixtures/basic/main.ts') | ||
const dist = path.resolve(__dirname, `${TEST_SANDBOX_FOLDER}/basic`) | ||
|
||
const bundle = await rollup({ | ||
input, | ||
plugins: [ | ||
UnpluginIsolatedDecl({ | ||
extraOutdir: 'temp', | ||
}), | ||
esbuild(), | ||
], | ||
logLevel: 'silent', | ||
}) | ||
|
||
const result = await bundle.generate({ | ||
dir: dist, | ||
}) | ||
|
||
expect( | ||
result.output.map((asset) => | ||
[ | ||
`// ${asset.fileName.replaceAll('\\', '/')}`, | ||
asset.type === 'chunk' ? asset.code : asset.source, | ||
].join('\n'), | ||
), | ||
).toMatchSnapshot() | ||
}) | ||
|
||
const result = await bundle.generate({ | ||
dir: dist, | ||
test('write entry-points', async () => { | ||
const input = { | ||
a: path.resolve(__dirname, 'fixtures/entry-points/a/index.ts'), | ||
b: path.resolve(__dirname, 'fixtures/entry-points/b/index.ts'), | ||
} | ||
const dist = path.resolve(__dirname, `${TEST_SANDBOX_FOLDER}/entry-points`) | ||
|
||
const bundle = await rollup({ | ||
input, | ||
plugins: [UnpluginIsolatedDecl(), esbuild()], | ||
logLevel: 'silent', | ||
}) | ||
|
||
// https://github.com/unplugin/unplugin-isolated-decl/issues/22 | ||
await bundle.write({ | ||
dir: dist, | ||
entryFileNames: '[name]/index.js', | ||
preserveModules: true, | ||
}) | ||
|
||
/** | ||
* Map written output from file system rather than from bundle due to | ||
* module execution order not consistent | ||
* | ||
* @see https://github.com/rollup/rollup/issues/3888 | ||
*/ | ||
const allBundledFiles = ( | ||
await fs.readdir(dist, { | ||
recursive: true, | ||
withFileTypes: true, | ||
}) | ||
).filter((it) => it.isFile()) | ||
|
||
const fileSystemOutput = allBundledFiles.map((it) => { | ||
return (async () => { | ||
const filePath = path.relative(dist, path.join(it.parentPath, it.name)) | ||
|
||
const content = await fs.readFile(path.join(dist, filePath), 'utf-8') | ||
|
||
return [ | ||
`// ${filePath.replaceAll('\\', '/')}`, | ||
content.toString(), | ||
].join('\n') | ||
})() | ||
}) | ||
|
||
expect(Promise.all(fileSystemOutput)).resolves.toMatchSnapshot() | ||
}) | ||
expect( | ||
result.output.map( | ||
(asset) => | ||
`// ${asset.fileName.replaceAll('\\', '/')}\n${asset.type === 'chunk' ? asset.code : asset.source}`, | ||
), | ||
).toMatchSnapshot() | ||
}) |