diff options
Diffstat (limited to 'src/mono/wasm/runtime/rollup.config.js')
-rw-r--r-- | src/mono/wasm/runtime/rollup.config.js | 62 |
1 files changed, 59 insertions, 3 deletions
diff --git a/src/mono/wasm/runtime/rollup.config.js b/src/mono/wasm/runtime/rollup.config.js index 7225a2c954e..f37712aa155 100644 --- a/src/mono/wasm/runtime/rollup.config.js +++ b/src/mono/wasm/runtime/rollup.config.js @@ -7,6 +7,7 @@ import * as path from "path"; import { createHash } from "crypto"; import dts from "rollup-plugin-dts"; import consts from "rollup-plugin-consts"; +import { createFilter } from "@rollup/pluginutils"; const configuration = process.env.Configuration; const isDebug = configuration !== "Release"; @@ -45,7 +46,19 @@ const banner_dts = banner + "//!\n//! This is generated file, see src/mono/wasm/ // emcc doesn't know how to load ES6 module, that's why we need the whole rollup.js const format = "iife"; const name = "__dotnet_runtime"; - +const inlineAssert = [ + { + pattern: /mono_assert\(([^,]*), *"([^"]*)"\);/gm, + // eslint-disable-next-line quotes + replacement: 'if (!($1)) throw new Error("Assert failed: $2"); // inlined mono_assert' + }, + { + pattern: /mono_assert\(([^,]*), \(\) => *`([^`]*)`\);/gm, + replacement: "if (!($1)) throw new Error(`Assert failed: $2`); // inlined mono_assert" + }, { + pattern: /^\s*mono_assert/gm, + failure: "previous regexp didn't inline all mono_assert statements" + }]; const iffeConfig = { treeshake: !isDebug, input: "exports.ts", @@ -72,7 +85,7 @@ const iffeConfig = { handler(warning); }, - plugins: [consts({ productVersion, configuration }), typescript()] + plugins: [regexReplace(inlineAssert), consts({ productVersion, configuration }), typescript()] }; const typesConfig = { input: "./export-types.ts", @@ -159,4 +172,47 @@ function checkFileExists(file) { return fs.promises.access(file, fs.constants.F_OK) .then(() => true) .catch(() => false); -}
\ No newline at end of file +} + +function regexReplace(replacements = []) { + const filter = createFilter("**/*.ts"); + + return { + name: "replace", + + renderChunk(code, chunk) { + const id = chunk.fileName; + if (!filter(id)) return null; + return executeReplacement(this, code, id); + }, + + transform(code, id) { + if (!filter(id)) return null; + return executeReplacement(this, code, id); + } + }; + + function executeReplacement(self, code, id) { + // TODO use MagicString for sourcemap support + let fixed = code; + for (const rep of replacements) { + const { pattern, replacement, failure } = rep; + if (failure) { + const match = pattern.test(fixed); + if (match) { + self.error(failure + " " + id, pattern.lastIndex); + return null; + } + } + else { + fixed = fixed.replace(pattern, replacement); + } + } + + if (fixed == code) { + return null; + } + + return { code: fixed }; + } +} |