npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dsaco/cra-template-typescript

v1.0.9

Published

```sh npx create-react-app my-app --template @dsaco/typescript ```

Downloads

12

Readme

使用方法

npx create-react-app my-app --template @dsaco/typescript

代码报错时,请配置路径别名

// cacro.config.js
const path = require('path');
module.exports = {
  webpack: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
};
// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}

模版编写步骤

配置路径别名需要安装 craco

yarn add -D @craco/craco

创建 craco.config.js

const path = require('path');

module.exports = {
  webpack: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
};

修改 package.json 的 scripts

react-scripts 替换为 craco

消除编辑器警告

tsconfig.json 里添加

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}

.env

根目录下创建.env

# 端口
PORT=3000
# 打包地址
BUILD_PATH=build
# 生产环境是否生成源代码映射文件,默认为true
GENERATE_SOURCEMAP=false

使用 sass

creact-react-app 默认支持,只需要安装 sass 即可

yarn add -D sass

使用 less

安装依赖

yarn add less craco-less

编辑 craco.config.js

const cracoLessPlugin = require('craco-less');

module.exports = {
  plugins: [
    {
      plugin: cracoLessPlugin,
    },
  ],
};

配置代理

安装依赖

yarn add -D http-proxy-middleware

创建 src/setupProxy.js

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function (app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: 'https://example.com',
      changeOrigin: true,
    })
  );
};

分析代码打包结果

安装依赖

yarn add -D source-map-explorer

package.json 增加(需要开启 GENERATE_SOURCEMAP)

{
  "scripts": {
    "analyze": "npm run build && source-map-explorer 'build/static/js/*.js'"
  }
}

使用 tailwindcss

安装

yarn add -D tailwindcss
npx tailwindcss init

修改 tailwind.config.js

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ['./src/**/*.{ts,tsx}'],
  theme: {
    extend: {},
  },
  plugins: [],
};

页面引入样式

@tailwind base;
@tailwind components;
@tailwind utilities;

消除 vscode 的样式警告

.vscode/settings.json

{
  "css.lint.unknownAtRules": "ignore",
  "less.lint.unknownAtRules": "ignore"
}

使用 prettier

安装

yarn add --dev --exact prettier

新增.prettierrc

{
  "singleQuote": true,
  "overrides": [
    {
      "files": "*.css",
      "options": {
        "singleQuote": false
      }
    },
    {
      "files": "*.less",
      "options": {
        "singleQuote": false
      }
    },
    {
      "files": "*.scss",
      "options": {
        "singleQuote": false
      }
    }
  ]
}
{
  "singleQuote": true,
  "trailingComma": "es5",
  "overrides": [
    {
      "files": "*.css",
      "options": {
        "singleQuote": false
      }
    },
    {
      "files": "*.less",
      "options": {
        "singleQuote": false
      }
    },
    {
      "files": "*.scss",
      "options": {
        "singleQuote": false
      }
    }
  ]
}

使用 stylelint

初始化

npm init stylelint

样式属性顺序、sass、less

yarn add -D stylelint-config-recess-order postcss-less postcss-scss

.vscode/settings.json

{
  "css.lint.unknownAtRules": "ignore",
  "less.lint.unknownAtRules": "ignore",
  "scss.lint.unknownAtRules": "ignore",
  "stylelint.validate": ["css", "less", "postcss", "scss"]
}

.stylelintrc.json

{
  "extends": ["stylelint-config-standard", "stylelint-config-recess-order"],
  "overrides": [
    {
      "files": ["**/*.less"],
      "customSyntax": "postcss-less"
    },
    {
      "files": ["**/*.scss"],
      "customSyntax": "postcss-scss"
    }
  ],
  "rules": {
    "at-rule-no-unknown": [
      true,
      {
        "ignoreAtRules": ["tailwind"]
      }
    ],
    "function-no-unknown": [
      true,
      {
        "ignoreFunctions": ["theme"]
      }
    ]
  }
}

package.json

{
  "scripts": {
    "lint:style": "stylelint src/**/*.{css,less,scss}",
    "fix:style": "npm run lint:style -- --fix"
  }
}

使用 eslint

安装

yarn add -D eslint eslint-plugin-react eslint-plugin-prettier @typescript-eslint/parser @typescript-eslint/eslint-plugin

创建.eslintrc.json

{
  "env": {
    "browser": true,
    "es2021": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:react/recommended"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": ["@typescript-eslint", "react", "prettier"],
  "rules": {
    "prettier/prettier": "error"
  }
}