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

How to get the current sourcemap in the transform hook ? #2983

Closed
billowz opened this issue Jul 4, 2019 · 3 comments · Fixed by #2993
Closed

How to get the current sourcemap in the transform hook ? #2983

billowz opened this issue Jul 4, 2019 · 3 comments · Fixed by #2993

Comments

@billowz
Copy link
Contributor

billowz commented Jul 4, 2019

I implemented an istanbul plugin using istanbul-lib-instrument, But the istanbul-lib-instrument require the inputSourceMap to maps the not instrumented code back to it's original form

How can i get the current sourcemap in the transform hook ?

  • istanbul.js

    import { createFilter } from 'rollup-pluginutils';
    import istanbul from 'istanbul-lib-instrument';
    
    export default function (options = {}) {
      return {
        name: 'istanbul',
        transform (code, id) {
    
          const instrumenter = new istanbul.createInstrumenter({
            produceSourceMap: sourceMap,
            esModules: true,
            codeGenerationOptions: {
              { sourceMap: id, sourceMapWithCode: true }
            }
          });
          
          // TODO require the inputSourceMap that generated by the previous plugins
          code = instrumenter.instrumentSync(code, id, ??? );
    
          return { code, map: instrumenter.lastSourceMap() };
        }
      };
    }
  • rollup config

    • the rollup-plugin-jscc well
    const nodeResolve = require('rollup-plugin-node-resolve'),
       commonjs = require('rollup-plugin-commonjs'),
      jscc = require('rollup-plugin-jscc'),
      typescript = require('rollup-plugin-typescript'),
      istanbul = require('./istanbul')
    
    module.exports = {
      input: 'src/index.ts',
      output: {
         name: 'test',
         format: 'umd',
         file: 'bundle.js',
         sourcemap: true,
         amd: {id: 'test'}
      },
      plugins: [
        nodeResolve({ jsnext: true, extensions: ['.ts'] }),
        commonjs(),
        jscc({ values: { _DEBUG: true } }),
        typescript({ module: 'ESNext', target }),
        istanbul()
      ]
    }
@lukastaegert
Copy link
Member

So what you need is the combined source maps of all previous plugins. Unfortunately at the moment, this is not available. The reason is that there was no use yet, and it would be costly to calculate it by default. This is how sourcemaps work in Rollup:

  • The loader and each transformer create source-maps that just describe their own transformations
  • These are stored untransformed in an array, the "sourcemap-chain"
  • If an output with sourcemaps is created, this chain is merged ("collapsed") with all transformations done by Rollup itself into a final map. This process is costly so we do not do it unless requested.

That being said, I think having a good way to run coverage in Rollup would be really nice, I would be very interested in having a good coverage solution for Rollups sources as well. So maybe we could find a way to request a pre-collapsed source-map from a transform hook?

For instance we could try to add a this.getCombinedSourceMap() just to the transform hook, would this solve your issue?

@billowz
Copy link
Contributor Author

billowz commented Jul 5, 2019

@lukastaegert Thank you, it's going to solve my issue.

@lukastaegert
Copy link
Member

Don't thank me yet, it still needs to be implemented...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants