Skip to content

Commit

Permalink
feat: support ant design mobile alll
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuoni committed Nov 24, 2021
1 parent 862bce0 commit d4239bc
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/layout/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"access": "public"
},
"dependencies": {
"@alitajs/alita-layout": "2.4.8"
"@alitajs/alita-layout": "2.4.9"
}
}
19 changes: 19 additions & 0 deletions packages/plugin-mobile5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @alitajs/plugin-antd-mobile

> @alitajs/plugin-antd-mobile.
See our website [@umijs/plugin-antd](https://umijs.org/plugins/plugin-antd) for more information.

## Install

Using npm:

```bash
$ npm install --save-dev @alitajs/plugin-antd-mobile
```

or using yarn:

```bash
$ yarn add @alitajs/plugin-antd-mobile --dev
```
35 changes: 35 additions & 0 deletions packages/plugin-mobile5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@alitajs/plugin-mobile5",
"version": "2.8.26-beta.2",
"description": "@alitajs/plugin-mobile5",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"src",
"templates"
],
"repository": {
"type": "git",
"url": "https://github.com/alitajs/alita"
},
"keywords": [
"umi"
],
"authors": [
"xiaohuoni <xiaohuoni@gmail.com> (https://github.com/xiaohuoni)"
],
"license": "MIT",
"bugs": "http://github.com/alitajs/alita/issues",
"homepage": "https://github.com/alitajs/alita/tree/master/packages/plugin-antd-mobile#readme",
"peerDependencies": {
"umi": "3.x"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"antd-mobile5": "npm:antd-mobile@next",
"fs-extra": "10.0.0"
}
}
58 changes: 58 additions & 0 deletions packages/plugin-mobile5/src/BaseGenerator/BaseGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { copyFileSync, statSync } from 'fs';
import { basename, dirname, join } from 'path';
import { prompts } from '@umijs/utils';
import fsExtra from 'fs-extra';
import Generator from '../Generator/Generator';

interface IOpts {
path: string;
target: string;
data?: any;
questions?: prompts.PromptObject[];
}

export default class BaseGenerator extends Generator {
path: string;
target: string;
data: any;
questions: prompts.PromptObject[];

constructor({ path, target, data, questions }: IOpts) {
super({ cwd: target, args: data });
this.path = path;
this.target = target;
this.data = data;
this.questions = questions || [];
}

prompting() {
return this.questions;
}

async writing() {
const context = {
...this.data,
...this.prompts,
};
if (statSync(this.path).isDirectory()) {
this.copyDirectory({
context,
path: this.path,
target: this.target,
});
} else {
const file = basename(this.path.replace(/\.tpl$/, ''));
if (this.path.endsWith('.tpl')) {
this.copyTpl({
templatePath: this.path,
target: join(this.target, file),
context,
});
} else {
const absTarget = join(this.target, file);
fsExtra.mkdirpSync(dirname(absTarget));
copyFileSync(this.path, absTarget);
}
}
}
}
67 changes: 67 additions & 0 deletions packages/plugin-mobile5/src/Generator/Generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { copyFileSync, readFileSync, statSync, writeFileSync } from 'fs';
import { dirname, join, relative } from 'path';
import { chalk, glob, Mustache, prompts, yParser } from '@umijs/utils';
import fsExtra from 'fs-extra';

interface IOpts {
cwd: string;
args: yParser.Arguments;
}

