|
| 1 | +// This module is bundled into the Vite template config, so keep the generated JavaScript |
| 2 | +// as close to runtime-free as practical. |
| 3 | +type transformResult = { |
| 4 | + code: string, |
| 5 | + map: Null.t<unit>, |
| 6 | +} |
| 7 | + |
| 8 | +type hotUpdateContext = { |
| 9 | + file: string, |
| 10 | + read: unit => promise<string>, |
| 11 | +} |
| 12 | + |
| 13 | +type plugin = { |
| 14 | + name: string, |
| 15 | + enforce: string, |
| 16 | + handleHotUpdate: hotUpdateContext => promise<Nullable.t<array<string>>>, |
| 17 | + transform: (string, string) => Nullable.t<transformResult>, |
| 18 | +} |
| 19 | + |
| 20 | +module JsArray = { |
| 21 | + @get external length: array<'a> => int = "length" |
| 22 | + @get_index external unsafeGet: (array<'a>, int) => 'a = "" |
| 23 | + @send external filter: (array<'a>, 'a => bool) => array<'a> = "filter" |
| 24 | + @send external map: (array<'a>, 'a => 'b) => array<'b> = "map" |
| 25 | + @send external join: (array<string>, string) => string = "join" |
| 26 | +} |
| 27 | + |
| 28 | +@send external splitByRegExp: (string, RegExp.t) => array<string> = "split" |
| 29 | + |
| 30 | +let rescriptReactFastRefresh = () => { |
| 31 | + let previousCode = Dict.make() |
| 32 | + |
| 33 | + let componentName = id => { |
| 34 | + let pathParts = id->String.split("/") |
| 35 | + |
| 36 | + if pathParts->JsArray.length === 0 { |
| 37 | + Nullable.undefined |
| 38 | + } else { |
| 39 | + let fileName = pathParts->JsArray.unsafeGet(pathParts->JsArray.length - 1) |
| 40 | + let fileName = fileName->String.replaceRegExp(/\.res\.mjs$/, "") |
| 41 | + |
| 42 | + if fileName->String.length === 0 { |
| 43 | + Nullable.undefined |
| 44 | + } else { |
| 45 | + let name = |
| 46 | + fileName |
| 47 | + ->splitByRegExp(/[^A-Za-z0-9_$]+/) |
| 48 | + ->JsArray.filter(part => part->String.length > 0) |
| 49 | + ->JsArray.map(part => |
| 50 | + part->String.charAt(0)->String.toUpperCase ++ part->String.slice(~start=1) |
| 51 | + ) |
| 52 | + ->JsArray.join("") |
| 53 | + |
| 54 | + Nullable.make(/^[A-Za-z_$]/->RegExp.test(name) ? name : `ReScript${name}`) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + { |
| 60 | + name: "rescript-react-fast-refresh", |
| 61 | + enforce: "pre", |
| 62 | + handleHotUpdate: async ({file, read}) => { |
| 63 | + if file->String.endsWith(".res") || file->String.endsWith(".resi") { |
| 64 | + Nullable.make([]) |
| 65 | + } else if !(file->String.endsWith(".res.mjs")) { |
| 66 | + Nullable.undefined |
| 67 | + } else { |
| 68 | + let code = await read() |
| 69 | + let previous = previousCode->Dict.get(file) |
| 70 | + previousCode->Dict.set(file, code) |
| 71 | + |
| 72 | + switch previous { |
| 73 | + | Some(previous) if previous === code => Nullable.make([]) |
| 74 | + | _ => Nullable.undefined |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | + transform: (code, id) => { |
| 79 | + let idParts = id->String.split("?") |
| 80 | + let file = idParts->JsArray.unsafeGet(0) |
| 81 | + |
| 82 | + if !(file->String.endsWith(".res.mjs")) { |
| 83 | + Nullable.undefined |
| 84 | + } else { |
| 85 | + previousCode->Dict.set(file, code) |
| 86 | + |
| 87 | + if ( |
| 88 | + !( |
| 89 | + code->String.includes("react/jsx-runtime") && |
| 90 | + !(code->String.includes("react-dom/client")) && |
| 91 | + /\bfunction\s+make\s*\(/->RegExp.test(code) && |
| 92 | + /\bexport\s*\{[\s\S]*\bmake\b[\s\S]*\}/m->RegExp.test(code) |
| 93 | + ) |
| 94 | + ) { |
| 95 | + Nullable.undefined |
| 96 | + } else { |
| 97 | + switch componentName(file) { |
| 98 | + | Null | Undefined | Value("Main") => Nullable.undefined |
| 99 | + | Value(name) => |
| 100 | + Nullable.make({ |
| 101 | + code: code |
| 102 | + ->String.replaceRegExp(/\bfunction\s+make\s*\(/, `function ${name}(`) |
| 103 | + ->String.replaceRegExp(/\nexport\s*\{/, `\nlet make = ${name};\n\nexport {`), |
| 104 | + map: Null.null, |
| 105 | + }) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
0 commit comments