Created
January 5, 2021 01:42
-
-
Save ankit/cbf5c27e8156ba6ed5e0434e4f49712f to your computer and use it in GitHub Desktop.
Custom tsdx.config.js to support @zerollup/ts-transform-paths and ttypescript
This file contains hidden or 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
// https://github.com/zerkalica/zerollup/tree/master/packages/ts-transform-paths | |
const ttypescript = require('ttypescript'); | |
const typescript = require('rollup-plugin-typescript2'); | |
module.exports = { | |
rollup(config, options) { | |
const rpt2Plugin = config.plugins.find(p => p.name === 'rpt2'); | |
const rpt2PluginIndex = config.plugins.indexOf(rpt2Plugin); | |
const tsconfigPath = options.tsconfig || 'tsconfig.json'; | |
// borrowed from https://github.com/facebook/create-react-app/pull/7248 | |
const tsconfigJSON = ttypescript.readConfigFile( | |
tsconfigPath, | |
ttypescript.sys.readFile | |
).config; | |
// borrowed from https://github.com/ezolenko/rollup-plugin-typescript2/blob/42173460541b0c444326bf14f2c8c27269c4cb11/src/parse-tsconfig.ts#L48 | |
const tsCompilerOptions = ttypescript.parseJsonConfigFileContent( | |
tsconfigJSON, | |
ttypescript.sys, | |
'./' | |
).options; | |
const customRPT2Plugin = typescript({ | |
typescript: ttypescript, | |
tsconfig: options.tsconfig, | |
tsconfigDefaults: { | |
exclude: [ | |
// all TS test files, regardless whether co-located or in test/ etc | |
'**/*.spec.ts', | |
'**/*.test.ts', | |
'**/*.spec.tsx', | |
'**/*.test.tsx', | |
// TS defaults below | |
'node_modules', | |
'bower_components', | |
'jspm_packages', | |
'dist', | |
], | |
compilerOptions: { | |
sourceMap: true, | |
declaration: true, | |
jsx: 'react', | |
}, | |
}, | |
tsconfigOverride: { | |
compilerOptions: { | |
// TS -> esnext, then leave the rest to babel-preset-env | |
target: 'esnext', | |
// don't output declarations more than once | |
...(!options.writeMeta | |
? { declaration: false, declarationMap: false } | |
: {}), | |
}, | |
}, | |
check: !options.transpileOnly && options.writeMeta, | |
useTsconfigDeclarationDir: Boolean( | |
tsCompilerOptions && tsCompilerOptions.declarationDir | |
), | |
}); | |
config.plugins.splice(rpt2PluginIndex, 1, customRPT2Plugin); | |
return config; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment