Skip to content

Commit

Permalink
fix: ast cache issue with hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Dec 29, 2020
1 parent 7749541 commit ca7bc22
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"name": "jest instrument",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"cwd": "${workspaceFolder}/core/instrument",
"args": ["extract-component"],
"args": ["mdx-story-source"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
Expand Down
4 changes: 2 additions & 2 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const parseSource = async (
options: Required<InstrumentOptions>,
): Promise<ParseStorieReturnType | undefined> => {
const source = await prettify(code, options.prettier, filePath);
const { ast } = parseFile(source, options.parser, source);
const { ast } = parseFile(filePath, options.parser, source);

const store = traverseFn(ast, options, { source, filePath });
if (!store) {
Expand Down Expand Up @@ -161,7 +161,7 @@ export const parseStories = async (
filepath: filePath,
...otherMDXOptions,
});
const { ast } = parseFile(filePath, mergedOptions.parser, code);
const { ast } = parseFile(filePath, mergedOptions.parser, code, false);

if (transformMDX) {
//second pass transform - inject any necessary attributes
Expand Down
15 changes: 10 additions & 5 deletions core/instrument/src/misc/ast_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ export const parseFile = (
filePath: string,
options?: parser.ParserOptions,
sourceCode?: string,
cache: boolean = true,
): CacheProps => {
if (astCache[filePath]) {
return astCache[filePath];
}
const source = sourceCode || fs.readFileSync(filePath, 'utf8');
if (!cache) {
return { ast: parser.parse(source, options), source };
}
const filekey = `${filePath}-${fs.statSync(filePath).mtime.toString()}`;
if (astCache[filekey]) {
return astCache[filekey];
}
const ast = parser.parse(source, options);
astCache[filePath] = { ast, source };
return astCache[filePath];
astCache[filekey] = { ast, source };
return astCache[filekey];
};

export const parseImports = (
Expand Down
16 changes: 8 additions & 8 deletions core/instrument/test/__snapshots__/mdx-story-source.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Object {
"components": Object {},
"doc": Object {
"componentsLookup": Object {},
"date": 2020-10-21T01:23:26.442Z,
"dateModified": 2020-10-21T01:23:26.442Z,
"date": 2020-12-29T05:27:27.469Z,
"dateModified": 2020-12-29T05:27:27.469Z,
"title": "MDX",
},
"exports": Object {
Expand Down Expand Up @@ -92,8 +92,8 @@ Object {
"components": Object {},
"doc": Object {
"componentsLookup": Object {},
"date": 2020-10-21T01:23:26.442Z,
"dateModified": 2020-10-21T01:23:26.442Z,
"date": 2020-12-29T05:27:27.468Z,
"dateModified": 2020-12-29T05:27:27.469Z,
"title": "MDX",
},
"exports": Object {
Expand Down Expand Up @@ -231,8 +231,8 @@ Object {
"components": Object {},
"doc": Object {
"componentsLookup": Object {},
"date": 2020-10-21T01:23:26.442Z,
"dateModified": 2020-10-21T01:23:26.442Z,
"date": 2020-12-29T05:27:27.469Z,
"dateModified": 2020-12-29T05:27:27.469Z,
"title": "MDX",
},
"exports": Object {
Expand Down Expand Up @@ -448,8 +448,8 @@ Object {
"components": Object {},
"doc": Object {
"componentsLookup": Object {},
"date": 2020-10-21T01:23:26.442Z,
"dateModified": 2020-10-21T01:23:26.442Z,
"date": 2020-12-29T05:27:27.469Z,
"dateModified": 2020-12-29T05:27:27.470Z,
"title": "MDX",
},
"exports": Object {
Expand Down

0 comments on commit ca7bc22

Please sign in to comment.