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