-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial SvelteKit support (#46)
- Loading branch information
1 parent
1033c4a
commit 0c008d7
Showing
15 changed files
with
615 additions
and
363 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"recommendations": [ | ||
"editorconfig.editorconfig", | ||
"standard.vscode-standard", | ||
"svelte.svelte-vscode" | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
// disable builtin validation | ||
"javascript.validate.enable": false, | ||
|
||
"standard.autoFixOnSave": true, | ||
|
||
// SVELTE | ||
"[svelte]": { | ||
"editor.defaultFormatter": "svelte.svelte-vscode" | ||
} | ||
} |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* For a detailed explanation regarding each configuration property and type check, visit: | ||
* https://jestjs.io/docs/en/configuration.html | ||
*/ | ||
export default { | ||
coverageProvider: 'v8', | ||
|
||
moduleFileExtensions: ['js', 'ts', 'cjs'], | ||
|
||
testMatch: [ | ||
'**/__tests__/**/*.?(c)[jt]s?(x)', | ||
'**/?(*.)+(spec|test).?(c)[tj]s?(x)', | ||
'!**/fixtures/**' | ||
], | ||
|
||
testRunner: 'jest-circus/runner', | ||
|
||
transform: { | ||
'^.+\\.ts$': 'esbuild-jest', | ||
'^.+\\.js$': 'esbuild-jest' | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const sharedTests = (dependencies) => { | ||
const { path, runTransformer } = dependencies | ||
|
||
it('should transform basic component', () => { | ||
runTransformer('BasicComp') | ||
}) | ||
|
||
it('should transform when using sass preprocessor', () => { | ||
runTransformer('SassComp', { preprocess: true }) | ||
}) | ||
|
||
it('should transform when using full path to preprocess', () => { | ||
const preprocessPath = path.resolve(__dirname, '../../../../_svelte.config.cjs') | ||
runTransformer('SassComp', { preprocess: preprocessPath }) | ||
}) | ||
|
||
it('should search for "svelte.config.cjs" as well as "svelte.config.js"', () => { | ||
const results = runTransformer('BasicComp', { preprocess: true, rootMode: 'upward' }) | ||
// this is a little brittle, but it demonstrates that the replacements in | ||
// "svelte.config.cjs" are working | ||
expect(results).toContain('text("Bye ");') | ||
}) | ||
|
||
it('should transform when using typescript preprocessor', () => { | ||
runTransformer('TypescriptComp', { preprocess: true }) | ||
}) | ||
|
||
it('should transform basic component and keep styles', () => { | ||
const code = runTransformer('BasicComp') | ||
expect(code).toContain('add_css()') | ||
expect(code).toContain('.counter.active') | ||
}) | ||
|
||
it('should accept compiler options', () => { | ||
const code = runTransformer('BasicComp', { compilerOptions: { css: false } }) | ||
expect(code).not.toContain('add_css()') | ||
expect(code).not.toContain('.counter.active') | ||
}) | ||
|
||
it('should output code to console when debug is true', () => { | ||
global.window.console.log = jest.fn() | ||
const code = runTransformer('BasicComp', { debug: true }) | ||
const esInterop = 'Object.defineProperty(exports, "__esModule", { value: true });' | ||
expect(global.window.console.log).toHaveBeenCalledWith(code.replace(esInterop, '')) | ||
}) | ||
|
||
it('should accept maxBuffer option for preprocess buffer limit', () => { | ||
expect( | ||
() => runTransformer('SassComp', { preprocess: true, maxBuffer: 1 }) | ||
).toThrow('spawnSync /bin/sh ENOBUFS') | ||
runTransformer('SassComp', { preprocess: true, maxBuffer: 5 * 1024 * 1024 }) | ||
}) | ||
|
||
it('should pass and transform process.env.NODE_ENV variable', () => { | ||
const code = runTransformer('BasicComp', { preprocess: true, rootMode: 'upward' }) | ||
|
||
// JEST sets NODE_ENV to test automatically | ||
expect(code).toContain('test') | ||
}) | ||
} | ||
|
||
export default sharedTests |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import sveltePreprocess from 'svelte-preprocess' | ||
import adapter from '@sveltejs/adapter-static' | ||
|
||
const { replace } = sveltePreprocess | ||
|
||
const config = { | ||
kit: { | ||
adapter: adapter | ||
}, | ||
preprocess: [ | ||
replace([ | ||
// strip style tag | ||
[/<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi, ''], | ||
[/Hello/gi, 'Bye'], | ||
// replace env var | ||
[/process\.env\.NODE_ENV/gi, JSON.stringify(process.env.NODE_ENV)] | ||
]) | ||
] | ||
} | ||
export default config |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const transformer = require('../transformer') | ||
const sharedTests = require('./fixtures/shared/commonTransformerTests').default | ||
|
||
const runTransformer = (filename, options) => { | ||
const process = transformer.createTransformer(options).process | ||
const path = require.resolve(`./fixtures/${filename}.svelte`) | ||
const source = fs.readFileSync(path).toString() | ||
const result = process(source, path) | ||
expect(result.code).toBeDefined() | ||
expect(result.code).toContain('SvelteComponent') | ||
expect(result.map).toBeDefined() | ||
return result.code | ||
} | ||
|
||
describe('CJS transformer', () => { | ||
it('should search for "svelte.config.cjs" as well as "svelte.config.js"', () => { | ||
const results = runTransformer('BasicComp', { preprocess: true, rootMode: 'upward' }) | ||
// this is a little brittle, but it demonstrates that the replacements in | ||
// "svelte.config.cjs" are working | ||
expect(results).toContain('text("Bye ");') | ||
}) | ||
|
||
sharedTests({ runTransformer, path }) | ||
}) |
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,11 @@ | ||
const svelte = require('svelte/compiler') | ||
const { cosmiconfigSync } = require('cosmiconfig') | ||
import { preprocess } from 'svelte/compiler' | ||
|
||
const { source, filename, svelteConfig } = process.env | ||
const config = cosmiconfigSync().load(svelteConfig).config | ||
import(svelteConfig).then(configImport => { | ||
// ESM or CommonJS | ||
const config = configImport.default ? configImport.default : configImport | ||
|
||
svelte.preprocess(source, config.preprocess || {}, { filename }).then((r) => { | ||
process.stdout.write(JSON.stringify(r)) | ||
}) | ||
preprocess(source, config.preprocess || {}, { filename }).then((r) => { | ||
process.stdout.write(JSON.stringify(r)) | ||
}) | ||
}).catch(err => process.stderr.write(err)) |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// const log = require('why-is-node-running') | ||
const { basename } = require('path') | ||
const { execSync } = require('child_process') | ||
const svelte = require('svelte/compiler') | ||
const { getSvelteConfig } = require('./svelteconfig.cjs') | ||
|
||
const transformer = (options = {}) => (source, filename) => { | ||
const { preprocess, rootMode, maxBuffer } = options | ||
|
||
if (!preprocess) { | ||
return compiler(options, filename, source) | ||
} | ||
|
||
const svelteConfig = getSvelteConfig(rootMode, filename, preprocess) | ||
const preprocessor = require.resolve('./preprocess.js') | ||
|
||
const preprocessResult = execSync( | ||
`node --unhandled-rejections=strict --abort-on-uncaught-exception ${preprocessor}`, | ||
{ | ||
env: { ...process.env, source, filename, svelteConfig }, | ||
maxBuffer: maxBuffer || 10 * 1024 * 1024 | ||
} | ||
).toString() | ||
|
||
const parsedPreprocessResult = JSON.parse(preprocessResult) | ||
return compiler(options, filename, parsedPreprocessResult.code, parsedPreprocessResult.map) | ||
} | ||
|
||
const compiler = (options = {}, filename, processedCode, processedMap) => { | ||
const { debug, compilerOptions } = options | ||
|
||
const result = svelte.compile(processedCode, { | ||
filename: basename(filename), | ||
css: true, | ||
accessors: true, | ||
dev: true, | ||
format: 'cjs', | ||
sourcemap: processedMap, | ||
...compilerOptions | ||
}) | ||
|
||
if (debug) { | ||
console.log(result.js.code) | ||
} | ||
|
||
const esInterop = 'Object.defineProperty(exports, "__esModule", { value: true });' | ||
|
||
return { | ||
code: result.js.code + esInterop, | ||
map: JSON.stringify(result.js.map) | ||
} | ||
} | ||
|
||
exports.createTransformer = (options) => ({ | ||
process: transformer(options) | ||
}) |
Oops, something went wrong.