Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for preprocessors in .stories.svelte #1

Merged
merged 1 commit into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions src/parser/svelte-stories-loader.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import dedent from 'ts-dedent';
import { readFileSync } from 'fs';

import * as svelte from 'svelte/compiler';
import { PreprocessorGroup } from 'svelte/types/compiler/preprocess/types';
import { extractStories } from './extract-stories';

const parser = require.resolve('./collect-stories').replace(/[/\\]/g, '/');

// From https://github.com/sveltejs/svelte/blob/8db3e8d0297e052556f0b6dde310ef6e197b8d18/src/compiler/compile/utils/get_name_from_filename.ts
// Copied because it is not exported from the compiler
export function getNameFromFilename(filename: string) {
export function getNameFromFilename(filename: string): string {
if (!filename) return null;

const parts = filename.split(/[/\\]/).map(encodeURI);

if (parts.length > 1) {
const index_match = parts[parts.length - 1].match(/^index(\.\w+)/);
if (index_match) {
const indexMatch = parts[parts.length - 1].match(/^index(\.\w+)/);
if (indexMatch) {
parts.pop();
parts[parts.length - 1] += index_match[1];
parts[parts.length - 1] += indexMatch[1];
}
}

Expand All @@ -36,13 +37,17 @@ export function getNameFromFilename(filename: string) {
return base[0].toUpperCase() + base.slice(1);
}

function transformSvelteStories(code: string) {
// eslint-disable-next-line no-underscore-dangle
const { resource } = this._module;

async function transformSvelteStories(
code: string,
resource: string,
preprocess?: PreprocessorGroup | PreprocessorGroup[]
) {
const componentName = getNameFromFilename(resource);

const source = readFileSync(resource).toString();
let source = readFileSync(resource).toString();
if (preprocess) {
source = (await svelte.preprocess(source, preprocess, { filename: resource })).code;
}

const storiesDef = extractStories(source);
const { stories } = storiesDef;
Expand All @@ -62,4 +67,12 @@ function transformSvelteStories(code: string) {
`;
}

export default transformSvelteStories;
function svelteStoriesLoader(code: string): void {
// eslint-disable-next-line no-underscore-dangle
const { resource } = this._module;
const callback = this.async();
const preprocess = typeof this.query === 'object' ? this.query.preprocess : undefined;
transformSvelteStories(code, resource, preprocess).then((processed) => callback(null, processed));
}

export default svelteStoriesLoader;
4 changes: 3 additions & 1 deletion src/preset/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export function managerEntries(entry = []) {
return [...entry, require.resolve('./manager')];
}

export function webpack(config) {
export async function webpack(config, options) {
const svelteOptions = await options.presets.apply('svelteOptions', {}, options);
return {
...config,
module: {
Expand All @@ -15,6 +16,7 @@ export function webpack(config) {
use: [
{
loader: require.resolve('../parser/svelte-stories-loader'),
options: svelteOptions,
},
],
},
Expand Down