Skip to content

Commit

Permalink
Source-loader: Add imports to top of file
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Jan 16, 2020
1 parent b45f98a commit bea119a
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 143 deletions.
50 changes: 0 additions & 50 deletions lib/source-loader/src/server/__snapshots__/build.test.js.snap

This file was deleted.

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
24 changes: 1 addition & 23 deletions lib/source-loader/src/server/build.js
@@ -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 Down Expand Up @@ -57,8 +36,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.

0 comments on commit bea119a

Please sign in to comment.