egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
8.48k stars 209 forks source link

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

Closed kettanaito closed 5 months ago

kettanaito commented 5 months ago

Reproduction steps

  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

Fund with Polar

kettanaito commented 5 months ago

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

https://github.com/egoist/tsup/blob/8c26e63c92711d60c05aedd3cdc358530ba266c5/src/plugin.ts#L146

kettanaito commented 5 months ago

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.