forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (60 loc) · 2.15 KB
/
index.js
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
import * as fs from "fs";
import * as path from "path";
import assert from "assert";
import { loadConfig, svelte } from "../helpers.js";
import { SourceMapConsumer } from "source-map";
import { getLocator } from "locate-character";
describe("sourcemaps", () => {
fs.readdirSync("test/sourcemaps/samples").forEach(dir => {
if (dir[0] === ".") return;
// add .solo to a sample directory name to only run that test
const solo = /\.solo/.test(dir);
if (solo && process.env.CI) {
throw new Error("Forgot to remove `solo: true` from test");
}
(solo ? it.only : it)(dir, () => {
const config = loadConfig(`./sourcemaps/samples/${dir}/_config.js`);
const filename = path.resolve(
`test/sourcemaps/samples/${dir}/input.html`
);
const outputFilename = path.resolve(
`test/sourcemaps/samples/${dir}/output`
);
const input = fs.readFileSync(filename, "utf-8").replace(/\s+$/, "");
const { code, map, css, cssMap } = svelte.compile(input, {
filename,
outputFilename: `${outputFilename}.js`,
cssOutputFilename: `${outputFilename}.css`,
cascade: config.cascade
});
const _code = code.replace(/Svelte v\d+\.\d+\.\d+/, match => match.replace(/\d/g, 'x'));
fs.writeFileSync(
`${outputFilename}.js`,
`${_code}\n//# sourceMappingURL=output.js.map`
);
fs.writeFileSync(
`${outputFilename}.js.map`,
JSON.stringify(map, null, " ")
);
if (css) {
fs.writeFileSync(
`${outputFilename}.css`,
`${css}\n/*# sourceMappingURL=output.css.map */`
);
fs.writeFileSync(
`${outputFilename}.css.map`,
JSON.stringify(cssMap, null, " ")
);
}
assert.deepEqual(map.sources, ["input.html"]);
if (cssMap) assert.deepEqual(cssMap.sources, ["input.html"]);
const { test } = require(`./samples/${dir}/test.js`);
const locateInSource = getLocator(input);
const smc = new SourceMapConsumer(map);
const locateInGenerated = getLocator(_code);
const smcCss = cssMap && new SourceMapConsumer(cssMap);
const locateInGeneratedCss = getLocator(css || '');
test({ assert, code: _code, map, smc, smcCss, locateInSource, locateInGenerated, locateInGeneratedCss });
});
});
});