Source-loader: Bypass if file has no exports (#9505)

Source-loader: Bypass if file has no exports
This commit is contained in:
Michael Shilman 2020-01-17 15:53:09 +08:00 committed by GitHub
commit 3e52df6b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View File

@ -0,0 +1,8 @@
export default {
title: 'Core/Re-export source loader',
};
const Story1 = () => 'story1';
const Story2 = () => 'story2';
export { Story1, Story2 };

View File

@ -3,7 +3,16 @@ import { getRidOfUselessFilePrefixes } from './dependencies-lookup/getRidOfUsele
export function transform(inputSource) {
return readStory(this, inputSource)
.then(getRidOfUselessFilePrefixes)
.then(sourceObject => {
// if source-loader had trouble parsing the story exports, return the original story
// example is
// const myStory = () => xxx
// export { myStory }
if (!sourceObject.source || sourceObject.source.length === 0) {
return { source: inputSource };
}
return getRidOfUselessFilePrefixes(sourceObject);
})
.then(
({
prefix,
@ -15,7 +24,8 @@ export function transform(inputSource) {
localDependencies,
idsToFrameworks,
}) => {
const preamble = `
const preamble = prefix
? `
/* eslint-disable */
// @ts-nocheck
// @ts-ignore
@ -36,7 +46,8 @@ var __MODULE_DEPENDENCIES__ = ${JSON.stringify(dependencies)};
var __LOCAL_DEPENDENCIES__ = ${JSON.stringify(localDependencies)};
// @ts-ignore
var __IDS_TO_FRAMEWORKS__ = ${JSON.stringify(idsToFrameworks)};
`;
`
: '';
return `${preamble}\n${source}`;
}
);