Jelajahi Sumber

use netlify functions (#3882)

* use netlify functions

* rm .gitignore key

* fix build error

* use new folder

* change config

* add redirects

* add redirects

* add router

* remobe "/"

* remove code

* add tag

* remove babel loader

* rm firebase config

* remove babel
陈帅 6 tahun lalu
induk
melakukan
401e65f4d2
16 mengubah file dengan 56 tambahan dan 90 penghapusan
  1. 1 1
      .eslintignore
  2. 0 5
      .firebaserc
  3. 2 2
      .gitignore
  4. 1 1
      README.md
  5. 1 1
      README.ru-RU.md
  6. 1 2
      README.tr-TR.md
  7. 1 1
      README.zh-CN.md
  8. 0 13
      firebase.json
  9. 0 10
      functions/index.js
  10. 0 23
      functions/package.json
  11. 11 0
      lambda/api.js
  12. 6 5
      functions/matchMock.js
  13. 5 2
      netlify.toml
  14. 25 22
      package.json
  15. 1 1
      scripts/generateMock.js
  16. 1 1
      tsconfig.json

+ 1 - 1
.eslintignore

@@ -1,3 +1,3 @@
-/functions/mock/**
+/lambda/mock/**
 /scripts
 /config

+ 0 - 5
.firebaserc

@@ -1,5 +0,0 @@
-{
-  "projects": {
-    "default": "antd-pro"
-  }
-}

+ 2 - 2
.gitignore

@@ -25,8 +25,8 @@ package-lock.json
 # visual studio code
 .history
 *.log
-
-functions/mock
+functions/*
+lambda/mock/index.js
 .temp/**
 
 # umi

File diff ditekan karena terlalu besar
+ 1 - 1
README.md


File diff ditekan karena terlalu besar
+ 1 - 1
README.ru-RU.md


+ 1 - 2
README.tr-TR.md

@@ -12,8 +12,7 @@ React ile kurumsal uygulamalar için taslak olarak geliştirilmiş kullanıma ha
 [![DevDependencies](https://img.shields.io/david/dev/ant-design/ant-design-pro.svg)](https://david-dm.org/ant-design/ant-design-pro?type=dev)
 [![Gitter](https://img.shields.io/gitter/room/ant-design/pro-english.svg)](https://gitter.im/ant-design/pro-english?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)(🇺🇸)
 [![Gitter](https://img.shields.io/gitter/room/ant-design/ant-design-pro.svg?style=flat-square)](https://gitter.im/ant-design/ant-design-pro?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)(🇨🇳)
-
-
+[![Netlify Status](https://api.netlify.com/api/v1/badges/b68e9850-a529-4364-9d3b-d70aade560f9/deploy-status)](https://app.netlify.com/sites/ant-design-pro/deploys)
 ![](https://user-images.githubusercontent.com/8186664/44953195-581e3d80-aec4-11e8-8dcb-54b9db38ec11.png)
 
 </div>

File diff ditekan karena terlalu besar
+ 1 - 1
README.zh-CN.md


+ 0 - 13
firebase.json

@@ -1,13 +0,0 @@
-{
-  "hosting": {
-    "public": "dist",
-    "rewrites": [
-      { "source": "/api/**", "function": "api" },
-      {
-        "source": "**",
-        "destination": "/index.html"
-      }
-    ],
-    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
-  }
-}

+ 0 - 10
functions/index.js

@@ -1,10 +0,0 @@
-// [START functionsimport]
-const functions = require('firebase-functions');
-const express = require('express');
-
-const matchMock = require('./matchMock');
-
-const app = express();
-
-app.use(matchMock);
-exports.api = functions.https.onRequest(app);

+ 0 - 23
functions/package.json

@@ -1,23 +0,0 @@
-{
-  "name": "functions",
-  "description": "Cloud Functions for Firebase",
-  "scripts": {
-    "serve": "firebase serve --only functions",
-    "shell": "firebase functions:shell",
-    "start": "npm run shell",
-    "deploy": "npm run mock && firebase deploy --only functions",
-    "logs": "firebase functions:log",
-    "mock": "node ../scripts/generateMock.js"
-  },
-  "dependencies": {
-    "@babel/runtime": "^7.0.0",
-    "body-parser": "^1.18.3",
-    "express": "^4.16.4",
-    "firebase-admin": "^6.4.0",
-    "firebase-functions": "^2.1.0",
-    "mockjs": "^1.0.1-beta3",
-    "moment": "^2.22.2",
-    "path-to-regexp": "^3.0.0"
-  },
-  "private": true
-}

+ 11 - 0
lambda/api.js

@@ -0,0 +1,11 @@
+// [START functions import]
+const express = require('express');
+const serverLess = require('serverless-http');
+
+const matchMock = require('./mock/matchMock');
+
+const app = express();
+
+app.use(matchMock);
+
+exports.handler = serverLess(app);

+ 6 - 5
functions/matchMock.js

@@ -1,7 +1,7 @@
 const pathToRegexp = require('path-to-regexp');
 const bodyParser = require('body-parser');
 
-const mockFile = require('./mock/index');
+const mockFile = require('./index');
 
 const BODY_PARSED_METHODS = ['post', 'put', 'patch'];
 
@@ -10,13 +10,14 @@ function parseKey(key) {
   let method = 'get';
   let path = key;
   if (key.indexOf(' ') > -1) {
-    const splited = key.split(' ');
-    method = splited[0].toLowerCase();
-    path = splited[1]; // eslint-disable-line
+    const spliced = key.split(' ');
+    method = spliced[0].toLowerCase();
+    path = spliced[1]; // eslint-disable-line
   }
+  const routerBasePath = process.env.NODE_ENV === 'dev' ? `${path}` : `/.netlify/functions${path}`;
   return {
     method,
-    path,
+    path: routerBasePath,
   };
 }
 

+ 5 - 2
netlify.toml

@@ -1,12 +1,15 @@
+[build]
+  functions = "./functions"
+
 [[redirects]]
   from = "/api/*"
-  to = "https://us-central1-antd-pro.cloudfunctions.net/api/api/:splat"
+  to = "/.netlify/functions/api/:splat"
   status = 200
   force = true
   [redirects.headers]
     X-From = "Netlify"
     X-Api-Key = "some-api-key-string"
-
+    
 [[redirects]]
   from = "/*"
   to = "/index.html"

+ 25 - 22
package.json

@@ -1,6 +1,6 @@
 {
   "name": "ant-design-pro",
-  "version": "2.3.0",
+  "version": "2.3.1",
   "private": true,
   "description": "An out-of-box UI solution for enterprise applications",
   "scripts": {
@@ -13,6 +13,9 @@
     "docker:dev": "docker-compose -f ./docker/docker-compose.dev.yml up",
     "docker:push": "npm run docker-hub:build && npm run docker:tag && docker push antdesign/ant-design-pro",
     "docker:tag": "docker tag ant-design-pro antdesign/ant-design-pro",
+    "functions:build": "npm run generateMock && netlify-lambda build ./lambda",
+    "functions:run": "npm run generateMock && cross-env NODE_ENV=dev netlify-lambda serve ./lambda",
+    "generateMock": "node ./scripts/generateMock",
     "lint": "eslint --ext .js src mock tests && npm run lint:style && npm run lint:prettier",
     "lint-staged": "lint-staged",
     "lint-staged:js": "eslint --ext .js",
@@ -20,8 +23,6 @@
     "lint:prettier": "check-prettier lint",
     "lint:style": "stylelint 'src/**/*.less' --syntax less",
     "prettier": "node ./scripts/prettier.js",
-    "presite": "cd functions && npm install",
-    "site": "npm run presite && cross-env APP_TYPE=site npm run build && firebase deploy && npm run docker:push",
     "start": "cross-env APP_TYPE=site umi dev",
     "start:no-mock": "cross-env MOCK=none umi dev",
     "test": "umi test",
@@ -30,20 +31,39 @@
     "tslint": "npm run tslint:fix",
     "tslint:fix": "tslint --fix 'src/**/*.ts*'"
   },
