UNPKG

777 BPlain TextView Raw
1/*
2 Adapted from https://github.com/lydell/source-map-url
3 Which carries the licensing:
4 Copyright 2014 Simon Lydell
5 X11 (“MIT”) Licensed.
6
7 Forked here because that one doesn't anchor to the end of the file, and that's pretty important for us.
8*/
9
10const innerRegex = /[#@] sourceMappingURL=([^\s'"]*)/;
11const regex = RegExp(
12 "(?:" +
13 "/\\*" +
14 "(?:\\s*\r?\n(?://)?)?" +
15 "(?:" + innerRegex.source + ")" +
16 "\\s*" +
17 "\\*/" +
18 "|" +
19 "//(?:" + innerRegex.source + ")" +
20 ")" +
21 "\\s*$"
22 );
23
24export function insertBefore(code: string, string: string) {
25 let match = code.match(regex);
26 if (match) {
27 return code.slice(0, match.index) + string + code.slice(match.index);
28 } else {
29 return code + string;
30 }
31}