Comparing version 1.2.5 to 1.3.0
@@ -7,2 +7,9 @@ # Changelog | ||
## [1.3.0] (07-26-2020) | ||
### Added | ||
- Added ability to specify tsconfig.json file for transformer (`tsConfig` option) | ||
Note: This opens up the door to specify compilerOptions, which can be useful | ||
- Added path mapping support (requires [tsconfig-paths](https://npmjs.com/tsconfig-paths)) | ||
## [1.2.2] (05-23-2020) | ||
@@ -68,2 +75,3 @@ | ||
[1.3.0]: https://github.com/nonara/ts-patch/compare/v1.2.2...v1.3.0 | ||
[1.2.2]: https://github.com/nonara/ts-patch/compare/v1.1.0...v1.2.2 | ||
@@ -70,0 +78,0 @@ [1.2.0]: https://github.com/nonara/ts-patch/compare/v1.1.0...v1.2.0 |
{ | ||
"name": "ts-patch", | ||
"version": "1.2.5", | ||
"version": "1.3.0", | ||
"description": "Patch typescript to support custom transformers in tsconfig.json", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -14,2 +14,6 @@ import { Bundle, CompilerHost, CompilerOptions, CustomTransformers, Diagnostic, LanguageService, Program, SourceFile, TransformationContext, Transformer, TransformerFactory, TypeChecker } from 'typescript'; | ||
/** | ||
* tsconfig.json file (for transformer) | ||
*/ | ||
tsConfig?: string; | ||
/** | ||
* The optional name of the exported transform plugin in the transform module. | ||
@@ -16,0 +20,0 @@ */ |
@@ -5,4 +5,4 @@ [![npm version](https://badge.fury.io/js/ts-patch.svg)](https://badge.fury.io/js/ts-patch) | ||
ts-patch | ||
======================================= | ||
# TS Patch | ||
Directly patch typescript installation to allow custom transformers (plugins). | ||
@@ -49,3 +49,3 @@ | ||
Add transformers to `compilerOptions` in `plugin` array. | ||
Add transformers to `compilerOptions` in `plugins` array. | ||
@@ -76,3 +76,3 @@ **Examples** | ||
// Program Transformer -> Only has one signature - notice no type specified, because it does not apply | ||
{ "transform": "transformer-module4", "transformProgram": true } | ||
{ "transform": "transformer-module5", "transformProgram": true } | ||
] | ||
@@ -89,2 +89,3 @@ } | ||
| import | string | Name of exported transformer function _(defaults to `default` export)_ | | ||
| tsConfig | string | tsconfig.json file _for transformer_ (allows specifying compileOptions, support path mapping, etc) | | ||
| after | boolean | Apply transformer after stock TS transformers. | | ||
@@ -191,3 +192,3 @@ | afterDeclarations | boolean | Apply transformer to declaration (*.d.ts) files _(TypeScript 2.9+)_. | | ||
There are some cases where a transformer isn't enough. Several examples of are if you want to: | ||
There are some cases where a transformer isn't enough. Several examples are if you want to: | ||
@@ -194,0 +195,0 @@ - TypeCheck code after it's been transformed |
@@ -15,2 +15,6 @@ declare namespace ts { | ||
/** | ||
* tsconfig.json file (for transformer) | ||
*/ | ||
tsConfig?: string; | ||
/** | ||
* The optional name of the exported transform plugin in the transform module. | ||
@@ -89,2 +93,3 @@ */ | ||
private resolveBaseDir; | ||
currentProject?: string; | ||
constructor(configs: PluginConfig[], resolveBaseDir?: string); | ||
@@ -91,0 +96,0 @@ mergeTransformers(into: TransformerList, source: CustomTransformers | TransformerBasePlugin): this; |
@@ -92,14 +92,14 @@ (function () { | ||
var _rollupPluginShim1 = require('fs'); | ||
var fs = require('fs'); | ||
var _rollupPluginShim1$1 = /*#__PURE__*/Object.freeze({ | ||
var _rollupPluginShim1 = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
'default': _rollupPluginShim1 | ||
'default': fs | ||
}); | ||
var _rollupPluginShim2 = require('path'); | ||
var path = require('path'); | ||
var _rollupPluginShim2$1 = /*#__PURE__*/Object.freeze({ | ||
var _rollupPluginShim2 = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
'default': _rollupPluginShim2 | ||
'default': path | ||
}); | ||
@@ -221,5 +221,5 @@ | ||
var path = getCjsExportFromNamespace(_rollupPluginShim2$1); | ||
var path$1 = getCjsExportFromNamespace(_rollupPluginShim2); | ||
var parse = path.parse || pathParse; | ||
var parse = path$1.parse || pathParse; | ||
@@ -243,3 +243,3 @@ var getNodeModulesDirs = function getNodeModulesDirs(absoluteStart, modules) { | ||
return dirs.concat(modules.map(function (moduleDir) { | ||
return path.resolve(prefix, aPath, moduleDir); | ||
return path$1.resolve(prefix, aPath, moduleDir); | ||
})); | ||
@@ -440,8 +440,8 @@ }, []); | ||
var fs = getCjsExportFromNamespace(_rollupPluginShim1$1); | ||
var fs$1 = getCjsExportFromNamespace(_rollupPluginShim1); | ||
var realpathFS = fs.realpath && typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath; | ||
var realpathFS = fs$1.realpath && typeof fs$1.realpath.native === 'function' ? fs$1.realpath.native : fs$1.realpath; | ||
var defaultIsFile = function isFile(file, cb) { | ||
fs.stat(file, function (err, stat) { | ||
fs$1.stat(file, function (err, stat) { | ||
if (!err) { | ||
@@ -456,3 +456,3 @@ return cb(null, stat.isFile() || stat.isFIFO()); | ||
var defaultIsDir = function isDirectory(dir, cb) { | ||
fs.stat(dir, function (err, stat) { | ||
fs$1.stat(dir, function (err, stat) { | ||
if (!err) { | ||
@@ -484,3 +484,3 @@ return cb(null, stat.isDirectory()); | ||
for (var i = 0; i < dirs.length; i++) { | ||
dirs[i] = path.join(dirs[i], x); | ||
dirs[i] = path$1.join(dirs[i], x); | ||
} | ||
@@ -508,3 +508,3 @@ return dirs; | ||
var isDirectory = opts.isDirectory || defaultIsDir; | ||
var readFile = opts.readFile || fs.readFile; | ||
var readFile = opts.readFile || fs$1.readFile; | ||
var realpath = opts.realpath || defaultRealpath; | ||
@@ -514,3 +514,3 @@ var packageIterator = opts.packageIterator; | ||
var extensions = opts.extensions || ['.js']; | ||
var basedir = opts.basedir || path.dirname(caller()); | ||
var basedir = opts.basedir || path$1.dirname(caller()); | ||
var parent = opts.filename || basedir; | ||
@@ -521,3 +521,3 @@ | ||
// ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory | ||
var absoluteStart = path.resolve(basedir); | ||
var absoluteStart = path$1.resolve(basedir); | ||
@@ -537,3 +537,3 @@ maybeRealpath( | ||
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) { | ||
res = path.resolve(basedir, x); | ||
res = path$1.resolve(basedir, x); | ||
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/'; | ||
@@ -601,3 +601,3 @@ if ((/\/$/).test(x) && res === basedir) { | ||
if (pkg) onpkg(null, pkg); | ||
else loadpkg(path.dirname(file), onpkg); | ||
else loadpkg(path$1.dirname(file), onpkg); | ||
@@ -608,3 +608,3 @@ function onpkg(err, pkg_, dir) { | ||
if (dir && pkg && opts.pathFilter) { | ||
var rfile = path.relative(dir, file); | ||
var rfile = path$1.relative(dir, file); | ||
var rel = rfile.slice(0, rfile.length - exts[0].length); | ||
@@ -614,3 +614,3 @@ var r = opts.pathFilter(pkg, x, rel); | ||
[''].concat(extensions.slice()), | ||
path.resolve(dir, r), | ||
path$1.resolve(dir, r), | ||
pkg | ||
@@ -637,7 +637,7 @@ ); | ||
maybeRealpath(realpath, dir, opts, function (unwrapErr, pkgdir) { | ||
if (unwrapErr) return loadpkg(path.dirname(dir), cb); | ||
var pkgfile = path.join(pkgdir, 'package.json'); | ||
if (unwrapErr) return loadpkg(path$1.dirname(dir), cb); | ||
var pkgfile = path$1.join(pkgdir, 'package.json'); | ||
isFile(pkgfile, function (err, ex) { | ||
// on err, ex is false | ||
if (!ex) return loadpkg(path.dirname(dir), cb); | ||
if (!ex) return loadpkg(path$1.dirname(dir), cb); | ||
@@ -667,6 +667,6 @@ readFile(pkgfile, function (err, body) { | ||
if (unwrapErr) return cb(unwrapErr); | ||
var pkgfile = path.join(pkgdir, 'package.json'); | ||
var pkgfile = path$1.join(pkgdir, 'package.json'); | ||
isFile(pkgfile, function (err, ex) { | ||
if (err) return cb(err); | ||
if (!ex) return loadAsFile(path.join(x, 'index'), fpkg, cb); | ||
if (!ex) return loadAsFile(path$1.join(x, 'index'), fpkg, cb); | ||
@@ -692,12 +692,12 @@ readFile(pkgfile, function (err, body) { | ||
} | ||
loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) { | ||
loadAsFile(path$1.resolve(x, pkg.main), pkg, function (err, m, pkg) { | ||
if (err) return cb(err); | ||
if (m) return cb(null, m, pkg); | ||
if (!pkg) return loadAsFile(path.join(x, 'index'), pkg, cb); | ||
if (!pkg) return loadAsFile(path$1.join(x, 'index'), pkg, cb); | ||
var dir = path.resolve(x, pkg.main); | ||
var dir = path$1.resolve(x, pkg.main); | ||
loadAsDirectory(dir, pkg, function (err, n, pkg) { | ||
if (err) return cb(err); | ||
if (n) return cb(null, n, pkg); | ||
loadAsFile(path.join(x, 'index'), pkg, cb); | ||
loadAsFile(path$1.join(x, 'index'), pkg, cb); | ||
}); | ||
@@ -708,3 +708,3 @@ }); | ||
loadAsFile(path.join(x, '/index'), pkg, cb); | ||
loadAsFile(path$1.join(x, '/index'), pkg, cb); | ||
}); | ||
@@ -719,3 +719,3 @@ }); | ||
isDirectory(path.dirname(dir), isdir); | ||
isDirectory(path$1.dirname(dir), isdir); | ||
@@ -749,7 +749,7 @@ function isdir(err, isdir) { | ||
var realpathFS$1 = fs.realpathSync && typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync; | ||
var realpathFS$1 = fs$1.realpathSync && typeof fs$1.realpathSync.native === 'function' ? fs$1.realpathSync.native : fs$1.realpathSync; | ||
var defaultIsFile$1 = function isFile(file) { | ||
try { | ||
var stat = fs.statSync(file); | ||
var stat = fs$1.statSync(file); | ||
} catch (e) { | ||
@@ -764,3 +764,3 @@ if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false; | ||
try { | ||
var stat = fs.statSync(dir); | ||
var stat = fs$1.statSync(dir); | ||
} catch (e) { | ||
@@ -794,3 +794,3 @@ if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false; | ||
for (var i = 0; i < dirs.length; i++) { | ||
dirs[i] = path.join(dirs[i], x); | ||
dirs[i] = path$1.join(dirs[i], x); | ||
} | ||
@@ -807,3 +807,3 @@ return dirs; | ||
var isFile = opts.isFile || defaultIsFile$1; | ||
var readFileSync = opts.readFileSync || fs.readFileSync; | ||
var readFileSync = opts.readFileSync || fs$1.readFileSync; | ||
var isDirectory = opts.isDirectory || defaultIsDir$1; | ||
@@ -814,3 +814,3 @@ var realpathSync = opts.realpathSync || defaultRealpathSync; | ||
var extensions = opts.extensions || ['.js']; | ||
var basedir = opts.basedir || path.dirname(caller()); | ||
var basedir = opts.basedir || path$1.dirname(caller()); | ||
var parent = opts.filename || basedir; | ||
@@ -821,6 +821,6 @@ | ||
// ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory | ||
var absoluteStart = maybeRealpathSync(realpathSync, path.resolve(basedir), opts); | ||
var absoluteStart = maybeRealpathSync(realpathSync, path$1.resolve(basedir), opts); | ||
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) { | ||
var res = path.resolve(absoluteStart, x); | ||
var res = path$1.resolve(absoluteStart, x); | ||
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/'; | ||
@@ -841,9 +841,9 @@ var m = loadAsFileSync(res) || loadAsDirectorySync(res); | ||
function loadAsFileSync(x) { | ||
var pkg = loadpkg(path.dirname(x)); | ||
var pkg = loadpkg(path$1.dirname(x)); | ||
if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) { | ||
var rfile = path.relative(pkg.dir, x); | ||
var rfile = path$1.relative(pkg.dir, x); | ||
var r = opts.pathFilter(pkg.pkg, x, rfile); | ||
if (r) { | ||
x = path.resolve(pkg.dir, r); // eslint-disable-line no-param-reassign | ||
x = path$1.resolve(pkg.dir, r); // eslint-disable-line no-param-reassign | ||
} | ||
@@ -871,6 +871,6 @@ } | ||
var pkgfile = path.join(maybeRealpathSync(realpathSync, dir, opts), 'package.json'); | ||
var pkgfile = path$1.join(maybeRealpathSync(realpathSync, dir, opts), 'package.json'); | ||
if (!isFile(pkgfile)) { | ||
return loadpkg(path.dirname(dir)); | ||
return loadpkg(path$1.dirname(dir)); | ||
} | ||
@@ -893,3 +893,3 @@ | ||
function loadAsDirectorySync(x) { | ||
var pkgfile = path.join(maybeRealpathSync(realpathSync, x, opts), '/package.json'); | ||
var pkgfile = path$1.join(maybeRealpathSync(realpathSync, x, opts), '/package.json'); | ||
if (isFile(pkgfile)) { | ||
@@ -916,5 +916,5 @@ try { | ||
try { | ||
var m = loadAsFileSync(path.resolve(x, pkg.main)); | ||
var m = loadAsFileSync(path$1.resolve(x, pkg.main)); | ||
if (m) return m; | ||
var n = loadAsDirectorySync(path.resolve(x, pkg.main)); | ||
var n = loadAsDirectorySync(path$1.resolve(x, pkg.main)); | ||
if (n) return n; | ||
@@ -925,3 +925,3 @@ } catch (e) {} | ||
return loadAsFileSync(path.join(x, '/index')); | ||
return loadAsFileSync(path$1.join(x, '/index')); | ||
} | ||
@@ -935,3 +935,3 @@ | ||
var dir = dirs[i]; | ||
if (isDirectory(path.dirname(dir))) { | ||
if (isDirectory(path$1.dirname(dir))) { | ||
var m = loadAsFileSync(dir); | ||
@@ -1013,3 +1013,3 @@ if (m) return m; | ||
continue; | ||
var factory = this.resolveFactory(config.transform, config.import); | ||
var factory = this.resolveFactory(config); | ||
if (factory === undefined) | ||
@@ -1040,3 +1040,3 @@ continue; | ||
continue; | ||
var factory = this.resolveFactory(config.transform, config.import); | ||
var factory = this.resolveFactory(config); | ||
if (factory === undefined) | ||
@@ -1059,17 +1059,49 @@ continue; | ||
* ***********************************************************/ | ||
PluginCreator.prototype.resolveFactory = function (transform, importKey) { | ||
if (importKey === void 0) { importKey = 'default'; } | ||
PluginCreator.prototype.resolveFactory = function (config) { | ||
var _a; | ||
var tsConfig = config.tsConfig; | ||
var transform = config.transform; | ||
var importKey = config.import || 'default'; | ||
/* Add support for TS transformers */ | ||
if (!tsNodeIncluded && transform.match(/\.ts$/)) { | ||
require('ts-node').register({ | ||
transpileOnly: true, | ||
skipProject: true, | ||
compilerOptions: { | ||
target: 'ES2018', | ||
jsx: 'react', | ||
esModuleInterop: true, | ||
module: 'commonjs', | ||
}, | ||
}); | ||
tsNodeIncluded = true; | ||
var tsConfigCleanup; | ||
if (transform.match(/\.ts$/)) { | ||
// If tsConfig is specified and it differs, we need to re-register tsNode | ||
if (tsNodeIncluded && (tsConfig !== this.currentProject)) | ||
tsNodeIncluded = false; | ||
if (tsConfig) | ||
tsConfig = path.resolve(this.resolveBaseDir, tsConfig); | ||
this.currentProject = tsConfig; | ||
if (!tsNodeIncluded) { | ||
/* Try to add path mapping support, if paths specified */ | ||
var tsConfigData = void 0; | ||
try { | ||
tsConfigData = tsConfig && JSON.parse(fs.readFileSync(tsConfig, 'utf8')); | ||
} | ||
catch (_b) { } | ||
if (tsConfig && ((_a = tsConfigData === null || tsConfigData === void 0 ? void 0 : tsConfigData.compilerOptions) === null || _a === void 0 ? void 0 : _a.paths)) { | ||
try { | ||
var tsConfigPaths = require('tsconfig-paths'); | ||
var absoluteBaseUrl = tsConfigPaths.loadConfig(tsConfig).absoluteBaseUrl; | ||
tsConfigCleanup = tsConfigPaths.register({ | ||
baseUrl: absoluteBaseUrl, | ||
paths: tsConfigData.compilerOptions.paths | ||
}); | ||
} | ||
catch (e) { | ||
if (e.code === 'MODULE_NOT_FOUND') | ||
console.warn("Warning: Paths specified in transformer tsconfig.json, but they can't be resolved. " + | ||
"Try adding 'tsconfig-paths' as a dev dependency"); | ||
else | ||
throw e; | ||
} | ||
} | ||
/* Register tsNode */ | ||
require('ts-node').register(__assign(__assign({ transpileOnly: true }, (tsConfig ? { project: tsConfig } : { skipProject: true })), { compilerOptions: { | ||
target: 'ES2018', | ||
jsx: 'react', | ||
esModuleInterop: true, | ||
module: 'commonjs', | ||
} })); | ||
tsNodeIncluded = true; | ||
} | ||
} | ||
@@ -1084,2 +1116,4 @@ var modulePath = resolve.sync(transform, { basedir: this.resolveBaseDir }); | ||
requireStack.pop(); | ||
// Un-register path mapping if in place | ||
tsConfigCleanup === null || tsConfigCleanup === void 0 ? void 0 : tsConfigCleanup(); | ||
var factoryModule = (typeof commonjsModule === 'function') ? { default: commonjsModule } : commonjsModule; | ||
@@ -1086,0 +1120,0 @@ var factory = factoryModule[importKey]; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
340553
2843
291