From 64547f80842ce7af5497230d7ece35186074c8db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B0=8F=E8=81=AA?= Date: Thu, 30 Apr 2020 16:41:18 +0800 Subject: [PATCH] feat: create plugin Upgrade to Umi 3 (#107) create plugin Upgrade to Umi 3 (#107) --- lib/generators/plugin/index.js | 24 ++++-- lib/generators/plugin/templates/.editorconfig | 16 ++++ lib/generators/plugin/templates/.fatherrc.js | 40 +++++++--- lib/generators/plugin/templates/.fatherrc.ts | 43 +++++++---- lib/generators/plugin/templates/.gitignore | 8 ++ .../plugin/templates/.prettierignore | 8 ++ lib/generators/plugin/templates/.prettierrc | 14 ++++ .../plugin/templates/CONTRIBUTING.md | 40 ++++++++++ lib/generators/plugin/templates/LICENSE | 21 ------ lib/generators/plugin/templates/README.md | 13 +--- lib/generators/plugin/templates/_gitignore | 11 --- .../plugin/templates/example/.gitignore | 5 ++ .../plugin/templates/example/.umirc.js | 16 ++-- .../plugin/templates/example/.umirc.ts | 17 ++--- .../plugin/templates/example/app.jsx | 3 + .../plugin/templates/example/app.tsx | 3 + .../plugin/templates/example/package.json | 4 +- .../plugin/templates/example/pages/index.css | 15 +++- .../plugin/templates/example/pages/index.jsx | 8 ++ .../plugin/templates/example/pages/index.tsx | 12 ++- .../plugin/templates/example/tsconfig.json | 36 ++++++++- .../example/{global.d.ts => typing.d.ts} | 0 .../plugin/templates/jest.config.js | 8 ++ lib/generators/plugin/templates/package.json | 74 ++++++++++++------- lib/generators/plugin/templates/src/index.js | 22 +++--- lib/generators/plugin/templates/src/index.ts | 26 ++++--- .../templates/test/fixtures/normal/.umirc.js | 4 + .../templates/test/fixtures/normal/.umirc.ts | 4 + .../test/fixtures/normal/pages/index.css | 4 + .../test/fixtures/normal/pages/index.jsx | 10 +++ .../fixtures/normal/pages/index.tsx} | 1 + .../templates/test/fixtures/normal/test.js | 10 +++ .../templates/test/fixtures/normal/test.ts | 10 +++ .../plugin/templates/test/index.e2e.js | 5 ++ .../plugin/templates/test/index.e2e.ts | 5 ++ lib/generators/plugin/templates/tsconfig.json | 25 ++++--- .../templates/ui/{index.js => index.jsx} | 6 +- lib/generators/plugin/templates/ui/index.tsx | 8 +- 38 files changed, 412 insertions(+), 167 deletions(-) create mode 100644 lib/generators/plugin/templates/.editorconfig create mode 100644 lib/generators/plugin/templates/.gitignore create mode 100644 lib/generators/plugin/templates/.prettierignore create mode 100644 lib/generators/plugin/templates/.prettierrc create mode 100644 lib/generators/plugin/templates/CONTRIBUTING.md delete mode 100644 lib/generators/plugin/templates/LICENSE delete mode 100644 lib/generators/plugin/templates/_gitignore create mode 100644 lib/generators/plugin/templates/example/.gitignore create mode 100644 lib/generators/plugin/templates/example/app.jsx create mode 100644 lib/generators/plugin/templates/example/app.tsx create mode 100644 lib/generators/plugin/templates/example/pages/index.jsx mode change 100755 => 100644 lib/generators/plugin/templates/example/pages/index.tsx rename lib/generators/plugin/templates/example/{global.d.ts => typing.d.ts} (100%) create mode 100644 lib/generators/plugin/templates/jest.config.js create mode 100644 lib/generators/plugin/templates/test/fixtures/normal/.umirc.js create mode 100644 lib/generators/plugin/templates/test/fixtures/normal/.umirc.ts create mode 100644 lib/generators/plugin/templates/test/fixtures/normal/pages/index.css create mode 100755 lib/generators/plugin/templates/test/fixtures/normal/pages/index.jsx rename lib/generators/plugin/templates/{example/pages/index.js => test/fixtures/normal/pages/index.tsx} (99%) create mode 100644 lib/generators/plugin/templates/test/fixtures/normal/test.js create mode 100644 lib/generators/plugin/templates/test/fixtures/normal/test.ts create mode 100644 lib/generators/plugin/templates/test/index.e2e.js create mode 100644 lib/generators/plugin/templates/test/index.e2e.ts rename lib/generators/plugin/templates/ui/{index.js => index.jsx} (80%) diff --git a/lib/generators/plugin/index.js b/lib/generators/plugin/index.js index 7f90107..c7105b8 100644 --- a/lib/generators/plugin/index.js +++ b/lib/generators/plugin/index.js @@ -26,9 +26,19 @@ class Generator extends BasicGenerator { }, { name: 'isTypeScript', - type: 'confirm', - message: 'Do you want to use typescript?', - default: false, + type: 'list', + message: 'Select the development language', + choices: [ + { + name: 'TypeScript', + value: true, + }, + { + name: 'JavaScript', + value: false, + }, + ], + default: true, }, { name: 'withUmiUI', @@ -44,9 +54,7 @@ class Generator extends BasicGenerator { // lang: ts || js isUIFiles(file, lang) { - const uiFile = lang === 'ts' - ? 'ui/index.tsx' - : 'ui/index.js'; + const uiFile = lang === 'ts' ? 'ui/index.tsx' : 'ui/index.jsx'; return file === uiFile; } @@ -54,9 +62,9 @@ class Generator extends BasicGenerator { this.writeFiles({ context: this.prompts, filterFiles: f => { - const { isTypeScript, withUmiUI } = this.prompts; + const { isTypeScript = true, withUmiUI } = this.prompts; if (isTypeScript) { - if (f.endsWith('.js')) return false; + if (f.endsWith('.js') || f.endsWith('.jsx')) return false; // filter ui files if (!withUmiUI && this.isUIFiles(f, 'ts')) return false; } else { diff --git a/lib/generators/plugin/templates/.editorconfig b/lib/generators/plugin/templates/.editorconfig new file mode 100644 index 0000000..7e3649a --- /dev/null +++ b/lib/generators/plugin/templates/.editorconfig @@ -0,0 +1,16 @@ +# http://editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false + +[Makefile] +indent_style = tab diff --git a/lib/generators/plugin/templates/.fatherrc.js b/lib/generators/plugin/templates/.fatherrc.js index 0109985..fe073d3 100644 --- a/lib/generators/plugin/templates/.fatherrc.js +++ b/lib/generators/plugin/templates/.fatherrc.js @@ -1,15 +1,35 @@ +<% if (withUmiUI) { -%> +const isProd = process.env.NODE_ENV === 'prod'; -export default [ - { - cjs: 'babel', - }, +<% } -%> +export default [{ + target: 'node', + cjs: { type: 'babel', lazy: true }, + disableTypeCheck: true, + extraBabelPlugins: [ + [ + 'babel-plugin-import', + { libraryName: 'antd', libraryDirectory: 'es', style: true }, + 'antd', + ], + ], <% if (withUmiUI) { -%> - { - entry: 'ui/index.js', - umd: { - name: '<%= name %>', - minFile: false, +}, +{ + entry: 'ui/index.tsx', + umd: { + name: 'tasks', + minFile: isProd, + sourcemap: !isProd, + }, + extraExternals: ['antd', 'react', 'react-dom', 'xterm'], + typescriptOpts: { + check: false, + globals: { + antd: 'window.antd', + react: 'window.React', + 'react-dom': 'window.ReactDOM', }, }, <% } -%> -]; +}]; diff --git a/lib/generators/plugin/templates/.fatherrc.ts b/lib/generators/plugin/templates/.fatherrc.ts index 602e842..fe073d3 100644 --- a/lib/generators/plugin/templates/.fatherrc.ts +++ b/lib/generators/plugin/templates/.fatherrc.ts @@ -1,18 +1,35 @@ +<% if (withUmiUI) { -%> +const isProd = process.env.NODE_ENV === 'prod'; -export default [ - { - cjs: 'babel', - }, +<% } -%> +export default [{ + target: 'node', + cjs: { type: 'babel', lazy: true }, + disableTypeCheck: true, + extraBabelPlugins: [ + [ + 'babel-plugin-import', + { libraryName: 'antd', libraryDirectory: 'es', style: true }, + 'antd', + ], + ], <% if (withUmiUI) { -%> - { - entry: 'ui/index.tsx', - typescriptOpts: { - check: false, - }, - umd: { - name: '<%= name %>', - minFile: false, +}, +{ + entry: 'ui/index.tsx', + umd: { + name: 'tasks', + minFile: isProd, + sourcemap: !isProd, + }, + extraExternals: ['antd', 'react', 'react-dom', 'xterm'], + typescriptOpts: { + check: false, + globals: { + antd: 'window.antd', + react: 'window.React', + 'react-dom': 'window.ReactDOM', }, }, <% } -%> -]; +}]; diff --git a/lib/generators/plugin/templates/.gitignore b/lib/generators/plugin/templates/.gitignore new file mode 100644 index 0000000..2927896 --- /dev/null +++ b/lib/generators/plugin/templates/.gitignore @@ -0,0 +1,8 @@ +node_modules +.changelog +.umi +.umi-test +.umi-production +.DS_Store +/lib +dist diff --git a/lib/generators/plugin/templates/.prettierignore b/lib/generators/plugin/templates/.prettierignore new file mode 100644 index 0000000..b890391 --- /dev/null +++ b/lib/generators/plugin/templates/.prettierignore @@ -0,0 +1,8 @@ +**/node_modules/** +package.json + +# fixtures +**/fixtures/** + +# templates +**/templates/** diff --git a/lib/generators/plugin/templates/.prettierrc b/lib/generators/plugin/templates/.prettierrc new file mode 100644 index 0000000..3f37483 --- /dev/null +++ b/lib/generators/plugin/templates/.prettierrc @@ -0,0 +1,14 @@ +{ + "printWidth": 80, + "singleQuote": true, + "trailingComma": "all", + "proseWrap": "never", + "overrides": [ + { + "files": ".prettierrc", + "options": { + "parser": "json" + } + } + ] +} diff --git a/lib/generators/plugin/templates/CONTRIBUTING.md b/lib/generators/plugin/templates/CONTRIBUTING.md new file mode 100644 index 0000000..6e1c2bc --- /dev/null +++ b/lib/generators/plugin/templates/CONTRIBUTING.md @@ -0,0 +1,40 @@ +# Contributing to plugin + +## Set up + +Install dev deps after git clone the repo. + +```bash +# npm is not allowed. +$ yarn +``` + +## Build + +Transform with babel and rollup. + +```bash +$ yarn build + +# Build and monitor file changes +$ yarn build --watch + +``` + +## Dev Plugin + +```bash +# This Step must only be executed in Build +$ yarn dev +``` + +## Debug + +TODO + +## Test + +```bash +$ yarn test +``` + diff --git a/lib/generators/plugin/templates/LICENSE b/lib/generators/plugin/templates/LICENSE deleted file mode 100644 index 8ff1dcd..0000000 --- a/lib/generators/plugin/templates/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2018-present <%= author %> (<%= mail %>) - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/lib/generators/plugin/templates/README.md b/lib/generators/plugin/templates/README.md index ae4f8db..1aa96e4 100644 --- a/lib/generators/plugin/templates/README.md +++ b/lib/generators/plugin/templates/README.md @@ -14,20 +14,11 @@ $ npm install <% if (withUmiUI) { -%> ## Development UI -UI start: - -```bash -$ npm run build --watch -$ npm run start -``` - - - UI mini start: ```bash $ npm run build --watch -$ npm run start:mini +$ npm run start ``` @@ -46,7 +37,7 @@ Configure in `.umirc.js`, ```js export default { plugins: [ - ['umi-plugin-<%= name %>', options], + ['<%= name %>'], ], } ``` diff --git a/lib/generators/plugin/templates/_gitignore b/lib/generators/plugin/templates/_gitignore deleted file mode 100644 index 2a0975c..0000000 --- a/lib/generators/plugin/templates/_gitignore +++ /dev/null @@ -1,11 +0,0 @@ -/node_modules -/yarn.lock -/package-lock.json -/example/dist -/example/node_modules -/example/pages/.umi -/example/pages/.umi-production -/lib -<% if (withUmiUI) { -%> -/dist -<% } -%> diff --git a/lib/generators/plugin/templates/example/.gitignore b/lib/generators/plugin/templates/example/.gitignore new file mode 100644 index 0000000..c7d9857 --- /dev/null +++ b/lib/generators/plugin/templates/example/.gitignore @@ -0,0 +1,5 @@ +/.umi +/.umi-production +/dist +/node_modules +/yarn.lock diff --git a/lib/generators/plugin/templates/example/.umirc.js b/lib/generators/plugin/templates/example/.umirc.js index fe9a16d..c8fa776 100644 --- a/lib/generators/plugin/templates/example/.umirc.js +++ b/lib/generators/plugin/templates/example/.umirc.js @@ -1,10 +1,8 @@ -import { join } from 'path'; +import { defineConfig } from 'umi'; -export default { - routes: [ - { path: '/', component: './index' }, - ], - plugins: [ - join(__dirname, '..', require('../package').main || 'index.js'), - ], -} +export default defineConfig({ +<% if (withUmiUI) { -%> + presets: [require.resolve('@umijs/preset-ui')], +<% } -%> + plugins: [require.resolve('../lib')], +}); diff --git a/lib/generators/plugin/templates/example/.umirc.ts b/lib/generators/plugin/templates/example/.umirc.ts index 8b700e6..c8fa776 100644 --- a/lib/generators/plugin/templates/example/.umirc.ts +++ b/lib/generators/plugin/templates/example/.umirc.ts @@ -1,11 +1,8 @@ -import { join } from 'path'; -import { IConfig } from 'umi-types'; +import { defineConfig } from 'umi'; -export default { - routes: [ - { path: '/', component: './index' }, - ], - plugins: [ - join(__dirname, '..', require('../package').main || 'index.js'), - ], -} as IConfig; +export default defineConfig({ +<% if (withUmiUI) { -%> + presets: [require.resolve('@umijs/preset-ui')], +<% } -%> + plugins: [require.resolve('../lib')], +}); diff --git a/lib/generators/plugin/templates/example/app.jsx b/lib/generators/plugin/templates/example/app.jsx new file mode 100644 index 0000000..35784ac --- /dev/null +++ b/lib/generators/plugin/templates/example/app.jsx @@ -0,0 +1,3 @@ +export function render(oldRender) { + oldRender(); +} diff --git a/lib/generators/plugin/templates/example/app.tsx b/lib/generators/plugin/templates/example/app.tsx new file mode 100644 index 0000000..12edfa3 --- /dev/null +++ b/lib/generators/plugin/templates/example/app.tsx @@ -0,0 +1,3 @@ +export function render(oldRender: Function) { + oldRender(); +} diff --git a/lib/generators/plugin/templates/example/package.json b/lib/generators/plugin/templates/example/package.json index 0967ef4..18a1e41 100644 --- a/lib/generators/plugin/templates/example/package.json +++ b/lib/generators/plugin/templates/example/package.json @@ -1 +1,3 @@ -{} +{ + "dependencies": {} +} diff --git a/lib/generators/plugin/templates/example/pages/index.css b/lib/generators/plugin/templates/example/pages/index.css index 29bc9a3..b0569c3 100644 --- a/lib/generators/plugin/templates/example/pages/index.css +++ b/lib/generators/plugin/templates/example/pages/index.css @@ -1,4 +1,17 @@ +body, +html, +#root { + height: 100%; + margin: 0; +} + +a { + color: #1890ff; + cursor: pointer; +} .normal { - background: #79F2AA; + background: #ddd; + height: 100vh; + padding: 48px; } diff --git a/lib/generators/plugin/templates/example/pages/index.jsx b/lib/generators/plugin/templates/example/pages/index.jsx new file mode 100644 index 0000000..70090be --- /dev/null +++ b/lib/generators/plugin/templates/example/pages/index.jsx @@ -0,0 +1,8 @@ +import React from 'react'; +import styles from './index.css'; + +export default () => ( +
+ Hello Umi! +
+); diff --git a/lib/generators/plugin/templates/example/pages/index.tsx b/lib/generators/plugin/templates/example/pages/index.tsx old mode 100755 new mode 100644 index b3069ce..70090be --- a/lib/generators/plugin/templates/example/pages/index.tsx +++ b/lib/generators/plugin/templates/example/pages/index.tsx @@ -1,10 +1,8 @@ import React from 'react'; import styles from './index.css'; -export default function() { - return ( -
-

Page index

-
- ); -} +export default () => ( +
+ Hello Umi! +
+); diff --git a/lib/generators/plugin/templates/example/tsconfig.json b/lib/generators/plugin/templates/example/tsconfig.json index 3c43903..9d0ff62 100644 --- a/lib/generators/plugin/templates/example/tsconfig.json +++ b/lib/generators/plugin/templates/example/tsconfig.json @@ -1,3 +1,37 @@ { - "extends": "../tsconfig.json" + "compilerOptions": { + "outDir": "build/dist", + "module": "esnext", + "target": "esnext", + "lib": ["esnext", "dom"], + "sourceMap": true, + "baseUrl": ".", + "jsx": "react", + "allowSyntheticDefaultImports": true, + "moduleResolution": "node", + "forceConsistentCasingInFileNames": true, + "noImplicitReturns": true, + "suppressImplicitAnyIndexErrors": true, + "noUnusedLocals": true, + "allowJs": true, + "skipLibCheck": true, + "experimentalDecorators": true, + "strict": true, + "paths": { + "@/*": ["/*"], + "@@/*": ["/.umi/*"] + } + }, + "exclude": [ + "node_modules", + "build", + "dist", + "scripts", + "acceptance-tests", + "webpack", + "jest", + "src/setupTests.ts", + "tslint:latest", + "tslint-config-prettier" + ] } diff --git a/lib/generators/plugin/templates/example/global.d.ts b/lib/generators/plugin/templates/example/typing.d.ts similarity index 100% rename from lib/generators/plugin/templates/example/global.d.ts rename to lib/generators/plugin/templates/example/typing.d.ts diff --git a/lib/generators/plugin/templates/jest.config.js b/lib/generators/plugin/templates/jest.config.js new file mode 100644 index 0000000..c83e113 --- /dev/null +++ b/lib/generators/plugin/templates/jest.config.js @@ -0,0 +1,8 @@ +module.exports = { + moduleNameMapper(memo) { + return Object.assign(memo, { + '^react$': require.resolve('react'), + '^react-dom$': require.resolve('react-dom'), + }); + }, +}; diff --git a/lib/generators/plugin/templates/package.json b/lib/generators/plugin/templates/package.json index ecf8a81..566b1f3 100644 --- a/lib/generators/plugin/templates/package.json +++ b/lib/generators/plugin/templates/package.json @@ -1,48 +1,66 @@ { "name": "<%= name %>", - "version": "0.0.1", + "main": "lib/index.js", "description": "<%= description %>", "authors": { "name": "<%= author %>", "email": "<%= mail %>" }, "repository": "<%= org %>/<%= name %>", - "peerDependencies": { -<% if (withUmiUI) { -%> - "antd": "4.x", - "react": "^16.8.6", - "react-dom": "^16.8.6", -<% } -%> - "umi": "2.x || ^2.9.0-0" - }, - "main": "lib/index.js", "scripts": { -<% if (withUmiUI) { -%> - "start:mini": "cross-env UMI_UI=1 APP_ROOT=example umi dev", - "start": "cross-env CURRENT_PROJECT=example umi ui", -<% } else { -%> "start": "cross-env APP_ROOT=example umi dev", -<% } -%> - "build": "father-build", - "prepublishOnly": "npm run build && np --no-cleanup --yolo --no-publish" + "build": "father-build", + "prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,md,json}'", + "test": "umi-test", + "test:coverage": "umi-test --coverage", + "test:update": "umi-test --updateSnapshot" + }, + "lint-staged": { + "*.ts?(x)": [ + "prettier --parser=typescript --write", + "git add" + ], + "*.{js,jsx,less,md,json}": [ + "prettier --write", + "git add" + ] }, "devDependencies": { - "cross-env": "^6.0.3", - "father-build": "^1.8.0", + "@testing-library/react": "^9.4.0", + "@testing-library/react-hooks": "^3.2.1", + "@types/jest": "^25.1.3", + "@types/node": "^13.7.7", <% if (withUmiUI) { -%> - "antd": "^4.0.0-alpha.0", + "@umijs/preset-ui": "^2.1.13", <% } -%> - "np": "^5.0.3", - "umi": "^2.9.0", - "umi-types": ">= 0.4.0-beta.4" + "@umijs/test": "^3.0.10", + "@umijs/test-utils": "^1.0.0", + "body-parser": "^1.18.2", + "cross-env": "^6.0.3", + "express": "^4.15.3", + "father-build": "^1.17.2", + "lerna": "^3.20.2", + "lint-staged": "^10.0.8", + "npm-run-all": "^4.1.5", + "pify": "^5.0.0", + "prettier": "^1.19.1", + "puppeteer": "^1.20.0", + "query-string": "^6.11.1", + "react": "^16.12.0", + "react-dom": "^16.12.0", + "react-test-renderer": "^16.9.0", + "test-umi-plugin": "^0.1.0", + "umi": "^3.1.0", + "yorkie": "^2.0.0" + }, + "gitHooks": { + "pre-commit": "lint-staged", + "commit-msg": "node scripts/verifyCommit.js" }, "files": [ <% if (withUmiUI) { -%> "dist", <% } -%> - "lib", - "src", - "ui" - ], - "license": "MIT" + "lib" + ] } diff --git a/lib/generators/plugin/templates/src/index.js b/lib/generators/plugin/templates/src/index.js index a657a30..5941eb9 100644 --- a/lib/generators/plugin/templates/src/index.js +++ b/lib/generators/plugin/templates/src/index.js @@ -1,22 +1,26 @@ // ref: -// - https://umijs.org/plugin/develop.html +// - https://umijs.org/plugins/api +<% if (withUmiUI) { -%> +import { join } from 'path'; +<% } -%> -export default function (api, options) { +export default function (api) { + api.logger.info('use plugin'); - // Example: output the webpack config - api.chainWebpackConfig(config => { - // console.log(config.toString()); + api.modifyHTML(($) => { + $('body').prepend(`

hello umi plugin

`); + return $; }); <% if (withUmiUI) { -%> - api.addUIPlugin(require.resolve('../dist/index.umd')); + api.addUIPlugin(() => join(__dirname, '../dist/index.umd.js')); api.onUISocket(({ action, failure, success }) => { - if (action.type === 'org.<%= author %>.<%= name %>.test') { + if (action.type === 'org.xiaohuoni.demo.test') { success({ - data: '<%= name %>.test', + data: 'demo.test', }); } }); -<% } %> +<% } -%> } diff --git a/lib/generators/plugin/templates/src/index.ts b/lib/generators/plugin/templates/src/index.ts index e1c9baf..8015b91 100644 --- a/lib/generators/plugin/templates/src/index.ts +++ b/lib/generators/plugin/templates/src/index.ts @@ -1,23 +1,29 @@ // ref: -// - https://umijs.org/plugin/develop.html -import { IApi } from 'umi-types'; +// - https://umijs.org/plugins/api +<% if (withUmiUI) { -%> +import { join } from 'path'; +<% } -%> +import { IApi } from '@umijs/types'; -export default function (api: IApi, options) { +export default function (api: IApi) { + api.logger.info('use plugin'); - // Example: output the webpack config - api.chainWebpackConfig(config => { - // console.log(config.toString()); + api.modifyHTML(($) => { + $('body').prepend(`

