Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
fix(index): correct loader sourcemaps usage (#67)
Browse files Browse the repository at this point in the history
* fix: pass sourcemaps to istanbul
  • Loading branch information
mattlewis92 authored and joshwiens committed Jul 12, 2017
1 parent d0cf823 commit 691b565
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 20 deletions.
42 changes: 40 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dist"
],
"dependencies": {
"convert-source-map": "^1.5.0",
"istanbul-lib-instrument": "^1.7.3",
"loader-utils": "^1.1.0"
},
Expand Down Expand Up @@ -39,6 +40,7 @@
"devDependencies": {
"babel-cli": "^6.24.0",
"babel-jest": "^20.0.3",
"babel-loader": "^7.1.1",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.6.0",
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { createInstrumenter } from 'istanbul-lib-instrument';
import loaderUtils from 'loader-utils';
import convert from 'convert-source-map';

export default function (source, sourceMap) {
let srcMap = sourceMap;
// use inline source map, if any
if (!srcMap) {
const inlineSourceMap = convert.fromSource(source);
if (inlineSourceMap) {
srcMap = inlineSourceMap.sourcemap;
}
}

export default function (source) {
const options = Object.assign({ produceSourceMap: true }, loaderUtils.getOptions(this));
const instrumenter = createInstrumenter(options);

instrumenter.instrument(source, this.resourcePath, (error, instrumentedSource) => {
this.callback(error, instrumentedSource, instrumenter.lastSourceMap());
});
}, srcMap);
}
9 changes: 5 additions & 4 deletions test/__snapshots__/index.test.js.snap

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions test/fixtures/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ module.exports = class Foo {
return !!this;
}

baz() {
return !this.bar();
}

};
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('instrument code', async () => {
test('sourcemap files on by default', async () => {
const stats = await webpack({
extend: {
devtool: 'sourcemap',
devtool: 'source-map',
},
});
const sourceMap = stats.compilation.assets['main.js.map'].source();
Expand All @@ -21,7 +21,7 @@ test('sourcemap files on by default', async () => {
test('disabled sourcemaps', async () => {
const stats = await webpack({
extend: {
devtool: 'sourcemap',
devtool: 'source-map',
},
options: {
produceSourceMap: false,
Expand Down
19 changes: 13 additions & 6 deletions test/utils/loader.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import path from 'path';
import loader from '../../src/cjs';

module.exports = function (...args) {
const normalize = str => str.split(path.sep).join('/');

module.exports = function (source, map, ...args) {
// hack the resourcePath to be consistent across systems so that tests always work
this.resourcePath = this.resourcePath
.replace(path.resolve(__dirname, '..'), '')
// make windows paths appear like unix
.replace(/\\/g, '/');
return loader.call(this, ...args);
this.resourcePath = normalize(this.resourcePath.replace(path.resolve(__dirname, '..'), ''));

// do the same hack for sourcemaps so tests are consistent
const sourceMap = map;
if (sourceMap) {
sourceMap.sourceRoot = '';
sourceMap.sources = sourceMap.sources.map(normalize);
}

return loader.call(this, source, sourceMap, ...args);
};
3 changes: 3 additions & 0 deletions test/utils/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export default function ({ fixture = 'basic.js', options, extend = {} } = {}) {
},
module: {
rules: [{
test: /\.js$/,
loader: 'babel-loader',
}, {
test: /\.js$/,
loader,
enforce: 'post',
Expand Down

0 comments on commit 691b565

Please sign in to comment.