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

JSX inline sourcemap issue #1726

Closed
Mokshit06 opened this issue May 21, 2021 · 4 comments · Fixed by #1832 or #5657
Closed

JSX inline sourcemap issue #1726

Mokshit06 opened this issue May 21, 2021 · 4 comments · Fixed by #1832 or #5657
Assignees
Labels
Milestone

Comments

@Mokshit06
Copy link

Describe the bug
When transpiling JSX using swc, if the jsx includes dynamic children, then the inline sourcemap generated is invalid.

Input code

const foo = <h1>{bar}</h1>

Config

{
  "sourceMaps": "inline",
  "jsc": {
    "target": "es5",
    "parser": {
      "syntax": "typescript",
      "tsx": true
    },
    "loose": true,
  }
}

Expected behavior
Inline sourcemap generated (invalid): data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxhbm9uPiJdLCJzb3VyY2VzQ29udGVudCI6WyJjb25zdCBmb28gPSA8aDE-e2Jhcn08L2gxPiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiSUFBTSxHQUFHLHNDQUFJLEVBQUUsU0FBRSxHQUFHIn0=

Sourcemap generated from result object with Buffer:

`data:application/json;base64,${Buffer.from(map, 'utf8').toString('base64')}`

data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxhbm9uPiJdLCJzb3VyY2VzQ29udGVudCI6WyJjb25zdCBmb28gPSA8aDE+e2Jhcn08L2gxPiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiSUFBTSxHQUFHLHNDQUFJLEVBQUUsU0FBRSxHQUFHIn0=

Both the sourcemaps are different. The one generated with Buffer is valid whereas the one generated by swc is invalid

Version
The version of @swc/core: ^1.2.58

Additional context
Add any other context about the problem here.

@kdy1 kdy1 added this to the v1.2.59 milestone May 21, 2021
@kdy1 kdy1 modified the milestones: v1.2.59, v1.2.60 May 30, 2021
@kdy1 kdy1 modified the milestones: v1.2.60, v1.2.61 Jun 7, 2021
@kdy1 kdy1 modified the milestones: v1.2.61, v1.2.62 Jun 16, 2021
@kdy1 kdy1 mentioned this issue Jun 18, 2021
10 tasks
@kdy1
Copy link
Member

kdy1 commented Jun 18, 2021

How did you get the valid map?

kdy1 added a commit to kdy1/swc that referenced this issue Jun 18, 2021
@kdy1 kdy1 reopened this Jun 18, 2021
@Mokshit06
Copy link
Author

Mokshit06 commented Jun 18, 2021

The map was the JSON string returned by swc when sourceMaps was set to true. The issue was with the data uri that swc created. Also, after trying this with many inputs, it seems like this issue is not really related to the props because

const foo = () => <h1 />;

creates an invalid sourcemap whereas

const foo = <h1 />;

doesn't, so I'm not exactly sure what is causing this issue.

Repro

const swc = require('@swc/core');

const code = `
const foo = () => <h1 />;
`;

/** @type {swc.Options} */
const swcConfig = {
  jsc: {
    target: 'es5',
    parser: {
      syntax: 'ecmascript',
      jsx: true,
    },
    loose: true,
  },
  sourceFileName: 'noop.js',
  sourceMaps: true,
};

const result = swc.transformSync(code, swcConfig);
// valid
console.log(
  `${
    result.code
  }\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(
    result.map,
    'utf8'
  ).toString('base64')}`
);

const resultInline = swc.transformSync(code, {
  ...swcConfig,
  sourceMaps: 'inline',
});
// invalid
console.log(resultInline.code);

@Mokshit06
Copy link
Author

Mokshit06 commented Jun 18, 2021

I think there are many things that are causing this issue. I was able to fix the above sample

const foo = () => <h1 />

by removing the \n character of the template literal but it doesn't work when the tag is not self closing like

const foo = () => <h1></h1>;

@kdy1 kdy1 modified the milestones: v1.2.62, v1.2.63 Jun 27, 2021
@kdy1 kdy1 modified the milestones: v1.2.63, v1.2.64 Jul 5, 2021
@kdy1 kdy1 modified the milestones: v1.2.64, v1.2.65, v1.2.66, v1.2.67 Jul 13, 2021
@kdy1 kdy1 modified the milestones: v1.2.67, v1.2.68, v1.2.72, v1.2.73 Jul 31, 2021
@kdy1 kdy1 modified the milestones: v1.2.142, v1.2.143, v1.2.144, v1.2.145 Feb 16, 2022
@kdy1 kdy1 modified the milestones: v1.2.145, v1.2.146, v1.2.147, v1.2.148 Feb 24, 2022
@kdy1 kdy1 modified the milestones: v1.2.148, v1.2.149, v1.2.150 Mar 4, 2022
@kdy1 kdy1 added this to the Planned milestone Mar 12, 2022
@kdy1 kdy1 modified the milestones: Planned, v1.2.199 Jun 11, 2022
@kdy1 kdy1 self-assigned this Aug 30, 2022
@kdy1 kdy1 modified the milestones: Planned, v1.3.4 Sep 30, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 30, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants