|
| 1 | +import * as fs from 'node:fs/promises'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 4 | + |
| 5 | +import type { RollupOptions, Plugin } from 'rollup'; |
| 6 | +import type { Node } from 'estree-walker'; |
| 7 | +import { asyncWalk, walk } from 'estree-walker'; |
| 8 | +import MagicString from 'magic-string'; |
| 9 | + |
| 10 | +import nodeResolve from '@rollup/plugin-node-resolve'; |
| 11 | +import terser from '@rollup/plugin-terser'; |
| 12 | + |
| 13 | +const workerFileUri: Plugin = { |
| 14 | + // In non-modular workers, the script URL is always globalThis.location.href. |
| 15 | + name: 'workerFileUrl', |
| 16 | + resolveFileUrl({ relativePath }) { |
| 17 | + return `new URL('${relativePath}', globalThis.location.href)`; |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +function bundleWorker(): RollupOptions { |
| 22 | + const plugins = [nodeResolve({ preferBuiltins: false, browser: true }), includeUriBundles(), workerFileUri, terser()]; |
| 23 | + |
| 24 | + return { |
| 25 | + input: 'lib/worker/worker.js', |
| 26 | + output: { |
| 27 | + dir: `dist/worker/`, |
| 28 | + format: 'esm', |
| 29 | + sourcemap: true |
| 30 | + }, |
| 31 | + plugins |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +function bundledModuleForReactNativeWeb(): RollupOptions { |
| 36 | + return { |
| 37 | + input: 'lib/index.js', |
| 38 | + output: { |
| 39 | + file: 'dist/index.react_native_web.js', |
| 40 | + format: 'esm', |
| 41 | + sourcemap: true |
| 42 | + }, |
| 43 | + external: ['@powersync/common', '@powersync/shared-internals', 'comlink'], |
| 44 | + plugins: disableDefaultWorkers() |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +const options: RollupOptions[] = [bundledModuleForReactNativeWeb(), bundleWorker()]; |
| 49 | + |
| 50 | +function disableDefaultWorkers(): Plugin { |
| 51 | + return { |
| 52 | + name: 'disableSpawnDefaultPowerSyncWorker', |
| 53 | + transform(code, id) { |
| 54 | + if (!code.includes('function spawnDefaultPowerSyncWorker')) return; |
| 55 | + |
| 56 | + // In the build for React Native web, import.meta.url must not appear anywhere in the bundled output as Metro does |
| 57 | + // not generate ESM modules. |
| 58 | + // For details, see the comment on spawnDefaultPowerSyncWorker in src/worker/client.ts. |
| 59 | + const ms = new MagicString(code); |
| 60 | + |
| 61 | + const ast = this.parse(code); |
| 62 | + walk(ast, { |
| 63 | + enter(node) { |
| 64 | + if (node.type === 'FunctionDeclaration' && node.id.name === 'spawnDefaultPowerSyncWorker') { |
| 65 | + const body = node.body; |
| 66 | + ms.overwrite( |
| 67 | + (body as any).start, |
| 68 | + (body as any).end, |
| 69 | + `{ |
| 70 | + throw new Error('You are using the React Native web build of the PowerSync SDK, which requires custom worker URLs. Please see the documentation at https://docs.powersync.com/client-sdks/frameworks/react-native-web-support for details.'); |
| 71 | +} |
| 72 | +` |
| 73 | + ); |
| 74 | + this.skip(); |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + return { |
| 80 | + code: ms.toString(), |
| 81 | + map: ms.generateMap() |
| 82 | + }; |
| 83 | + } |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +// Plugin that replaces `new URL(x, import.meta.url)` with a resolved asset URL. |
| 88 | +// |
| 89 | +// We need to do this to ensure WebAssembly modules referenced by @journeyapps/wa-sqlite are copied into dist/. |
| 90 | +function includeUriBundles(): Plugin { |
| 91 | + function isImportMetaUrl(node: Node): boolean { |
| 92 | + if (node.type !== 'MemberExpression') return false; |
| 93 | + |
| 94 | + const { object, property } = node; |
| 95 | + if (property.type !== 'Identifier' || property.name !== 'url') return false; |
| 96 | + |
| 97 | + return object.type === 'MetaProperty' && object.meta.name === 'import' && object.property.name === 'meta'; |
| 98 | + } |
| 99 | + |
| 100 | + function extractUriImport(node: Node): string | undefined { |
| 101 | + // Do we have a new URI(x, y) expression? |
| 102 | + if ( |
| 103 | + node.type != 'NewExpression' || |
| 104 | + node.callee.type !== 'Identifier' || |
| 105 | + node.callee.name != 'URL' || |
| 106 | + node.arguments.length != 2 |
| 107 | + ) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const [importDesc, base] = node.arguments; |
| 112 | + if (importDesc.type !== 'Literal' || typeof (importDesc as any).value !== 'string') { |
| 113 | + return; |
| 114 | + } |
| 115 | + if (!isImportMetaUrl(base)) { |
| 116 | + return; |
| 117 | + } |
| 118 | + return (importDesc as any).value as string; |
| 119 | + } |
| 120 | + |
| 121 | + return { |
| 122 | + name: 'includeUriBundles', |
| 123 | + async transform(code, id) { |
| 124 | + if (!code.includes('import.meta.url')) return; |
| 125 | + |
| 126 | + let ms: MagicString | undefined; |
| 127 | + |
| 128 | + const parsed = this.parse(code); |
| 129 | + const plugin = this; |
| 130 | + await asyncWalk(parsed, { |
| 131 | + async enter(node) { |
| 132 | + const importedUri = extractUriImport(node); |
| 133 | + if (importedUri != null) { |
| 134 | + const resolvedUrl = new URL(importedUri, pathToFileURL(id)); |
| 135 | + const resolvedPath = fileURLToPath(resolvedUrl); |
| 136 | + |
| 137 | + // See https://rollupjs.org/plugin-development/#file-urls |
| 138 | + const referenceId = plugin.emitFile({ |
| 139 | + type: 'asset', |
| 140 | + name: path.basename(resolvedPath), |
| 141 | + source: await fs.readFile(resolvedPath) |
| 142 | + }); |
| 143 | + |
| 144 | + ms ??= new MagicString(code); |
| 145 | + const start = (node as any).start as number; |
| 146 | + const end = (node as any).end as number; |
| 147 | + ms.overwrite(start, end, `import.meta.ROLLUP_FILE_URL_OBJ_${referenceId}`); |
| 148 | + this.skip(); |
| 149 | + } |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + if (ms) { |
| 154 | + return { |
| 155 | + code: ms.toString(), |
| 156 | + map: ms.generateMap() |
| 157 | + }; |
| 158 | + } |
| 159 | + } |
| 160 | + }; |
| 161 | +} |
| 162 | + |
| 163 | +export default options; |
0 commit comments