-
Notifications
You must be signed in to change notification settings - Fork 119
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
feat: add writeBundle
hook
#179
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2c1836a
Add typedefinition for writeBundle hook
lforst 77ab46d
Add tests for writeBundle hook
lforst 1289c49
Add hook call in esbuild
lforst 359adf5
Add hook call in webpack
lforst 94dabcb
Add writeBundle hook to README
lforst 0b96499
Try fix tests for webpack 4 vs. 5
lforst 75cdc11
Retrigger CI
lforst d1f9fd0
docs: add note in README that `writeBundle` currently doesn't pass ar…
lforst 7e1824f
chore: update
antfu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ | ||
test-out |
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,3 @@ | ||
import someString, { someOtherString } from './import' | ||
|
||
process.stdout.write(JSON.stringify({ someString, someOtherString })) |
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,3 @@ | ||
export default 'some string' | ||
|
||
export const someOtherString = 'some other string' |
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,143 @@ | ||
import * as path from 'path' | ||
import * as fs from 'fs' | ||
import { it, describe, expect, vi, afterEach, Mock, beforeAll } from 'vitest' | ||
import { build, toArray, webpackVersion } from '../utils' | ||
import { createUnplugin, UnpluginOptions, VitePlugin } from 'unplugin' | ||
|
||
function createUnpluginWithCallback (writeBundleCallback: UnpluginOptions['writeBundle']) { | ||
return createUnplugin(() => ({ | ||
name: 'test-plugin', | ||
writeBundle: writeBundleCallback | ||
})) | ||
} | ||
|
||
function generateMockWriteBundleHook (outputPath: string) { | ||
return () => { | ||
// We want to check that at the time the `writeBundle` hook is called, all | ||
// build-artifacts have already been written to disk. | ||
|
||
const bundleExists = fs.existsSync(path.join(outputPath, 'output.js')) | ||
const sourceMapExists = fs.existsSync(path.join(outputPath, 'output.js.map')) | ||
|
||
expect(bundleExists).toBe(true) | ||
expect(sourceMapExists).toBe(true) | ||
|
||
return undefined | ||
} | ||
} | ||
|
||
// We extract this check because all bundlers should behave the same | ||
function checkWriteBundleHook (writeBundleCallback: Mock): void { | ||
expect(writeBundleCallback).toHaveBeenCalledOnce() | ||
} | ||
|
||
describe('writeBundle hook', () => { | ||
beforeAll(() => { | ||
fs.rmSync(path.resolve(__dirname, 'test-out'), { recursive: true, force: true }) | ||
}) | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
it('vite', async () => { | ||
expect.assertions(3) | ||
const mockWriteBundleHook = vi.fn(generateMockWriteBundleHook(path.resolve(__dirname, 'test-out/vite'))) | ||
const plugin = createUnpluginWithCallback(mockWriteBundleHook).vite | ||
// we need to define `enforce` here for the plugin to be run | ||
const plugins = toArray(plugin()).map((plugin): VitePlugin => ({ ...plugin, enforce: 'pre' })) | ||
|
||
await build.vite({ | ||
clearScreen: false, | ||
plugins: [plugins], | ||
build: { | ||
lib: { | ||
entry: path.resolve(__dirname, 'test-src/entry.js'), | ||
name: 'TestLib', | ||
fileName: 'output', | ||
formats: ['cjs'] | ||
}, | ||
outDir: path.resolve(__dirname, 'test-out/vite'), | ||
sourcemap: true | ||
} | ||
}) | ||
|
||
checkWriteBundleHook(mockWriteBundleHook) | ||
}) | ||
|
||
it('rollup', async () => { | ||
expect.assertions(3) | ||
const mockResolveIdHook = vi.fn(generateMockWriteBundleHook(path.resolve(__dirname, 'test-out/rollup'))) | ||
const plugin = createUnpluginWithCallback(mockResolveIdHook).rollup | ||
|
||
const rollupBuild = await build.rollup({ | ||
input: path.resolve(__dirname, 'test-src/entry.js') | ||
}) | ||
|
||
await rollupBuild.write({ | ||
plugins: [plugin()], | ||
file: path.resolve(__dirname, 'test-out/rollup/output.js'), | ||
format: 'cjs', | ||
exports: 'named', | ||
sourcemap: true | ||
}) | ||
|
||
checkWriteBundleHook(mockResolveIdHook) | ||
}) | ||
|
||
it('webpack', async () => { | ||
expect.assertions(3) | ||
const mockResolveIdHook = vi.fn(generateMockWriteBundleHook(path.resolve(__dirname, 'test-out/webpack'))) | ||
const plugin = createUnpluginWithCallback(mockResolveIdHook).webpack | ||
|
||
const webpack4Options = { | ||
entry: path.resolve(__dirname, 'test-src/entry.js'), | ||
cache: false, | ||
output: { | ||
path: path.resolve(__dirname, 'test-out/webpack'), | ||
filename: 'output.js', | ||
libraryTarget: 'commonjs' | ||
}, | ||
plugins: [plugin()], | ||
devtool: 'source-map' | ||
} | ||
|
||
const webpack5Options = { | ||
entry: path.resolve(__dirname, 'test-src/entry.js'), | ||
plugins: [plugin()], | ||
devtool: 'source-map', | ||
output: { | ||
path: path.resolve(__dirname, 'test-out/webpack'), | ||
filename: 'output.js', | ||
library: { | ||
type: 'commonjs' | ||
} | ||
} | ||
} | ||
|
||
await new Promise((resolve) => { | ||
build.webpack(webpackVersion!.startsWith('4') ? webpack4Options : webpack5Options, | ||
resolve | ||
) | ||
}) | ||
|
||
checkWriteBundleHook(mockResolveIdHook) | ||
}) | ||
|
||
it('esbuild', async () => { | ||
expect.assertions(3) | ||
const mockResolveIdHook = vi.fn(generateMockWriteBundleHook(path.resolve(__dirname, 'test-out/esbuild'))) | ||
const plugin = createUnpluginWithCallback(mockResolveIdHook).esbuild | ||
|
||
await build.esbuild({ | ||
entryPoints: [path.resolve(__dirname, 'test-src/entry.js')], | ||
plugins: [plugin()], | ||
bundle: true, // actually traverse imports | ||
outfile: path.resolve(__dirname, 'test-out/esbuild/output.js'), | ||
format: 'cjs', | ||
sourcemap: true | ||
}) | ||
|
||
checkWriteBundleHook(mockResolveIdHook) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to have a note saying this hook does not pass any arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! I added a note in d1f9fd0.
Btw feel free to make any edits to this PR as you see fit!