1、启动命令 增加--devMode 区分前后台模式

2、根据环境变量加载文件后缀prod、dev、staging
This commit is contained in:
wkj 2024-04-23 19:37:12 +08:00
parent 25e4d022e0
commit 93d26e8995
14 changed files with 174 additions and 80 deletions

View File

@ -59,6 +59,7 @@ function getClientEnvironment() {
// process.env.OAK_PLATFORM: wechatMp | web | node
OAK_PLATFORM: 'wechatMp',
PROD: process.env.PROD,
OAK_DEV_MODE: process.env.OAK_DEV_MODE || 'server',
}
);
// Stringify all values so we can feed into webpack DefinePlugin

View File

@ -17,17 +17,37 @@ let moduleFileExtensions = [
'mp.ts',
'ts',
];
if (
process.env.NODE_ENV === 'production' ||
process.env.NODE_ENV === 'staging' ||
process.env.PROD === 'true'
) {
if (process.env.PROD === 'true' || process.env.OAK_DEV_MODE === 'server') {
moduleFileExtensions = [
'prod.mp.js',
'prod.mp.ts',
'prod.js',
'prod.ts',
'server.js',
'server.ts',
].concat(moduleFileExtensions);
} else {
moduleFileExtensions = [
'frontend.js',
'frontend.ts',
].concat(moduleFileExtensions);
}
const prodFileExtensions = ['prod.mp.js', 'prod.mp.ts', 'prod.js', 'prod.ts'];
if (
process.env.NODE_ENV === 'production'
) {
moduleFileExtensions = prodFileExtensions.concat(moduleFileExtensions);
}
else if (process.env.NODE_ENV === 'staging') {
moduleFileExtensions = [
'staging.mp.js',
'staging.mp.ts',
'staging.js',
'staging.ts',
]
.concat(prodFileExtensions)
.concat(moduleFileExtensions);
} else {
moduleFileExtensions = [
'dev.mp.js',

View File

@ -4,6 +4,7 @@ const assert = require('assert');
const REPLACE_HOLDERS = {
OAK_PLATFORM: 'native',
PROD: process.env.PROD,
OAK_DEV_MODE: process.env.OAK_DEV_MODE,
};
function replaceEnvExpressionPlugin() {

View File

@ -5,44 +5,65 @@ const watchFolders = [
'../../oak-frontend-base', '../../oak-general-business', '../../oak-memory-tree-store'
];
const sourceExts =
process.env.NODE_ENV === 'production' ||
process.env.NODE_ENV === 'staging' ||
process.env.PROD === 'true'
? [
'prod.native.js',
'prod.native.ts',
'prod.native.jsx',
'prod.native.tsx',
'prod.js',
'prod.ts',
'prod.jsx',
'prod.tsx',
'js',
'ts',
'jsx',
'tsx',
'less',
'json',
'svg',
]
: [
'dev.native.js',
'dev.native.ts',
'dev.native.jsx',
'dev.native.tsx',
'dev.js',
'dev.ts',
'dev.jsx',
'dev.tsx',
'js',
'ts',
'jsx',
'tsx',
'less',
'json',
'svg',
];
let moduleFileExtensions = ['js', 'ts', 'jsx', 'tsx', 'less', 'json', 'svg'];
if (process.env.PROD === 'true' || process.env.OAK_DEV_MODE === 'server') {
moduleFileExtensions = [
'server.js',
'server.ts',
'server.tsx',
'server.jsx',
].concat(moduleFileExtensions);
} else {
moduleFileExtensions = [
'frontend.js',
'frontend.ts',
'frontend.tsx',
'frontend.jsx',
].concat(moduleFileExtensions);
}
const prodFileExtensions = [
'prod.native.js',
'prod.native.ts',
'prod.native.jsx',
'prod.native.tsx',
'prod.js',
'prod.ts',
'prod.jsx',
'prod.tsx',
];
if (process.env.NODE_ENV === 'production') {
moduleFileExtensions = prodFileExtensions.concat(moduleFileExtensions);
}
else if (process.env.NODE_ENV === 'staging') {
moduleFileExtensions = [
'staging.native.js',
'staging.native.ts',
'staging.native.jsx',
'staging.native.tsx',
'staging.js',
'staging.ts',
'staging.jsx',
'staging.tsx',
]
.concat(prodFileExtensions)
.concat(moduleFileExtensions);
} else {
moduleFileExtensions = [
'dev.native.js',
'dev.native.ts',
'dev.native.jsx',
'dev.native.tsx',
'dev.js',
'dev.ts',
'dev.jsx',
'dev.tsx',
].concat(moduleFileExtensions);
}
const sourceExts = moduleFileExtensions;
const NullModules = ['fs', 'url'];
/**

View File

@ -88,9 +88,10 @@ function getClientEnvironment(publicUrl) {
// Whether or not react-refresh is enabled.
// It is defined here so it is available in the webpackHotDevClient.
FAST_REFRESH: process.env.FAST_REFRESH !== 'false',
// process.env.OAK_PLATFORM: wechatMp | web | node
// process.env.OAK_PLATFORM: wechatMp | web | native
OAK_PLATFORM: 'web',
PROD: process.env.PROD,
OAK_DEV_MODE: process.env.OAK_DEV_MODE || 'server',
}
);
// Stringify all values so we can feed into webpack DefinePlugin

View File

@ -40,21 +40,52 @@ let moduleFileExtensions = [
'pc.jsx',
'jsx',
];
if (
process.env.NODE_ENV === 'production' ||
process.env.NODE_ENV === 'staging' ||
process.env.PROD === 'true'
) {
if (process.env.PROD === 'true' || process.env.OAK_DEV_MODE === 'server') {
moduleFileExtensions = [
'prod.web.js',
'prod.web.ts',
'prod.web.jsx',
'prod.web.tsx',
'prod.js',
'prod.ts',
'prod.jsx',
'prod.tsx',
'server.mjs',
'server.js',
'server.ts',
'server.tsx',
'server.jsx',
].concat(moduleFileExtensions);
}
else {
moduleFileExtensions = [
'frontend.mjs',
'frontend.js',
'frontend.ts',
'frontend.tsx',
'frontend.jsx',
].concat(moduleFileExtensions);
}
const prodFileExtensions = [
'prod.web.js',
'prod.web.ts',
'prod.web.jsx',
'prod.web.tsx',
'prod.js',
'prod.ts',
'prod.jsx',
'prod.tsx',
];
if (process.env.NODE_ENV === 'production') {
moduleFileExtensions = prodFileExtensions.concat(moduleFileExtensions);
} else if (process.env.NODE_ENV === 'staging') {
moduleFileExtensions = [
'staging.web.js',
'staging.web.ts',
'staging.web.jsx',
'staging.web.tsx',
'staging.js',
'staging.ts',
'staging.jsx',
'staging.tsx',
]
.concat(prodFileExtensions)
.concat(moduleFileExtensions);
} else {
moduleFileExtensions = [
'dev.web.js',

View File

@ -10,6 +10,7 @@ const fs_1 = require("fs");
async function build(cmd) {
const mode = (cmd.mode || 'development');
const target = cmd.target;
const devMode = (cmd.devMode || 'server');
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;
@ -34,7 +35,7 @@ async function build(cmd) {
//主要web受影响error级别的话 控制台和网页都报错warning级别的话 控制台报错
// development/staging/production
const errorLevel = cmd.check !== 'error';
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`build ${target} environment:${mode} ${['development'].includes(mode) ? `server:${!!cmd.prod}` : ''} ${['mp', 'wechatMp'].includes(target) &&
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`build ${target} environment:${mode} ${['development'].includes(mode) ? `devMode:${devMode}` : ''} ${['mp', 'wechatMp'].includes(target) &&
['development'].includes(mode)
? `split:${!!cmd.split}`
: ''}`)}`);
@ -47,6 +48,7 @@ async function build(cmd) {
const result = cross_spawn_1.default.sync(`cross-env`, [
`NODE_ENV=${mode}`,
`NODE_TARGET=${target}`,
`OAK_DEV_MODE=${devMode}`,
`SUB_DIR_NAME=${subdir}`,
`TSC_COMPILE_ON_ERROR=${errorLevel}`,
`COMPILE_ANALYZE=${!!cmd.analyze}`,
@ -78,6 +80,7 @@ async function build(cmd) {
const result = cross_spawn_1.default.sync(`cross-env`, [
`NODE_ENV=${mode}`,
`NODE_TARGET=${target}`,
`OAK_DEV_MODE=${devMode}`,
`SUB_DIR_NAME=${subdir}`,
`TSC_COMPILE_ON_ERROR=${errorLevel}`,
`COMPILE_ANALYZE=${!!cmd.analyze}`,
@ -112,6 +115,7 @@ async function build(cmd) {
'&& cross-env',
`NODE_ENV=${mode}`,
'OAK_PLATFORM=native',
`OAK_DEV_MODE=${devMode}`,
'./gradlew assembleRelease',
].filter(Boolean), {
cwd,
@ -125,6 +129,7 @@ async function build(cmd) {
'&& cross-env',
`NODE_ENV=${mode}`,
'OAK_PLATFORM=native',
`OAK_DEV_MODE=${devMode}`,
'./gradlew assembleStaging',
].filter(Boolean), {
cwd,
@ -137,6 +142,7 @@ async function build(cmd) {
`NODE_ENV=${mode}`,
'OAK_PLATFORM=native',
`PROD=${!!cmd.prod}`,
`OAK_DEV_MODE=${devMode}`,
'react-native',
'start',
].filter(Boolean), {

View File

@ -231,7 +231,7 @@ async function create(dirName, cmd) {
}
// 获取package.json内容
const packageJson = (0, template_1.packageJsonContent)({
name: DEFAULT_PROJECT_NAME,
name: DEFAULT_PROJECT_NAME, // 后面再统一rename
version,
description,
cliName: config_1.CLI_NAME,

View File

@ -69,6 +69,7 @@ commander_1.default
.option('-d, --subDir <subDirName>', 'subDirName')
.option('-c, --check <level>', 'level')
.option('-p, --port <port>', 'port')
.option('-dm, --devMode <devMode>', 'devMode')
.description('build project of start on demand')
.action(build_1.default);
commander_1.default

View File

@ -62,21 +62,23 @@ function packageJsonContent({ name, version, description, cliName, cliBinName, i
"version": "${version}",
"description": "${description}",
"scripts": {
"make:domain": "${isModule ? `cross-env COMPLING_AS_LIB=yes ${cliBinName}` : cliBinName} make:domain",
"make:domain": "${isModule
? `cross-env COMPLING_AS_LIB=yes ${cliBinName}`
: cliBinName} make:domain",
"make:locale": "${cliBinName} make:locale ${isModule ? '-m' : ''}",
"make:dep": "${cliBinName} make:dependency",
"clean:cache": "rimraf node_modules/.cache",
"copy-config-json": "copyfiles -u 1 src/config/*.json lib/",
"start:mp": "${cliBinName} start --target mp --mode development",
"start:mp:prod": "${cliBinName} start --target mp --mode development --prod",
"start:mp": "${cliBinName} start --target mp --mode development --devMode frontend",
"start:mp:server": "${cliBinName} start --target mp --mode development",
"build:mp:staging": "${cliBinName} build --target mp --mode staging",
"build-analyze:mp:staging": "${cliBinName} build --target mp --mode staging --analyze",
"build:mp": "${cliBinName} build --target mp --mode production",
"build-analyze:mp": "${cliBinName} build --target mp --mode production --analyze",
"start:web": "${cliBinName} start --target web --mode development",
"start:web:prod": "${cliBinName} start --target web --mode development --prod",
"start:native": "${cliBinName} start --target rn --mode development",
"start:native:prod": "${cliBinName} start --target rn --mode development --prod",
"start:web": "${cliBinName} start --target web --mode development --devMode frontend",
"start:web:server": "${cliBinName} start --target web --mode development",
"start:native": "${cliBinName} start --target rn --mode development --devMode frontend",
"start:native:server": "${cliBinName} start --target rn --mode development",
"build:web:staging": "${cliBinName} build --target web --mode staging",
"build-analyze:web:staging": "${cliBinName} build --target web --mode staging --analyze",
"build:web": "${cliBinName} build --target web --mode production",

View File

@ -16,10 +16,12 @@ import { copyFileSync } from 'fs';
type Mode = 'development' | 'staging' | 'production';
type Target = 'mp' | 'wechatMp' | 'web' | 'rn' | 'native';
type DevMode = 'frontend' | 'server'
export default async function build(cmd: any) {
const mode = (cmd.mode || 'development') as Mode;
const target = cmd.target as Target;
const devMode = (cmd.devMode || 'server') as DevMode;
if (!cmd.target) {
Error(
`${error(
@ -51,7 +53,7 @@ export default async function build(cmd: any) {
Success(
`${success(
`build ${target} environment:${mode} ${
['development'].includes(mode) ? `server:${!!cmd.prod}` : ''
['development'].includes(mode) ? `devMode:${devMode}` : ''
} ${
['mp', 'wechatMp'].includes(target) &&
['development'].includes(mode)
@ -71,6 +73,7 @@ export default async function build(cmd: any) {
[
`NODE_ENV=${mode}`,
`NODE_TARGET=${target}`,
`OAK_DEV_MODE=${devMode}`,
`SUB_DIR_NAME=${subdir}`,
`TSC_COMPILE_ON_ERROR=${errorLevel}`,
`COMPILE_ANALYZE=${!!cmd.analyze}`,
@ -104,6 +107,7 @@ export default async function build(cmd: any) {
[
`NODE_ENV=${mode}`,
`NODE_TARGET=${target}`,
`OAK_DEV_MODE=${devMode}`,
`SUB_DIR_NAME=${subdir}`,
`TSC_COMPILE_ON_ERROR=${errorLevel}`,
`COMPILE_ANALYZE=${!!cmd.analyze}`,
@ -143,6 +147,7 @@ export default async function build(cmd: any) {
'&& cross-env',
`NODE_ENV=${mode}`,
'OAK_PLATFORM=native',
`OAK_DEV_MODE=${devMode}`,
'./gradlew assembleRelease',
].filter(Boolean),
{
@ -159,6 +164,7 @@ export default async function build(cmd: any) {
'&& cross-env',
`NODE_ENV=${mode}`,
'OAK_PLATFORM=native',
`OAK_DEV_MODE=${devMode}`,
'./gradlew assembleStaging',
].filter(Boolean),
{
@ -174,6 +180,7 @@ export default async function build(cmd: any) {
`NODE_ENV=${mode}`,
'OAK_PLATFORM=native',
`PROD=${!!cmd.prod}`,
`OAK_DEV_MODE=${devMode}`,
'react-native',
'start',
].filter(Boolean),

View File

@ -78,6 +78,7 @@ program
.option('-d, --subDir <subDirName>', 'subDirName')
.option('-c, --check <level>', 'level')
.option('-p, --port <port>', 'port')
.option('-dm, --devMode <devMode>', 'devMode')
.description('build project of start on demand')
.action(build);
program

View File

@ -2,9 +2,7 @@
import { AppLoader } from 'oak-backend-base';
import { EntityDict } from 'oak-domain/lib/types';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
import { AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
import { BackendRuntimeContext } from 'oak-frontend-base/lib/context/BackendRuntimeContext';
import { IncomingHttpHeaders } from 'http';
export async function initialize<ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(
path: string,

View File

@ -78,21 +78,25 @@ export function packageJsonContent({
"version": "${version}",
"description": "${description}",
"scripts": {
"make:domain": "${isModule ? `cross-env COMPLING_AS_LIB=yes ${cliBinName}` : cliBinName} make:domain",
"make:domain": "${
isModule
? `cross-env COMPLING_AS_LIB=yes ${cliBinName}`
: cliBinName
} make:domain",
"make:locale": "${cliBinName} make:locale ${isModule ? '-m' : ''}",
"make:dep": "${cliBinName} make:dependency",
"clean:cache": "rimraf node_modules/.cache",
"copy-config-json": "copyfiles -u 1 src/config/*.json lib/",
"start:mp": "${cliBinName} start --target mp --mode development",
"start:mp:prod": "${cliBinName} start --target mp --mode development --prod",
"start:mp": "${cliBinName} start --target mp --mode development --devMode frontend",
"start:mp:server": "${cliBinName} start --target mp --mode development",
"build:mp:staging": "${cliBinName} build --target mp --mode staging",
"build-analyze:mp:staging": "${cliBinName} build --target mp --mode staging --analyze",
"build:mp": "${cliBinName} build --target mp --mode production",
"build-analyze:mp": "${cliBinName} build --target mp --mode production --analyze",
"start:web": "${cliBinName} start --target web --mode development",
"start:web:prod": "${cliBinName} start --target web --mode development --prod",
"start:native": "${cliBinName} start --target rn --mode development",
"start:native:prod": "${cliBinName} start --target rn --mode development --prod",
"start:web": "${cliBinName} start --target web --mode development --devMode frontend",
"start:web:server": "${cliBinName} start --target web --mode development",
"start:native": "${cliBinName} start --target rn --mode development --devMode frontend",
"start:native:server": "${cliBinName} start --target rn --mode development",
"build:web:staging": "${cliBinName} build --target web --mode staging",
"build-analyze:web:staging": "${cliBinName} build --target web --mode staging --analyze",
"build:web": "${cliBinName} build --target web --mode production",