Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sourcemaps #115

Open
3 tasks
m-mujica opened this issue Sep 28, 2017 · 0 comments
Open
3 tasks

Fix sourcemaps #115

m-mujica opened this issue Sep 28, 2017 · 0 comments

Comments

@m-mujica
Copy link
Contributor

m-mujica commented Sep 28, 2017

Ref: stealjs/steal-tools#832

TL; DR

recast ability to take previous source maps fixes the problem of multistage sourcemaps in the different transforms. I propose creating issues to tackle each transform independently.

I tried updated all in one shot and it was bananas

  • - ES6 to AMD
  • - CJS to AMD
  • - AMD to Slim ?

** Also, we need to figure out how to validate a generated sourcemap.

Long story:

I was able to replicate with the code below, sourcemap-to-ast is unable to update the esprima ast correctly based on babel's source map; babel's ast has some deviations from ASTree and I think that causes the problem.

I tried recast, that uses esprima under the hood and has a bunch of code to keep track of the AST changes and it fixes the problem reported in steal-tools.

But replacing esprima with recast in transpile is a lot more work than I expected:

  • The expected file tests need all to be updated, recast.print format is different than escodegen's
  • Some transpile transforms access the AST directly without using a traverse function, this transforms break with recast since a "File" node wrapper is used to keep track of line numbers and stuff like that. (unwrapping the "program" node causes the recast printer to fail due to line number mismatches)

The code replicating the issue:

var babel = require("babel-core");
var esprima = require("esprima");
var escodegen = require("escodegen");
var sourceMapToAst = require("sourcemap-to-ast");

var babelResult = babel.transform(source, {
  filename: "main.js",
  plugins: ["transform-es2015-modules-amd"],
  presets: ["react", "stage-0", ["es2015", { loose: false }]],
  sourceMaps: true
});

var esprimaAst = esprima.parseScript(babelResult.code, {
  loc: true,
  range: true,
  source: "main.js"
});

sourceMapToAst(esprimaAst, babelResult.map);

var escodegenResult = escodegen.generate(esprimaAst, {
  sourceMap: true,
  sourceMapWithCode: true
});

// escodegenResult.map has incorrect mappings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant