【问题标题】:Rollup is not generating typescript sourcemap汇总未生成打字稿源图
【发布时间】:2020-11-22 20:35:20
【问题描述】:

我正在使用带有 svelte + typescript + scss 的汇总。我的问题是我无法生成源地图。

以下是我的汇总配置文件:

import svelte from 'rollup-plugin-svelte'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import livereload from 'rollup-plugin-livereload'
import { terser } from 'rollup-plugin-terser'
import typescript from '@rollup/plugin-typescript'
import alias from '@rollup/plugin-alias'
    
const production = !process.env.ROLLUP_WATCH
const path = require('path').resolve(__dirname, 'src')
const svelteOptions = require('./svelte.config')
    
function serve() {
    let server
    
    function toExit() {
        if (server) server.kill(0)
    }
    
    return {
        writeBundle() {
            if (server) return
            server = require('child_process').spawn(
                'yarn',
                ['run', 'start', '--', '--dev'],
                {
                    stdio: ['ignore', 'inherit', 'inherit'],
                    shell: true,
                }
            )
    
            process.on('SIGTERM', toExit)
            process.on('exit', toExit)
        },
    }
}
    
export default {
    input: 'src/main.ts',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js',
    },
    plugins: [
        alias({
            entries: [
                { find: '@app', replacement: `${path}` },
                { find: '@components', replacement: `${path}/components` },
                { find: '@includes', replacement: `${path}/includes` },
                { find: '@styles', replacement: `${path}/styles` },
                { find: '@pages', replacement: `${path}/pages` },
            ],
        }),
        svelte(svelteOptions),
    
        // If you have external dependencies installed from
        // npm, you'll most likely need these plugins. In
        // some cases you'll need additional configuration -
        // consult the documentation for details:
        // https://github.com/rollup/plugins/tree/master/packages/commonjs
        resolve({
            browser: true,
            dedupe: ['svelte'],
        }),
        commonjs(),
        typescript({ sourceMap: !production }),
    
        // In dev mode, call `npm run start` once
        // the bundle has been generated
        !production && serve(),
    
        // Watch the `public` directory and refresh the
        // browser on changes when not in production
        !production && livereload('public'),
    
        // If we're building for production (npm run build
        // instead of npm run dev), minify
        production && terser(),
    ],
    watch: {
        clearScreen: false,
    },
}

我不确定我到底做错了什么。这是我正在使用的代码的link。 任何帮助将不胜感激!

【问题讨论】:

  • 我刚刚克隆了你的项目,运行了npm run build,并得到了生成的打字稿源图。它输出bundle.js.map。你正在运行什么命令给你带来麻烦?
  • @CarlosRoso 在 TS 文件中的 chrome 控制台上记录一些内容,然后单击行号以检查是否在 chrome 检查器中看到 TS 文件。 (我在这里描述了我的问题:github.com/sveltejs/template/issues/157

标签: rollup


【解决方案1】:

这对我有用:您需要在 typescript 汇总插件选项中设置 sourceMap: false

export default {
  input: 'src/main.ts',
  output: {
    sourcemap: true,
    format: 'iife',
    ...
  },
  plugins: [
    ...
    svelte(...),
    typescript({ sourceMap: false }),
    ...
  ]
}

结果是 rollup 的 sourcemap 折叠器与 typescript 的插件 sourcemap 生成器冲突。这就是为什么它适用于 prod 构建但不适用于 dev 构建(因为最初它是 sourceMap: !production)。只需让 rollup 完成繁重的工作。

【讨论】:

  • 当你浪费了将近半个小时试图弄清楚系统背后的不合逻辑逻辑是什么时,这太荒谬了,太令人沮丧了。
【解决方案2】:

正如其他人所提到的,TypeScript 和 Rollup 的组合似乎会导致问题。在 TypeScript 中禁用源映射仅解决了将 Svelte 映射到 TypeScript 的问题。但是,您只会收到显示 已编译 JavaScript 中的源代码的源映射,而不是原始 TypeScript 中的源映射。我终于找到了一个对我有用的解决方案:只需将 Option inlineSources: true 添加到 TypeScript 选项中:

typescript({ sourceMap: !production, inlineSources: !production }),

这通过简单地不创建重复的 SourceMap 而是通过将源代码从 TypeScript 复制到 SourceMap 来规避问题。

【讨论】:

  • 我试过这个,但我仍然只是获取已编译 JavaScript 的源映射(总比没有好),但没有映射到原始 TypeScript。缩小器有什么不同吗?我使用的是更简洁的,而不是苗条的。
【解决方案3】:

对于使用 terser 而不是 svelte 的任何人,这为我解决了同样的问题:

import sourcemaps from 'rollup-plugin-sourcemaps';
import { terser } from 'rollup-plugin-terser';
import typescript from '@rollup/plugin-typescript';

export default [
  {
    input: 'dist/index.js',
    output: [
      {
        file: 'dist/cjs/index.js',
        format: 'cjs'
      },
      {
        file: 'dist/fesm2015/index.js',
        format: 'es'
      }
    ],
    plugins: [
      sourcemaps(),
      terser(),
      typescript({ sourceMap: true, inlineSources: true })
    ]
  }
];

显然需要rollup-plugin-sourcemaps 来发挥必要的魔力,以利用 TypeScript 编译器生成的地图文件并将它们提供给 terser。

【讨论】:

    【解决方案4】:

    对我来说,我可以通过 sourcemap: "inline"

    进行映射

    在/build/index.esm.js文件里面会有映射。

    export default {
      input: "src/index.ts",
      output: [
        {
          file: 'build/index.esm.js',
          format: 'es',
          sourcemap:  "inline"
        },
      ],
      plugins: [
        typescript({ sourceMap: false, inlineSources: true  }),
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 1970-01-01
      • 2022-11-02
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多