From efd571ffa9db5f7a219d12368cfe9751a44bdb32 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Mon, 23 Oct 2023 17:35:03 +0800 Subject: [PATCH] chore: fix test run when linking vite (#773) --- .../__tests__/preprocess.spec.js | 14 +++++++++++--- packages/vite-plugin-svelte/package.json | 1 + .../src/utils/load-svelte-config.js | 15 +++++++-------- pnpm-lock.yaml | 3 +++ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/vite-plugin-svelte/__tests__/preprocess.spec.js b/packages/vite-plugin-svelte/__tests__/preprocess.spec.js index 2db7b1748..062c8d775 100644 --- a/packages/vite-plugin-svelte/__tests__/preprocess.spec.js +++ b/packages/vite-plugin-svelte/__tests__/preprocess.spec.js @@ -8,9 +8,15 @@ const fixtureDir = normalizePath( path.join(path.dirname(fileURLToPath(import.meta.url)), 'fixtures', 'preprocess') ); +/** @type {import('vite').InlineConfig} */ +const inlineConfig = { + configFile: false, + root: fixtureDir +}; + describe('vitePreprocess', () => { it('returns function', () => { - const preprocessorGroup = vitePreprocess({ script: true, style: true }); + const preprocessorGroup = vitePreprocess({ script: true, style: inlineConfig }); expect(typeof preprocessorGroup).toBe('object'); expect(typeof preprocessorGroup.script).toBe('function'); expect(typeof preprocessorGroup.style).toBe('function'); @@ -18,7 +24,7 @@ describe('vitePreprocess', () => { describe('style', async () => { it('preprocess with postcss if no lang', async () => { - const preprocessorGroup = vitePreprocess({ style: {} }); + const preprocessorGroup = vitePreprocess({ style: inlineConfig }); const style = /**@type {import('svelte/types/compiler/preprocess').Preprocessor} */ ( preprocessorGroup.style ); @@ -37,7 +43,9 @@ describe('vitePreprocess', () => { }); it('produces sourcemap with relative filename', async () => { - const preprocessorGroup = vitePreprocess({ style: { css: { devSourcemap: true } } }); + const preprocessorGroup = vitePreprocess({ + style: { ...inlineConfig, css: { devSourcemap: true } } + }); const style = /**@type {import('svelte/types/compiler/preprocess').Preprocessor} */ ( preprocessorGroup.style ); diff --git a/packages/vite-plugin-svelte/package.json b/packages/vite-plugin-svelte/package.json index d9eabd56b..89494b618 100644 --- a/packages/vite-plugin-svelte/package.json +++ b/packages/vite-plugin-svelte/package.json @@ -53,6 +53,7 @@ "devDependencies": { "@types/debug": "^4.1.10", "esbuild": "^0.19.5", + "sass": "^1.69.4", "svelte": "^4.2.2", "vite": "^5.0.0-beta.11" } diff --git a/packages/vite-plugin-svelte/src/utils/load-svelte-config.js b/packages/vite-plugin-svelte/src/utils/load-svelte-config.js index e0bb20e1b..0e153fb0e 100644 --- a/packages/vite-plugin-svelte/src/utils/load-svelte-config.js +++ b/packages/vite-plugin-svelte/src/utils/load-svelte-config.js @@ -16,14 +16,13 @@ export const knownSvelteConfigNames = [ 'svelte.config.mjs' ]; -// hide dynamic import from ts transform to prevent it turning into a require -// see https://github.com/microsoft/TypeScript/issues/43329#issuecomment-811606238 -// also use timestamp query to avoid caching on reload -const dynamicImportDefault = new Function( - 'path', - 'timestamp', - 'return import(path + "?t=" + timestamp).then(m => m.default)' -); +/** + * @param {string} filePath + * @param {number} timestamp + */ +async function dynamicImportDefault(filePath, timestamp) { + return await import(filePath + '?t=' + timestamp).then((m) => m.default); +} /** @type {import('../index.d.ts').loadSvelteConfig} */ export async function loadSvelteConfig(viteConfig, inlineOptions) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4eef0b111..3bbeb0377 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -767,6 +767,9 @@ importers: esbuild: specifier: ^0.19.5 version: 0.19.5 + sass: + specifier: ^1.69.4 + version: 1.69.4 svelte: specifier: ^4.2.2 version: 4.2.2