Skip to content

fix: handle webpack source maps when library is set #264

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

Merged
merged 1 commit into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@
"@types/lodash": "4.14.121",
"@types/mocha": "5.2.6",
"@types/node": "6.0.46",
"@types/proxyquire": "1.3.28",
"@types/semver": "5.5.0",
"@types/sinon": "7.0.13",
"@types/universal-analytics": "0.4.1",
"cpx": "1.5.0",
"mocha": "5.2.0",
"sinon": "5.1.1",
"tslint": "5.10.0",
"proxyquire": "2.1.3",
"sinon": "7.4.2",
"tslint": "5.19.0",
"tslint-eslint-rules": "5.4.0",
"typescript": "2.6.2",
"typescript": "3.6.2",
"vsce": "1.57.1",
"vscode": "1.1.30",
"vscode-debugprotocol": "1.34.0"
Expand Down
27 changes: 23 additions & 4 deletions src/debug-adapter/nativeScriptDebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ISetBreakpointsResponseBody,
ITelemetryPropertyCollector,
} from 'vscode-chrome-debug-core';
import { Event, TerminatedEvent } from 'vscode-debugadapter';
import { Event, logger, TerminatedEvent } from 'vscode-debugadapter';
import { DebugProtocol } from 'vscode-debugprotocol';
import * as extProtocol from '../common/extensionProtocol';
import { NativeScriptSourceMapTransformer } from './nativeScriptSourceMapTransformer';
Expand Down Expand Up @@ -179,13 +179,32 @@ export class NativeScriptDebugAdapter extends ChromeDebugAdapter {
args.sourceMapPathOverrides = {};
}

if (!args.sourceMapPathOverrides['webpack:///*']) {
const appDirPath = this.getAppDirPath(args.webRoot) || 'app';
const fullAppDirPath = join(args.webRoot, appDirPath);
const appDirPath = this.getAppDirPath(args.webRoot) || 'app';
const fullAppDirPath = join(args.webRoot, appDirPath);

if (!args.sourceMapPathOverrides['webpack:///*']) {
args.sourceMapPathOverrides['webpack:///*'] = `${fullAppDirPath}/*`;
}

const webpackConfigFile = join(`./${args.webRoot}`, 'webpack.config.js');

if (existsSync(webpackConfigFile)) {
try {
const webpackConfig = require(webpackConfigFile);
const platform = args.platform && args.platform.toLowerCase();
const config = webpackConfig({ [`${platform}`]: platform });

if (config && config.output && config.output.library) {
const sourceMapPathOverrideWithLib = `webpack://${config.output.library}/*`;

args.sourceMapPathOverrides[sourceMapPathOverrideWithLib] = args.sourceMapPathOverrides[sourceMapPathOverrideWithLib] ||
`${fullAppDirPath}/*`;
}
} catch (err) {
logger.warn(`Error when trying to require webpack.config.js file from path '${webpackConfigFile}'. Error is: ${err}`);
}
}

return args;
}

Expand Down
67 changes: 62 additions & 5 deletions src/tests/nativeScriptDebugAdapter.tests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import * as fs from 'fs';
import * as _ from 'lodash';
import { join } from 'path';
import * as proxyquire from 'proxyquire';
import * as sinon from 'sinon';
import { ChromeDebugAdapter } from 'vscode-chrome-debug-core';
import { Event } from 'vscode-debugadapter';
import * as extProtocol from '../common/extensionProtocol';
import { NativeScriptDebugAdapter } from '../debug-adapter/nativeScriptDebugAdapter';
const appRoot = 'appRootMock';
const webpackConfigFunctionStub = sinon.stub();

proxyquire.noCallThru();
const nativeScriptDebugAdapterLib = proxyquire('../debug-adapter/nativeScriptDebugAdapter', {
[join(appRoot, 'webpack.config.js')]: webpackConfigFunctionStub,
});

const examplePort = 456;

Expand All @@ -17,7 +26,7 @@ const customMessagesResponses = {
};

const defaultArgsMock: any = {
appRoot: 'appRootMock',
appRoot,
diagnosticLogging: true,
platform: 'android',
request: 'attach',
Expand Down Expand Up @@ -67,7 +76,7 @@ describe('NativeScriptDebugAdapter', () => {
setTransformOptions: () => undefined,
};

nativeScriptDebugAdapter = new NativeScriptDebugAdapter({
nativeScriptDebugAdapter = new nativeScriptDebugAdapterLib.NativeScriptDebugAdapter({
chromeConnection: mockConstructor(chromeConnectionMock),
pathTransformer: mockConstructor(pathTransformerMock),
sourceMapTransformer: mockConstructor(sourceMapTransformer),
Expand All @@ -83,7 +92,11 @@ describe('NativeScriptDebugAdapter', () => {

platforms.forEach((platform) => {
launchMethods.forEach((method) => {
const argsMock = _.merge({}, defaultArgsMock, { platform, request: method });
let argsMock: any;

beforeEach(() => {
argsMock = _.merge({}, defaultArgsMock, { platform, request: method });
});

it(`${method} for ${platform} should raise debug start event`, async () => {
const spy = sinon.spy(chromeSessionMock, 'sendEvent');
Expand Down Expand Up @@ -121,7 +134,51 @@ describe('NativeScriptDebugAdapter', () => {

sinon.assert.calledWith(spy, sinon.match({
trace: true,
webRoot: 'appRootMock',
webRoot: appRoot,
}));
});

it(`${method} for ${platform} should add sourceMapPathOverrides data`, async () => {
const spy = sinon.spy(ChromeDebugAdapter.prototype, 'attach');
const existsSyncStub = sinon.stub(fs, 'existsSync');

existsSyncStub.returns(true);
webpackConfigFunctionStub
.withArgs({ [platform]: platform })
.returns({ output: { library: 'myLib' } });

await nativeScriptDebugAdapter[method](argsMock);

existsSyncStub.restore();
sinon.assert.calledWith(spy, sinon.match({
sourceMapPathOverrides: {
'webpack:///*': `${join(appRoot, 'app')}/*`,
'webpack://myLib/*': `${join(appRoot, 'app')}/*`,
},
trace: true,
webRoot: appRoot,
}));

});

it(`${method} for ${platform} should not fail when unable to require webpack.config.js`, async () => {
const spy = sinon.spy(ChromeDebugAdapter.prototype, 'attach');
const existsSyncStub = sinon.stub(fs, 'existsSync');

existsSyncStub.returns(true);
webpackConfigFunctionStub
.withArgs({ [platform]: platform })
.throws(new Error('test'));

await nativeScriptDebugAdapter[method](argsMock);

existsSyncStub.restore();
sinon.assert.calledWith(spy, sinon.match({
sourceMapPathOverrides: {
'webpack:///*': `${join(appRoot, 'app')}/*`,
},
trace: true,
webRoot: appRoot,
}));
});

Expand Down
2 changes: 1 addition & 1 deletion src/tests/nativeScriptTargetDiscovery.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('NativeScriptTargetDiscovery', () => {
});

it(`getTargets calls getTarget`, async () => {
const testTarget = {
const testTarget: any = {
devtoolsFrontendUrl: 'url',
webSocketDebuggerUrl: 'socket',
};
Expand Down
1 change: 0 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"check-type"
],
"newline-before-return": true,
"no-unused-variable": true,
"no-duplicate-variable": true,
"no-unused-expression": [
true,
Expand Down