8000 feat: better sourcemap resolution · Sv443/Userscript.ts@fcee63c · GitHub
[go: up one dir, main page]

Skip to content

Commit fcee63c

Browse files
committed
feat: better sourcemap resolution
1 parent 5a4d86c commit fcee63c

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Like this template? Please consider [supporting the development ❤️](https://
2727
2. Inside `package.json`, update the properties `name`, `userscriptName`, `description`, `homepage`, `author`, `license` and `repository.url`
2828
3. Replace the icon at `assets/icon.png` with your own or use [Google's or DuckDuckGo's favicon API](https://codepen.io/djekl/pen/QWKNNjv) in the userscript header
2929
4. Replace the LICENSE.txt file with your own license or remove it if you want your code to be "all rights reserved"
30-
5. Modify the userscript header and variables at the top of `src/tools/post-build.ts` to whatever your heart desires (see [reference](https://wiki.greasespot.net/Metadata_Block))
30+
5. Modify the variables and userscript header inside `src/tools/post-build.ts` to whatever you need (see also [GM header reference](https://wiki.greasespot.net/Metadata_Block))
3131
6. The eslint configuration at `.eslintrc.cjs` is what I use, feel free to remove rules if there are too many or modify them to your preferences
3232
7. Add your own initialization functions to `init()` and `onLoad()` inside the entrypoint file at `src/index.ts` (read the comments for more info)
3333
8. Remove the example HTML, CSS and TS files

src/tools/post-build.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ const iconUrl = `https://raw.githubusercontent.com/${repo}/main/assets/icon.png`
3333
* See `index.ts` for an example.
3434
*/
3535
const injectBuildNbr = true;
36-
/**
37-
* Whether to trigger the bell sound in some terminals when the code has finished compiling
38-
*/
36+
/** Whether to trigger the bell sound in some terminals when the code has finished compiling */
3937
const ringBell = env.RING_BELL && (env.RING_BELL.length > 0 && env.RING_BELL.trim().toLowerCase() !== "false");
4038

4139
const mode = process.argv.find((v) => v.trim().match(/^(--)?mode=production$/)) ? "production" : "development";
@@ -45,6 +43,9 @@ const distFolderPath = webpackCfgOutput.path;
4543
const scriptUrl = `https://raw.githubusercontent.com/${repo}/main/dist/${encodeURI(userscriptDistFile)}`;
4644
const matchDirectives = matchUrls.reduce((a, c) => a + `// @match ${c}\n`, "");
4745

46+
const envPort = Number(env.DEV_SERVER_PORT);
47+
const devServerPort = isNaN(envPort) || envPort === 0 ? 8710 : envPort;
48+
4849
/** See https://wiki.greasespot.net/Metadata_Block */
4950
const header = `\
5051
// ==UserScript==
@@ -87,21 +88,27 @@ ${matchDirectives}\
8788
const globalStyle = await exists(globalStylePath) ? String(await readFile(globalStylePath)).replace(/\n\s*\/\*.+\*\//gm, "") : undefined;
8889

8990
// read userscript
90-
const userscriptRaw = String(await readFile(scriptPath));
91+
let userscript = String(await readFile(scriptPath));
9192

9293
// inject values if found
93-
let userscript = userscriptRaw;
94-
9594
if(globalStyle)
9695
userscript = userscript.replace(/"{{GLOBAL_STYLE}}"/gm, `\`${globalStyle}\``);
9796
if(lastCommitSha)
9897
userscript = userscript.replace(/\/?\*?{{BUILD_NUMBER}}\*?\/?/gm, lastCommitSha);
9998

99+
// remove sourcemap comments in prod or replace them with the dev server URL in dev
100+
if(mode === "production")
101+
userscript = removeSourcemapComments(userscript);
102+
else
103+
userscript = userscript.replace(/sourceMappingURL=/gm, `sourceMappingURL=http://localhost:${devServerPort}/`);
104+
105+
// add userscript header and final newline
100106
const finalUserscript = `${header}\n${userscript}${userscript.endsWith("\n") ? "" : "\n"}`;
101107

102-
// overwrite with finished userscript
108+
// overwrite file with finished userscript
103109
await writeFile(scriptPath, finalUserscript);
104110

111+
// print stuff to the console
105112
const modeText = `${mode === "production" ? "\x1b[32m" : "\x1b[33m"}${mode}`;
106113
const sizeKiB = (Buffer.byteLength(finalUserscript, "utf8") / 1024).toFixed(2);
107114

@@ -115,8 +122,7 @@ ${matchDirectives}\
115122
setImmediate(() => exit(0));
116123
}
117124
catch(err) {
118-
conso 8000 le.error("Error while adding userscript header:");
119-
console.error(err);
125+
console.error("Error while adding userscript header:\n", err);
120126

121127
// schedule exit after I/O finishes
122128
setImmediate(() => exit(1));
@@ -143,12 +149,17 @@ async function exists(path: string) {
143149
*/
144150
function getLastCommitSha() {
145151
return new Promise<string>((res, rej) => {
146-
exec("git rev-parse HEAD", (err, stdout, stderr) => {
152+
exec("git rev-parse --short HEAD", (err, stdout, stderr) => {
147153
if(err) {
148-
console.error("\x1b[31mError while checking for last Git commit. Do you have Git installed?\x1b[0m\n", stderr);
154+
console.error("\x1b[31mError while checking for last Git commit.\nDo you have Git installed and is there at least one commit in the history?\x1b[0m\n", stderr);
149155
return rej(err);
150156
}
151-
return res(String(stdout).replace(/\r?\n/gm, "").trim().substring(0, 7));
157+
return res(String(stdout).replace(/\r?\n/gm, "").trim());
152158
});
153159
});
154160
}
161+
162+
/** Removes all sourcemap comments so the console isn't spammed with warnings */
163+
function removeSourcemapComments(input: string) {
164+
return input.replace(/\n\s*\/(\*|\/)\s?#.+(\*\/)?$/gm, "");
165+
}

0 commit comments

Comments
 (0)
0