-
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.
Showing
8 changed files
with
175 additions
and
191 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
const { replace } = require("svelte-preprocess"); | ||
const { replace } = require('svelte-preprocess') | ||
|
||
module.exports = { | ||
preprocess: [ | ||
// strip style tag | ||
replace([[/<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi, ""]]), | ||
], | ||
}; | ||
replace([[/<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi, '']]) | ||
] | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
const { replace } = require("svelte-preprocess"); | ||
const { replace } = require('svelte-preprocess') | ||
|
||
console.log("This should not influence the compiler/preprocessor"); | ||
console.log('This should not influence the compiler/preprocessor') | ||
|
||
module.exports = { | ||
preprocess: [ | ||
replace([ | ||
[/Hello/gi, "Bye"], | ||
[/Hello/gi, 'Bye'], | ||
// replace env var | ||
[/process\.env\.NODE_ENV/gi, JSON.stringify(process.env.NODE_ENV)], | ||
]), | ||
], | ||
}; | ||
[/process\.env\.NODE_ENV/gi, JSON.stringify(process.env.NODE_ENV)] | ||
]) | ||
] | ||
} |
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,21 +1,21 @@ | ||
import sveltePreprocess from "svelte-preprocess"; | ||
import adapter from "@sveltejs/adapter-static"; | ||
const { replace } = sveltePreprocess; | ||
import sveltePreprocess from 'svelte-preprocess' | ||
import adapter from '@sveltejs/adapter-static' | ||
const { replace } = sveltePreprocess | ||
|
||
console.log("This should not influence the compiler/preprocessor"); | ||
console.log('This should not influence the compiler/preprocessor') | ||
|
||
const config = { | ||
kit: { | ||
adapter: adapter, | ||
adapter: adapter | ||
}, | ||
preprocess: [ | ||
replace([ | ||
// strip style tag | ||
[/<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi, ""], | ||
[/Hello/gi, "Bye"], | ||
[/<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi, ''], | ||
[/Hello/gi, 'Bye'], | ||
// replace env var | ||
[/process\.env\.NODE_ENV/gi, JSON.stringify(process.env.NODE_ENV)], | ||
]), | ||
], | ||
}; | ||
export default config; | ||
[/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 |
---|---|---|
@@ -1,122 +1,121 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import { readFileSync } from "fs"; | ||
import { dirname } from "path"; | ||
import { fileURLToPath } from "url"; | ||
import { jest } from "@jest/globals"; | ||
import { readFileSync } from 'fs' | ||
import { dirname } from 'path' | ||
import { fileURLToPath } from 'url' | ||
import { jest } from '@jest/globals' | ||
|
||
import createTransformer from "../transformer"; | ||
import processAsync from '../transformer' | ||
|
||
// Node API __dirname is missing in ESM | ||
export const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
export const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
|
||
const runTransformer = async (filename, options) => { | ||
const processAsync = createTransformer(options).processAsync; | ||
const path = `${__dirname}/fixtures/${filename}.svelte`; | ||
const source = readFileSync(path).toString(); | ||
const result = await processAsync(source, path); | ||
expect(result.code).toBeDefined(); | ||
expect(result.code).toContain("SvelteComponent"); | ||
expect(result.map).toBeDefined(); | ||
return result.code; | ||
}; | ||
|
||
describe("ESM transformer", () => { | ||
it("should transform with config in ESM format", async () => { | ||
const svelteKitConfigPath = `${__dirname}/fixtures/sveltekit.config.js`; | ||
const results = await runTransformer("BasicComp", { | ||
preprocess: svelteKitConfigPath, | ||
}); | ||
const path = `${__dirname}/fixtures/${filename}.svelte` | ||
const source = readFileSync(path).toString() | ||
const result = await processAsync.processAsync(source, path, { transformerConfig: options }) | ||
expect(result.code).toBeDefined() | ||
expect(result.code).toContain('SvelteComponent') | ||
expect(result.map).toBeDefined() | ||
return result.code | ||
} | ||
|
||
describe('ESM transformer', () => { | ||
it('should transform with config in ESM format', async () => { | ||
const svelteKitConfigPath = `${__dirname}/fixtures/sveltekit.config.js` | ||
const results = await runTransformer('BasicComp', { | ||
preprocess: svelteKitConfigPath | ||
}) | ||
// this is a little brittle, but it demonstrates that the replacements in | ||
// "sveltekit.config.js" are working | ||
expect(results).toContain('text("Bye ");'); | ||
}); | ||
expect(results).toContain('text("Bye ");') | ||
}) | ||
|
||
it('should search for "svelte.config.cjs" as well as "svelte.config.js"', async () => { | ||
const results = await runTransformer("BasicComp", { | ||
const results = await runTransformer('BasicComp', { | ||
preprocess: true, | ||
rootMode: "upward", | ||
}); | ||
rootMode: 'upward' | ||
}) | ||
// this is a little brittle, but it demonstrates that the replacements in | ||
// "svelte.config.cjs" are working | ||
expect(results).toContain('text("Bye ");'); | ||
}); | ||
expect(results).toContain('text("Bye ");') | ||
}) | ||
|
||
it("should transform basic component", async () => { | ||
await runTransformer("BasicComp"); | ||
}); | ||
it('should transform basic component', async () => { | ||
await runTransformer('BasicComp') | ||
}) | ||
|
||
it("should transform when using sass preprocessor", async () => { | ||
await runTransformer("SassComp", { preprocess: true }); | ||
}); | ||
it('should transform when using sass preprocessor', async () => { | ||
await runTransformer('SassComp', { preprocess: true }) | ||
}) | ||
|
||
it("should transform when using full path to preprocess", async () => { | ||
const preprocessPath = `${__dirname}/../../_svelte.config.cjs`; | ||
await runTransformer("SassComp", { preprocess: preprocessPath }); | ||
}); | ||
it('should transform when using full path to preprocess', async () => { | ||
const preprocessPath = `${__dirname}/../../_svelte.config.cjs` | ||
await runTransformer('SassComp', { preprocess: preprocessPath }) | ||
}) | ||
|
||
it('should search for "svelte.config.cjs" as well as "svelte.config.js"', async () => { | ||
const results = await runTransformer("BasicComp", { | ||
const results = await runTransformer('BasicComp', { | ||
preprocess: true, | ||
rootMode: "upward", | ||
}); | ||
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", async () => { | ||
await runTransformer("TypescriptComp", { preprocess: true }); | ||
}); | ||
|
||
it("should transform basic component and keep styles", async () => { | ||
const code = await runTransformer("BasicComp"); | ||
expect(code).toContain("add_css(target)"); | ||
expect(code).toContain(".counter.active"); | ||
}); | ||
|
||
it("should accept compiler options", async () => { | ||
const code = await runTransformer("BasicComp", { | ||
compilerOptions: { css: false }, | ||
}); | ||
expect(code).not.toContain("add_css(target)"); | ||
expect(code).not.toContain(".counter.active"); | ||
}); | ||
|
||
it("should output code to console when debug is true", async () => { | ||
console.log = jest.fn(); | ||
const code = await runTransformer("BasicComp", { debug: true }); | ||
expect(results).toContain('text("Bye ");') | ||
}) | ||
|
||
it('should transform when using typescript preprocessor', async () => { | ||
await runTransformer('TypescriptComp', { preprocess: true }) | ||
}) | ||
|
||
it('should transform basic component and keep styles', async () => { | ||
const code = await runTransformer('BasicComp') | ||
expect(code).toContain('add_css(target)') | ||
expect(code).toContain('.counter.active') | ||
}) | ||
|
||
it('should accept compiler options', async () => { | ||
const code = await runTransformer('BasicComp', { | ||
compilerOptions: { css: false } | ||
}) | ||
expect(code).not.toContain('add_css(target)') | ||
expect(code).not.toContain('.counter.active') | ||
}) | ||
|
||
it('should output code to console when debug is true', async () => { | ||
console.log = jest.fn() | ||
const code = await runTransformer('BasicComp', { debug: true }) | ||
const esInterop = | ||
'Object.defineProperty(exports, "__esModule", { value: true });'; | ||
expect(console.log).toHaveBeenCalledTimes(1); | ||
expect(console.log).toHaveBeenCalledWith(code.replace(esInterop, "")); | ||
}); | ||
'Object.defineProperty(exports, "__esModule", { value: true });' | ||
expect(console.log).toHaveBeenCalledTimes(1) | ||
expect(console.log).toHaveBeenCalledWith(code.replace(esInterop, '')) | ||
}) | ||
|
||
it("should pass, if console.logs are disabled (default) during preprocessing and there is a console.log statement in the svelte config", async () => { | ||
await runTransformer("BasicComp", { preprocess: true, rootMode: "upward" }); | ||
}); | ||
it('should pass, if console.logs are disabled (default) during preprocessing and there is a console.log statement in the svelte config', async () => { | ||
await runTransformer('BasicComp', { preprocess: true, rootMode: 'upward' }) | ||
}) | ||
|
||
it("should pass, if console.logs are disabled during preprocessing and there is a console.log statement in the svelte config", async () => { | ||
await runTransformer("BasicComp", { | ||
it('should pass, if console.logs are disabled during preprocessing and there is a console.log statement in the svelte config', async () => { | ||
await runTransformer('BasicComp', { | ||
preprocess: true, | ||
rootMode: "upward", | ||
showConsoleLog: false, | ||
}); | ||
await runTransformer("BasicComp", { | ||
rootMode: 'upward', | ||
showConsoleLog: false | ||
}) | ||
await runTransformer('BasicComp', { | ||
preprocess: true, | ||
rootMode: "upward", | ||
showConsoleLog: "false", | ||
}); | ||
}); | ||
rootMode: 'upward', | ||
showConsoleLog: 'false' | ||
}) | ||
}) | ||
|
||
it("should pass and transform process.env.NODE_ENV variable", async () => { | ||
const code = await runTransformer("BasicComp", { | ||
it('should pass and transform process.env.NODE_ENV variable', async () => { | ||
const code = await runTransformer('BasicComp', { | ||
preprocess: true, | ||
rootMode: "upward", | ||
}); | ||
rootMode: 'upward' | ||
}) | ||
|
||
// JEST sets NODE_ENV to test automatically | ||
expect(code).toContain("test"); | ||
}); | ||
}); | ||
expect(code).toContain('test') | ||
}) | ||
}) |
Oops, something went wrong.