-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove espower typescript (#160)
* fix: espower typescript * fix: add extensions
- Loading branch information
Showing
5 changed files
with
72 additions
and
17 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
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,41 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const espowerSource = require('espower-source'); | ||
const minimatch = require('minimatch'); | ||
const sourceMapSupport = require('source-map-support'); | ||
const sourceCache = {}; | ||
const cwd = process.cwd(); | ||
|
||
espowerTypeScript({ | ||
pattern: path.resolve(cwd, 'test/**/*.@(ts|tsx)'), | ||
extensions: [ 'ts', 'tsx' ], | ||
}); | ||
|
||
function espowerTypeScript(options) { | ||
// install source-map-support again to correct the source-map | ||
sourceMapSupport.install({ | ||
environment: 'node', | ||
retrieveFile: p => sourceCache[p], | ||
}); | ||
|
||
options.extensions.forEach(ext => { | ||
espowerTsRegister(`.${ext}`, options); | ||
}); | ||
} | ||
|
||
function espowerTsRegister(ext, options) { | ||
const originalExtension = require.extensions[ext]; | ||
require.extensions[ext] = (module, filepath) => { | ||
if (!minimatch(filepath, options.pattern)) { | ||
return originalExtension(module, filepath); | ||
} | ||
const originalCompile = module._compile; | ||
module._compile = function(code, filepath) { | ||
const newSource = espowerSource(code, filepath, options); | ||
sourceCache[filepath] = newSource; | ||
return originalCompile.call(this, newSource, filepath); | ||
}; | ||
return originalExtension(module, filepath); | ||
}; | ||
} |
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
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