From cc09385d64d188efac16817c6fd584d8db0e54bf Mon Sep 17 00:00:00 2001 From: jacob-8 Date: Tue, 27 Jun 2023 13:12:11 +0800 Subject: [PATCH] make sure mdsvex is run first, update vitest to join workspace --- packages/kitbook/svelte.config.js | 19 +-- .../mdsvex-shiki-twoslash/src/index.test.ts | 7 +- packages/mdsvex-shiki-twoslash/vite.config.js | 9 -- .../mdsvex-shiki-twoslash/vitest.config.ts | 11 ++ .../personal-unified-learning/process.test.ts | 6 +- .../rehype-display-link-titles/vite.config.ts | 8 -- .../vitest.config.ts | 10 ++ .../src/index.test.ts | 4 +- .../remark-story-code-preview/vite.config.ts | 8 -- .../vitest.config.ts | 10 ++ .../src/augmentSvelteConfigForKitbook.test.ts | 135 +++++++++++++----- .../src/augmentSvelteConfigForKitbook.ts | 10 +- packages/vite-plugin-kitbook/src/plugin.ts | 11 +- .../src/utils/immutableDeepMerge.test.ts | 47 ++++-- .../src/utils/immutableDeepMerge.ts | 13 +- packages/vite-plugin-kitbook/vite.config.ts | 8 -- packages/vite-plugin-kitbook/vitest.config.ts | 11 ++ vitest.workspace.ts | 4 + 18 files changed, 226 insertions(+), 105 deletions(-) delete mode 100644 packages/mdsvex-shiki-twoslash/vite.config.js create mode 100644 packages/mdsvex-shiki-twoslash/vitest.config.ts delete mode 100644 packages/rehype-display-link-titles/vite.config.ts create mode 100644 packages/rehype-display-link-titles/vitest.config.ts delete mode 100644 packages/remark-story-code-preview/vite.config.ts create mode 100644 packages/remark-story-code-preview/vitest.config.ts delete mode 100644 packages/vite-plugin-kitbook/vite.config.ts create mode 100644 packages/vite-plugin-kitbook/vitest.config.ts diff --git a/packages/kitbook/svelte.config.js b/packages/kitbook/svelte.config.js index f24423af..992cc6b3 100644 --- a/packages/kitbook/svelte.config.js +++ b/packages/kitbook/svelte.config.js @@ -1,11 +1,10 @@ import adapter from '@sveltejs/adapter-auto'; import { vitePreprocess } from '@sveltejs/kit/vite'; import UnoCSS from '@unocss/svelte-scoped/preprocess'; -import { MDSVEX_EXTENSIONS } from '@kitbook/vite-plugin-kitbook'; +import { augmentSvelteConfigForKitbook } from '@kitbook/vite-plugin-kitbook'; /** @type {import('@sveltejs/kit').Config} */ const config = { - extensions: ['.svelte', ...MDSVEX_EXTENSIONS], preprocess: [ UnoCSS({ classPrefix: 'kb-', @@ -15,11 +14,6 @@ const config = { kit: { adapter: adapter(), - files: { - appTemplate: 'src/lib/app.html', - routes: 'src/lib/routes', - assets: 'src/lib/assets', - } }, // https://github.com/sveltejs/language-tools/issues/650#issuecomment-1337317336 @@ -35,4 +29,13 @@ const config = { }, }; -export default config; +export default augmentSvelteConfigForKitbook(config, { + kit: { + files: { + appTemplate: 'src/lib/app.html', + routes: 'src/lib/routes', + assets: 'src/lib/assets', + }, + outDir: '.svelte-kit', + }, +}); diff --git a/packages/mdsvex-shiki-twoslash/src/index.test.ts b/packages/mdsvex-shiki-twoslash/src/index.test.ts index c1d99adf..b252f9c4 100644 --- a/packages/mdsvex-shiki-twoslash/src/index.test.ts +++ b/packages/mdsvex-shiki-twoslash/src/index.test.ts @@ -24,20 +24,21 @@ const htmlShell = ` ` describe("mdsvex-shiki-twoslash", () => { - fs.readdirSync('./src/fixtures').forEach((file) => { + const fixturesDirectory = 'packages/mdsvex-shiki-twoslash/src/fixtures'; + fs.readdirSync(fixturesDirectory).forEach((file) => { if (!file.includes('txt')) { return; } const name = file.replace('.txt', ''); test(name, async () => { - const file = fs.readFileSync(`./src/fixtures/${name}.txt`, 'utf8'); + const file = fs.readFileSync(`${fixturesDirectory}/${name}.txt`, 'utf8'); const SPLIT = '__SPLIT__' const [firstLine, code] = file.replace('\r\n', SPLIT).split(SPLIT); const [lang, meta] = firstLine.replace(' ', SPLIT).split(SPLIT); const highlightedCode = await highlight(code, lang, meta); const htmlDocument = htmlShell.replace(REPLACE_BODY, highlightedCode).replace(REPLACE_TITLE, name); - fs.writeFileSync(`./src/fixtures/${name}.html`, htmlDocument, 'utf8'); + fs.writeFileSync(`${fixturesDirectory}/${name}.html`, htmlDocument, 'utf8'); }); }); }); diff --git a/packages/mdsvex-shiki-twoslash/vite.config.js b/packages/mdsvex-shiki-twoslash/vite.config.js deleted file mode 100644 index ad18a246..00000000 --- a/packages/mdsvex-shiki-twoslash/vite.config.js +++ /dev/null @@ -1,9 +0,0 @@ -/// -import { defineConfig } from 'vite' - -export default defineConfig({ - test: { - includeSource: ['src/**/*.ts'], - globals: true, - }, -}) diff --git a/packages/mdsvex-shiki-twoslash/vitest.config.ts b/packages/mdsvex-shiki-twoslash/vitest.config.ts new file mode 100644 index 00000000..2e22c0bd --- /dev/null +++ b/packages/mdsvex-shiki-twoslash/vitest.config.ts @@ -0,0 +1,11 @@ +import { defineProject } from 'vitest/config' + +export default defineProject({ + test: { + name: 'mdsvex-shiki-twoslash:unit', + globals: true, + includeSource: ['src/**/*.ts'], + }, +}) + + diff --git a/packages/rehype-display-link-titles/src/personal-unified-learning/process.test.ts b/packages/rehype-display-link-titles/src/personal-unified-learning/process.test.ts index d9389f12..23be6f2c 100644 --- a/packages/rehype-display-link-titles/src/personal-unified-learning/process.test.ts +++ b/packages/rehype-display-link-titles/src/personal-unified-learning/process.test.ts @@ -1,7 +1,7 @@ import { processHere, processToFileBeside } from "./process"; test('processHere', async () => { - expect(await processHere('./src/personal-unified-learning/example.md')).toMatchInlineSnapshot(` + expect(await processHere('packages/rehype-display-link-titles/src/personal-unified-learning/example.md')).toMatchInlineSnapshot(` "

Hello World

Table of Content

@@ -21,9 +21,9 @@ test('processHere', async () => { }); test('processToFileBeside', async () => { - expect(await processToFileBeside('./src/personal-unified-learning/example.md')).toMatchInlineSnapshot('undefined'); + expect(await processToFileBeside('packages/rehype-display-link-titles/src/personal-unified-learning/example.md')).toMatchInlineSnapshot('undefined'); }); test('processToFileBeside', async () => { - expect(await processToFileBeside('./src/personal-unified-learning/link.md')).toMatchInlineSnapshot('undefined'); + expect(await processToFileBeside('packages/rehype-display-link-titles/src/personal-unified-learning/link.md')).toMatchInlineSnapshot('undefined'); }); diff --git a/packages/rehype-display-link-titles/vite.config.ts b/packages/rehype-display-link-titles/vite.config.ts deleted file mode 100644 index c09c1b52..00000000 --- a/packages/rehype-display-link-titles/vite.config.ts +++ /dev/null @@ -1,8 +0,0 @@ -/// -import { defineConfig } from 'vite' - -export default defineConfig({ - test: { - globals: true, - }, -}) diff --git a/packages/rehype-display-link-titles/vitest.config.ts b/packages/rehype-display-link-titles/vitest.config.ts new file mode 100644 index 00000000..bf617ff0 --- /dev/null +++ b/packages/rehype-display-link-titles/vitest.config.ts @@ -0,0 +1,10 @@ +import { defineProject } from 'vitest/config' + +export default defineProject({ + test: { + name: 'rehype-display-link-titles:unit', + globals: true, + }, +}) + + diff --git a/packages/remark-story-code-preview/src/index.test.ts b/packages/remark-story-code-preview/src/index.test.ts index cd50c63b..f3fb97dd 100644 --- a/packages/remark-story-code-preview/src/index.test.ts +++ b/packages/remark-story-code-preview/src/index.test.ts @@ -59,8 +59,8 @@ describe('placeContentIntoCodeAttribute', () => { }); test('backticks and ${', async () => { - const inputFile = fs.readFileSync('./src/fixtures/input/Backticks.svelte', 'utf-8'); + const inputFile = fs.readFileSync('packages/remark-story-code-preview/src/fixtures/input/Backticks.svelte', 'utf-8'); const result = placeContentIntoCodeAttribute(inputFile, 'Story'); - fs.writeFileSync('./src/fixtures/output/Backticks.svelte', result, 'utf-8'); + fs.writeFileSync('packages/remark-story-code-preview/src/fixtures/output/Backticks.svelte', result, 'utf-8'); }); }); \ No newline at end of file diff --git a/packages/remark-story-code-preview/vite.config.ts b/packages/remark-story-code-preview/vite.config.ts deleted file mode 100644 index c09c1b52..00000000 --- a/packages/remark-story-code-preview/vite.config.ts +++ /dev/null @@ -1,8 +0,0 @@ -/// -import { defineConfig } from 'vite' - -export default defineConfig({ - test: { - globals: true, - }, -}) diff --git a/packages/remark-story-code-preview/vitest.config.ts b/packages/remark-story-code-preview/vitest.config.ts new file mode 100644 index 00000000..28622708 --- /dev/null +++ b/packages/remark-story-code-preview/vitest.config.ts @@ -0,0 +1,10 @@ +import { defineProject } from 'vitest/config' + +export default defineProject({ + test: { + name: 'remark-story-code-preview:unit', + globals: true, + }, +}) + + diff --git a/packages/vite-plugin-kitbook/src/augmentSvelteConfigForKitbook.test.ts b/packages/vite-plugin-kitbook/src/augmentSvelteConfigForKitbook.test.ts index 5545aae9..caa1ec41 100644 --- a/packages/vite-plugin-kitbook/src/augmentSvelteConfigForKitbook.test.ts +++ b/packages/vite-plugin-kitbook/src/augmentSvelteConfigForKitbook.test.ts @@ -1,8 +1,18 @@ import type { Config } from '@sveltejs/kit'; import { augmentSvelteConfigForKitbook, wrapExportedConfigWithAugmentFunction } from './augmentSvelteConfigForKitbook'; +import type { PreprocessorGroup } from 'svelte/types/compiler/preprocess'; + +function vitePreprocess(opts?: any) { + const vitePreprocessMock: PreprocessorGroup = { + name: 'vitePreprocessMock', + script: () => { code: '' }, + } + return vitePreprocessMock; +} // current svelte.config.js const svelteConfig: Config = { + preprocess: vitePreprocess(), kit: { files: { routes: 'src/bananas', @@ -11,20 +21,20 @@ const svelteConfig: Config = { }, }; -test('augmentSvelteConfigForKitbook first takes options from user, then from kitbook defaults, then from the current svelte.config.js', () => { +describe('augmentSvelteConfigForKitbook', () => { process.env.KITBOOK = 'yes'; - const kitbookOptionsFromUser: Config = { - kit: { - files: { - routes: 'src/shazambook', + test('Mdsvex placed at front or preprocess array, then rank options from user, then kitbook defaults, then the current svelte.config.js', () => { + const kitbookOptionsFromUser: Config = { + kit: { + files: { + routes: 'src/shazambook', + }, + version: null, }, - version: null, - }, - }; - + }; - expect(augmentSvelteConfigForKitbook(svelteConfig, kitbookOptionsFromUser)).toMatchInlineSnapshot(` + expect(augmentSvelteConfigForKitbook(svelteConfig, kitbookOptionsFromUser)).toMatchInlineSnapshot(` { "extensions": [ ".svelte", @@ -41,38 +51,93 @@ test('augmentSvelteConfigForKitbook first takes options from user, then from kit "outDir": ".svelte-kit-kitbook", "version": null, }, + "preprocess": [ + { + "markup": [Function], + "name": "mdsvex", + }, + { + "name": "vitePreprocessMock", + "script": [Function], + }, + ], } `); -}); + }); -test('augmentSvelteConfigForKitbook updates extensions and files locations', () => { - expect(augmentSvelteConfigForKitbook(svelteConfig)).toMatchInlineSnapshot(` - { - "extensions": [ - ".svelte", - ".md", - ".svx", - ], - "kit": { - "files": { - "appTemplate": "node_modules/kitbook/dist/app.html", - "assets": "node_modules/kitbook/dist/assets", - "routes": "src/.kitbook/routes", + test('works without any adjustments from user', () => { + expect(augmentSvelteConfigForKitbook(svelteConfig)).toMatchInlineSnapshot(` + { + "extensions": [ + ".svelte", + ".md", + ".svx", + ], + "kit": { + "files": { + "appTemplate": "node_modules/kitbook/dist/app.html", + "assets": "node_modules/kitbook/dist/assets", + "routes": "src/.kitbook/routes", + }, + "inlineStyleThreshold": 0, + "outDir": ".svelte-kit-kitbook", }, - "inlineStyleThreshold": 0, - "outDir": ".svelte-kit-kitbook", - }, - } - `); + "preprocess": [ + { + "markup": [Function], + "name": "mdsvex", + }, + { + "name": "vitePreprocessMock", + "script": [Function], + }, + ], + } + `); + }); + + test('works without any adjustments from user', () => { + const svelteConfigWithPreprocessArray: Config = { + preprocess: [vitePreprocess()], + }; + expect(augmentSvelteConfigForKitbook(svelteConfig)).toMatchInlineSnapshot(` + { + "extensions": [ + ".svelte", + ".md", + ".svx", + ], + "kit": { + "files": { + "appTemplate": "node_modules/kitbook/dist/app.html", + "assets": "node_modules/kitbook/dist/assets", + "routes": "src/.kitbook/routes", + }, + "inlineStyleThreshold": 0, + "outDir": ".svelte-kit-kitbook", + }, + "preprocess": [ + { + "markup": [Function], + "name": "mdsvex", + }, + { + "name": "vitePreprocessMock", + "script": [Function], + }, + ], + } + `); + }); }); test('wrapExportedConfigWithAugmentFunction', () => { expect(wrapExportedConfigWithAugmentFunction(`import {foo} from 'somewhere';\n\nconst config = {}\n\nexport default config;`)).toMatchInlineSnapshot(` - "import {foo} from 'somewhere'; + "import {foo} from 'somewhere'; - const config = {} + const config = {} - import { augmentSvelteConfigForKitbook } from 'kitbook/plugins/vite'; - export default augmentSvelteConfigForKitbook(config);" - `); -}); \ No newline at end of file + import { augmentSvelteConfigForKitbook } from 'kitbook/plugins/vite'; + export default augmentSvelteConfigForKitbook(config);" + `); +}); diff --git a/packages/vite-plugin-kitbook/src/augmentSvelteConfigForKitbook.ts b/packages/vite-plugin-kitbook/src/augmentSvelteConfigForKitbook.ts index 2d534315..a83a9d28 100644 --- a/packages/vite-plugin-kitbook/src/augmentSvelteConfigForKitbook.ts +++ b/packages/vite-plugin-kitbook/src/augmentSvelteConfigForKitbook.ts @@ -1,6 +1,14 @@ import type { Config } from '@sveltejs/kit'; import { AUGMENT_FUNCTION_TEXT, MDSVEX_EXTENSIONS } from './constants'; import { immutableDeepMerge } from './utils/immutableDeepMerge'; +import { mdsvex } from 'mdsvex'; +import DEFAULT_KITBOOK_MDSVEX_CONFIG from './mdsvex/mdsvex.config'; + +const MDSVEX_PREPROCESSOR: Config = { + preprocess: [ + mdsvex(DEFAULT_KITBOOK_MDSVEX_CONFIG) + ], +} const DEFAULT_KITBOOK_OPTIONS: Config = { extensions: ['.svelte', ...MDSVEX_EXTENSIONS], @@ -16,7 +24,7 @@ const DEFAULT_KITBOOK_OPTIONS: Config = { export function augmentSvelteConfigForKitbook(config: Config, kitbookOptions: Config = {}) { if (process.env.KITBOOK) - return immutableDeepMerge(config, DEFAULT_KITBOOK_OPTIONS, kitbookOptions); + return immutableDeepMerge(MDSVEX_PREPROCESSOR, config, DEFAULT_KITBOOK_OPTIONS, kitbookOptions); return config; } diff --git a/packages/vite-plugin-kitbook/src/plugin.ts b/packages/vite-plugin-kitbook/src/plugin.ts index c8152996..79e7d54e 100644 --- a/packages/vite-plugin-kitbook/src/plugin.ts +++ b/packages/vite-plugin-kitbook/src/plugin.ts @@ -1,7 +1,6 @@ import type { Plugin, UserConfig } from 'vite'; -import { mdsvex, type MdsvexOptions } from 'mdsvex'; -import DEFAULT_KITBOOK_MDSVEX_CONFIG from './mdsvex/mdsvex.config'; +import { type MdsvexOptions } from 'mdsvex'; import { initKitbook } from './initKitbook'; import { modifyViteConfigForKitbook } from './modifyViteConfigForKitbook'; @@ -25,7 +24,7 @@ export function kitbookPlugin({ isKitbookItself?: boolean; } = {}): Plugin { const isKitbookMode = process.env.npm_lifecycle_script?.includes('--mode kitbook'); - if (isKitbookMode && !isKitbookItself) initKitbook(); + if (isKitbookMode) initKitbook(); return { name: 'vite-plugin-svelte-kitbook', @@ -39,9 +38,9 @@ export function kitbookPlugin({ if (mode === 'kitbook') return modifyViteConfigForKitbook(viteConfigAdjustments) }, - api: { - sveltePreprocess: isKitbookMode && mdsvex(mdsvexConfig || DEFAULT_KITBOOK_MDSVEX_CONFIG), - }, + // api: { + // sveltePreprocess: isKitbookMode && mdsvex(mdsvexConfig || DEFAULT_KITBOOK_MDSVEX_CONFIG), + // }, resolveId(id) { if (id === VIRTUAL_MODULES_IMPORT_ID) { diff --git a/packages/vite-plugin-kitbook/src/utils/immutableDeepMerge.test.ts b/packages/vite-plugin-kitbook/src/utils/immutableDeepMerge.test.ts index d8c0c942..fd6c3b4d 100644 --- a/packages/vite-plugin-kitbook/src/utils/immutableDeepMerge.test.ts +++ b/packages/vite-plugin-kitbook/src/utils/immutableDeepMerge.test.ts @@ -1,4 +1,5 @@ import { immutableDeepMerge } from './immutableDeepMerge'; +import type { PreprocessorGroup } from 'svelte/types/compiler/preprocess'; describe('immutableDeepMerge', () => { test('basic', () => { @@ -37,12 +38,12 @@ describe('immutableDeepMerge', () => { test('plugins', () => { const plugIn = () => { return { - process: 'plugIn' + name: 'plugIn' } }; const fooIn = () => { return { - process: 'fooIn' + name: 'fooIn' } }; @@ -54,17 +55,17 @@ describe('immutableDeepMerge', () => { } expect(immutableDeepMerge(obj1, obj2)).toMatchInlineSnapshot(` - { - "process": [ - { - "process": "plugIn", - }, - { - "process": "fooIn", - }, - ], - } - `); + { + "process": [ + { + "name": "plugIn", + }, + { + "name": "fooIn", + }, + ], + } + `); }); test('works with different types', () => { @@ -177,4 +178,24 @@ describe('immutableDeepMerge', () => { } `); }); + + test('if given a string or object and an array, incorporates the non-iterable as the first element of the array', () => { + const configWithOneObject: { preprocess: PreprocessorGroup } = { + preprocess: { name: 'first' }, + } + const configWithArrayOfObjects = { + preprocess: [{ name: 'second' }, { name: 'third' }], + } + const anotherConfigWithOneObject = { + preprocess: { name: 'fourth' }, + } + expect(immutableDeepMerge(configWithOneObject, configWithArrayOfObjects, anotherConfigWithOneObject)).toEqual({ + preprocess: [ + { name: 'first' }, + { name: 'second' }, + { name: 'third' }, + { name: 'fourth' }, + ] + }); + }); }); diff --git a/packages/vite-plugin-kitbook/src/utils/immutableDeepMerge.ts b/packages/vite-plugin-kitbook/src/utils/immutableDeepMerge.ts index 516e80b9..15ce22e1 100644 --- a/packages/vite-plugin-kitbook/src/utils/immutableDeepMerge.ts +++ b/packages/vite-plugin-kitbook/src/utils/immutableDeepMerge.ts @@ -10,11 +10,22 @@ export function immutableDeepMerge(...objects: Record[]) { const previousValue = previousObj[key]; const currentValue = currentObj[key]; + // Note that an Array is also an Object so check for array first if (Array.isArray(previousValue) && Array.isArray(currentValue)) { previousObj[key] = [...new Set([...previousValue, ...currentValue])] // if you don't want to remove duplicates, use this instead // previousObj[key] = [...previousValue, ...currentValue] - } else if (isObject(previousValue) && isObject(currentValue)) { + } + + // Svelte Preprocessors can be an array of objects, or just one object + else if (isObject(previousValue) && Array.isArray(currentValue)) { + previousObj[key] = [previousValue, ...currentValue]; + } + else if (Array.isArray(previousValue) && isObject(currentValue)) { + previousObj[key] = [...previousValue, currentValue]; + } + + else if (isObject(previousValue) && isObject(currentValue)) { previousObj[key] = immutableDeepMerge(previousValue, currentValue); } else { previousObj[key] = currentValue; diff --git a/packages/vite-plugin-kitbook/vite.config.ts b/packages/vite-plugin-kitbook/vite.config.ts deleted file mode 100644 index c09c1b52..00000000 --- a/packages/vite-plugin-kitbook/vite.config.ts +++ /dev/null @@ -1,8 +0,0 @@ -/// -import { defineConfig } from 'vite' - -export default defineConfig({ - test: { - globals: true, - }, -}) diff --git a/packages/vite-plugin-kitbook/vitest.config.ts b/packages/vite-plugin-kitbook/vitest.config.ts new file mode 100644 index 00000000..85b31812 --- /dev/null +++ b/packages/vite-plugin-kitbook/vitest.config.ts @@ -0,0 +1,11 @@ +import { defineProject } from 'vitest/config' + +export default defineProject({ + test: { + name: 'vite-plugin-kitbook:unit', + globals: true, + // includeSource: ['src/**/*.ts'], + }, +}) + + diff --git a/vitest.workspace.ts b/vitest.workspace.ts index 4ab84fcb..d595544d 100644 --- a/vitest.workspace.ts +++ b/vitest.workspace.ts @@ -2,5 +2,9 @@ import { defineWorkspace } from 'vitest/config' export default defineWorkspace([ 'packages/kitbook/vitest.config.ts', + 'packages/mdsvex-shiki-twoslash/vitest.config.ts', + 'packages/rehype-display-link-titles/vitest.config.ts', + 'packages/remark-story-code-preview/vitest.config.ts', 'packages/svelte-pieces/vitest.config.ts', + 'packages/vite-plugin-kitbook/vitest.config.ts', ])