-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
chore: save compiled output for test suites #4251
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
Closed
tanhauhau
wants to merge
7
commits into
sveltejs:svelte-4
from
tanhauhau:tanhauhau/test-save-compiled-output
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc5a4ae
feat save compiled output for test suites
tanhauhau ac9e977
fix failed test
tanhauhau 130aab9
fix test and update server-side-rendering test
tanhauhau 19eb2cc
shift the registry code down close to the require call
tanhauhau 7cb210c
fix failed test
tanhauhau 991acd0
handling line ending differences in test
tanhauhau b29dcab
remove comment
tanhauhau 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { mkdirp } from './helpers'; | ||
import * as svelte from 'svelte/compiler'; | ||
|
||
type Compile = typeof svelte.compile; | ||
type CompileOptions = Parameters<Compile>[1]; | ||
|
||
const extensions = ['.svelte', '.html']; | ||
let compileOptions = {}; | ||
let compile; | ||
let folderName = 'normal'; | ||
|
||
const sveltePath = process.cwd().split('\\').join('/'); | ||
|
||
function capitalise(name: string) { | ||
return name[0].toUpperCase() + name.slice(1); | ||
} | ||
|
||
export function setCompileOptions(_compileOptions: CompileOptions) { | ||
compileOptions = _compileOptions; | ||
} | ||
|
||
export function setCompile(_compile: Compile) { | ||
compile = _compile; | ||
} | ||
|
||
export function setOutputFolderName(_folderName: string) { | ||
folderName = _folderName; | ||
} | ||
|
||
export function clearRequireCache() { | ||
Object.keys(require.cache) | ||
.filter(x => x.endsWith('.svelte')) | ||
.forEach(file => { | ||
delete require.cache[file]; | ||
}); | ||
} | ||
|
||
function registerExtension(extension: string) { | ||
require.extensions[extension] = function (module, filename) { | ||
const name = path | ||
.parse(filename) | ||
.name.replace(/^\d/, '_$&') | ||
.replace(/[^a-zA-Z0-9_$]/g, ''); | ||
|
||
const options = Object.assign({}, compileOptions, { | ||
filename, | ||
sveltePath, | ||
name: capitalise(name), | ||
format: 'cjs' | ||
}); | ||
|
||
const { | ||
js: { code } | ||
} = compile(fs.readFileSync(filename, 'utf-8').replace(/\r/g, ''), options); | ||
|
||
if (!process.env.CI) { | ||
saveGeneratedOutputToCache(code, filename); | ||
} | ||
|
||
return module._compile(code, filename); | ||
}; | ||
} | ||
|
||
const outputCache = new Map(); | ||
function saveGeneratedOutputToCache(code: string, filename: string) { | ||
filename = filename.split('\\').join('/'); | ||
filename = filename.replace( | ||
/samples\/([^/]+)\/(.*)\.svelte$/, | ||
`samples/$1/_output/${folderName}/$2.js` | ||
); | ||
outputCache.set(filename, code); | ||
} | ||
|
||
export function clearCompileOutputCache() { | ||
outputCache.clear(); | ||
} | ||
|
||
export function writeCompileOutputCacheToFile() { | ||
for (const [filename, code] of outputCache) { | ||
mkdirp(path.dirname(filename)); | ||
fs.writeFileSync(filename, code, 'utf8'); | ||
} | ||
clearCompileOutputCache(); | ||
} | ||
|
||
extensions.forEach(registerExtension); |
Oops, something went wrong.
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.
should these methods be named with the underscore convention like
set_compile_options
?