jackieli123723 / jackieli123723.github.io

✅lilidong 个人博客
9 stars 0 forks source link

node xxx.ts文件开发部署热更新问题 #80

Open jackieli123723 opened 3 years ago

jackieli123723 commented 3 years ago

目前需求有个spa页面构建出来的js版本 来部署


//koa + ts  how to write server.ts  ??
const port = process.env.PORT || 7000
const Koa = require('koa')
const serve = require('koa-serve')
const path = require('path')
const app = new Koa()
const historyApiFallback = require('koa2-history-api-fallback')
//解决spa路由匹配问题
//dumi doc ssr model not found   ---需要添加 不管是spa 还是ssr 最报保险
app.use(historyApiFallback());
//静态资源
app.use(serve('', path.resolve(__dirname, 'docs-dist')))

app.listen(port, () => {
     console.log(`代码部署成功访问端口http://localhost:${port}`);
});

koa + ts how to write server.ts ??

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "moduleResolution": "node",
    "importHelpers": true,
    "jsx": "react",
    "esModuleInterop": true,
    "sourceMap": true,
    "baseUrl": "./",
    "strict": true,
    "paths": {
      "@/*": ["src/*"],
      "@@/*": ["src/.umi/*"]
    },
    "allowSyntheticDefaultImports": true,
    "typeRoots": [
      "./@types",
      "/node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "lib",
    "es",
    "dist",
    "typings",
    "**/__test__",
    "test"
  ],
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "@types/**/*.d.ts"
  ]
}

/// <reference path="@types/index.d.ts" />
type Ports = number;
const portws: Ports = 7000
import Koa from 'koa';
import Serve from 'koa-serve'
import * as path from 'path'
import historyApiFallback from 'koa2-history-api-fallback';

const App = new Koa()

App.use(historyApiFallback());
//静态资源
App.use(Serve('', path.resolve(__dirname, 'docs-dist')))

App.listen(portws, () => {
     console.log(`代码部署成功访问端口http://localhost:${portws}`);
});

declare module '*.css';
declare module '*.less';

declare module 'koa-serve';
declare module 'koa2-history-api-fallback';

"scripts": {
    "start": "dumi dev",
    "docs:build": "dumi build",
    "docs:deploy": "gh-pages -d docs-dist",
    "build": "father-build",
    "deploy": "npm run docs:build && npm run docs:deploy",
    "release": "npm run build && npm publish",
    "prettier": "prettier --write \"**/*.{js,jsx,tsx,ts,less,md,json}\"",
    "test": "umi-test",
    "test:coverage": "umi-test --coverage",
    "dev0": "nodemon --watch ./**/* -e ts,tsx --exec ts-node server.ts ",
    "dev": "ts-node server.ts",
    "dev1": "nodemon --watch ./**/* -e ts,tsx --exec babel-node --extensions '.ts,.tsx'",
    "start:server": "nodemon server.ts --experimentabl-modules --exec babel-node --extensions '.ts,.tsx'",
    "build:server": "cross-env NODE_ENV=production babel src/app-server.tsx --out-file dist/app-server.js"
  },
  "main": "dist/index.js",
  "module": "dist/index.esm.js",
  "typings": "dist/index.d.ts",
  "gitHooks": {
    "pre-commit": "lint-staged"
  },
  "lint-staged": {
    "*.{js,jsx,less,md,json}": [
      "prettier --write"
    ],
    "*.ts?(x)": [
      "prettier --parser=typescript --write"
    ]
  },
  "dependencies": {
    "@types/koa": "^2.11.6",
    "@types/node": "^14.14.8",
    "koa": "^2.13.0",
    "koa-serve": "^0.1.7",
    "koa2-history-api-fallback": "^0.1.0",
    "nodemon": "^2.0.6",
    "react": "^16.12.0",
    "ts-node": "^9.0.0"
  },
  "devDependencies": {
    "@babel/core": "^7.6.0",
    "@babel/preset-env": "^7.6.0",
    "@babel/preset-typescript": "^7.6.0",
    "@umijs/test": "^3.0.5",
    "babel-cli": "^6.26.0",
    "dumi": "^1.0.10",
    "father-build": "^1.17.2",
    "gh-pages": "^3.0.0",
    "lint-staged": "^10.0.7",
    "prettier": "^1.19.1",
    "yorkie": "^2.0.0"
  }