Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.js: Set env.bugfixes in SWC so destructuring is never transpiled #28363

Merged
merged 9 commits into from
Jun 27, 2024
4 changes: 4 additions & 0 deletions code/frameworks/nextjs/src/swc/next-swc-loader-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ async function loaderTransform(this: any, parentTrace: any, source?: string, inp
// modules.
sourceFileName: filename,
};
// Transpiles the broken syntax to the closest non-broken modern syntax.
// E.g. it won't transpile parameter destructuring in Safari
// which would break how we detect if the mount context property is used in the play function.
programmaticOptions.env.bugfixes = true;

if (!programmaticOptions.inputSourceMap) {
delete programmaticOptions.inputSourceMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ describe('PreviewWeb', () => {
openBlockLoadersGate({ l: 8 });
await waitForRender();

// Assert - renderToCanvas to be called the first time with initial args
// Assert - renderToCanvas to be called the first time with initial args and returned `loaded` value.
expect(projectAnnotations.renderToCanvas).toHaveBeenCalledOnce();
expect(projectAnnotations.renderToCanvas).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down
4 changes: 1 addition & 3 deletions code/lib/test/template/stories/before-each.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const meta = {
export default meta;

export const BeforeEachOrder = {
parameters: {
chromatic: { disable: true },
},
parameters: { chromatic: { disable: true } },
beforeEach() {
console.log('3 - [from story beforeEach]');
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from '@storybook/test';
import { global as globalThis } from '@storybook/global';

export default {
component: globalThis.Components.Button,
args: { label: 'Button' },
};

// We must not transpile destructuring, to make sure that we can analyze the context properties that are used in play.
// See: https://github.com/storybookjs/storybook/discussions/27389
export const DestructureNotTranspiled = {
parameters: { chromatic: { disable: true } },
async play() {
async function fn({ destructured }: { destructured: unknown }) {
console.log(destructured);
}
const match = fn.toString().match(/[^(]*\(([^)]*)/);
const params = match?.[0];
await expect(params).toContain('destructured');
},
};
2 changes: 1 addition & 1 deletion scripts/tasks/sandbox-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function addEsbuildLoaderToStories(mainConfig: ConfigFile) {
loader: '${esbuildLoaderPath}',
options: {
loader: 'tsx',
target: 'es2015',
target: 'es2022',
},
},
// Handle MDX files per the addon-docs presets (ish)
Expand Down
Loading