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

Commit d33103f

Browse files
authored
feat: force sourceMap: true on all typescript projects (#92)
1 parent 834af3c commit d33103f

File tree

10 files changed

+292
-10
lines changed

10 files changed

+292
-10
lines changed

.mocharc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"./test/_test-output",
44
"node_modules"
55
],
6+
"require": "ts-node/register",
67
"exit": true
78
}

examples/use-ts-loader/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"sourceMap": true,
34
"outDir": "./dist/",
45
"noImplicitAny": true,
56
"module": "commonJS",

import-sorter.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"generalConfiguration": {
3+
"exclude": [
4+
"index.ts"
5+
]
6+
}
7+
}

index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { overrideSourceMaps } from './lib/typescript-overrides'
2+
13
import * as Promise from 'bluebird'
24
import * as events from 'events'
35
import * as _ from 'lodash'
@@ -95,7 +97,7 @@ interface WebpackPreprocessor extends WebpackPreprocessorFn {
9597
*/
9698
// @ts-ignore
9799
const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): FilePreprocessor => {
98-
debug('user options:', options)
100+
debug('user options: %o', options)
99101

100102
// we return function that accepts the arguments provided by
101103
// the event 'file:preprocessor'
@@ -151,11 +153,21 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F
151153
},
152154
})
153155
.tap((opts) => {
154-
if (opts.devtool !== false) {
155-
debug('setting devtool to inline-source-map')
156+
if (opts.devtool === false) {
157+
// disable any overrides if
158+
// we've explictly turned off sourcemaps
159+
overrideSourceMaps(false)
156160

157-
opts.devtool = 'inline-source-map'
161+
return
158162
}
163+
164+
debug('setting devtool to inline-source-map')
165+
166+
opts.devtool = 'inline-source-map'
167+
168+
// override typescript to always generate
169+
// proper source maps
170+
overrideSourceMaps(true)
159171
})
160172
.value() as any
161173

lib/typescript-overrides.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const debug = require('debug')('cypress:webpack')
2+
3+
let sourceMapOverride: null | boolean = null
4+
5+
export const tryRequireTypescript = () => {
6+
try {
7+
// reset each time this is called
8+
sourceMapOverride = null
9+
10+
const typescript = require('typescript')
11+
12+
debug('typescript found, overriding typescript.createProgram()')
13+
14+
const { createProgram } = typescript
15+
16+
typescript.createProgram = (...args: [any]) => {
17+
const [programOptions] = args
18+
const { options } = programOptions
19+
20+
debug('typescript unmodified createProgram options %o', options)
21+
22+
// if sourceMap has been set then apply
23+
// these overrides to force typescript
24+
// to generate the right sourcemaps
25+
if (sourceMapOverride !== null) {
26+
options.sourceMap = sourceMapOverride
27+
28+
delete options.inlineSources
29+
delete options.inlineSourceMap
30+
31+
debug('typescript modified createProgram options %o', options)
32+
}
33+
34+
return createProgram.apply(typescript, args)
35+
}
36+
37+
return typescript
38+
} catch (err) {
39+
debug('typescript not found')
40+
41+
// for testing
42+
return err
43+
}
44+
}
45+
46+
export const overrideSourceMaps = (val: boolean) => {
47+
sourceMapOverride = val
48+
}
49+
50+
export const getSourceMapOverride = () => {
51+
return sourceMapOverride
52+
}
53+
54+
tryRequireTypescript()

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "dist",
77
"scripts": {
88
"ban": "ban",
9-
"build": "tsc",
9+
"build": "rm -rf dist && tsc",
1010
"deps": "deps-ok && dependency-check --no-dev .",
1111
"license": "license-checker --production --onlyunknown --csv",
1212
"lint": "eslint --ext .js,.jsx,.json,.ts,.tsx .",
@@ -17,9 +17,9 @@
1717
"pretest": "yarn lint && yarn build",
1818
"test": "yarn test-unit && yarn test-e2e",
1919
"test-debug": "node --inspect --debug-brk ./node_modules/.bin/_mocha",
20-
"test-e2e": "mocha test/e2e/*.spec.js",
21-
"test-unit": "mocha test/unit/*.spec.js",
22-
"test-watch": "yarn test-unit & chokidar '*.js' 'test/unit/*.js' -c 'yarn test-unit'",
20+
"test-e2e": "mocha test/e2e/*.spec.*",
21+
"test-unit": "mocha test/unit/*.spec.*",
22+
"test-watch": "yarn test-unit & chokidar '**/*.(js|ts)' 'test/unit/*.(js|ts)' -c 'yarn test-unit'",
2323
"types": "tsc --noEmit"
2424
},
2525
"husky": {
@@ -61,6 +61,7 @@
6161
"mocha": "^7.1.0",
6262
"mockery": "2.1.0",
6363
"prettier-eslint-cli": "4.4.0",
64+
"proxyquire": "2.1.3",
6465
"react": "16.13.1",
6566
"react-dom": "16.13.1",
6667
"react-scripts": "3.2",
@@ -69,6 +70,7 @@
6970
"sinon-chai": "^3.5.0",
7071
"snap-shot-it": "7.9.2",
7172
"start-server-and-test": "1.10.11",
73+
"ts-node": "8.10.1",
7274
"typescript": "3.8.3",
7375
"webpack": "^4.18.1"
7476
},

test/unit/dist.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { expect } = require('chai')
2+
const preprocessor = require('../../dist/index')
3+
4+
describe('tyepscript ./dist output', () => {
5+
it('builds dist correctly', () => {
6+
expect(preprocessor).to.be.a('function')
7+
expect(preprocessor).to.have.property('defaultOptions')
8+
})
9+
})

test/unit/index.spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ mockery.enable({
1616

1717
mockery.registerMock('webpack', webpack)
1818

19-
const preprocessor = require('../../dist/index')
19+
const preprocessor = require('../../index')
20+
const { getSourceMapOverride } = require('../../lib/typescript-overrides')
2021

2122
describe('webpack preprocessor', function () {
2223
beforeEach(function () {
@@ -155,6 +156,8 @@ describe('webpack preprocessor', function () {
155156
expect(webpack).to.be.calledWithMatch({
156157
devtool: 'inline-source-map',
157158
})
159+
160+
expect(getSourceMapOverride()).to.be.true
158161
})
159162
})
160163

@@ -165,6 +168,8 @@ describe('webpack preprocessor', function () {
165168
expect(webpack).to.be.calledWithMatch({
166169
devtool: false,
167170
})
171+
172+
expect(getSourceMapOverride()).to.be.false
168173
})
169174
})
170175

@@ -175,6 +180,8 @@ describe('webpack preprocessor', function () {
175180
expect(webpack).to.be.calledWithMatch({
176181
devtool: 'inline-source-map',
177182
})
183+
184+
expect(getSourceMapOverride()).to.be.true
178185
})
179186
})
180187
})
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
const { expect } = require('chai')
2+
const sinon = require('sinon')
3+
const proxyquire = require('proxyquire').noPreserveCache()
4+
5+
type Typescript = {
6+
createProgram: sinon.SinonStub
7+
}
8+
9+
let typescript: Typescript
10+
let createProgram: Typescript['createProgram']
11+
12+
import '../../lib/typescript-overrides'
13+
14+
describe('./lib/typescript-overrides', () => {
15+
beforeEach(() => {
16+
createProgram = sinon.stub()
17+
typescript = {
18+
createProgram,
19+
}
20+
})
21+
22+
context('.getSourceMapOverride', () => {
23+
it('is null by default', () => {
24+
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', {
25+
typescript,
26+
})
27+
28+
expect(typescriptOverrides.getSourceMapOverride()).to.be.null
29+
})
30+
})
31+
32+
context('.tryRequireTypescript', () => {
33+
it('gracefully returns error when typescript cannot be required', () => {
34+
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', {
35+
typescript: null,
36+
})
37+
38+
const err = typescriptOverrides.tryRequireTypescript()
39+
40+
expect(err).to.be.instanceOf(Error)
41+
expect(err.message).to.eq(`Cannot find module 'typescript'`)
42+
})
43+
})
44+
45+
context('.overrideSourceMaps', () => {
46+
it('it sets sourceMap: true', () => {
47+
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', {
48+
typescript,
49+
})
50+
51+
typescriptOverrides.overrideSourceMaps(true)
52+
53+
expect(typescriptOverrides.getSourceMapOverride()).to.be.true
54+
55+
typescript.createProgram({
56+
options: {
57+
sourceMap: false,
58+
inlineSources: true,
59+
inlineSourceMap: true,
60+
},
61+
})
62+
63+
expect(createProgram).to.be.calledOn(typescript)
64+
expect(createProgram).to.be.calledWith({
65+
options: {
66+
sourceMap: true,
67+
},
68+
})
69+
})
70+
71+
it('it sets sourceMap: false', () => {
72+
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', {
73+
typescript,
74+
})
75+
76+
typescriptOverrides.overrideSourceMaps(false)
77+
78+
expect(typescriptOverrides.getSourceMapOverride()).to.be.false
79+
80+
typescript.createProgram({
81+
options: {
82+
sourceMap: true,
83+
inlineSources: true,
84+
inlineSourceMap: true,
85+
},
86+
})
87+
88+
expect(createProgram).to.be.calledOn(typescript)
89+
expect(createProgram).to.be.calledWith({
90+
options: {
91+
sourceMap: false,
92+
},
93+
})
94+
})
95+
96+
it('does not override sourcemaps', () => {
97+
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', {
98+
typescript,
99+
})
100+
101+
expect(typescriptOverrides.getSourceMapOverride()).to.be.null
102+
103+
typescript.createProgram({
104+
options: {
105+
sourceMap: true,
106+
inlineSources: true,
107+
inlineSourceMap: true,
108+
},
109+
})
110+
111+
expect(createProgram).to.be.calledOn(typescript)
112+
expect(createProgram).to.be.calledWith({
113+
options: {
114+
sourceMap: true,
115+
inlineSources: true,
116+
inlineSourceMap: true,
117+
},
118+
})
119+
})
120+
})
121+
})

0 commit comments

Comments
 (0)