+  "husky": {
+    "hooks": {
+      "pre-commit": "npm run lint-staged"
+    }
+  },
+  "lint-staged": {
+    "**/*.less": "stylelint --syntax less",
+    "**/*.{js,jsx}": "npm run lint-staged:js",
+    "**/*.{js,ts,tsx,json,jsx,less}": [
+      "node ./scripts/lint-prettier.js",
+      "git add"
+    ]
+  },
+  "browserslist": [
+    "> 1%",
+    "last 2 versions",
+    "not ie <= 10"
+  ],
   "dependencies": {
     "@antv/data-set": "^0.10.1",
-    "@babel/runtime": "^7.3.1",
     "antd": "^3.13.0",
     "bizcharts": "^3.4.3",
     "bizcharts-plugin-slider": "^2.1.1-beta.1",
     "classnames": "^2.2.6",
     "dva": "^2.4.1",
     "enquire-js": "^0.2.1",
+    "express": "^4.16.4",
     "gg-editor": "^2.0.2",
     "lodash": "^4.17.11",
     "lodash-decorators": "^6.0.1",
     "memoize-one": "^5.0.0",
     "moment": "^2.24.0",
+    "netlify-lambda": "^1.4.3",
     "numeral": "^2.0.6",
     "nzh": "^1.0.4",
     "omit.js": "^1.0.0",
@@ -58,6 +78,7 @@
     "react-dom": "^16.7.0",
     "react-fittext": "^1.0.0",
     "react-media": "^1.9.2",
+    "serverless-http": "^1.9.1",
     "umi": "^2.4.4",
     "umi-plugin-react": "^1.7.2",
     "umi-request": "^1.0.5"
@@ -110,24 +131,6 @@
   "engines": {
     "node": ">=8.0.0"
   },
-  "husky": {
-    "hooks": {
-      "pre-commit": "npm run lint-staged"
-    }
-  },
-  "lint-staged": {
-    "**/*.less": "stylelint --syntax less",
-    "**/*.{js,jsx}": "npm run lint-staged:js",
-    "**/*.{js,ts,tsx,json,jsx,less}": [
-      "node ./scripts/lint-prettier.js",
-      "git add"
-    ]
-  },
-  "browserslist": [
-    "> 1%",
-    "last 2 versions",
-    "not ie <= 10"
-  ],
   "checkFiles": [
     "src/**/*.js*",
     "src/**/*.ts*",

+ 1 - 1
scripts/generateMock.js

@@ -1,3 +1,3 @@
 const generateMock = require('merge-umi-mock-data');
 const path = require('path');
-generateMock(path.join(__dirname, '../mock'), path.join(__dirname, '../functions/mock/index.js'));
+generateMock(path.join(__dirname, '../mock'), path.join(__dirname, '../lambda/mock/index.js'));

+ 1 - 1
tsconfig.json

@@ -9,7 +9,7 @@
     "jsx": "react",
     "allowSyntheticDefaultImports": true,
     "moduleResolution": "node",
-    "rootDirs": ["/src", "/test", "/mock","./typings"],
+    "rootDirs": ["/src", "/test", "/mock", "./typings"],
     "forceConsistentCasingInFileNames": true,
     "noImplicitReturns": true,
     "suppressImplicitAnyIndexErrors": true,