/
write-bundle.test.ts
143 lines (120 loc) · 4.3 KB
/
write-bundle.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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)
})
})