You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Rollup无法生成TypeScript源映射的问题求助

问题描述

我试了各种方法都没用,没法把原始TypeScript源码关联到编译后的JS文件里。不管用不用Babel都不行。我怀疑是依赖这个项目的其他应用有问题,于是直接检查了生成的代码,结果里面完全找不到能用来调试的TS源码痕迹。

环境信息

  • Node v18.15.0
  • npm 9.6.2
  • macOS 13.3.1

相关配置文件

rollup.config.js

import { babel } from '@rollup/plugin-babel'
import resolve from '@rollup/plugin-node-resolve'
import terser from '@rollup/plugin-terser'
import common from '@rollup/plugin-commonjs'
import { DEFAULT_EXTENSIONS } from '@babel/core'
import typescript from '@rollup/plugin-typescript'

const packageJson = require('./package.json')
const isDev = process.env.NODE_ENV === 'development'
const isProd = process.env.NODE_ENV === 'production'
const extensions = [...DEFAULT_EXTENSIONS, '.ts', '.tsx']
const tsconfig = isDev ? 'tsconfig.dev.json' : 'tsconfig.json'

export default {
  input: './src/index.ts',
  external: ['react'],
  output: [
    {
      file: packageJson.module,
      format: 'esm',
      sourcemap: 'inline',
    },
  ],
  plugins: [
    common(),
    typescript({ tsconfig }), // This is used now only to generate the .d.ts files
    resolve({
      extensions,
    }),
    babel({
      babelHelpers: 'bundled',
      extensions,
    }),
    isProd &&
      terser({
        maxWorkers: 4,
      }),
  ].filter(Boolean),
}

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "incremental": true,
    "target": "es6",
    "module": "esnext",
    "jsx": "react",
    "noEmit": true,
    "outDir": "dist",
    "strict": true,
    "moduleResolution": "node",
    "isolatedModules": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "declaration": true,
    "declarationDir": "./dist/types",
    "declarationMap": true
  },
  "include": ["src/**/*.ts", "src/**/*.tsx"],
  "exclude": ["node_modules", "dist", "src/**/*.test.ts"]
}

.babelrc

{
  "presets": [
    "@babel/env",
    "@babel/preset-typescript",
    "@babel/preset-react"
  ]
}

package.json(版本信息供参考)

{
  "scripts": {
    "type-check": "tsc --noEmit",
    "types:dev": "tsc --project tsconfig.dev.json",
    "clear": "rimraf ./dist",
    "build": "npm run clear && rollup -c",
    "dev": "npm run clear && rollup -c rollup.config.js -w",
    "lint": "eslint . --cache"
  },
  "devDependencies": {
    "@babel/core": "^7.21.8",
    "@babel/preset-env": "^7.21.5",
    "@babel/preset-react": "^7.18.6",
    "@babel/preset-typescript": "^7.21.5",
    "@rollup/plugin-babel": "^6.0.3",
    "@rollup/plugin-commonjs": "^25.0.0",
    "@rollup/plugin-node-resolve": "^15.0.2",
    "@rollup/plugin-terser": "^0.4.3",
    "@rollup/plugin-typescript": "^11.1.1",
    "@types/react": "18.2.6",
    "@types/react-dom": "18.2.4",
    "rollup": "^2.79.1",
    "tailwindcss": "^3.3.2",
    "typescript": "5.0.4"
  },
  "peerDependencies": {
    "react": "18.2.0",
    "react-dom": "18.2.0"
  }
}

内容的提问来源于stack exchange,提问作者llanfair

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 06:10:38