Skip to content

Commit

Permalink
Merge pull request #9492 from storybookjs/9490-fix-source-loader-imports
Browse files Browse the repository at this point in the history
Source-loader: Add imports to top of file
  • Loading branch information
shilman authored Jan 16, 2020
2 parents b45f98a + dbce421 commit 66c524f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable import/first,import/no-duplicates */
import React from 'react';
import { action } from '@storybook/addon-actions';

export default {
title: 'Core/Interleaved exports',
parameters: { chromatic: { disable: true } },
};

import { Welcome } from '@storybook/react/demo';

export const First = () => <Welcome showApp={action('show')} />;

import { Button } from '@storybook/react/demo';

export const Second = () => <Button onClick={action('click')}>Second</Button>;
50 changes: 0 additions & 50 deletions lib/source-loader/src/server/__snapshots__/build.test.js.snap

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -257,22 +257,6 @@ export function findDependencies(ast) {
return { dependencies, storiesOfIdentifiers };
}

export function endOfImports(ast) {
let end = 0;

estraverse.traverse(ast, {
fallback: 'iteration',
enter: node => {
patchNode(node);

if (node.type === 'ImportDeclaration') {
end = Math.max(node.end, end);
}
},
});
return end;
}

export function popParametersObjectFromDefaultExport(source, ast) {
let splicedSource = source;
let parametersSliceOfCode = '';
Expand Down
25 changes: 2 additions & 23 deletions lib/source-loader/src/server/build.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
import { getOptions } from 'loader-utils';
import { readStory } from './dependencies-lookup/readAsObject';
import { getRidOfUselessFilePrefixes } from './dependencies-lookup/getRidOfUselessFilePrefixes';
import getParser from './abstract-syntax-tree/parsers';
import { endOfImports } from './abstract-syntax-tree/traverse-helpers';

export function insertAfterImports(classLoader, insert, source) {
const options = getOptions(classLoader) || {};
let ast;
try {
ast = getParser(options.parser || classLoader.extension || 'javascript').parse(source);
} catch (e) {
// if not working, then we will fallback to not adding anything
// perhaps the code was not written in javascript
return source;
}
if (!ast) return `${insert}${source}`;
const endOfImportsIndex = endOfImports(ast);
const result = `${source.substring(0, endOfImportsIndex)}\n${insert}\n${source.substring(
endOfImportsIndex
)}`;
return result;
}

export function transform(inputSource) {
return readStory(this, inputSource)
Expand All @@ -38,6 +17,7 @@ export function transform(inputSource) {
}) => {
const preamble = `
/* eslint-disable */
// @ts-nocheck
// @ts-ignore
var withSourceLoader = require('@storybook/source-loader/preview').withSource;
// @ts-ignore
Expand All @@ -57,8 +37,7 @@ var __LOCAL_DEPENDENCIES__ = ${JSON.stringify(localDependencies)};
// @ts-ignore
var __IDS_TO_FRAMEWORKS__ = ${JSON.stringify(idsToFrameworks)};
`;
return insertAfterImports(this, preamble, source);
// return `${preamble}${source}`;
return `${preamble}\n${source}`;
}
);
}
54 changes: 0 additions & 54 deletions lib/source-loader/src/server/build.test.js

This file was deleted.

1 comment on commit 66c524f

@vercel
Copy link

@vercel vercel bot commented on 66c524f Jan 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.