|
1 | 1 | import fs from 'node:fs'; |
2 | 2 | import path from 'node:path'; |
| 3 | +import type { dh as DhType } from '@deephaven/jsapi-types'; |
3 | 4 |
|
4 | 5 | import { downloadFromURL, urlToDirectoryName } from './serverUtils.js'; |
5 | 6 | import { polyfillWs } from './polyfillWs.js'; |
@@ -84,4 +85,65 @@ export async function loadModules<TMainModule>({ |
84 | 85 | return require(mainModulePath); |
85 | 86 | } |
86 | 87 |
|
87 | | -export default loadModules; |
| 88 | +/** |
| 89 | + * Load `jsapi/dh-core.js` and `jsapi/dh-internal.js` modules from a Core Server. |
| 90 | + * @param serverUrl The URL of the server to load from. |
| 91 | + * @param storageDir The directory to store the downloaded modules. |
| 92 | + * @param targetModuleType The type of module to load. Can be either 'esm' or 'cjs'. |
| 93 | + * @returns The default export the `jsapi/dh-core.js` module. |
| 94 | + */ |
| 95 | +export async function loadDhModules({ |
| 96 | + serverUrl, |
| 97 | + storageDir, |
| 98 | + targetModuleType, |
| 99 | +}: Pick< |
| 100 | + LoadModuleOptions, |
| 101 | + 'serverUrl' | 'storageDir' | 'targetModuleType' |
| 102 | +>): Promise<typeof DhType> { |
| 103 | + if (targetModuleType === 'esm') { |
| 104 | + // These will not be needed if we ever update the JSAPI to not rely on |
| 105 | + // `window` and `self`. |
| 106 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 107 | + // @ts-ignore |
| 108 | + globalThis.self = globalThis; |
| 109 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 110 | + // @ts-ignore |
| 111 | + globalThis.window = globalThis; |
| 112 | + } |
| 113 | + |
| 114 | + const coreModule = await loadModules< |
| 115 | + typeof DhType & { default?: typeof DhType } |
| 116 | + >({ |
| 117 | + serverUrl, |
| 118 | + serverPaths: ['jsapi/dh-core.js', 'jsapi/dh-internal.js'], |
| 119 | + storageDir, |
| 120 | + targetModuleType, |
| 121 | + download: |
| 122 | + targetModuleType === 'esm' |
| 123 | + ? // ESM does not need any transformation since the server modules are already ESM. |
| 124 | + true |
| 125 | + : // CJS needs a post-download transform to convert the ESM modules to CJS. |
| 126 | + (serverPath, content) => { |
| 127 | + if (serverPath === 'jsapi/dh-core.js') { |
| 128 | + return content |
| 129 | + .replace( |
| 130 | + `import {dhinternal} from './dh-internal.js';`, |
| 131 | + `const {dhinternal} = require("./dh-internal.js");` |
| 132 | + ) |
| 133 | + .replace(`export default dh;`, `module.exports = dh;`); |
| 134 | + } |
| 135 | + |
| 136 | + if (serverPath === 'jsapi/dh-internal.js') { |
| 137 | + return content.replace( |
| 138 | + `export{__webpack_exports__dhinternal as dhinternal};`, |
| 139 | + `module.exports={dhinternal:__webpack_exports__dhinternal};` |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + return content; |
| 144 | + }, |
| 145 | + }); |
| 146 | + |
| 147 | + // ESM uses `default` export. CJS does not. |
| 148 | + return coreModule.default ?? coreModule; |
| 149 | +} |
0 commit comments