forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor sourcemap and preprocessor tests (sveltejs#5583)
Co-authored-by: Milan Hauth <milahu@gmail.com>
- Loading branch information
1 parent
d531ccd
commit f70bfc9
Showing
14 changed files
with
133 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,113 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as assert from 'assert'; | ||
import { svelte } from '../helpers'; | ||
import { SourceMapConsumer } from 'source-map'; | ||
import { loadConfig, svelte } from '../helpers'; | ||
// keep source-map at version 0.7.x | ||
// https://github.com/mozilla/source-map/issues/400 | ||
import { getLocator } from 'locate-character'; | ||
import { SourceMapConsumer } from 'source-map'; | ||
|
||
|
||
describe('sourcemaps', () => { | ||
fs.readdirSync(`${__dirname}/samples`).forEach(dir => { | ||
if (dir[0] === '.') return; | ||
|
||
const config = loadConfig(`${__dirname}/samples/${dir}/_config.js`); | ||
|
||
// add .solo to a sample directory name to only run that test | ||
const solo = /\.solo/.test(dir); | ||
const skip = /\.skip/.test(dir); | ||
const solo = config.solo || /\.solo/.test(dir); | ||
const skip = config.skip || /\.skip/.test(dir); | ||
|
||
if (solo && process.env.CI) { | ||
throw new Error('Forgot to remove `solo: true` from test'); | ||
} | ||
|
||
(solo ? it.only : skip ? it.skip : it)(dir, async () => { | ||
const filename = path.resolve( | ||
`${__dirname}/samples/${dir}/input.svelte` | ||
); | ||
const outputFilename = path.resolve( | ||
`${__dirname}/samples/${dir}/output` | ||
); | ||
const { test } = require(`./samples/${dir}/test.js`); | ||
const inputFile = path.resolve(`${__dirname}/samples/${dir}/input.svelte`); | ||
const outputName = '_actual'; | ||
const outputBase = path.resolve(`${__dirname}/samples/${dir}/${outputName}`); | ||
|
||
const inputCode = fs.readFileSync(inputFile, 'utf-8'); | ||
const input = { | ||
code: inputCode, | ||
locate: getLocator(inputCode) | ||
}; | ||
|
||
const input = fs.readFileSync(filename, 'utf-8').replace(/\s+$/, ''); | ||
const { js, css } = svelte.compile(input, { | ||
filename, | ||
outputFilename: `${outputFilename}.js`, | ||
cssOutputFilename: `${outputFilename}.css` | ||
const preprocessed = await svelte.preprocess( | ||
input.code, | ||
config.preprocess || {}, | ||
{ | ||
filename: 'input.svelte' | ||
} | ||
); | ||
|
||
const { js, css } = svelte.compile( | ||
preprocessed.code, { | ||
filename: 'input.svelte', | ||
// filenames for sourcemaps | ||
outputFilename: `${outputName}.js`, | ||
cssOutputFilename: `${outputName}.css` | ||
}); | ||
|
||
const _code = js.code.replace(/Svelte v\d+\.\d+\.\d+/, match => match.replace(/\d/g, 'x')); | ||
js.code = js.code.replace( | ||
/generated by Svelte v\d+\.\d+\.\d+/, | ||
match => match.replace(/\d/g, 'x') | ||
); | ||
|
||
fs.writeFileSync(`${outputBase}.svelte`, preprocessed.code); | ||
if (preprocessed.map) { | ||
fs.writeFileSync( | ||
`${outputBase}.svelte.map`, | ||
// TODO encode mappings for output - svelte.preprocess returns decoded mappings | ||
JSON.stringify(preprocessed.map, null, 2) | ||
); | ||
} | ||
fs.writeFileSync( | ||
`${outputFilename}.js`, | ||
`${_code}\n//# sourceMappingURL=output.js.map` | ||
`${outputBase}.js`, | ||
`${js.code}\n//# sourceMappingURL=${outputName}.js.map` | ||
); | ||
fs.writeFileSync( | ||
`${outputFilename}.js.map`, | ||
JSON.stringify(js.map, null, ' ') | ||
`${outputBase}.js.map`, | ||
JSON.stringify(js.map, null, 2) | ||
); | ||
|
||
if (css.code) { | ||
fs.writeFileSync( | ||
`${outputFilename}.css`, | ||
`${css.code}\n/*# sourceMappingURL=output.css.map */` | ||
`${outputBase}.css`, | ||
`${css.code}\n/*# sourceMappingURL=${outputName}.css.map */` | ||
); | ||
fs.writeFileSync( | ||
`${outputFilename}.css.map`, | ||
`${outputBase}.css.map`, | ||
JSON.stringify(css.map, null, ' ') | ||
); | ||
} | ||
|
||
assert.deepEqual(js.map.sources, ['input.svelte']); | ||
if (css.map) assert.deepEqual(css.map.sources, ['input.svelte']); | ||
|
||
const { test } = require(`./samples/${dir}/test.js`); | ||
assert.deepEqual( | ||
js.map.sources.slice().sort(), | ||
(config.js_map_sources || ['input.svelte']).sort() | ||
); | ||
if (css.map) { | ||
assert.deepEqual( | ||
css.map.sources.slice().sort(), | ||
(config.css_map_sources || ['input.svelte']).sort() | ||
); | ||
} | ||
|
||
const locateInSource = getLocator(input); | ||
// use locate_1 with mapConsumer: | ||
// lines are one-based, columns are zero-based | ||
|
||
const smc = await new SourceMapConsumer(js.map); | ||
const locateInGenerated = getLocator(_code); | ||
preprocessed.mapConsumer = preprocessed.map && await new SourceMapConsumer(preprocessed.map); | ||
preprocessed.locate = getLocator(preprocessed.code); | ||
preprocessed.locate_1 = getLocator(preprocessed.code, { offsetLine: 1 }); | ||
|
||
const smcCss = css.map && await new SourceMapConsumer(css.map); | ||
const locateInGeneratedCss = getLocator(css.code || ''); | ||
js.mapConsumer = js.map && await new SourceMapConsumer(js.map); | ||
js.locate = getLocator(js.code); | ||
js.locate_1 = getLocator(js.code, { offsetLine: 1 }); | ||
|
||
test({ assert, code: _code, map: js.map, smc, smcCss, locateInSource, locateInGenerated, locateInGeneratedCss }); | ||
css.mapConsumer = css.map && await new SourceMapConsumer(css.map); | ||
css.locate = getLocator(css.code || ''); | ||
css.locate_1 = getLocator(css.code || '', { offsetLine: 1 }); | ||
test({ assert, input, preprocessed, js, css }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
export function test({ assert, map }) { | ||
assert.deepEqual( map.sources, [ 'input.svelte' ]); | ||
assert.deepEqual( map.sourcesContent, [ | ||
fs.readFileSync( path.join( __dirname, 'input.svelte' ), 'utf-8' ) | ||
export function test({ assert, js }) { | ||
assert.deepEqual(js.map.sources, ['input.svelte']); | ||
assert.deepEqual(js.map.sourcesContent, [ | ||
fs.readFileSync(path.join(__dirname, 'input.svelte'), 'utf-8') | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters