From 1a7a3e282d079d5057b250f42328f91c02f04ded Mon Sep 17 00:00:00 2001
From: xiaohuoni <448627663@qq.com>
Date: Wed, 29 Apr 2020 15:42:25 +0800
Subject: [PATCH 1/9] feat: Upgrade to Umi 3
---
lib/generators/plugin/index.js | 18 ++---
lib/generators/plugin/templates/.editorconfig | 16 ++++
lib/generators/plugin/templates/.fatherrc.js | 15 ----
lib/generators/plugin/templates/.fatherrc.ts | 41 ++++++----
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/_gitignore | 11 ---
.../plugin/templates/example/.gitignore | 5 ++
.../plugin/templates/example/.umirc.js | 10 ---
.../plugin/templates/example/.umirc.ts | 17 ++---
.../plugin/templates/example/app.tsx | 3 +
.../plugin/templates/example/package.json | 4 +-
.../plugin/templates/example/pages/index.css | 15 +++-
.../plugin/templates/example/pages/index.tsx | 8 +-
.../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 +
.../test/fixtures/normal/pages/index.css | 4 +
.../fixtures/normal}/pages/index.js | 2 +-
.../templates/test/fixtures/normal/test.js | 10 +++
.../plugin/templates/test/index.e2e.js | 5 ++
lib/generators/plugin/templates/tsconfig.json | 25 ++++---
lib/generators/plugin/templates/ui/index.js | 28 -------
lib/generators/plugin/templates/ui/index.tsx | 8 +-
31 files changed, 306 insertions(+), 200 deletions(-)
create mode 100644 lib/generators/plugin/templates/.editorconfig
delete mode 100644 lib/generators/plugin/templates/.fatherrc.js
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
delete mode 100644 lib/generators/plugin/templates/example/.umirc.js
create mode 100644 lib/generators/plugin/templates/example/app.tsx
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
delete mode 100644 lib/generators/plugin/templates/src/index.js
create mode 100644 lib/generators/plugin/templates/test/fixtures/normal/.umirc.js
create mode 100644 lib/generators/plugin/templates/test/fixtures/normal/pages/index.css
rename lib/generators/plugin/templates/{example => test/fixtures/normal}/pages/index.js (85%)
create mode 100644 lib/generators/plugin/templates/test/fixtures/normal/test.js
create mode 100644 lib/generators/plugin/templates/test/index.e2e.js
delete mode 100644 lib/generators/plugin/templates/ui/index.js
diff --git a/lib/generators/plugin/index.js b/lib/generators/plugin/index.js
index 7f90107..de2e00e 100644
--- a/lib/generators/plugin/index.js
+++ b/lib/generators/plugin/index.js
@@ -24,12 +24,12 @@ class Generator extends BasicGenerator {
name: 'org',
message: `Which organization is your plugin stored under github?`,
},
- {
- name: 'isTypeScript',
- type: 'confirm',
- message: 'Do you want to use typescript?',
- default: false,
- },
+ // {
+ // name: 'isTypeScript',
+ // type: 'confirm',
+ // message: 'Do you want to use typescript?',
+ // default: false,
+ // },
{
name: 'withUmiUI',
type: 'confirm',
@@ -44,9 +44,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.js';
return file === uiFile;
}
@@ -54,7 +52,7 @@ 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;
// filter ui files
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
deleted file mode 100644
index 0109985..0000000
--- a/lib/generators/plugin/templates/.fatherrc.js
+++ /dev/null
@@ -1,15 +0,0 @@
-
-export default [
- {
- cjs: 'babel',
- },
-<% if (withUmiUI) { -%>
- {
- entry: 'ui/index.js',
- umd: {
- name: '<%= name %>',
- minFile: false,
- },
- },
-<% } -%>
-];
diff --git a/lib/generators/plugin/templates/.fatherrc.ts b/lib/generators/plugin/templates/.fatherrc.ts
index 602e842..1d56a04 100644
--- a/lib/generators/plugin/templates/.fatherrc.ts
+++ b/lib/generators/plugin/templates/.fatherrc.ts
@@ -1,18 +1,31 @@
-
-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/_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
deleted file mode 100644
index fe9a16d..0000000
--- a/lib/generators/plugin/templates/example/.umirc.js
+++ /dev/null
@@ -1,10 +0,0 @@
-import { join } from 'path';
-
-export default {
- routes: [
- { path: '/', component: './index' },
- ],
- plugins: [
- join(__dirname, '..', require('../package').main || 'index.js'),
- ],
-}
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.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.tsx b/lib/generators/plugin/templates/example/pages/index.tsx
old mode 100755
new mode 100644
index b3069ce..8f6ae14
--- a/lib/generators/plugin/templates/example/pages/index.tsx
+++ b/lib/generators/plugin/templates/example/pages/index.tsx
@@ -1,10 +1,12 @@
import React from 'react';
+
import styles from './index.css';
-export default function() {
+export default () => {
+
return (
-
Page index
+ 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
deleted file mode 100644
index a657a30..0000000
--- a/lib/generators/plugin/templates/src/index.js
+++ /dev/null
@@ -1,22 +0,0 @@
-// ref:
-// - https://umijs.org/plugin/develop.html
-
-export default function (api, options) {
-
- // Example: output the webpack config
- api.chainWebpackConfig(config => {
- // console.log(config.toString());
- });
-
-<% if (withUmiUI) { -%>
- api.addUIPlugin(require.resolve('../dist/index.umd'));
-
- api.onUISocket(({ action, failure, success }) => {
- if (action.type === 'org.<%= author %>.<%= name %>.test') {
- success({
- data: '<%= name %>.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/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/example/pages/index.js b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.js
similarity index 85%
rename from lib/generators/plugin/templates/example/pages/index.js
rename to lib/generators/plugin/templates/test/fixtures/normal/pages/index.js
index b3069ce..cda40c2 100755
--- a/lib/generators/plugin/templates/example/pages/index.js
+++ b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.js
@@ -1,4 +1,4 @@
-import React from 'react';
+
import styles from './index.css';
export default function() {
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/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/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.js
deleted file mode 100644
index b0d175b..0000000
--- a/lib/generators/plugin/templates/ui/index.js
+++ /dev/null
@@ -1,28 +0,0 @@
-import { Button } from 'antd';
-
-export default (api) => {
- const { callRemote } = api;
-
- function PluginPanel() {
- return (
-
-
-
- );
- }
-
- api.addPanel({
- title: '<%= name %>',
- path: '/<%= name %>',
- 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,
});
From 808d84cca548fa046a6bdcb751252634b4a4b0c6 Mon Sep 17 00:00:00 2001
From: xiaohuoni <448627663@qq.com>
Date: Wed, 29 Apr 2020 15:47:53 +0800
Subject: [PATCH 2/9] =?UTF-8?q?fix:=20=E6=96=87=E4=BB=B6=E6=9C=AA=E4=BF=9D?=
=?UTF-8?q?=E5=AD=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
lib/generators/plugin/templates/.fatherrc.ts | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/lib/generators/plugin/templates/.fatherrc.ts b/lib/generators/plugin/templates/.fatherrc.ts
index 1d56a04..fe073d3 100644
--- a/lib/generators/plugin/templates/.fatherrc.ts
+++ b/lib/generators/plugin/templates/.fatherrc.ts
@@ -1,3 +1,7 @@
+<% if (withUmiUI) { -%>
+const isProd = process.env.NODE_ENV === 'prod';
+
+<% } -%>
export default [{
target: 'node',
cjs: { type: 'babel', lazy: true },
From d9e018c3dc08f8a7305e57d6cd6483e1c63e9dc1 Mon Sep 17 00:00:00 2001
From: xiaohuoni <448627663@qq.com>
Date: Wed, 29 Apr 2020 15:51:18 +0800
Subject: [PATCH 3/9] chore: change Usage
---
lib/generators/plugin/templates/README.md | 13 ++-----------
1 file changed, 2 insertions(+), 11 deletions(-)
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 %>'],
],
}
```
From 2b2ae452ac63ab978b5d46edf65c32f9fda761f5 Mon Sep 17 00:00:00 2001
From: xiaohuoni <448627663@qq.com>
Date: Wed, 29 Apr 2020 16:05:12 +0800
Subject: [PATCH 4/9] feat: Support JavaScript
---
lib/generators/plugin/index.js | 12 +++----
lib/generators/plugin/templates/.fatherrc.js | 35 +++++++++++++++++++
.../plugin/templates/example/.umirc.js | 8 +++++
.../plugin/templates/example/app.jsx | 3 ++
.../plugin/templates/example/pages/index.jsx | 13 +++++++
.../plugin/templates/example/pages/index.tsx | 1 +
lib/generators/plugin/templates/src/index.js | 26 ++++++++++++++
lib/generators/plugin/templates/ui/index.jsx | 28 +++++++++++++++
8 files changed, 120 insertions(+), 6 deletions(-)
create mode 100644 lib/generators/plugin/templates/.fatherrc.js
create mode 100644 lib/generators/plugin/templates/example/.umirc.js
create mode 100644 lib/generators/plugin/templates/example/app.jsx
create mode 100644 lib/generators/plugin/templates/example/pages/index.jsx
create mode 100644 lib/generators/plugin/templates/src/index.js
create mode 100644 lib/generators/plugin/templates/ui/index.jsx
diff --git a/lib/generators/plugin/index.js b/lib/generators/plugin/index.js
index de2e00e..eb4586b 100644
--- a/lib/generators/plugin/index.js
+++ b/lib/generators/plugin/index.js
@@ -24,12 +24,12 @@ class Generator extends BasicGenerator {
name: 'org',
message: `Which organization is your plugin stored under github?`,
},
- // {
- // name: 'isTypeScript',
- // type: 'confirm',
- // message: 'Do you want to use typescript?',
- // default: false,
- // },
+ {
+ name: 'isTypeScript',
+ type: 'confirm',
+ message: 'Do you want to use typescript?',
+ default: false,
+ },
{
name: 'withUmiUI',
type: 'confirm',
diff --git a/lib/generators/plugin/templates/.fatherrc.js b/lib/generators/plugin/templates/.fatherrc.js
new file mode 100644
index 0000000..fe073d3
--- /dev/null
+++ b/lib/generators/plugin/templates/.fatherrc.js
@@ -0,0 +1,35 @@
+<% if (withUmiUI) { -%>
+const isProd = process.env.NODE_ENV === 'prod';
+
+<% } -%>
+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',
+ 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/example/.umirc.js b/lib/generators/plugin/templates/example/.umirc.js
new file mode 100644
index 0000000..c8fa776
--- /dev/null
+++ b/lib/generators/plugin/templates/example/.umirc.js
@@ -0,0 +1,8 @@
+import { defineConfig } from 'umi';
+
+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/pages/index.jsx b/lib/generators/plugin/templates/example/pages/index.jsx
new file mode 100644
index 0000000..4f4f2bf
--- /dev/null
+++ b/lib/generators/plugin/templates/example/pages/index.jsx
@@ -0,0 +1,13 @@
+
+import React from 'react';
+
+import styles from './index.css';
+
+export default () => {
+
+ return (
+
+ Hello Umi!
+
+ );
+};
diff --git a/lib/generators/plugin/templates/example/pages/index.tsx b/lib/generators/plugin/templates/example/pages/index.tsx
index 8f6ae14..4f4f2bf 100644
--- a/lib/generators/plugin/templates/example/pages/index.tsx
+++ b/lib/generators/plugin/templates/example/pages/index.tsx
@@ -1,3 +1,4 @@
+
import React from 'react';
import styles from './index.css';
diff --git a/lib/generators/plugin/templates/src/index.js b/lib/generators/plugin/templates/src/index.js
new file mode 100644
index 0000000..5941eb9
--- /dev/null
+++ b/lib/generators/plugin/templates/src/index.js
@@ -0,0 +1,26 @@
+// ref:
+// - https://umijs.org/plugins/api
+<% if (withUmiUI) { -%>
+import { join } from 'path';
+<% } -%>
+
+export default function (api) {
+ api.logger.info('use plugin');
+
+ api.modifyHTML(($) => {
+ $('body').prepend(`hello umi plugin
`);
+ return $;
+ });
+
+<% if (withUmiUI) { -%>
+ api.addUIPlugin(() => join(__dirname, '../dist/index.umd.js'));
+
+ api.onUISocket(({ action, failure, success }) => {
+ if (action.type === 'org.xiaohuoni.demo.test') {
+ success({
+ data: 'demo.test',
+ });
+ }
+ });
+<% } -%>
+}
diff --git a/lib/generators/plugin/templates/ui/index.jsx b/lib/generators/plugin/templates/ui/index.jsx
new file mode 100644
index 0000000..915fb1e
--- /dev/null
+++ b/lib/generators/plugin/templates/ui/index.jsx
@@ -0,0 +1,28 @@
+import { Button } from 'antd';
+
+export default (api) => {
+ const { callRemote } = api;
+
+ function PluginPanel() {
+ return (
+
+
+
+ );
+ }
+
+ api.addPanel({
+ title: 'demo',
+ path: '/demo',
+ icon: 'home',
+ component: PluginPanel,
+ });
+}
From 21241a332d4359bfaaf46d8322073d448cc3c357 Mon Sep 17 00:00:00 2001
From: xiaohuoni <448627663@qq.com>
Date: Wed, 29 Apr 2020 16:17:32 +0800
Subject: [PATCH 5/9] chore: change prompt type
---
lib/generators/plugin/index.js | 16 +++++++++++++---
.../plugin/templates/example/pages/index.jsx | 2 --
.../plugin/templates/example/pages/index.tsx | 2 --
3 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/lib/generators/plugin/index.js b/lib/generators/plugin/index.js
index eb4586b..e0f971e 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',
diff --git a/lib/generators/plugin/templates/example/pages/index.jsx b/lib/generators/plugin/templates/example/pages/index.jsx
index 4f4f2bf..2682ed8 100644
--- a/lib/generators/plugin/templates/example/pages/index.jsx
+++ b/lib/generators/plugin/templates/example/pages/index.jsx
@@ -1,6 +1,4 @@
-
import React from 'react';
-
import styles from './index.css';
export default () => {
diff --git a/lib/generators/plugin/templates/example/pages/index.tsx b/lib/generators/plugin/templates/example/pages/index.tsx
index 4f4f2bf..2682ed8 100644
--- a/lib/generators/plugin/templates/example/pages/index.tsx
+++ b/lib/generators/plugin/templates/example/pages/index.tsx
@@ -1,6 +1,4 @@
-
import React from 'react';
-
import styles from './index.css';
export default () => {
From 17931368fbb319427f6d823538f810879076a8d6 Mon Sep 17 00:00:00 2001
From: xiaohuoni <448627663@qq.com>
Date: Wed, 29 Apr 2020 16:20:20 +0800
Subject: [PATCH 6/9] chore: change example demo
---
.../plugin/templates/example/pages/index.jsx | 13 +++++--------
.../plugin/templates/example/pages/index.tsx | 13 +++++--------
2 files changed, 10 insertions(+), 16 deletions(-)
diff --git a/lib/generators/plugin/templates/example/pages/index.jsx b/lib/generators/plugin/templates/example/pages/index.jsx
index 2682ed8..70090be 100644
--- a/lib/generators/plugin/templates/example/pages/index.jsx
+++ b/lib/generators/plugin/templates/example/pages/index.jsx
@@ -1,11 +1,8 @@
import React from 'react';
import styles from './index.css';
-export default () => {
-
- return (
-
- Hello Umi!
-
- );
-};
+export default () => (
+
+ Hello Umi!
+
+);
diff --git a/lib/generators/plugin/templates/example/pages/index.tsx b/lib/generators/plugin/templates/example/pages/index.tsx
index 2682ed8..70090be 100644
--- a/lib/generators/plugin/templates/example/pages/index.tsx
+++ b/lib/generators/plugin/templates/example/pages/index.tsx
@@ -1,11 +1,8 @@
import React from 'react';
import styles from './index.css';
-export default () => {
-
- return (
-
- Hello Umi!
-
- );
-};
+export default () => (
+
+ Hello Umi!
+
+);
From 410048d3714d655c7c4ccc4e098766abd6245a4b Mon Sep 17 00:00:00 2001
From: xiaohuoni <448627663@qq.com>
Date: Wed, 29 Apr 2020 17:05:19 +0800
Subject: [PATCH 7/9] fix: js file
---
lib/generators/plugin/index.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/generators/plugin/index.js b/lib/generators/plugin/index.js
index e0f971e..0d1b866 100644
--- a/lib/generators/plugin/index.js
+++ b/lib/generators/plugin/index.js
@@ -54,7 +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;
}
From 53a2d823eb6056a795ac2cdb37b7e236373863f6 Mon Sep 17 00:00:00 2001
From: xiaohuoni <448627663@qq.com>
Date: Wed, 29 Apr 2020 17:07:53 +0800
Subject: [PATCH 8/9] fix: js file
---
lib/generators/plugin/index.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/generators/plugin/index.js b/lib/generators/plugin/index.js
index 0d1b866..c7105b8 100644
--- a/lib/generators/plugin/index.js
+++ b/lib/generators/plugin/index.js
@@ -64,7 +64,7 @@ class Generator extends BasicGenerator {
filterFiles: f => {
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 {
From fe7b5da2d694f3ff89a9dc54d754f94db31aeb65 Mon Sep 17 00:00:00 2001
From: xiaohuoni <448627663@qq.com>
Date: Wed, 29 Apr 2020 17:34:23 +0800
Subject: [PATCH 9/9] fix: ts file
---
.../plugin/templates/test/fixtures/normal/.umirc.ts | 4 ++++
.../fixtures/normal/pages/{index.js => index.jsx} | 0
.../templates/test/fixtures/normal/pages/index.tsx | 11 +++++++++++
.../plugin/templates/test/fixtures/normal/test.ts | 10 ++++++++++
lib/generators/plugin/templates/test/index.e2e.ts | 5 +++++
5 files changed, 30 insertions(+)
create mode 100644 lib/generators/plugin/templates/test/fixtures/normal/.umirc.ts
rename lib/generators/plugin/templates/test/fixtures/normal/pages/{index.js => index.jsx} (100%)
create mode 100755 lib/generators/plugin/templates/test/fixtures/normal/pages/index.tsx
create mode 100644 lib/generators/plugin/templates/test/fixtures/normal/test.ts
create mode 100644 lib/generators/plugin/templates/test/index.e2e.ts
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.js b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.jsx
similarity index 100%
rename from lib/generators/plugin/templates/test/fixtures/normal/pages/index.js
rename to lib/generators/plugin/templates/test/fixtures/normal/pages/index.jsx
diff --git a/lib/generators/plugin/templates/test/fixtures/normal/pages/index.tsx b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.tsx
new file mode 100755
index 0000000..1ec372f
--- /dev/null
+++ b/lib/generators/plugin/templates/test/fixtures/normal/pages/index.tsx
@@ -0,0 +1,11 @@
+
+import React from 'react';
+import styles from './index.css';
+
+export default function() {
+ return (
+
+
Page index
+
+ );
+}
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.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'),
+});