Skip to content

Commit

Permalink
fix(babel): bug with concurrent processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Sep 2, 2023
1 parent 4936598 commit 27d2e2e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/babel/src/transform/Entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,12 @@ export class Entrypoint extends BaseEntrypoint {
this.generation + 1
);

this.log('superseded by %s', newEntrypoint.name);
this.log(
'superseded by %s (%o -> %o)',
newEntrypoint.name,
this.only,
newEntrypoint.only
);
this.#supersededWith = newEntrypoint;
this.onSupersedeHandlers.forEach((handler) => handler(newEntrypoint));

Expand Down
14 changes: 13 additions & 1 deletion packages/babel/src/transform/generators/processEntrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,21 @@ export function* processEntrypoint(
const { only, log } = this.entrypoint;
log('start processing (only: %s)', only);

if (this.entrypoint.supersededWith) {
log('entrypoint already superseded, rescheduling processing');
yield [
'processEntrypoint',
this.entrypoint.supersededWith,
undefined,
null,
];
return;
}

const abortController = new AbortController();

const onParentAbort = () => {
log('parent aborted, aborting processing');
abortController.abort();
};

Expand All @@ -43,7 +55,7 @@ export function* processEntrypoint(
);
this.entrypoint.setTransformResult(result);
} catch (e) {
if (isAborted(e)) {
if (isAborted(e) && this.entrypoint.supersededWith) {
log('aborting processing');
} else {
throw e;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel/src/transform/generators/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function* workflow(
try {
yield* this.getNext('processEntrypoint', entrypoint, undefined);
} catch (e) {
if (isAborted(e)) {
if (isAborted(e) && entrypoint.supersededWith) {
entrypoint.log('entrypoint superseded, rescheduling workflow');
yield* this.getNext('workflow', entrypoint.supersededWith!, undefined);
} else {
Expand Down
84 changes: 84 additions & 0 deletions packages/testkit/src/__snapshots__/babel.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,90 @@ Dependencies: NA
`;
exports[`strategy shaker concurrent multiple parallel chains of reexports 1`] = `
"import { styled } from '@linaria/react';
export const H1foo = /*#__PURE__*/styled('h1')({
name: "H1foo",
class: "H1foo_h1m1lid5",
propsAsIs: false
});"
`;
exports[`strategy shaker concurrent multiple parallel chains of reexports 2`] = `
CSS:
.H1foo_h1m1lid5 {
color: foo;
}
Dependencies: ./__fixtures__/re-exports
`;
exports[`strategy shaker concurrent multiple parallel chains of reexports 3`] = `
"import { styled } from '@linaria/react';
export const H1bar = /*#__PURE__*/styled('h1')({
name: "H1bar",
class: "H1bar_h1r70tzz",
propsAsIs: false
});"
`;
exports[`strategy shaker concurrent multiple parallel chains of reexports 4`] = `
CSS:
.H1bar_h1r70tzz {
color: bar;
}
Dependencies: ./__fixtures__/re-exports
`;
exports[`strategy shaker concurrent multiple parallel chains of reexports 5`] = `
"import { styled } from '@linaria/react';
export const H1bar1 = /*#__PURE__*/styled('h1')({
name: "H1bar1",
class: "H1bar1_hsluxkf",
propsAsIs: false
});"
`;
exports[`strategy shaker concurrent multiple parallel chains of reexports 6`] = `
CSS:
.H1bar1_hsluxkf {
color: bar1;
}
Dependencies: ./__fixtures__/re-exports
`;
exports[`strategy shaker concurrent multiple parallel chains of reexports 7`] = `
"import { styled } from '@linaria/react';
export const H1bar2 = /*#__PURE__*/styled('h1')({
name: "H1bar2",
class: "H1bar2_ht4s9ym",
propsAsIs: false
});"
`;
exports[`strategy shaker concurrent multiple parallel chains of reexports 8`] = `
CSS:
.H1bar2_ht4s9ym {
color: bar2;
}
Dependencies: ./__fixtures__/re-exports
`;
exports[`strategy shaker concurrent two parallel chains of reexports 1`] = `
"import { styled } from '@linaria/react';
export const H1 = /*#__PURE__*/styled('h1')({
Expand Down
36 changes: 35 additions & 1 deletion packages/testkit/src/babel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
TransformCacheCollection,
Entrypoint,
} from '@linaria/babel-preset';
import { linariaLogger } from '@linaria/logger';
import { enableDebug, linariaLogger } from '@linaria/logger';
import type { Evaluator, StrictOptions, OnEvent } from '@linaria/utils';
import { EventEmitter } from '@linaria/utils';

Expand Down Expand Up @@ -3245,5 +3245,39 @@ describe('strategy shaker', () => {
'single'
);
});

it('multiple parallel chains of reexports', async () => {
enableDebug();
const cache = new TransformCacheCollection();

const onEvent = jest.fn<void, Parameters<OnEvent>>();
const emitter = new EventEmitter(onEvent);

const tokens = ['foo', 'bar', 'bar1', 'bar2'];

const results = await Promise.all(
tokens.map((token) =>
transform(
dedent`
import { styled } from '@linaria/react';
import { ${token} } from "./__fixtures__/re-exports";
export const H1${token} = styled.h1\`
color: ${`\${${token}}`};
\`
`,
[evaluator],
cache,
emitter,
token
)
)
);

for (const { code, metadata } of results) {
expect(code).toMatchSnapshot();
expect(metadata).toMatchSnapshot();
}
});
});
});

0 comments on commit 27d2e2e

Please sign in to comment.