Guest User

create-test-craco-alias.sh

a guest
Aug 13th, 2020
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.86 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Create TypeScript CRA
  4. npx create-react-app test-craco-alias --template typescript
  5.  
  6. cd test-craco-alias
  7.  
  8. # 1. Install craco and craco-alias
  9. npm install @craco/craco --save
  10. npm i -D craco-alias
  11.  
  12. # 2. Create tsconfig.paths.json in root directory
  13. cat > tsconfig.paths.json <<- EOM
  14. {
  15.   "compilerOptions": {
  16.     "baseUrl": "./src",
  17.     "paths": {
  18.       "@components/*" : ["./components/*"]
  19.     }
  20.   }
  21. }
  22. EOM
  23.  
  24. # 3. Extend tsconfig.paths.json in tsconfig.json
  25. cp tsconfig.json tsconfig.json.bak
  26. sed '1a\
  27. \  "extends": "./tsconfig.paths.json",
  28. ' < tsconfig.json.bak > tsconfig.json
  29. rm tsconfig.json.bak
  30.  
  31. # 4. Create craco.config.js in root direcory
  32. cat > craco.config.js <<- EOM
  33. const CracoAlias = require("craco-alias");
  34.  
  35. module.exports = {
  36.    plugins: [
  37.      {
  38.         plugin: CracoAlias,
  39.         options: {
  40.            source: "tsconfig",
  41.            // baseUrl SHOULD be specified
  42.            // plugin does not take it from tsconfig
  43.            baseUrl: "./src",
  44.            /* tsConfigPath should point to the file where "baseUrl" and "paths"
  45.            are specified*/
  46.            tsConfigPath: "./tsconfig.paths.json"
  47.         }
  48.      }
  49.   ]
  50. };
  51. EOM
  52.  
  53. # 5. in package.json swap "start": "react-scripts start" with "start": "craco start"
  54. cp package.json package.json.bak
  55. sed -E 's/react-scripts (start|build|test)/craco \1/g' < package.json.bak > package.json
  56. rm package.json.bak
  57.  
  58. # Add "components" package with "Bla" component
  59. mkdir components
  60.  
  61. cat > components/Bla.tsx <<- EOM
  62. import React from 'react';
  63.  
  64. const Bla = () => <span>Hello World!</span>;
  65.  
  66. export default Bla;
  67. EOM
  68.  
  69. # Update App.tsx to use Bla
  70. cp src/App.tsx src/App.tsx.bak
  71. cat src/App.tsx.bak | sed '1a\
  72. import Bla from "@components/Bla";
  73. ' | sed 's/Learn React/<Bla \/>/' > src/App.tsx
  74. rm src/App.tsx.bak
  75.  
  76. echo "Now:"
  77. echo "cd test-craco-alias"
  78. echo "npm start"
  79.  
Advertisement
Add Comment
Please, Sign In to add comment