Created
August 29, 2017 17:31
-
-
Save jaredpalmer/17797fad3ec33ded7e88f250e87367f2 to your computer and use it in GitHub Desktop.
Modify Create React App TypeScript
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
'use strict'; | |
const fs = require('fs'); | |
const path = require('path'); | |
const paths = { | |
webpackDev: path.resolve( | |
`./node_modules/react-scripts-ts/config/webpack.config.dev.js` | |
), | |
webpackProd: path.resolve( | |
`./node_modules/react-scripts-ts/config/webpack.config.prod.js` | |
), | |
jestConfig: path.resolve( | |
`./node_modules/react-scripts-ts/scripts/utils/createJestConfig.js` | |
), | |
tsConfig: path.resolve(`./tsconfig.json`), | |
}; | |
function patchFile(file, fn) { | |
const config = fs.readFileSync(file, 'utf8'); | |
fs.writeFileSync(file, fn(config), 'utf8'); | |
} | |
// Patches | |
function aliasCommonDir(webpackConfig) { | |
if (!webpackConfig.includes('alias: {common')) { | |
webpackConfig.replace( | |
`alias: {`, | |
`alias: {common: ${path.resolve('./src/common')},` | |
); | |
} | |
return webpackConfig; | |
} | |
function modifyJestModulePath(jestConfig) { | |
if (!jestConfig.includes(`modulePaths: ['src/'],`)) { | |
jestConfig.replace( | |
`moduleNameMapper: { | |
'^react-native$': 'react-native-web', | |
},`, | |
`moduleNameMapper: { | |
'^react-native$': 'react-native-web', | |
}, | |
modulePaths: ['src/'],` | |
); | |
} | |
if (!jestConfig.includes(`'modulePaths',`)) { | |
jestConfig.replace( | |
`'snapshotSerializers',`, | |
`'snapshotSerializers', | |
'modulePaths',` | |
); | |
} | |
return jestConfig; | |
} | |
function modifyTsConfig(config) { | |
if (!config.includes(`"baseUrl": ".",`)) { | |
config.replace( | |
`"noUnusedLocals": true,`, | |
`"noUnusedLocals": true, | |
"baseUrl": ".", | |
"paths": { | |
"common/*": ["src/common/*"] | |
}` | |
); | |
} | |
return config; | |
} | |
patchFile(paths.webpackDev, aliasCommonDir); | |
patchFile(paths.webpackProd, aliasCommonDir); | |
patchFile(paths.jestConfig, modifyJestModulePath); | |
patchFile(paths.tsConfig, modifyTsConfig); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment