Skip to content

Commit

Permalink
fix: module not found error (#60)
Browse files Browse the repository at this point in the history
fixes #59
  • Loading branch information
sebastianrothe authored Aug 5, 2021
1 parent 38aa0e2 commit 3748f49
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 191 deletions.
8 changes: 4 additions & 4 deletions _svelte.config.cjs
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, '']])
]
}
14 changes: 7 additions & 7 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* https://jestjs.io/docs/en/configuration.html
*/
export default {
coverageProvider: "v8",
coverageProvider: 'v8',

moduleFileExtensions: ["js", "ts", "cjs"],
extensionsToTreatAsEsm: ['.ts'],

testMatch: ["**/?(*.)+(spec|test).?(c)[tj]s?(x)"],
moduleFileExtensions: ['js', 'ts', 'cjs'],

testRunner: "jest-circus/runner",
testMatch: ['**/?(*.)+(spec|test).?(c)[tj]s?(x)'],

transform: {
"^.+\\.ts$": "esbuild-jest",
},
};
'^.+\\.ts$': 'esbuild-jest'
}
}
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions src/__tests__/fixtures/svelte.config.cjs
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)]
])
]
}
24 changes: 12 additions & 12 deletions src/__tests__/fixtures/sveltekit.config.js
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
179 changes: 89 additions & 90 deletions src/__tests__/transformer.test.js
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')
})
})
Loading

0 comments on commit 3748f49

Please sign in to comment.