mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-17 00:05:59 +08:00
60 lines
1.9 KiB
JavaScript
60 lines
1.9 KiB
JavaScript
import { readStory } from './dependencies-lookup/readAsObject';
|
|
import { getRidOfUselessFilePrefixes } from './dependencies-lookup/getRidOfUselessFilePrefixes';
|
|
|
|
export function insertAfterImports(insert, source) {
|
|
let start = source.lastIndexOf('\nimport ');
|
|
if (start === -1) {
|
|
start = 0;
|
|
} else {
|
|
if (/import\s+{/.test(source.slice(start + 1, start + 10))) {
|
|
start = source.indexOf('}', start + 1);
|
|
}
|
|
start = 1 + source.indexOf('\n', start + 1);
|
|
}
|
|
const imports = source.slice(0, start);
|
|
const rest = source.slice(start);
|
|
return `${imports}${insert}${rest}`;
|
|
}
|
|
|
|
export function transform(inputSource) {
|
|
return readStory(this, inputSource)
|
|
.then(getRidOfUselessFilePrefixes)
|
|
.then(
|
|
({
|
|
prefix,
|
|
resource,
|
|
source,
|
|
sourceJson,
|
|
addsMap,
|
|
dependencies,
|
|
localDependencies,
|
|
idsToFrameworks,
|
|
}) => {
|
|
const preamble = `
|
|
/* eslint-disable no-unused-vars,@typescript-eslint/no-unused-vars */
|
|
// @ts-ignore
|
|
var withSourceLoader = require('@storybook/source-loader/preview').withSource;
|
|
// @ts-ignore
|
|
var addSourceDecorator = require("@storybook/source-loader/preview").addSource;
|
|
// @ts-ignore
|
|
var __SOURCE_PREFIX__ = "${prefix.replace(/\\([^\\ ])/g, '\\\\$1')}";
|
|
// @ts-ignore
|
|
var __STORY__ = ${sourceJson};
|
|
// @ts-ignore
|
|
var __ADDS_MAP__ = ${JSON.stringify(addsMap)};
|
|
// @ts-ignore
|
|
var __MAIN_FILE_LOCATION__ = ${JSON.stringify(resource)};
|
|
// @ts-ignore
|
|
var __MODULE_DEPENDENCIES__ = ${JSON.stringify(dependencies)};
|
|
// @ts-ignore
|
|
var __LOCAL_DEPENDENCIES__ = ${JSON.stringify(localDependencies)};
|
|
// @ts-ignore
|
|
var __IDS_TO_FRAMEWORKS__ = ${JSON.stringify(idsToFrameworks)};
|
|
/* eslint-enable no-unused-vars,@typescript-eslint/no-unused-vars */
|
|
`;
|
|
// return insertAfterImports(preamble, source);
|
|
return `${preamble}${source}`;
|
|
}
|
|
);
|
|
}
|