hello umi plugin

`); + return $; }); <% if (withUmiUI) { -%> - api.addUIPlugin(require.resolve('../dist/index.umd')); + // @ts-ignore + api.addUIPlugin(() => join(__dirname, '../dist/index.umd.js')); + // @ts-ignore api.onUISocket(({ action, failure, success }) => { - if (action.type === 'org.<%= author %>.<%= name %>.test') { + if (action.type === 'org.xiaohuoni.demo.test') { success({ - data: '<%= name %>.test', + data: 'demo.test', }); } }); -<% } %> +<% } -%> } diff --git a/lib/generators/plugin/templates/test/fixtures/normal/.umirc.js b/lib/generators/plugin/templates/test/fixtures/normal/.umirc.js new file mode 100644 index 0000000..ead8d16 --- /dev/null +++ b/lib/generators/plugin/templates/test/fixtures/normal/.umirc.js @@ -0,0 +1,4 @@ + +export default { + plugins: [require.resolve('../../../lib')] +} diff --git a/lib/generators/plugin/templates/test/fixtures/normal/.umirc.ts b/lib/generators/plugin/templates/test/fixtures/normal/.umirc.ts new file mode 100644 index 0000000..ead8d16 --- /dev/null +++ b/lib/generators/plugin/templates/test/fixtures/normal/.umirc.ts @@ -0,0 +1,4 @@ + +export default { + plugins: [require.resolve('../../../lib')] +} diff --git a/lib/generators/plugin/templates/test/fixtures/normal/pages/index.css b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.css new file mode 100644 index 0000000..32b1849 --- /dev/null +++ b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.css @@ -0,0 +1,4 @@ + +.normal { + background: #7F79F2; +} diff --git a/lib/generators/plugin/templates/test/fixtures/normal/pages/index.jsx b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.jsx new file mode 100755 index 0000000..cda40c2 --- /dev/null +++ b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.jsx @@ -0,0 +1,10 @@ + +import styles from './index.css'; + +export default function() { + return ( +
+

Page index

+
+ ); +} diff --git a/lib/generators/plugin/templates/example/pages/index.js b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.tsx similarity index 99% rename from lib/generators/plugin/templates/example/pages/index.js rename to lib/generators/plugin/templates/test/fixtures/normal/pages/index.tsx index b3069ce..1ec372f 100755 --- a/lib/generators/plugin/templates/example/pages/index.js +++ b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.tsx @@ -1,3 +1,4 @@ + import React from 'react'; import styles from './index.css'; diff --git a/lib/generators/plugin/templates/test/fixtures/normal/test.js b/lib/generators/plugin/templates/test/fixtures/normal/test.js new file mode 100644 index 0000000..a0f14a3 --- /dev/null +++ b/lib/generators/plugin/templates/test/fixtures/normal/test.js @@ -0,0 +1,10 @@ + +export default async function ({ page, host }) { + await page.goto(`${host}/`, { + waitUntil: 'networkidle2', + }); + const text = await page.evaluate( + () => document.querySelector('h1').innerHTML, + ); + expect(text).toEqual('hello umi plugin'); +}; diff --git a/lib/generators/plugin/templates/test/fixtures/normal/test.ts b/lib/generators/plugin/templates/test/fixtures/normal/test.ts new file mode 100644 index 0000000..a0f14a3 --- /dev/null +++ b/lib/generators/plugin/templates/test/fixtures/normal/test.ts @@ -0,0 +1,10 @@ + +export default async function ({ page, host }) { + await page.goto(`${host}/`, { + waitUntil: 'networkidle2', + }); + const text = await page.evaluate( + () => document.querySelector('h1').innerHTML, + ); + expect(text).toEqual('hello umi plugin'); +}; diff --git a/lib/generators/plugin/templates/test/index.e2e.js b/lib/generators/plugin/templates/test/index.e2e.js new file mode 100644 index 0000000..46067fd --- /dev/null +++ b/lib/generators/plugin/templates/test/index.e2e.js @@ -0,0 +1,5 @@ +const { join } = require('path'); + +require('test-umi-plugin')({ + fixtures: join(__dirname, 'fixtures'), +}); diff --git a/lib/generators/plugin/templates/test/index.e2e.ts b/lib/generators/plugin/templates/test/index.e2e.ts new file mode 100644 index 0000000..46067fd --- /dev/null +++ b/lib/generators/plugin/templates/test/index.e2e.ts @@ -0,0 +1,5 @@ +const { join } = require('path'); + +require('test-umi-plugin')({ + fixtures: join(__dirname, 'fixtures'), +}); diff --git a/lib/generators/plugin/templates/tsconfig.json b/lib/generators/plugin/templates/tsconfig.json index f555536..1d6b547 100644 --- a/lib/generators/plugin/templates/tsconfig.json +++ b/lib/generators/plugin/templates/tsconfig.json @@ -1,18 +1,23 @@ { "compilerOptions": { - "module": "esnext", "target": "esnext", - "lib": ["esnext", "dom"], - "sourceMap": true, - "baseUrl": ".", - "jsx": "react", - "allowSyntheticDefaultImports": true, "moduleResolution": "node", + "jsx": "preserve", + "allowJs": true, + "esModuleInterop": true, + "experimentalDecorators": true, + "strict": true, "forceConsistentCasingInFileNames": true, "noImplicitReturns": true, "suppressImplicitAnyIndexErrors": true, - "noUnusedLocals": true, - "experimentalDecorators": true, - "declaration": false - } + "declaration": true + }, + "exclude": [ + "node_modules", + "dist", + "**/*.spec.ts", + "lib", + "fixtures", + "examples" + ] } diff --git a/lib/generators/plugin/templates/ui/index.js b/lib/generators/plugin/templates/ui/index.jsx similarity index 80% rename from lib/generators/plugin/templates/ui/index.js rename to lib/generators/plugin/templates/ui/index.jsx index b0d175b..915fb1e 100644 --- a/lib/generators/plugin/templates/ui/index.js +++ b/lib/generators/plugin/templates/ui/index.jsx @@ -10,7 +10,7 @@ export default (api) => { type="primary" onClick={async () => { const { data } = await callRemote({ - type: 'org.<%= author %>.<%= name %>.test', + type: 'org.xiaohuoni.demo.test', }); alert(data); }} @@ -20,8 +20,8 @@ export default (api) => { } api.addPanel({ - title: '<%= name %>', - path: '/<%= name %>', + title: 'demo', + path: '/demo', icon: 'home', component: PluginPanel, }); diff --git a/lib/generators/plugin/templates/ui/index.tsx b/lib/generators/plugin/templates/ui/index.tsx index 2c751b1..15438e7 100644 --- a/lib/generators/plugin/templates/ui/index.tsx +++ b/lib/generators/plugin/templates/ui/index.tsx @@ -1,5 +1,5 @@ import { Button } from 'antd'; -import { IUiApi } from 'umi-types' +import { IUiApi } from '@umijs/ui-types'; export default (api: IUiApi) => { const { callRemote } = api; @@ -11,7 +11,7 @@ export default (api: IUiApi) => { type="primary" onClick={async () => { const { data } = await callRemote({ - type: 'org.<%= author %>.<%= name %>.test', + type: 'org.xiaohuoni.demo.test', }); alert(data); }} @@ -21,8 +21,8 @@ export default (api: IUiApi) => { } api.addPanel({ - title: '<%= name %>', - path: '/<%= name %>', + title: 'demo', + path: '/demo', icon: 'home', component: PluginPanel, });