-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Refactor SourceMap and Preprocessor tests #5583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conduitry
merged 1 commit into
sveltejs:master
from
halfnelson:feature/sourcemap-test-refactor
Oct 25, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I would have changed this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't really related to the sourcemap/preprocessor test changes, but I think it is necessary for linting to work on Windows.