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

Add getCombinedSourceMap in transform plugin context (#2983) #2993

Merged
merged 5 commits into from Jul 15, 2019

Conversation

billowz
Copy link
Contributor

@billowz billowz commented Jul 12, 2019

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:

Resolves #2983

Description

  • Add this.getCombinedSourceMap() => SourceMap in transform plugin context

    When the sourcemap chain is not not empty, replace the original source map with the combined source source map and clear the sourcemap chain

  • Refactor collapseSourcemaps

Copy link
Member

@lukastaegert lukastaegert left a comment

Awesome for looking into this! Did you already try if this works to generate proper instrumentation for coverage? I will probably not have time today for actually running your code but I hope we can merge this soon. Just some stylistic remarks you may or may not address.

docs/05-plugin-development.md Outdated Show resolved Hide resolved
src/utils/collapseSourcemaps.ts Outdated Show resolved Hide resolved
src/utils/collapseSourcemaps.ts Outdated Show resolved Hide resolved
src/utils/collapseSourcemaps.ts Outdated Show resolved Hide resolved
@billowz
Copy link
Contributor Author

@billowz billowz commented Jul 13, 2019

Awesome for looking into this! Did you already try if this works to generate proper instrumentation for coverage? I will probably not have time today for actually running your code but I hope we can merge this soon. Just some stylistic remarks you may or may not address.

I have tested it with karma-cover-reporter, and the generated coverage report is correct

@billowz
Copy link
Contributor Author

@billowz billowz commented Jul 13, 2019

Awesome for looking into this! Did you already try if this works to generate proper instrumentation for coverage? I will probably not have time today for actually running your code but I hope we can merge this soon. Just some stylistic remarks you may or may not address.

Here is the test configuration:

  • rollup-plugin-istanbul.js

    import { createFilter } from 'rollup-pluginutils';
    import istanbul from 'istanbul-lib-instrument';
    
    export default function (options = {}) {
      const instrumenter = new istanbul.createInstrumenter({
        produceSourceMap: sourceMap,
        esModules: true
      });
      return {
        name: 'istanbul',
        transform (code, id) {
          // getCombinedSourceMap: https://github.com/rollup/rollup/issues/2983
          const { version, sources, sourcesContent, names, mappings } = this.getCombinedSourceMap()
          code = instrumenter.instrumentSync(code, id, { version, sources, sourcesContent, names, mappings })
    
          return { code, map: instrumenter.lastSourceMap() };
        }
      };
    }
  • karma-config.js

    const jscc = require('rollup-plugin-jscc'),
      typescript = require('rollup-plugin-typescript'),
      istanbul = require('./rollup-plugin-istanbul')
      
      
    module.exports = function(config) {
      config.set({
        browsers: ['Chrome'],
        transports: ['websocket', 'polling', 'jsonp-polling'],
        frameworks: ['mocha'],
        reporters: ['coverage-istanbul'],
        basePath: path.join(__dirname, '../'),
        files: ['test/**/*.ts'],
        preprocessors: {
          'test/**/*.ts': ['rollup']
        },
        rollupPreprocessor: {
          options: {
            input: 'src/index.ts',
            output: {
              format: 'iife',
              name: 'test'
            },
            plugins: [
              jscc({ values: { _DEBUG: true } }),
              typescript({ module: 'ESNext' }),
              istanbul()
            ]
          },
          transformPath(filepath) {
            return filepath.replace(/\.ts$/, '.js')
          }
        },
        coverageIstanbulReporter: {
          dir: 'coverage/%browser%',
          reports: ['html', 'lcov'],
          combineBrowserReports: false,
          skipFilesWithNoCoverage: false
        },
        plugins: [
          'karma-chrome-launcher',
          'karma-mocha',
          'karma-rollup',
          'karma-coverage-istanbul-reporter'
        ]
      })
    }

Copy link
Member

@lukastaegert lukastaegert left a comment

Finally managed to get it to run with Mocha and nyc (pro tip: By default, nyc does not like "ts" extensions...). Works really well!

One last thing I noticed: The spelling of "SourceMap" in the context function differs from our spelling of "Sourcemap" in all options so I would like to change the name of the function to "getCombinedSourcemap".

@billowz
Copy link
Contributor Author

@billowz billowz commented Jul 14, 2019

Finally managed to get it to run with Mocha and nyc (pro tip: By default, nyc does not like "ts" extensions...). Works really well!

One last thing I noticed: The spelling of "SourceMap" in the context function differs from our spelling of "Sourcemap" in all options so I would like to change the name of the function to "getCombinedSourcemap".

I will fix it asap

Copy link
Member

@lukastaegert lukastaegert left a comment

👍

@lukastaegert lukastaegert merged commit 6b500b8 into rollup:master Jul 15, 2019
5 of 6 checks passed
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

Successfully merging this pull request may close these issues.

2 participants