Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 39edd2e

Browse files
committed
fix(webpack): source maps link to original src for ide debugging
source maps link to original src for ide debugging
1 parent 81f1d75 commit 39edd2e

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ npm run build --rollup ./config/rollup.config.js
108108
| Config Values | package.json Config | Cmd-line Flag | Defaults | Details |
109109
|-----------------|---------------------|---------------|-----------------|----------------|
110110
| bundler | `ionic_bundler` | `--bundler` | `webpack` | Chooses which bundler to use: `webpack` or `rollup` |
111-
| source map type | `ionic_source_map` | `--sourceMap` | `eval` | Chooses the webpack `devtool` option. We recommend `eval` or `source-map` |
111+
| source map type | `ionic_source_map` | `--sourceMap` | `eval` | Chooses the webpack `devtool` option. We only support `eval` or `source-map` for now |
112112
| root directory | `ionic_root_dir` | `--rootDir` | `process.cwd()` | The directory path of the Ionic app |
113113
| tmp directory | `ionic_tmp_dir` | `--tmpDir` | `.tmp` | A temporary directory for codegen'd files using the Angular `ngc` AoT compiler |
114114
| src directory | `ionic_src_dir` | `--srcDir` | `src` | The directory holding the Ionic src code |
@@ -163,7 +163,7 @@ Example NPM Script:
163163
```
164164

165165
## Tips
166-
1. The Webpack `devtool` setting is driven by the `ionic_source_map` variable. It defaults to `eval` for fast builds, but can provide the original source map by changing the value to `source-map`.
166+
1. The Webpack `devtool` setting is driven by the `ionic_source_map` variable. It defaults to `eval` for fast builds, but can provide the original source map by changing the value to `source-map`. There are additional values that Webpack supports, but we only support `eval` and `source-maps` for now.
167167

168168

169169
## The Stack

config/webpack.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ module.exports = {
5656
entry: getEntryPoint(),
5757
output: {
5858
path: '{{BUILD}}',
59-
filename: 'main.js'
59+
filename: 'main.js',
60+
devtoolModuleFilenameTemplate: ionicWebpackFactory.getSourceMapperFunction(),
6061
},
6162
devtool: getDevtool(),
6263

src/webpack/ionic-webpack-factory.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { IonicEnvironmentPlugin } from './ionic-environment-plugin';
2+
import { provideCorrectSourcePath } from './source-mapper';
23
import { getContext } from '../util/helpers';
34

45

56
export function getIonicEnvironmentPlugin() {
67
const context = getContext();
78
return new IonicEnvironmentPlugin(context.fileCache);
89
}
10+
11+
export function getSourceMapperFunction(): Function {
12+
return provideCorrectSourcePath;
13+
}

src/webpack/source-mapper.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { BuildContext } from '../util/interfaces';
2+
import { getContext} from '../util/helpers';
3+
import { normalize, resolve, sep } from 'path';
4+
5+
export function provideCorrectSourcePath(webpackObj: any) {
6+
const context = getContext();
7+
return provideCorrectSourcePathInternal(webpackObj, context);
8+
}
9+
10+
function provideCorrectSourcePathInternal(webpackObj: any, context: BuildContext) {
11+
const webpackResourcePath = webpackObj.resourcePath;
12+
const noTilde = webpackResourcePath.replace(/~/g, 'node_modules');
13+
const absolutePath = resolve(normalize(noTilde));
14+
if (process.env.IONIC_SOURCE_MAP === 'eval') {
15+
// add another path.sep to the front to account for weird webpack behavior
16+
return sep + absolutePath;
17+
}
18+
return absolutePath;
19+
}

src/webpack/typescript-sourcemap-loader-memory.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { normalize, resolve } from 'path';
12
import { changeExtension, getContext} from '../util/helpers';
23

34
module.exports = function typescriptSourcemapLoaderMemory(source: string, map: any) {
45
this.cacheable();
56
var callback = this.async();
67
const context = getContext();
78

9+
const absolutePath = resolve(normalize(this.resourcePath));
10+
811
const javascriptPath = changeExtension(this.resourcePath, '.js');
912
const javascriptFile = context.fileCache.get(javascriptPath);
1013
const sourceMapPath = javascriptPath + '.map';
@@ -13,6 +16,7 @@ module.exports = function typescriptSourcemapLoaderMemory(source: string, map: a
1316
let sourceMapObject = map;
1417
if (sourceMapFile) {
1518
sourceMapObject = JSON.parse(sourceMapFile.content);
19+
sourceMapObject.sources = [absolutePath];
1620
if (!sourceMapObject.sourcesContent || sourceMapObject.sourcesContent.length === 0) {
1721
sourceMapObject.sourcesContent = [source];
1822
}

0 commit comments

Comments
 (0)