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

fix(index): merge input and instrumented sourcemaps #87

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ jobs:
node_js: 6
env: WEBPACK_VERSION=latest JOB_PART=test
script: npm run travis:$JOB_PART
- <<: *test-latest
node_js: 4.8
env: WEBPACK_VERSION=latest JOB_PART=test
script: npm run travis:$JOB_PART
- <<: *test-latest
node_js: 8
env: WEBPACK_VERSION=latest JOB_PART=lint
Expand Down
3 changes: 0 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ environment:
- nodejs_version: '6'
webpack_version: latest
job_part: test
- nodejs_version: '4.8'
webpack_version: latest
job_part: test
build: 'off'
matrix:
fast_finish: true
Expand Down
7,223 changes: 5,040 additions & 2,183 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
],
"dependencies": {
"convert-source-map": "^1.5.0",
"istanbul-lib-instrument": "^1.7.3",
"istanbul-lib-instrument": "^2.3.2",
"loader-utils": "^1.1.0",
"merge-source-map": "^1.1.0",
"schema-utils": "^0.3.0"
},
"engines": {
Expand Down Expand Up @@ -55,7 +56,7 @@
"nsp": "^2.8.1",
"pre-commit": "^1.2.2",
"standard-version": "^4.2.0",
"webpack": "^3.8.1",
"webpack": "^4.20.2",
"webpack-defaults": "^1.6.0"
},
"homepage": "https://github.com/webpack-contrib/istanbul-instrumenter-loader",
Expand Down
19 changes: 16 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createInstrumenter } from 'istanbul-lib-instrument';
import loaderUtils from 'loader-utils';
import validateOptions from 'schema-utils';
import convert from 'convert-source-map';
import merge from 'merge-source-map';
/* eslint-disable-line */
const schema = require('./options');

Expand All @@ -21,7 +22,19 @@ export default function (source, sourceMap) {

const instrumenter = createInstrumenter(options);

instrumenter.instrument(source, this.resourcePath, (error, instrumentedSource) => {
this.callback(error, instrumentedSource, instrumenter.lastSourceMap());
}, srcMap);
instrumenter.instrument(
source,
// This name must be unique for source map merging to work with multiple merges regardless of
// file names.
`${this.resourcePath}.pre-istanbul.js`,
(error, instrumentedSource) => {
let instrumentedSourceMap = instrumenter.lastSourceMap();
if (srcMap && instrumentedSourceMap) {
// merge srcMap and instrumentedSourceMap together
instrumentedSourceMap = merge(srcMap, instrumentedSourceMap);
}
this.callback(error, instrumentedSource, instrumentedSourceMap);
},
srcMap,
);
}
18 changes: 12 additions & 6 deletions test/__snapshots__/index.test.js.snap

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import path from 'path';
import fs from 'fs';
import webpack from './utils/webpack';

const fixture = 'basic.js';

test('instrument code', async () => {
const stats = await webpack();
const stats = await webpack({ fixture });
const instrumentedSource = stats.compilation.assets['main.js'].source();
expect(instrumentedSource).toMatchSnapshot();
});
Expand All @@ -11,8 +15,12 @@ test('sourcemap files on by default', async () => {
extend: {
devtool: 'source-map',
},
fixture,
});
const sourceMap = stats.compilation.assets['main.js.map'].source();
const originalSource = fs.readFileSync(path.join(__dirname, 'fixtures', fixture), 'utf8');
const [sourceMapSource] = JSON.parse(sourceMap).sourcesContent;
expect(sourceMapSource).toEqual(originalSource);
expect(sourceMap).toMatchSnapshot();
expect(stats.compilation.errors).toMatchSnapshot('errors');
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
Expand All @@ -26,8 +34,14 @@ test('disabled sourcemaps', async () => {
options: {
produceSourceMap: false,
},
fixture,
});
const sourceMap = stats.compilation.assets['main.js.map'].source();
const instrumentedSource = stats.compilation.assets['main.js'].source();
const [originalSource] = JSON.parse(sourceMap).sourcesContent;
// Webpack adds newline after "use strict" that we need to remove to compare.
const trimmedSourceWithoutFooter = originalSource.slice(16, originalSource.length);
expect(instrumentedSource).toContain(trimmedSourceWithoutFooter);
expect(sourceMap).toMatchSnapshot();
expect(stats.compilation.errors).toMatchSnapshot('errors');
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
Expand Down
9 changes: 4 additions & 5 deletions test/utils/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const loader = require.resolve('./loader');

export default function ({ fixture = 'basic.js', options, extend = {} } = {}) {
const config = {
mode: 'development',
entry: path.join(__dirname, '..', 'fixtures', fixture),
output: {
path: path.join(__dirname, '..', 'fixtures', 'dist'),
Expand All @@ -22,11 +23,9 @@ export default function ({ fixture = 'basic.js', options, extend = {} } = {}) {
}],
},
...extend,
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
}),
],
optimization: {
runtimeChunk: 'single',
},
};

return new Promise((resolve, reject) => {
Expand Down