Skip to content

Commit eaf068f

Browse files
committed
fix: The analysis array gets added to while processing it so we have to monitor the array until its length doesn't change.
1 parent d833bf0 commit eaf068f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/jsx-analyzer/src/index.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export function parse(filename: string, data: string, factory: BlockFactory, opt
146146

147147
return Promise.resolve().then(() => {
148148
return parseWith(template, metaAnalysis, factory, resolvedOpts).then(analysis => {
149-
return Promise.all(metaAnalysis.analysisPromises).then((analyses) => {
149+
return resolveAllRecursively(metaAnalysis.analysisPromises).then((analyses) => {
150150
for (let analysis of (new Set(analyses))) {
151151
traverse(analysis.template.ast, analyzer(analysis));
152152
metaAnalysis.addAnalysis(analysis);
@@ -160,6 +160,25 @@ export function parse(filename: string, data: string, factory: BlockFactory, opt
160160
});
161161
}
162162

163+
function resolveAllRecursively<T>(promiseArray: Array<Promise<T>>): Promise<Array<T>> {
164+
return new Promise<Array<T>>((resolve, reject) => {
165+
let currentLength = promiseArray.length;
166+
let waitAgain = (promise: Promise<Array<T>>): void => {
167+
promise.then((values) => {
168+
if (promiseArray.length === currentLength) {
169+
resolve(values);
170+
} else {
171+
currentLength = promiseArray.length;
172+
waitAgain(Promise.all(promiseArray));
173+
}
174+
}, (error) => {
175+
reject(error);
176+
});
177+
};
178+
waitAgain(Promise.all(promiseArray));
179+
});
180+
}
181+
163182
/**
164183
* Provided a file path, return a promise for the fully parsed analytics object.
165184
* // TODO: Make streaming?

packages/jsx-analyzer/src/transformer/webpackLoader.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export default function CSSBlocksWebpackAdapter(this: any, source: any, map: any
5353

5454
.then((mappings: StyleMapping[]) => {
5555
mappings.forEach((mapping: StyleMapping) => {
56-
let styleMapping: StyleMapping | undefined = mapping.analyses && mapping.analyses.find(a => a.template.identifier === path ) && mapping;
56+
// When an css or analysis error happens the mapping seems to be undefined and generates a confusing error.
57+
let styleMapping: StyleMapping | undefined = mapping && mapping.analyses && mapping.analyses.find(a => a.template.identifier === path ) && mapping;
5758
if ( !styleMapping ) {
5859
return;
5960
}

0 commit comments

Comments
 (0)