source-maps - Sourcemaps 不适用于 spago(纯脚本)

标签 source-maps purescript spago

Spago 不使用源映射来引用堆栈跟踪中错误的源代码位置。

这是我的Main.purs:

f :: Unit -> Unit 
f _ = unsafeCrashWith "error"

main :: Effect Unit 
main = do 
  pure $ f unit

我运行了这些命令来构建和运行程序:

spago build --purs-args "-g source maps"
spago run

在输出中,我得到了对 index.js 文件中行的引用,例如

<my-project>/output/Partial/foreign.js:6
  throw new Error(msg);
  ^

Error: error
    at exports._crashWith (<my-project>/output/Partial/foreign.js:6:9)
    at <my-project>/output/Partial.Unsafe/index.js:8:35
    at exports._unsafePartial (<my-project>/output/Partial.Unsafe/foreign.js:6:10)
    at Object.unsafeCrashWith (<my-project>/output/Partial.Unsafe/index.js:7:12)
    at f (<my-project>/output/Main/index.js:14:27)
    at Object.<anonymous> (<my-project>/output/Main/index.js:16:63)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)

我想要的是让此堆栈跟踪使用源映射来引用实际的纯脚本源代码位置。

最佳答案

好的,这里有很多。有些是您的错误,有些是未记录的陷阱。

所以首先,如果您运行 purs compile --help,您应该会看到以下部分:

  -g,--codegen ARG         Specifies comma-separated codegen targets to include.
                           Accepted codegen targets are 'corefn', 'docs', 'js',
                           'sourcemaps'. The default target is 'js', but if this
                           option is used only the targets specified will be
                           used.

这给了我们两条信息:

  1. 应该是sourcemaps(没有空格),而不是source maps。所以你的参数应该是 --purs-args "-g sourcemaps"
  2. 如果您只指定sourcemaps,您将只获得源映射,没有实际的JS 输出。所以你的参数应该是 --purs-args "-g sourcemaps,js"

但除了这两个之外,还有一个未记录的陷阱:-g 之后不应该有空格(惊喜!)

嗯,公平地说,它并没有那么狂野,它是一种在某种程度上被接受的模式。它是这样的:

  1. 对于短版本的参数,值应该紧跟在字母后面,没有空格。所以 --purs-args "-gsourcemaps,js"
  2. 对于长版本,值应该在等号之后。所以 --purs-args "--codegen=sourcemaps,js"

好的,现在,如果您运行 spago build --purs-args "-gsourcemaps,js",您应该会看到接下来生成的 index.js.map 文件到 index.js 文件。到目前为止一切顺利。

但是 - 哦不!当您spago 运行 时,您仍然只能看到 JS 调用堆栈。怎么回事?

好吧,事实证明,尽管 Node does support source maps starting with v12.12 ,默认情况下它们是禁用的。您必须通过将 --enable-source-maps 参数传递给 Node 本身来显式启用它们。为了向 Node 传递额外的参数,Spago 提供了方便的 -b|--node-args 选项。

因此,综合以上所有内容,这是您的最终解决方案:

> spago build --purs-args "-gsourcemaps,js"
...

> spago run --node-args "--enable-source-maps"

Error: error
    at exports._crashWith (C:\o\purs-pg\output\Partial\foreign.js:6:9)
    at C:\o\purs-pg\output\Partial.Unsafe\index.js:8:35
        -> C:\o\purs-pg\.spago\partial\v3.0.0\src\Partial\Unsafe.purs:24:23
    at exports._unsafePartial (C:\o\purs-pg\output\Partial.Unsafe\foreign.js:6:10)
    at Object.unsafeCrashWith (C:\o\purs-pg\output\Partial.Unsafe\index.js:7:12)
        -> C:\o\purs-pg\.spago\partial\v3.0.0\src\Partial\Unsafe.purs:24:23
    at f (C:\o\purs-pg\output\Main\index.js:39:27)
        -> C:\o\purs-pg\src\Main.purs:79:1
    at Object.<anonymous> (C:\o\purs-pg\output\Main\index.js:41:63)
        -> C:\o\purs-pg\src\Main.purs:84:10
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:895:16)

总而言之,我想指出 Spago 本身有一个参数 -x|--source-maps,出于某种原因,run 允许使用该参数和 build 命令,但实际上并不适用于它们:它仅适用于 bundle-appbundle-module

关于source-maps - Sourcemaps 不适用于 spago(纯脚本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71832250/

相关文章:

node.js - 如何配置使用从 PureScript 编译的 JavaScript 模块的 TypeScript 项目?

javascript - 如何更改 spago 的 Node 堆栈限制?

purescript - 如何使用 spago 卸载软件包?

build - packages.dhall 和 spago.dhall 文件有什么区别?

typescript - Webpack 4 加载库 Sourcemap

java - Chrome 不加载 gwt 源 map

css - Google Chrome "Failed parsing SourceMap": css. map (Web Essential)

gulp - 带有 Gulp、Browserify、Babel、ES6 和 React 的源映射

purescript - 如何更新用 `data`而不是 `type`定义的PureScript记录?

purescript - 有没有办法用 Purescript 解析 XML?