|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import assert from 'node:assert'; |
| 4 | +import { describe, it } from 'node:test'; |
| 5 | + |
| 6 | +import bundleCode from '../bundle.mjs'; |
| 7 | + |
| 8 | +describe('bundleCode', () => { |
| 9 | + describe('server builds', () => { |
| 10 | + it('should bundle a single server entry', async () => { |
| 11 | + const code = ` |
| 12 | + import { createElement } from 'preact'; |
| 13 | + const element = createElement('div', null, 'Hello'); |
| 14 | + return 'output'; |
| 15 | + `; |
| 16 | + |
| 17 | + const result = await bundleCode(code, { server: true }); |
| 18 | + |
| 19 | + assert.ok(result.jsMap, 'should have jsMap'); |
| 20 | + assert.ok(result.jsMap['entrypoint.jsx'], 'should have entrypoint.jsx'); |
| 21 | + assert.ok( |
| 22 | + result.jsMap['entrypoint.jsx'].includes('Hello'), |
| 23 | + 'should include the code content' |
| 24 | + ); |
| 25 | + assert.strictEqual(result.css, '', 'should have empty CSS'); |
| 26 | + assert.strictEqual(result.jsChunks.length, 0, 'should have no JS chunks'); |
| 27 | + assert.strictEqual( |
| 28 | + result.importMapHtml, |
| 29 | + '', |
| 30 | + 'should have empty import map' |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should handle external dependencies', async () => { |
| 35 | + const code = ` |
| 36 | + import { render } from 'preact-render-to-string'; |
| 37 | + import { createElement } from 'preact'; |
| 38 | + const element = createElement('div', null, 'Test'); |
| 39 | + return render(element); |
| 40 | + `; |
| 41 | + |
| 42 | + const result = await bundleCode(code, { server: true }); |
| 43 | + |
| 44 | + assert.ok(result.jsMap['entrypoint.jsx'], 'should bundle successfully'); |
| 45 | + // External dependencies should not be bundled |
| 46 | + assert.ok( |
| 47 | + result.jsMap['entrypoint.jsx'].includes('require'), |
| 48 | + 'should use require for external deps' |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should apply SERVER define flag', async () => { |
| 53 | + const code = ` |
| 54 | + const value = SERVER ? 'server-side' : 'client-side'; |
| 55 | + return value; |
| 56 | + `; |
| 57 | + |
| 58 | + const result = await bundleCode(code, { server: true }); |
| 59 | + |
| 60 | + // SERVER should be replaced with 'true' string, which will then be evaluated |
| 61 | + assert.ok(result.jsMap, 'should have jsMap'); |
| 62 | + assert.ok( |
| 63 | + result.jsMap['entrypoint.jsx'] !== undefined, |
| 64 | + 'should have entrypoint.jsx in jsMap' |
| 65 | + ); |
| 66 | + // Code might be empty if optimization removed everything, that's okay |
| 67 | + assert.ok( |
| 68 | + typeof result.jsMap['entrypoint.jsx'] === 'string', |
| 69 | + 'should be a string' |
| 70 | + ); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('client builds', () => { |
| 75 | + it('should bundle a single client entry', async () => { |
| 76 | + const code = ` |
| 77 | + import { createElement } from 'preact'; |
| 78 | + const element = createElement('div', null, 'Hello'); |
| 79 | + export default element; |
| 80 | + `; |
| 81 | + |
| 82 | + const result = await bundleCode(code, { server: false }); |
| 83 | + |
| 84 | + assert.ok(result.jsMap, 'should have jsMap'); |
| 85 | + assert.ok(result.jsMap['entrypoint.jsx'], 'should have entrypoint.jsx'); |
| 86 | + assert.strictEqual(typeof result.css, 'string', 'should have CSS string'); |
| 87 | + assert.ok(Array.isArray(result.jsChunks), 'should have jsChunks array'); |
| 88 | + assert.strictEqual( |
| 89 | + typeof result.importMapHtml, |
| 90 | + 'string', |
| 91 | + 'should have importMapHtml' |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should bundle multiple client entries', async () => { |
| 96 | + const codeMap = new Map([ |
| 97 | + [ |
| 98 | + 'entry1.jsx', |
| 99 | + `import { createElement } from 'preact'; export default createElement('div', null, 'One');`, |
| 100 | + ], |
| 101 | + [ |
| 102 | + 'entry2.jsx', |
| 103 | + `import { createElement } from 'preact'; export default createElement('div', null, 'Two');`, |
| 104 | + ], |
| 105 | + ]); |
| 106 | + |
| 107 | + const result = await bundleCode(codeMap, { server: false }); |
| 108 | + |
| 109 | + assert.ok(result.jsMap['entry1.jsx'], 'should have entry1.jsx'); |
| 110 | + assert.ok(result.jsMap['entry2.jsx'], 'should have entry2.jsx'); |
| 111 | + assert.ok( |
| 112 | + result.jsMap['entry1.jsx'].includes('One'), |
| 113 | + 'entry1 should include its content' |
| 114 | + ); |
| 115 | + assert.ok( |
| 116 | + result.jsMap['entry2.jsx'].includes('Two'), |
| 117 | + 'entry2 should include its content' |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should handle module imports', async () => { |
| 122 | + const code = ` |
| 123 | + import { createElement } from 'preact'; |
| 124 | + export default createElement('div', null, 'Test'); |
| 125 | + `; |
| 126 | + |
| 127 | + const result = await bundleCode(code, { server: false }); |
| 128 | + |
| 129 | + assert.ok(result.jsMap['entrypoint.jsx'], 'should have JS'); |
| 130 | + // CSS handling depends on the cssLoader plugin |
| 131 | + assert.strictEqual( |
| 132 | + typeof result.css, |
| 133 | + 'string', |
| 134 | + 'should have CSS property' |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should apply CLIENT define flag', async () => { |
| 139 | + const code = ` |
| 140 | + if (CLIENT) { |
| 141 | + console.log('client-side'); |
| 142 | + } else { |
| 143 | + console.log('server-side'); |
| 144 | + } |
| 145 | + `; |
| 146 | + |
| 147 | + const result = await bundleCode(code, { server: false }); |
| 148 | + |
| 149 | + assert.ok( |
| 150 | + result.jsMap['entrypoint.jsx'].includes('client-side') || |
| 151 | + result.jsMap['entrypoint.jsx'].includes('true'), |
| 152 | + 'should process CLIENT flag' |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should minify client builds', async () => { |
| 157 | + const code = ` |
| 158 | + import { createElement } from 'preact'; |
| 159 | + // This is a comment that should be removed |
| 160 | + const veryLongVariableName = 'value'; |
| 161 | + export default createElement('div', null, veryLongVariableName); |
| 162 | + `; |
| 163 | + |
| 164 | + const result = await bundleCode(code, { server: false }); |
| 165 | + |
| 166 | + // Minified code should not contain comments and should be shorter |
| 167 | + assert.ok( |
| 168 | + !result.jsMap['entrypoint.jsx'].includes('This is a comment'), |
| 169 | + 'should remove comments' |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should generate import map for multiple entries', async () => { |
| 174 | + const codeMap = new Map([ |
| 175 | + [ |
| 176 | + 'page1.jsx', |
| 177 | + `import { createElement } from 'preact'; export default createElement('div', null, 'Page 1');`, |
| 178 | + ], |
| 179 | + [ |
| 180 | + 'page2.jsx', |
| 181 | + `import { createElement } from 'preact'; export default createElement('div', null, 'Page 2');`, |
| 182 | + ], |
| 183 | + ]); |
| 184 | + |
| 185 | + const result = await bundleCode(codeMap, { server: false }); |
| 186 | + |
| 187 | + assert.ok( |
| 188 | + result.importMapHtml.includes('importmap'), |
| 189 | + 'should contain importmap script tag' |
| 190 | + ); |
| 191 | + // Import map should not contain virtual entries |
| 192 | + assert.ok( |
| 193 | + !result.importMapHtml.includes('_virtual_'), |
| 194 | + 'should not include virtual entries' |
| 195 | + ); |
| 196 | + assert.ok( |
| 197 | + !result.importMapHtml.includes('entrypoint'), |
| 198 | + 'should not include entrypoint in importmap' |
| 199 | + ); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe('code splitting', () => { |
| 204 | + it('should support multiple entries in client builds', async () => { |
| 205 | + const codeMap = new Map([ |
| 206 | + [ |
| 207 | + 'entry1.jsx', |
| 208 | + ` |
| 209 | + import { createElement } from 'preact'; |
| 210 | + export default createElement('div', null, 'Entry 1'); |
| 211 | + `, |
| 212 | + ], |
| 213 | + [ |
| 214 | + 'entry2.jsx', |
| 215 | + ` |
| 216 | + import { createElement } from 'preact'; |
| 217 | + export default createElement('div', null, 'Entry 2'); |
| 218 | + `, |
| 219 | + ], |
| 220 | + ]); |
| 221 | + |
| 222 | + const result = await bundleCode(codeMap, { server: false }); |
| 223 | + |
| 224 | + assert.ok(result.jsMap['entry1.jsx'], 'should have entry1'); |
| 225 | + assert.ok(result.jsMap['entry2.jsx'], 'should have entry2'); |
| 226 | + // Code splitting behavior depends on rolldown configuration |
| 227 | + assert.ok(Array.isArray(result.jsChunks), 'should have jsChunks array'); |
| 228 | + }); |
| 229 | + }); |
| 230 | + |
| 231 | + describe('virtual entries', () => { |
| 232 | + it('should handle Map input correctly', async () => { |
| 233 | + const codeMap = new Map([['test.jsx', `export default 'test';`]]); |
| 234 | + |
| 235 | + const result = await bundleCode(codeMap, { server: false }); |
| 236 | + |
| 237 | + assert.ok(result.jsMap['test.jsx'], 'should handle Map key'); |
| 238 | + }); |
| 239 | + |
| 240 | + it('should handle string input correctly', async () => { |
| 241 | + const code = `export default 'test';`; |
| 242 | + |
| 243 | + const result = await bundleCode(code, { server: true }); |
| 244 | + |
| 245 | + assert.ok(result.jsMap['entrypoint.jsx'], 'should create entrypoint.jsx'); |
| 246 | + }); |
| 247 | + }); |
| 248 | +}); |
0 commit comments