|
8 | 8 | */
|
9 | 9 | 'use strict';
|
10 | 10 |
|
11 |
| -import * as tmp from 'tmp'; |
12 |
| -import * as fs from 'fs'; |
13 |
| -import replace from '@rollup/plugin-replace'; |
14 |
| -import resolve from '@rollup/plugin-node-resolve'; |
15 |
| -import {rollup} from 'rollup'; |
16 |
| -import path from 'path'; |
17 |
| - |
18 |
| -const rollupCache: Map<string, string | null> = new Map(); |
19 |
| - |
20 |
| -// Utility function to read and bundle a standalone browser script |
21 |
| -async function getRollupResult(scriptSrc: string): Promise<string | null> { |
22 |
| - const cachedResult = rollupCache.get(scriptSrc); |
23 |
| - if (cachedResult !== undefined) { |
24 |
| - return cachedResult; |
25 |
| - } |
26 |
| - let tmpFile; |
27 |
| - try { |
28 |
| - tmpFile = tmp.fileSync(); |
29 |
| - const rollupConfig = { |
30 |
| - input: require.resolve(scriptSrc), |
31 |
| - onwarn: console.warn, |
32 |
| - plugins: [ |
33 |
| - replace({__DEV__: 'true', preventAssignment: true}), |
34 |
| - resolve({ |
35 |
| - rootDir: path.join(__dirname, '..', '..', '..'), |
36 |
| - }), |
37 |
| - ], |
38 |
| - output: { |
39 |
| - externalLiveBindings: false, |
40 |
| - freeze: false, |
41 |
| - interop: false, |
42 |
| - esModule: false, |
43 |
| - }, |
44 |
| - }; |
45 |
| - const outputConfig = { |
46 |
| - file: tmpFile.name, |
47 |
| - format: 'iife', |
48 |
| - }; |
49 |
| - const bundle = await rollup(rollupConfig); |
50 |
| - await bundle.write(outputConfig); |
51 |
| - const bundleBuffer = Buffer.alloc(4096); |
52 |
| - let bundleStr = ''; |
53 |
| - while (true) { |
54 |
| - // $FlowFixMe[incompatible-call] |
55 |
| - const bytes = fs.readSync(tmpFile.fd, bundleBuffer); |
56 |
| - if (bytes <= 0) { |
57 |
| - break; |
58 |
| - } |
59 |
| - bundleStr += bundleBuffer.slice(0, bytes).toString(); |
60 |
| - } |
61 |
| - rollupCache.set(scriptSrc, bundleStr); |
62 |
| - return bundleStr; |
63 |
| - } catch (e) { |
64 |
| - rollupCache.set(scriptSrc, null); |
65 |
| - return null; |
66 |
| - } finally { |
67 |
| - if (tmpFile) { |
68 |
| - tmpFile.removeCallback(); |
69 |
| - } |
70 |
| - } |
71 |
| -} |
72 |
| - |
73 | 11 | async function insertNodesAndExecuteScripts(
|
74 | 12 | source: Document | Element,
|
75 | 13 | target: Node,
|
@@ -150,12 +88,17 @@ async function executeScript(script: Element) {
|
150 | 88 | const parent = script.parentNode;
|
151 | 89 | const scriptSrc = script.getAttribute('src');
|
152 | 90 | if (scriptSrc) {
|
153 |
| - const rollupOutput = await getRollupResult(scriptSrc); |
154 |
| - if (rollupOutput) { |
155 |
| - const transientScript = ownerDocument.createElement('script'); |
156 |
| - transientScript.textContent = rollupOutput; |
157 |
| - parent.appendChild(transientScript); |
158 |
| - parent.removeChild(transientScript); |
| 91 | + if (document !== ownerDocument) { |
| 92 | + throw new Error( |
| 93 | + 'You must set the current document to the global document to use script src in tests', |
| 94 | + ); |
| 95 | + } |
| 96 | + // $FlowFixMe |
| 97 | + try { |
| 98 | + require(scriptSrc); |
| 99 | + } catch (x) { |
| 100 | + const event = new window.ErrorEvent('error', {error: x}); |
| 101 | + window.dispatchEvent(event); |
159 | 102 | }
|
160 | 103 | } else {
|
161 | 104 | const newScript = ownerDocument.createElement('script');
|
|
0 commit comments