汇总未生成打字稿源映射

das*_*dsa 4 rollup

我正在使用带有 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,
    },
}
Run Code Online (Sandbox Code Playgroud)

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

car*_*222 8

这对我有用:您需要sourceMap: falsetypescript汇总插件选项中进行设置。

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

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

  • 当你浪费了近半个小时试图弄清楚系统背后不合逻辑的逻辑是什么时,这是荒谬且令人沮丧的。 (3认同)
  • 谢谢你的提示!就我而言,我的插件选项是:`typescript({ tsconfig: './tsconfig.json' })`,但我的`tsconfig.json`中有`"sourceMap": true`。删除该编译器选项为我解决了这个问题。 (2认同)

fre*_*tag 7

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

typescript({ sourceMap: !production, inlineSources: !production }),
Run Code Online (Sandbox Code Playgroud)

通过简单地不创建重复的 SourceMap,而是将源代码从 TypeScript 复制到 SourceMap 中,就可以避免该问题。