class Generator {
cwd: string;
args: yParser.Arguments;
prompts: any;

constructor({ cwd, args }: IOpts) {
this.cwd = cwd;
this.args = args;
this.prompts = {};
}

async run() {
const questions = this.prompting();
this.prompts = await prompts(questions);
await this.writing();
}

prompting() {
return [] as any;
}

async writing() {}

copyTpl(opts: { templatePath: string; target: string; context: object }) {
const tpl = readFileSync(opts.templatePath, 'utf-8');
const content = Mustache.render(tpl, opts.context);
fsExtra.mkdirpSync(dirname(opts.target));
console.log(`${chalk.green('Write:')} ${relative(this.cwd, opts.target)}`);
writeFileSync(opts.target, content, 'utf-8');
}

copyDirectory(opts: { path: string; context: object; target: string }) {
const files = glob.sync('**/*', {
cwd: opts.path,
dot: true,
ignore: ['**/node_modules/**'],
});
files.forEach((file: any) => {
const absFile = join(opts.path, file);
if (statSync(absFile).isDirectory()) return;
if (file.endsWith('.tpl')) {
this.copyTpl({
templatePath: absFile,
target: join(opts.target, file.replace(/\.tpl$/, '')),
context: opts.context,
});
} else {
console.log(`${chalk.green('Copy: ')} ${file}`);
const absTarget = join(opts.target, file);
fsExtra.mkdirpSync(dirname(absTarget));
copyFileSync(absFile, absTarget);
}
});
}
}

export default Generator;
48 changes: 48 additions & 0 deletions packages/plugin-mobile5/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { dirname, join } from 'path';
import { IApi } from '@umijs/types';
import BaseGenerator from './BaseGenerator/BaseGenerator';
import { existsSync } from 'fs';
export default (api: IApi) => {
api.describe({
key: 'mobile5',
config: {
schema(joi) {
return joi.boolean();
},
},
});
// 忽略用户安装,强制指定 mobile@5 版本
api.chainWebpack((memo) => {
if (!!api.config.mobile5) {
memo.resolve.alias.set(
'antd-mobile5',
join(dirname(require.resolve('antd-mobile5/package.json')), '2x'),
);
}
return memo;
});

api.onStart(async () => {
if (!!api.config.mobile5) {
if (
!existsSync(
`${api.paths.absNodeModulesPath}/@types/antd-mobile5/index.d.ts`,
)
) {
// logger.event('Create @types/antd-mobile5 Package');
const generator = new BaseGenerator({
path: join(__dirname, '..', 'templates', 'alias'),
target: `${api.paths.absNodeModulesPath}/@types/antd-mobile5`,
data: {
antdMobilePath: join(
dirname(require.resolve('antd-mobile5/package.json')),
),
antdMobile: 'antd-mobile5',
},
questions: [],
});
await generator.run();
}
}
});
};
1 change: 1 addition & 0 deletions packages/plugin-mobile5/templates/alias/index.d.ts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '{{{ antdMobilePath }}}'
13 changes: 13 additions & 0 deletions packages/plugin-mobile5/templates/alias/package.json.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "{{{ antdMobile }}}",
"version": "1.0.0",
"description": "",
"main": "",
"types": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
2 changes: 1 addition & 1 deletion packages/umi-presets-alita/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@alitajs/plugin-layout": "2.8.26-beta.2",
"@alitajs/router": "2.8.15",
"@alitajs/routes": "2.8.26-beta.2",
"@umijs/plugin-antd": "0.12.1",
"@umijs/plugin-antd": "0.13.0",
"@umijs/plugin-esbuild": "1.3.1",
"@umijs/plugin-helmet": "1.1.3",
"@umijs/plugin-request": "2.5.2"
Expand Down
3 changes: 2 additions & 1 deletion packages/umi-presets-alita/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default function (api: IApi) {
require.resolve('./plugins/features/packageId'),
require.resolve('./plugins/features/displayIcon'),
require.resolve('./plugins/features/noBuiltInPlugins'),
require.resolve('./plugins/features/mobile5'),
require.resolve('@alitajs/plugin-mobile5'),
require.resolve('@umijs/plugin-antd'),
require.resolve('@umijs/plugin-helmet'),
];
if (api.userConfig.appType !== 'pc') {
Expand Down
18 changes: 0 additions & 18 deletions packages/umi-presets-alita/src/plugins/features/mobile5.ts

This file was deleted.

Loading

0 comments on commit d4239bc

Please sign in to comment.