This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: force sourceMap: true on all typescript projects (#92)
- Loading branch information
1 parent
834af3c
commit d33103f
Showing
10 changed files
with
292 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
"./test/_test-output", | ||
"node_modules" | ||
], | ||
"require": "ts-node/register", | ||
"exit": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"sourceMap": true, | ||
"outDir": "./dist/", | ||
"noImplicitAny": true, | ||
"module": "commonJS", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"generalConfiguration": { | ||
"exclude": [ | ||
"index.ts" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const debug = require('debug')('cypress:webpack') | ||
|
||
let sourceMapOverride: null | boolean = null | ||
|
||
export const tryRequireTypescript = () => { | ||
try { | ||
// reset each time this is called | ||
sourceMapOverride = null | ||
|
||
const typescript = require('typescript') | ||
|
||
debug('typescript found, overriding typescript.createProgram()') | ||
|
||
const { createProgram } = typescript | ||
|
||
typescript.createProgram = (...args: [any]) => { | ||
const [programOptions] = args | ||
const { options } = programOptions | ||
|
||
debug('typescript unmodified createProgram options %o', options) | ||
|
||
// if sourceMap has been set then apply | ||
// these overrides to force typescript | ||
// to generate the right sourcemaps | ||
if (sourceMapOverride !== null) { | ||
options.sourceMap = sourceMapOverride | ||
|
||
delete options.inlineSources | ||
delete options.inlineSourceMap | ||
|
||
debug('typescript modified createProgram options %o', options) | ||
} | ||
|
||
return createProgram.apply(typescript, args) | ||
} | ||
|
||
return typescript | ||
} catch (err) { | ||
debug('typescript not found') | ||
|
||
// for testing | ||
return err | ||
} | ||
} | ||
|
||
export const overrideSourceMaps = (val: boolean) => { | ||
sourceMapOverride = val | ||
} | ||
|
||
export const getSourceMapOverride = () => { | ||
return sourceMapOverride | ||
} | ||
|
||
tryRequireTypescript() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const { expect } = require('chai') | ||
const preprocessor = require('../../dist/index') | ||
|
||
describe('tyepscript ./dist output', () => { | ||
it('builds dist correctly', () => { | ||
expect(preprocessor).to.be.a('function') | ||
expect(preprocessor).to.have.property('defaultOptions') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const proxyquire = require('proxyquire').noPreserveCache() | ||
|
||
type Typescript = { | ||
createProgram: sinon.SinonStub | ||
} | ||
|
||
let typescript: Typescript | ||
let createProgram: Typescript['createProgram'] | ||
|
||
import '../../lib/typescript-overrides' | ||
|
||
describe('./lib/typescript-overrides', () => { | ||
beforeEach(() => { | ||
createProgram = sinon.stub() | ||
typescript = { | ||
createProgram, | ||
} | ||
}) | ||
|
||
context('.getSourceMapOverride', () => { | ||
it('is null by default', () => { | ||
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', { | ||
typescript, | ||
}) | ||
|
||
expect(typescriptOverrides.getSourceMapOverride()).to.be.null | ||
}) | ||
}) | ||
|
||
context('.tryRequireTypescript', () => { | ||
it('gracefully returns error when typescript cannot be required', () => { | ||
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', { | ||
typescript: null, | ||
}) | ||
|
||
const err = typescriptOverrides.tryRequireTypescript() | ||
|
||
expect(err).to.be.instanceOf(Error) | ||
expect(err.message).to.eq(`Cannot find module 'typescript'`) | ||
}) | ||
}) | ||
|
||
context('.overrideSourceMaps', () => { | ||
it('it sets sourceMap: true', () => { | ||
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', { | ||
typescript, | ||
}) | ||
|
||
typescriptOverrides.overrideSourceMaps(true) | ||
|
||
expect(typescriptOverrides.getSourceMapOverride()).to.be.true | ||
|
||
typescript.createProgram({ | ||
options: { | ||
sourceMap: false, | ||
inlineSources: true, | ||
inlineSourceMap: true, | ||
}, | ||
}) | ||
|
||
expect(createProgram).to.be.calledOn(typescript) | ||
expect(createProgram).to.be.calledWith({ | ||
options: { | ||
sourceMap: true, | ||
}, | ||
}) | ||
}) | ||
|
||
it('it sets sourceMap: false', () => { | ||
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', { | ||
typescript, | ||
}) | ||
|
||
typescriptOverrides.overrideSourceMaps(false) | ||
|
||
expect(typescriptOverrides.getSourceMapOverride()).to.be.false | ||
|
||
typescript.createProgram({ | ||
options: { | ||
sourceMap: true, | ||
inlineSources: true, | ||
inlineSourceMap: true, | ||
}, | ||
}) | ||
|
||
expect(createProgram).to.be.calledOn(typescript) | ||
expect(createProgram).to.be.calledWith({ | ||
options: { | ||
sourceMap: false, | ||
}, | ||
}) | ||
}) | ||
|
||
it('does not override sourcemaps', () => { | ||
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', { | ||
typescript, | ||
}) | ||
|
||
expect(typescriptOverrides.getSourceMapOverride()).to.be.null | ||
|
||
typescript.createProgram({ | ||
options: { | ||
sourceMap: true, | ||
inlineSources: true, | ||
inlineSourceMap: true, | ||
}, | ||
}) | ||
|
||
expect(createProgram).to.be.calledOn(typescript) | ||
expect(createProgram).to.be.calledWith({ | ||
options: { | ||
sourceMap: true, | ||
inlineSources: true, | ||
inlineSourceMap: true, | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.