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

Incorrect JSON in "sourcesContent" with the "sourcemap" option #1073

Closed
kettanaito opened this issue Jan 7, 2024 · 2 comments
Closed

Incorrect JSON in "sourcesContent" with the "sourcemap" option #1073

kettanaito opened this issue Jan 7, 2024 · 2 comments

Comments

@kettanaito
Copy link

kettanaito commented Jan 7, 2024

Reproduction steps

  • tsup: 8.0.1
  • node: v18.17.0
  1. Clone https://github.com/mswjs/msw.
  2. Go to tsup.config.ts. Add the sourcemap: true option to the first config.
  3. pnpm install (please use pnpm v7.12).
  4. pnpm build.

See the following error printed by tsup:

SyntaxError: Unexpected token 
 in JSON at position 1744
    at JSON.parse (<anonymous>)
    at /msw/node_modules/.pnpm/tsup@6.7.0_4s7jzcjqpdttwnwh3e3glkuq6y/node_modules/tsup/dist/index.js:1959:18
    at async Promise.all (index 3)
    at async PluginContainer.buildFinished (/msw/node_modules/.pnpm/tsup@6.7.0_4s7jzcjqpdttwnwh3e3glkuq6y/node_modules/tsup/dist/index.js:1912:5)
    at async runEsbuild (/msw/node_modules/.pnpm/tsup@6.7.0_4s7jzcjqpdttwnwh3e3glkuq6y/node_modules/tsup/dist/index.js:1763:5)
    at async /msw/node_modules/.pnpm/tsup@6.7.0_4s7jzcjqpdttwnwh3e3glkuq6y/node_modules/tsup/dist/index.js:2362:19
    at async Promise.all (index 0)

Inspecting the problematic info.map reveals the problem:

{
  "version": 3,
  "sources": ["../../src/core/index.ts"],
  "sourcesContent": ["import { checkGlobals } from './utils/internal/checkGlobals'\n\nexport { SetupApi } from './SetupApi'\n\n/* Request handlers */\nexport { RequestHandler } from './handlers/RequestHandler'\nexport { http } from './http'\nexport { HttpHandler, HttpMethods } from './handlers/HttpHandler'\nexport { graphql } from './graphql'\nexport { GraphQLHandler } from './handlers/GraphQLHandler'\n\n/* Utils */\nexport { matchRequestUrl } from './utils/matching/matchRequestUrl'\nexport * from './utils/handleRequest'\nexport { cleanUrl } from './utils/url/cleanUrl'\n\n/**\n * Type definitions.\n */\n\nexport type { SharedOptions, LifeCycleEventsMap } from './sharedOptions'\n\nexport type {\n  ResponseResolver,\n  ResponseResolverReturnType,\n  AsyncResponseResolverReturnType,\n  RequestHandlerOptions,\n  DefaultBodyType,\n  DefaultRequestMultipartBody,\n  JsonBodyType,\n} from './handlers/RequestHandler'\n\nexport type {\n  RequestQuery,\n  HttpRequestParsedResult,\n} from './handlers/HttpHandler'\n\nexport type {\n  GraphQLVariables,\n  GraphQLRequestBody,\n  GraphQLJsonRequestBody,\n} from './handlers/GraphQLHandler'\n\nexport type { Path, PathParams, Match } from './utils/matching/matchRequestUrl'\nexport type { ParsedGraphQLRequest } from './utils/internal/parseGraphQLRequest'\n\nexport * from './HttpResponse'\nexport * from './delay'\nexport { bypass } from './bypass'\nexport { passthrough } from './passthrough'\n\n// Validate environmental globals before executing any code.\n// This ensures that the library gives user-friendly errors\n// when ran in the environments that require additional polyfills\n// from the end user.\ncheckGlobals()\n.mjs'],
  "mappings": "AAAA,SAAS,oBAAoB;AAE7B,SAAS,gBAAgB;AAGzB,SAAS,sBAAsB;AAC/B,SAAS,YAAY;AACrB,SAAS,aAAa,mBAAmB;AACzC,SAAS,eAAe;AACxB,SAAS,sBAAsB;AAG/B,SAAS,uBAAuB;AAChC,cAAc;AACd,SAAS,gBAAgB;AAgCzB,cAAc;AACd,cAAc;AACd,SAAS,cAAc;AACvB,SAAS,mBAAmB;AAM5B,aAAa;",
  "names": []
}

The sourcesContent key contains a string that opens with double quotes " but ends with a single quote ', causing invalid JSON and failing the sourcemap generation.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@kettanaito kettanaito changed the title Incorrect "sourcesContent" when using the "sourcemap" option Incorrect JSON in "sourcesContent" with the "sourcemap" option Jan 7, 2024
@kettanaito kettanaito reopened this Jan 8, 2024
@kettanaito
Copy link
Author

info.map contains the invalid sourcesContent as early as this line:

files.map(async (info) => {

@kettanaito
Copy link
Author

Found the root cause. I had a custom plugin that was modifying the outputted chunks. Since with sourcemap: true sourcemaps also became outputted chunks, the plugin was modifying them also, resulting in invalid sourcemaps. Not an issue of tsup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant