Skip to content

Commit

Permalink
make sure mdsvex is run first, update vitest to join workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-8 committed Jun 27, 2023
1 parent 403a2be commit cc09385
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 105 deletions.
19 changes: 11 additions & 8 deletions packages/kitbook/svelte.config.js
Original file line number Diff line number Diff line change
@@ -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-',
Expand All @@ -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
Expand All @@ -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',
},
});
7 changes: 4 additions & 3 deletions packages/mdsvex-shiki-twoslash/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ const htmlShell = `<!DOCTYPE html>
</html>`

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');
});
});
});
Expand Down
9 changes: 0 additions & 9 deletions packages/mdsvex-shiki-twoslash/vite.config.js

This file was deleted.

11 changes: 11 additions & 0 deletions packages/mdsvex-shiki-twoslash/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineProject } from 'vitest/config'

export default defineProject({
test: {
name: 'mdsvex-shiki-twoslash:unit',
globals: true,
includeSource: ['src/**/*.ts'],
},
})


Original file line number Diff line number Diff line change
@@ -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(`
"
<h1 id=\\"hello-world\\">Hello World</h1>
<h2 id=\\"table-of-content\\">Table of Content</h2>
Expand All @@ -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');
});
8 changes: 0 additions & 8 deletions packages/rehype-display-link-titles/vite.config.ts

This file was deleted.

10 changes: 10 additions & 0 deletions packages/rehype-display-link-titles/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineProject } from 'vitest/config'

export default defineProject({
test: {
name: 'rehype-display-link-titles:unit',
globals: true,
},
})


4 changes: 2 additions & 2 deletions packages/remark-story-code-preview/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
8 changes: 0 additions & 8 deletions packages/remark-story-code-preview/vite.config.ts

This file was deleted.

10 changes: 10 additions & 0 deletions packages/remark-story-code-preview/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineProject } from 'vitest/config'

export default defineProject({
test: {
name: 'remark-story-code-preview:unit',
globals: true,
},
})


135 changes: 100 additions & 35 deletions packages/vite-plugin-kitbook/src/augmentSvelteConfigForKitbook.test.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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",
Expand All @@ -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);"
`);
});
import { augmentSvelteConfigForKitbook } from 'kitbook/plugins/vite';
export default augmentSvelteConfigForKitbook(config);"
`);
});
Original file line number Diff line number Diff line change
@@ -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],
Expand All @@ -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;
}
Expand Down
11 changes: 5 additions & 6 deletions packages/vite-plugin-kitbook/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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',
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit cc09385

Please sign in to comment.