TypeSript Reach

TypeSript Reach

Posted by nomadli on December 6, 2021

环境配置

    npx create-react-app project --template typescript
    cd project
    npm start || npm test || PUBLIC_URL=dns_path npm run build
  • node_modules react及其他库代码, 包括webpack、eslint等
  • public 静态内容和html文件robots定义等
    • manifest.json web app信息:应用名称作者等,如果当手机创建快捷方式时
  • src 项目代码
    • reportWebVitals.ts 性能监测 (F L)CP(首次 最大)绘制时间 FID首次输入延迟 CLS累计布局重算 TTFB首次收到数据延迟

手动环境

npm i 查找最适合当前nodejs的包 -D 开发环境 -S 生成环境

cat > "package.json" <<EOF
{
    "scripts": {
        "build": "webpack",
        "dev": "webpack-dev-server --open"
    }
}
EOF

npm i -D webpack webpack-cli webpack-dev-server typescript ts-loader @types/react @types/react-dom html-webpack-plugin html-loader style-loader css-loader sass-loader node-sass
npm i -S react react-dom

mkdir -p src/typings dist public
cat > "public/index.html" <<EOF
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="theme-color" content="#000000" />
    <meta name="description" content="Webpack with TypeScript" />
    <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
  <script src="bundle.js"></script>
</html>
EOF

cat > "src/typings/scss.d.ts" <<EOF
declare module '*.css' {
    const content: { [className: string]: string; };
    export default content;
}
  
declare module '*.scss' {
    const content: { [className: string]: string; };
    export default content;
}

EOF

cat > "src/index.scss" <<EOF
.title { 
    color: red;
}

EOF

cat > "src/index.tsx" <<EOF
import React from 'react';
import ReactDOM from 'react-dom';
import styles from './index.scss';

const App = () => {
  return <h1 className={styles.title}>Hello Wepack!</h1>;
};

ReactDOM.render(<App />, document.getElementById('root'));

EOF

cat > "tsconfig.json" <<EOF
{
    "compileOnSave": true,
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "allowJs": true,
        "jsx": "react",
        "outDir": "./dist",
        "rootDir": "./",
        "isolatedModules": true,
        "strict": true,
        "moduleResolution": "node",
        "baseUrl": "./src",
        "typeRoots": ["node_modules/@types", "src/typings"],
        "esModuleInterop": true
    },
    "include": ["src/**/*"],
    "exclude": ["node_modules"]
}
EOF

cat > "webpack.config.js" <<EOF
const path = require('path');
module.exports = {
    entry: path.resolve(__dirname, 'src', 'index.tsx'),
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    mode: 'development',
    resolve: {
        extensions: ['.js', '.ts', '.tsx']
    },
    externals : {
        "react"     : 'React',
        "react-dom" : 'ReactDOM'
    },
    module: {
        rules: [
            {test: /\.html$/, use: ['html-loader']},
            {test: /\.tsx?$/, use: ['ts-loader']},
            {
                test: /\.s(a|c)ss$/,
                use: [
                    'style-loader',
                    {loader: 'css-loader', options: { modules: true }},
                    'sass-loader'
                ]
            }
        ]
    },
    devServer: {
        host: '0.0.0.0',
        port: 8888
    }
};
EOF

#npm run dev
#npm run build