|
| 1 | +//! Licensed to the .NET Foundation under one or more agreements. |
| 2 | +//! The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +import { createHash } from "crypto"; |
| 5 | +import { readFile, writeFile, mkdir } from "fs/promises"; |
| 6 | +import virtual from "@rollup/plugin-virtual"; |
| 7 | +import * as fs from "fs"; |
| 8 | +import * as path from "path"; |
| 9 | +import terser from "@rollup/plugin-terser"; |
| 10 | + |
| 11 | +import { isContinuousIntegrationBuild, keep_classnames, reserved } from "./rollup.config.defines.js" |
| 12 | + |
| 13 | +export const terserPlugin = () => terser({ |
| 14 | + compress: { |
| 15 | + defaults: true, |
| 16 | + passes: 2, |
| 17 | + drop_debugger: false, // we invoke debugger |
| 18 | + drop_console: false, // we log to console |
| 19 | + keep_classnames, |
| 20 | + ecma: "2015", |
| 21 | + toplevel: true, |
| 22 | + }, |
| 23 | + format: { |
| 24 | + beautify: true, |
| 25 | + }, |
| 26 | + mangle: { |
| 27 | + keep_classnames, |
| 28 | + reserved, |
| 29 | + toplevel: true, |
| 30 | + }, |
| 31 | +}); |
| 32 | + |
| 33 | +// this would create .sha256 file next to the output file, so that we do not touch datetime of the file if it's same -> faster incremental build. |
| 34 | +export const writeOnChangePlugin = () => ({ |
| 35 | + name: "writeOnChange", |
| 36 | + generateBundle: writeWhenChanged |
| 37 | +}); |
| 38 | + |
| 39 | +// Drop invocation from IIFE |
| 40 | +export function iife2fe() { |
| 41 | + return { |
| 42 | + name: "iife2fe", |
| 43 | + generateBundle: (options, bundle) => { |
| 44 | + const name = Object.keys(bundle)[0]; |
| 45 | + if (name.endsWith(".map")) return; |
| 46 | + const asset = bundle[name]; |
| 47 | + const code = asset.code; |
| 48 | + //throw new Error("iife2fe " + code); |
| 49 | + asset.code = code |
| 50 | + .replace(/}\({}\);/, "};") // }({}); ->}; |
| 51 | + .replace(/}\)\({}\);/, "});") // })({}); ->}); |
| 52 | + ; |
| 53 | + } |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +// force always unix line ending |
| 58 | +export const alwaysLF = () => ({ |
| 59 | + name: "writeOnChange", |
| 60 | + generateBundle: (options, bundle) => { |
| 61 | + const name = Object.keys(bundle)[0]; |
| 62 | + const asset = bundle[name]; |
| 63 | + const code = asset.code; |
| 64 | + asset.code = code.replace(/\r/g, ""); |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | +async function writeWhenChanged(options, bundle) { |
| 69 | + try { |
| 70 | + const name = Object.keys(bundle)[0]; |
| 71 | + const asset = bundle[name]; |
| 72 | + const code = asset.code; |
| 73 | + const hashFileName = options.file + ".sha256"; |
| 74 | + const oldHashExists = await checkFileExists(hashFileName); |
| 75 | + const oldFileExists = await checkFileExists(options.file); |
| 76 | + |
| 77 | + const newHash = createHash("sha256").update(code).digest("hex"); |
| 78 | + |
| 79 | + let isOutputChanged = true; |
| 80 | + if (oldHashExists && oldFileExists) { |
| 81 | + const oldHash = await readFile(hashFileName, { encoding: "ascii" }); |
| 82 | + isOutputChanged = oldHash !== newHash; |
| 83 | + } |
| 84 | + if (isOutputChanged) { |
| 85 | + const dir = path.dirname(options.file); |
| 86 | + if (!await checkFileExists(dir)) { |
| 87 | + await mkdir(dir, { recursive: true }); |
| 88 | + } |
| 89 | + await writeFile(hashFileName, newHash); |
| 90 | + } else { |
| 91 | + // this.warn('No change in ' + options.file) |
| 92 | + delete bundle[name]; |
| 93 | + } |
| 94 | + } catch (ex) { |
| 95 | + this.warn(ex.toString()); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +function checkFileExists(file) { |
| 100 | + return fs.promises.access(file, fs.constants.F_OK) |
| 101 | + .then(() => true) |
| 102 | + .catch(() => false); |
| 103 | +} |
| 104 | + |
| 105 | +export function onwarn(warning) { |
| 106 | + if (warning.code === "CIRCULAR_DEPENDENCY") { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + if (warning.code === "UNRESOLVED_IMPORT" && warning.exporter === "process") { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (warning.code === "PLUGIN_WARNING" && warning.message.indexOf("sourcemap") !== -1) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + // eslint-disable-next-line no-console |
| 119 | + console.warn(`(!) ${warning.toString()} ${warning.code}`); |
| 120 | +} |
| 121 | + |
| 122 | +export function consts(dict) { |
| 123 | + // implement rollup-plugin-const in terms of @rollup/plugin-virtual |
| 124 | + // It's basically the same thing except "consts" names all its modules with a "consts:" prefix, |
| 125 | + // and the virtual module always exports a single default binding (the const value). |
| 126 | + |
| 127 | + let newDict = {}; |
| 128 | + for (const k in dict) { |
| 129 | + const newKey = "consts:" + k; |
| 130 | + const newVal = JSON.stringify(dict[k]); |
| 131 | + newDict[newKey] = `export default ${newVal}`; |
| 132 | + } |
| 133 | + return virtual(newDict); |
| 134 | +} |
| 135 | + |
| 136 | +const locationCache = {}; |
| 137 | +export function sourcemapPathTransform(relativeSourcePath, sourcemapPath) { |
| 138 | + let res = locationCache[relativeSourcePath]; |
| 139 | + if (res === undefined) { |
| 140 | + if (!isContinuousIntegrationBuild) { |
| 141 | + const sourcePath = path.resolve( |
| 142 | + path.dirname(sourcemapPath), |
| 143 | + relativeSourcePath |
| 144 | + ); |
| 145 | + res = `file:///${sourcePath.replace(/\\/g, "/")}`; |
| 146 | + } else { |
| 147 | + relativeSourcePath = relativeSourcePath.substring(12); |
| 148 | + res = `https://raw.githubusercontent.com/dotnet/runtime/${gitHash}/${relativeSourcePath}`; |
| 149 | + } |
| 150 | + locationCache[relativeSourcePath] = res; |
| 151 | + } |
| 152 | + return res; |
| 153 | +} |
0 commit comments