|
1 | 1 | import * as fs from 'fs';
|
2 | 2 | import * as path from 'path';
|
3 | 3 | import * as assert from 'assert';
|
4 |
| -import { svelte } from '../helpers'; |
5 |
| -import { SourceMapConsumer } from 'source-map'; |
| 4 | +import { loadConfig, svelte } from '../helpers'; |
| 5 | +// keep source-map at version 0.7.x |
| 6 | +// https://github.com/mozilla/source-map/issues/400 |
6 | 7 | import { getLocator } from 'locate-character';
|
| 8 | +import { SourceMapConsumer } from 'source-map'; |
| 9 | + |
7 | 10 |
|
8 | 11 | describe('sourcemaps', () => {
|
9 | 12 | fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
|
10 | 13 | if (dir[0] === '.') return;
|
11 | 14 |
|
| 15 | + const config = loadConfig(`${__dirname}/samples/${dir}/_config.js`); |
| 16 | + |
12 | 17 | // add .solo to a sample directory name to only run that test
|
13 |
| - const solo = /\.solo/.test(dir); |
14 |
| - const skip = /\.skip/.test(dir); |
| 18 | + const solo = config.solo || /\.solo/.test(dir); |
| 19 | + const skip = config.skip || /\.skip/.test(dir); |
15 | 20 |
|
16 | 21 | if (solo && process.env.CI) {
|
17 | 22 | throw new Error('Forgot to remove `solo: true` from test');
|
18 | 23 | }
|
19 | 24 |
|
20 | 25 | (solo ? it.only : skip ? it.skip : it)(dir, async () => {
|
21 |
| - const filename = path.resolve( |
22 |
| - `${__dirname}/samples/${dir}/input.svelte` |
23 |
| - ); |
24 |
| - const outputFilename = path.resolve( |
25 |
| - `${__dirname}/samples/${dir}/output` |
26 |
| - ); |
| 26 | + const { test } = require(`./samples/${dir}/test.js`); |
| 27 | + const inputFile = path.resolve(`${__dirname}/samples/${dir}/input.svelte`); |
| 28 | + const outputName = '_actual'; |
| 29 | + const outputBase = path.resolve(`${__dirname}/samples/${dir}/${outputName}`); |
| 30 | + |
| 31 | + const inputCode = fs.readFileSync(inputFile, 'utf-8'); |
| 32 | + const input = { |
| 33 | + code: inputCode, |
| 34 | + locate: getLocator(inputCode) |
| 35 | + }; |
27 | 36 |
|
28 |
| - const input = fs.readFileSync(filename, 'utf-8').replace(/\s+$/, ''); |
29 |
| - const { js, css } = svelte.compile(input, { |
30 |
| - filename, |
31 |
| - outputFilename: `${outputFilename}.js`, |
32 |
| - cssOutputFilename: `${outputFilename}.css` |
| 37 | + const preprocessed = await svelte.preprocess( |
| 38 | + input.code, |
| 39 | + config.preprocess || {}, |
| 40 | + { |
| 41 | + filename: 'input.svelte' |
| 42 | + } |
| 43 | + ); |
| 44 | + |
| 45 | + const { js, css } = svelte.compile( |
| 46 | + preprocessed.code, { |
| 47 | + filename: 'input.svelte', |
| 48 | + // filenames for sourcemaps |
| 49 | + outputFilename: `${outputName}.js`, |
| 50 | + cssOutputFilename: `${outputName}.css` |
33 | 51 | });
|
34 | 52 |
|
35 |
| - const _code = js.code.replace(/Svelte v\d+\.\d+\.\d+/, match => match.replace(/\d/g, 'x')); |
| 53 | + js.code = js.code.replace( |
| 54 | + /generated by Svelte v\d+\.\d+\.\d+/, |
| 55 | + match => match.replace(/\d/g, 'x') |
| 56 | + ); |
36 | 57 |
|
| 58 | + fs.writeFileSync(`${outputBase}.svelte`, preprocessed.code); |
| 59 | + if (preprocessed.map) { |
| 60 | + fs.writeFileSync( |
| 61 | + `${outputBase}.svelte.map`, |
| 62 | + // TODO encode mappings for output - svelte.preprocess returns decoded mappings |
| 63 | + JSON.stringify(preprocessed.map, null, 2) |
| 64 | + ); |
| 65 | + } |
37 | 66 | fs.writeFileSync(
|
38 |
| - `${outputFilename}.js`, |
39 |
| - `${_code}\n//# sourceMappingURL=output.js.map` |
| 67 | + `${outputBase}.js`, |
| 68 | + `${js.code}\n//# sourceMappingURL=${outputName}.js.map` |
40 | 69 | );
|
41 | 70 | fs.writeFileSync(
|
42 |
| - `${outputFilename}.js.map`, |
43 |
| - JSON.stringify(js.map, null, ' ') |
| 71 | + `${outputBase}.js.map`, |
| 72 | + JSON.stringify(js.map, null, 2) |
44 | 73 | );
|
45 |
| - |
46 | 74 | if (css.code) {
|
47 | 75 | fs.writeFileSync(
|
48 |
| - `${outputFilename}.css`, |
49 |
| - `${css.code}\n/*# sourceMappingURL=output.css.map */` |
| 76 | + `${outputBase}.css`, |
| 77 | + `${css.code}\n/*# sourceMappingURL=${outputName}.css.map */` |
50 | 78 | );
|
51 | 79 | fs.writeFileSync(
|
52 |
| - `${outputFilename}.css.map`, |
| 80 | + `${outputBase}.css.map`, |
53 | 81 | JSON.stringify(css.map, null, ' ')
|
54 | 82 | );
|
55 | 83 | }
|
56 | 84 |
|
57 |
| - assert.deepEqual(js.map.sources, ['input.svelte']); |
58 |
| - if (css.map) assert.deepEqual(css.map.sources, ['input.svelte']); |
59 |
| - |
60 |
| - const { test } = require(`./samples/${dir}/test.js`); |
| 85 | + assert.deepEqual( |
| 86 | + js.map.sources.slice().sort(), |
| 87 | + (config.js_map_sources || ['input.svelte']).sort() |
| 88 | + ); |
| 89 | + if (css.map) { |
| 90 | + assert.deepEqual( |
| 91 | + css.map.sources.slice().sort(), |
| 92 | + (config.css_map_sources || ['input.svelte']).sort() |
| 93 | + ); |
| 94 | + } |
61 | 95 |
|
62 |
| - const locateInSource = getLocator(input); |
| 96 | + // use locate_1 with mapConsumer: |
| 97 | + // lines are one-based, columns are zero-based |
63 | 98 |
|
64 |
| - const smc = await new SourceMapConsumer(js.map); |
65 |
| - const locateInGenerated = getLocator(_code); |
| 99 | + preprocessed.mapConsumer = preprocessed.map && await new SourceMapConsumer(preprocessed.map); |
| 100 | + preprocessed.locate = getLocator(preprocessed.code); |
| 101 | + preprocessed.locate_1 = getLocator(preprocessed.code, { offsetLine: 1 }); |
66 | 102 |
|
67 |
| - const smcCss = css.map && await new SourceMapConsumer(css.map); |
68 |
| - const locateInGeneratedCss = getLocator(css.code || ''); |
| 103 | + js.mapConsumer = js.map && await new SourceMapConsumer(js.map); |
| 104 | + js.locate = getLocator(js.code); |
| 105 | + js.locate_1 = getLocator(js.code, { offsetLine: 1 }); |
69 | 106 |
|
70 |
| - test({ assert, code: _code, map: js.map, smc, smcCss, locateInSource, locateInGenerated, locateInGeneratedCss }); |
| 107 | + css.mapConsumer = css.map && await new SourceMapConsumer(css.map); |
| 108 | + css.locate = getLocator(css.code || ''); |
| 109 | + css.locate_1 = getLocator(css.code || '', { offsetLine: 1 }); |
| 110 | + test({ assert, input, preprocessed, js, css }); |
71 | 111 | });
|
72 | 112 | });
|
73 | 113 | });
|
0 commit comments