Skip to content

Instantly share code, notes, and snippets.

@ankit
Created January 5, 2021 01:42
Show Gist options
  • Save ankit/cbf5c27e8156ba6ed5e0434e4f49712f to your computer and use it in GitHub Desktop.
Save ankit/cbf5c27e8156ba6ed5e0434e4f49712f to your computer and use it in GitHub Desktop.
Custom tsdx.config.js to support @zerollup/ts-transform-paths and ttypescript
// 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