|
14 | 14 | * the License. |
15 | 15 | */ |
16 | 16 |
|
| 17 | +import { resolve } from 'path'; |
| 18 | +import { readFileSync, writeFileSync } from 'fs'; |
17 | 19 | import WorkerPlugin from '../src'; |
18 | | -import { runWebpack, CountApplyWebpackPlugin } from './_util'; |
| 20 | +import { runWebpack, CountApplyWebpackPlugin, watchWebpack, statsWithAssets, sleep } from './_util'; |
| 21 | + |
| 22 | +jest.setTimeout(30000); |
19 | 23 |
|
20 | 24 | describe('worker-plugin', () => { |
21 | 25 | test('exports a class', () => { |
@@ -45,6 +49,23 @@ describe('worker-plugin', () => { |
45 | 49 | expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"0\.worker\.js"/g); |
46 | 50 | }); |
47 | 51 |
|
| 52 | + test('it replaces multiple Worker exports with __webpack_require__', async () => { |
| 53 | + const stats = await runWebpack('multiple', { |
| 54 | + plugins: [ |
| 55 | + new WorkerPlugin() |
| 56 | + ] |
| 57 | + }); |
| 58 | + |
| 59 | + const assetNames = Object.keys(stats.assets); |
| 60 | + expect(assetNames).toHaveLength(3); |
| 61 | + expect(assetNames).toContainEqual('0.worker.js'); |
| 62 | + expect(assetNames).toContainEqual('1.worker.js'); |
| 63 | + |
| 64 | + const main = stats.assets['main.js']; |
| 65 | + expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"0\.worker\.js"/g); |
| 66 | + expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"1\.worker\.js"/g); |
| 67 | + }); |
| 68 | + |
48 | 69 | test('retainModule:true leaves {type:module} in worker init', async () => { |
49 | 70 | const { assets } = await runWebpack('basic', { |
50 | 71 | plugins: [ |
@@ -150,4 +171,57 @@ describe('worker-plugin', () => { |
150 | 171 |
|
151 | 172 | expect(stats.assets['main.js']).toMatch(/new\s+Worker\s*\(\s*new\s+Blob\s*\(\s*\[\s*'onmessage=\(\)=>\{postMessage\("right back at ya"\)\}'\s*\]\s*\)\s*\)/g); |
152 | 173 | }); |
| 174 | + |
| 175 | + describe('watch mode', () => { |
| 176 | + const workerFile = resolve(__dirname, 'fixtures', 'watch', 'worker.js'); |
| 177 | + const workerCode = readFileSync(workerFile, 'utf-8'); |
| 178 | + afterAll(() => { |
| 179 | + writeFileSync(workerFile, workerCode); |
| 180 | + }); |
| 181 | + |
| 182 | + test('it produces consistent modules in watch mode', async () => { |
| 183 | + const compiler = watchWebpack('watch', { |
| 184 | + plugins: [ |
| 185 | + new WorkerPlugin() |
| 186 | + ] |
| 187 | + }); |
| 188 | + |
| 189 | + function Deferred () { |
| 190 | + let controller; |
| 191 | + const p = new Promise((resolve, reject) => { |
| 192 | + controller = { resolve, reject }; |
| 193 | + }); |
| 194 | + Object.assign(p, controller); |
| 195 | + return p; |
| 196 | + } |
| 197 | + |
| 198 | + let stats; |
| 199 | + let ready = new Deferred(); |
| 200 | + |
| 201 | + const watcher = compiler.watch({ |
| 202 | + aggregateTimeout: 1, |
| 203 | + poll: 50, |
| 204 | + ignored: /node_modules|dist/ |
| 205 | + }, (err, stats) => { |
| 206 | + if (err) ready.reject(err); |
| 207 | + else ready.resolve(statsWithAssets(stats)); |
| 208 | + }); |
| 209 | + |
| 210 | + try { |
| 211 | + for (let i = 1; i < 5; i++) { |
| 212 | + ready = new Deferred(); |
| 213 | + writeFileSync(workerFile, workerCode.replace(/console\.log\('hello from worker( \d+)?'\)/, `console.log('hello from worker ${i}')`)); |
| 214 | + await sleep(1000); |
| 215 | + stats = await ready; |
| 216 | + await sleep(1000); |
| 217 | + expect(Object.keys(stats.assets).sort()).toEqual(['0.worker.js', 'main.js']); |
| 218 | + expect(stats.assets['0.worker.js']).toContain(`hello from worker ${i}`); |
| 219 | + } |
| 220 | + } finally { |
| 221 | + watcher.close(); |
| 222 | + } |
| 223 | + |
| 224 | + await sleep(1000); |
| 225 | + }); |
| 226 | + }); |
153 | 227 | }); |
0 commit comments