修改了router的自动构建方式

This commit is contained in:
Xu Chang 2023-12-24 11:58:15 +08:00
parent 0b19246569
commit 0cc9ecd725
7 changed files with 113 additions and 15 deletions

View File

@ -33,7 +33,7 @@ const createEnvironmentHash = require('./webpack/persistentCache/createEnvironme
const oakPathTsxPlugin = require('../babel-plugin/oakPath');
const oakRenderTsxPlugin = require('../babel-plugin/oakRender');
const oakRouterPlugin = require('../babel-plugin/oakRouter2');
// const oakRouterPlugin = require('../babel-plugin/oakRouter2');
const oakI18nPlugin = require('../babel-plugin/oakI18n');
const oakStylePlugin = require('../babel-plugin/oakStyle');
const oakRpxToPxPlugin = require('../postcss-plugin/oakRpxToPx');
@ -547,7 +547,7 @@ module.exports = function (webpackEnv) {
),
oakPathTsxPlugin,
oakRenderTsxPlugin,
oakRouterPlugin,
// oakRouterPlugin,
oakI18nPlugin,
// [
// 'import',

View File

@ -5,27 +5,34 @@ const tip_style_1 = require("./tip-style");
const cross_spawn_1 = tslib_1.__importDefault(require("cross-spawn"));
const path_1 = require("path");
const makeLocale_1 = tslib_1.__importDefault(require("./makeLocale"));
const makeRouter_1 = tslib_1.__importDefault(require("./makeRouter"));
const fs_1 = require("fs");
async function build(cmd) {
if (!cmd.target) {
(0, tip_style_1.Error)(`${(0, tip_style_1.error)(`Please add --target web or --target mp(wechatMp) or --target rn(native) to run the project in Web/WechatMp/ReactNative environment`)}`);
return;
}
let subdir = cmd.subDir;
if (!subdir) {
subdir = ['mp', 'wechatMp'].includes(cmd.target) ? 'wechatMp' : (['native', 'rn'].includes(cmd.target) ? 'native' : 'web');
}
// 先makeLocale
(0, makeLocale_1.default)('', true);
(0, makeLocale_1.default)('', cmd.mode === 'development');
// 再尝试makeRouter
(0, makeRouter_1.default)({ subdir }, cmd.mode === 'development');
//ts类型检查 waring 还是error,
//主要web受影响error级别的话 控制台和网页都报错warning级别的话 控制台报错
// development/staging/production
const TSC_COMPILE_ON_ERROR = cmd.check !== 'error';
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`build ${cmd.target} environment:${cmd.mode} ${cmd.mode !== 'production'
? `server:${!!cmd.prod}`
: ''} ${cmd.target !== 'web' && cmd.mode !== 'production'
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`build ${cmd.target} environment:${cmd.mode} ${['development'].includes(cmd.mode) ? `server:${!!cmd.prod}` : ''} ${['mp', 'wechatMp'].includes(cmd.target) &&
['development'].includes(cmd.mode)
? `split:${!!cmd.split}`
: ''}`)}`);
if (['mp', 'wechatMp'].includes(cmd.target)) {
const result = cross_spawn_1.default.sync(`cross-env`, [
`NODE_ENV=${cmd.mode}`,
`NODE_TARGET=${cmd.target}`,
`SUB_DIR_NAME=${cmd.subDir || 'wechatMp'}`,
`SUB_DIR_NAME=${subdir}`,
`TSC_COMPILE_ON_ERROR=${TSC_COMPILE_ON_ERROR}`,
`COMPILE_ANALYZE=${!!cmd.analyze}`,
`GENERATE_SOURCEMAP=${!!cmd.sourcemap}`,
@ -52,7 +59,7 @@ async function build(cmd) {
const result = cross_spawn_1.default.sync(`cross-env`, [
`NODE_ENV=${cmd.mode}`,
`NODE_TARGET=${cmd.target}`,
`SUB_DIR_NAME=${cmd.subDir || 'web'}`,
`SUB_DIR_NAME=${subdir}`,
`TSC_COMPILE_ON_ERROR=${TSC_COMPILE_ON_ERROR}`,
`COMPILE_ANALYZE=${!!cmd.analyze}`,
`GENERATE_SOURCEMAP=${!!cmd.sourcemap}`,
@ -76,7 +83,7 @@ async function build(cmd) {
}
else if (['native', 'rn'].includes(cmd.target)) {
const prjDir = process.cwd();
const cwd = (0, path_1.resolve)(prjDir, cmd.subDir || 'native');
const cwd = (0, path_1.resolve)(prjDir, subdir);
(0, fs_1.copyFileSync)((0, path_1.resolve)(prjDir, 'package.json'), (0, path_1.resolve)(cwd, 'package.json'));
// rn不支持注入NODE_ENVIRONMENT这样的环境变量cross-env没有用
/* const result = spawn.sync(
@ -95,7 +102,7 @@ async function build(cmd) {
'OAK_PLATFORM=native',
`PROD=${!!cmd.prod}`,
'react-native',
'start'
'start',
].filter(Boolean), {
cwd,
stdio: 'inherit',

1
lib/makeRouter.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export default function make(cmd: any, watch?: boolean): Promise<void>;

32
lib/makeRouter.js Normal file
View File

@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const tip_style_1 = require("./tip-style");
const cross_spawn_1 = tslib_1.__importDefault(require("cross-spawn"));
async function make(cmd, watch) {
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`make router`)}`);
// node scripts/make-router.js subdir watch
const args = [require.resolve('../scripts/' + 'make-router.js'), cmd.subdir];
if (watch) {
args.push('true');
const result = (0, cross_spawn_1.default)('node', args, {
stdio: 'inherit',
shell: true,
});
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`make router执行监控中……`)}`);
return;
}
const result = cross_spawn_1.default.sync('node', args, {
stdio: 'inherit',
shell: true,
});
// const result2 = spawn.sync('npm -v', [], { stdio: 'inherit', shell: true });
if (result.status === 0) {
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`make router执行完成`)}`);
}
else {
(0, tip_style_1.Error)(`${(0, tip_style_1.error)(`make router执行失败`)}`);
process.exit(-1);
}
}
exports.default = make;

4
scripts/make-router.js Normal file
View File

@ -0,0 +1,4 @@
const subdir = process.argv[2];
const watch = process.argv[3];
const { buildRouter } = require('oak-domain/lib/compiler/routerBuilder');
buildRouter(process.cwd(), subdir, !!watch);

View File

@ -10,6 +10,7 @@ import {
import spawn from 'cross-spawn';
import { resolve } from 'path';
import makeLocale from './makeLocale';
import makeRouter from './makeRouter';
import { copyFileSync } from 'fs';
export default async function build(cmd: any) {
@ -21,8 +22,16 @@ export default async function build(cmd: any) {
);
return;
}
let subdir = cmd.subDir;
if (!subdir) {
subdir = ['mp', 'wechatMp'].includes(cmd.target) ? 'wechatMp' : (
['native', 'rn'].includes(cmd.target) ? 'native' : 'web'
);
}
// 先makeLocale
makeLocale('', true);
makeLocale('', cmd.mode === 'development');
// 再尝试makeRouter
makeRouter({ subdir }, cmd.mode === 'development');
//ts类型检查 waring 还是error,
//主要web受影响error级别的话 控制台和网页都报错warning级别的话 控制台报错
// development/staging/production
@ -45,7 +54,7 @@ export default async function build(cmd: any) {
[
`NODE_ENV=${cmd.mode}`,
`NODE_TARGET=${cmd.target}`,
`SUB_DIR_NAME=${cmd.subDir || 'wechatMp'}`,
`SUB_DIR_NAME=${subdir}`,
`TSC_COMPILE_ON_ERROR=${TSC_COMPILE_ON_ERROR}`,
`COMPILE_ANALYZE=${!!cmd.analyze}`,
`GENERATE_SOURCEMAP=${!!cmd.sourcemap}`,
@ -78,7 +87,7 @@ export default async function build(cmd: any) {
[
`NODE_ENV=${cmd.mode}`,
`NODE_TARGET=${cmd.target}`,
`SUB_DIR_NAME=${cmd.subDir || 'web'}`,
`SUB_DIR_NAME=${subdir}`,
`TSC_COMPILE_ON_ERROR=${TSC_COMPILE_ON_ERROR}`,
`COMPILE_ANALYZE=${!!cmd.analyze}`,
`GENERATE_SOURCEMAP=${!!cmd.sourcemap}`,
@ -86,7 +95,7 @@ export default async function build(cmd: any) {
!!cmd.memoryLimit && `MEMORY_LIMIT=${cmd.memoryLimit}`,
`node`,
cmd.memoryLimit && `--max_old_space_size=${cmd.memoryLimit}`,
resolve(
require.resolve(
`../scripts/${
cmd.mode === 'production'
? 'build-web.js'
@ -106,7 +115,7 @@ export default async function build(cmd: any) {
}
} else if (['native', 'rn'].includes(cmd.target)) {
const prjDir = process.cwd();
const cwd = resolve(prjDir, cmd.subDir || 'native');
const cwd = resolve(prjDir, subdir);
copyFileSync(
resolve(prjDir, 'package.json'),
resolve(cwd, 'package.json')

45
src/makeRouter.ts Normal file
View File

@ -0,0 +1,45 @@
import {
Success,
Error,
error,
primary,
success,
warn,
Warn,
} from './tip-style';
import spawn from 'cross-spawn';
export default async function make(cmd: any, watch?: boolean) {
Success(`${success(`make router`)}`);
// node scripts/make-router.js subdir watch
const args = [require.resolve('../scripts/' + 'make-router.js'), cmd.subdir];
if (watch) {
args.push('true');
const result = spawn(
'node',
args,
{
stdio: 'inherit',
shell: true,
}
);
Success(`${success(`make router执行监控中……`)}`);
return;
}
const result = spawn.sync(
'node',
args,
{
stdio: 'inherit',
shell: true,
}
);
// const result2 = spawn.sync('npm -v', [], { stdio: 'inherit', shell: true });
if (result.status === 0) {
Success(`${success(`make router执行完成`)}`);
} else {
Error(`${error(`make router执行失败`)}`);
process.exit(-1);
}
}