Skip to content

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

Closed
@kettanaito

Description

@kettanaito

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/[email protected]_4s7jzcjqpdttwnwh3e3glkuq6y/node_modules/tsup/dist/index.js:1959:18
    at async Promise.all (index 3)
    at async PluginContainer.buildFinished (/msw/node_modules/.pnpm/[email protected]_4s7jzcjqpdttwnwh3e3glkuq6y/node_modules/tsup/dist/index.js:1912:5)
    at async runEsbuild (/msw/node_modules/.pnpm/[email protected]_4s7jzcjqpdttwnwh3e3glkuq6y/node_modules/tsup/dist/index.js:1763:5)
    at async /msw/node_modules/.pnpm/[email protected]_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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions