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

fix(index): correct loader sourcemaps usage #67

Merged
merged 3 commits into from Jul 12, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions appveyor.yml
Expand Up @@ -19,6 +19,7 @@ matrix:
fast_finish: true
install:
- ps: Install-Product node $env:nodejs_version x64
- cmd: npm i -g npm@latest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fixing the current falky builds for branches ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to fix appveyor on node 4, which uses npm 2.x which apparently isn't compatible with babel

- npm install
before_test:
- cmd: npm install webpack@%webpack_version%
Expand Down
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
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
@@ -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);
}
78 changes: 7 additions & 71 deletions test/__snapshots__/index.test.js.snap

Large diffs are not rendered by default.

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

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

};
4 changes: 0 additions & 4 deletions test/helpers.js

This file was deleted.

9 changes: 4 additions & 5 deletions test/index.test.js
@@ -1,16 +1,15 @@
import webpack from './webpack';
import { stripLocalPaths } from './helpers';
import webpack from './utils/webpack';

test('instrument code', async () => {
const stats = await webpack();
const instrumentedSource = stripLocalPaths(stats.compilation.assets['main.js'].source());
const instrumentedSource = stats.compilation.assets['main.js'].source();
expect(instrumentedSource).toMatchSnapshot();
});

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 @@ -22,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
18 changes: 18 additions & 0 deletions test/utils/loader.js
@@ -0,0 +1,18 @@
import path from 'path';
import loader from '../../src/cjs';

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 = 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);
};
14 changes: 11 additions & 3 deletions test/webpack.js → test/utils/webpack.js
Expand Up @@ -2,23 +2,31 @@ import path from 'path';
import webpack from 'webpack';
import MemoryFileSystem from 'memory-fs';

const loader = require.resolve('../src/cjs.js');
const loader = require.resolve('./loader');

export default function ({ fixture = 'basic.js', options, extend = {} } = {}) {
const config = {
entry: path.join(__dirname, 'fixtures', fixture),
entry: path.join(__dirname, '..', 'fixtures', fixture),
output: {
path: path.join(__dirname, 'fixtures', 'dist'),
path: path.join(__dirname, '..', 'fixtures', 'dist'),
},
module: {
rules: [{
test: /\.js$/,
loader: 'babel-loader',
}, {
test: /\.js$/,
loader,
enforce: 'post',
options,
}],
},
...extend,
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
}),
],
};

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