支持es目录

This commit is contained in:
Wang Kejun 2023-09-05 17:04:19 +08:00
parent e5745b71be
commit 9c22caf4ab
14 changed files with 31 additions and 353 deletions

View File

@ -5,7 +5,7 @@ const assert = require('assert');
const { fork } = require('child_process');
const Regex =
/([\\/]*[a-zA-Z0-9_-\w\W]|[\\/]*[a-zA-Z0-9_-\w\W]:)*[\\/](lib|src)([\\/]*[a-zA-Z0-9_-])*[\\/](pages|components)+[\\/]/;
/([\\/]*[a-zA-Z0-9_-\w\W]|[\\/]*[a-zA-Z0-9_-\w\W]:)*[\\/](lib|src|es)([\\/]*[a-zA-Z0-9_-])*[\\/](pages|components)+[\\/]/;
const ModuleDict = {};

View File

@ -12,7 +12,7 @@ function isOakNamespaceIdentifier(node, name) {
}
const Regex =
/([\\/]*[a-zA-Z0-9_-\w\W]|[\\/]*[a-zA-Z0-9_-\w\W]:)*[\\/](lib|src)[\\/](pages|components)+[\\/]/;
/([\\/]*[a-zA-Z0-9_-\w\W]|[\\/]*[a-zA-Z0-9_-\w\W]:)*[\\/](lib|src|es)[\\/](pages|components)+[\\/]/;
module.exports = (babel) => {
return {

View File

@ -9,8 +9,8 @@ module.exports = (babel) => {
Program(path, state) {
const { cwd, filename } = state;
const rel = relative(cwd, filename).replace(/\\/g, '/');
const tsPage = /(pages|components)[\\/][\w|\W]+[\\/]index\.ts$/.test(rel);
const jsPage = /(pages|components)[\\/][\w|\W]+[\\/]index\.js$/.test(rel);
const tsPage = /(pages|components|namespaces)[\\/][\w|\W]+[\\/]index\.ts$/.test(rel);
const jsPage = /(pages|components|namespaces)[\\/][\w|\W]+[\\/]index\.js$/.test(rel);
if (tsPage || jsPage) {
const tsxFile = filename.replace(
/index\.(ts|js)$/,

View File

@ -167,10 +167,6 @@ module.exports = () => {
)
)
);
const { code } = transformFromAstSync(path.container);
console.log(code);
}
},
},

View File

@ -170,7 +170,7 @@ function parseXmlFile(appRootPath, appRootSrcPath, appSrcPath, filePath) {
const isSelf = filePath.startsWith(appRootSrcPath);
const filePath2 = filePath.replace(/\\/g, '/');
const fileProjectPath = filePath2.replace(/((\w|\W)*)(\/src|\/lib)(\/pages\/|\/components\/)((\w|\W)*)/g, '$1');
const fileProjectPath = filePath2.replace(/((\w|\W)*)(\/src|\/lib|\/es)(\/pages\/|\/components\/)((\w|\W)*)/g, '$1');
let moduleName = ModuleNameDict[fileProjectPath];
if (!moduleName) {
const { name } = require(join(fileProjectPath, 'package.json'));

View File

@ -67,11 +67,11 @@ module.exports = {
oakConfigJson: resolveApp('src/oak.config.json'),
oakGeneralBusinessPath: resolveRoot(
'node_modules/oak-general-business/' +
(process.env.NODE_ENV !== 'production' ? 'src' : 'lib')
(process.env.NODE_ENV !== 'production' ? 'src' : 'es')
),
oakFrontendBasePath: resolveRoot(
'node_modules/oak-frontend-base/' +
(process.env.NODE_ENV !== 'production' ? 'src' : 'lib')
(process.env.NODE_ENV !== 'production' ? 'src' : 'es')
),
oakAppDomainPath: resolveRoot('src/oak-app-domain'),
};

View File

@ -43,7 +43,7 @@ const copyPatterns = [].concat(pkg.copyWebpack || []).map((pattern) =>
}
: pattern
);
const oakRegex = /(\/*[a-zA-Z0-9_-])*\/(lib|src)\/|(\\*[a-zA-Z0-9_-])*\\(lib|src)\\/;
const oakRegex = /(\/*[a-zA-Z0-9_-])*\/(lib|src|es)\/|(\\*[a-zA-Z0-9_-])*\\(lib|src|es)\\/;
module.exports = function (webpackEnv) {
const isEnvDevelopment = webpackEnv === 'development';
@ -65,16 +65,14 @@ module.exports = function (webpackEnv) {
};
const getOakInclude = () => {
return isEnvProduction
? [/oak-general-business/, /oak-frontend-base/]
: [
/oak-domain/,
/oak-external-sdk/,
/oak-frontend-base/,
/oak-general-business/,
/oak-memory-tree-store/,
/oak-common-aspect/,
];
return [
/oak-domain/,
/oak-external-sdk/,
/oak-frontend-base/,
/oak-general-business/,
/oak-memory-tree-store/,
/oak-common-aspect/,
];
};
return {
@ -115,6 +113,7 @@ module.exports = function (webpackEnv) {
crypto: require.resolve('crypto-browserify'),
buffer: require.resolve('safe-buffer'),
stream: require.resolve('stream-browserify'),
zlib: require.resolve('browserify-zlib'),
events: require.resolve('events/'),
url: false,
path: false,
@ -211,7 +210,7 @@ module.exports = function (webpackEnv) {
include: [paths.appSrc, paths.appRootSrc].concat(
getOakInclude()
),
//exclude: /node_modules/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
plugins: [oakI18nPlugin, oakPathPlugin],
@ -224,7 +223,7 @@ module.exports = function (webpackEnv) {
include: [paths.appSrc, paths.appRootSrc].concat(
getOakInclude()
),
//exclude: /node_modules/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
@ -267,7 +266,7 @@ module.exports = function (webpackEnv) {
options: {
appSrcPath: paths.appSrc,
appRootPath: paths.appRootPath,
appRootSrcPath: paths.appRootSrc,
appRootSrcPath: paths.appRootSrc,
cacheDirectory: false,
},
},

View File

@ -99,11 +99,11 @@ module.exports = {
oakConfigJson: resolveApp('src/oak.config.json'),
oakGeneralBusinessPath: resolveRoot(
'node_modules/oak-general-business/' +
(process.env.NODE_ENV !== 'production' ? 'src' : 'lib')
(process.env.NODE_ENV !== 'production' ? 'src' : 'es')
),
oakFrontendBasePath: resolveRoot(
'node_modules/oak-frontend-base/' +
(process.env.NODE_ENV !== 'production' ? 'src' : 'lib')
(process.env.NODE_ENV !== 'production' ? 'src' : 'es')
),
oakAppDomainPath: resolveRoot('src/oak-app-domain'),
};

View File

@ -206,16 +206,14 @@ module.exports = function (webpackEnv) {
};
const getOakInclude = () => {
return isEnvProduction
? [/oak-general-business/, /oak-frontend-base/]
: [
/oak-domain/,
/oak-external-sdk/,
/oak-frontend-base/,
/oak-general-business/,
/oak-memory-tree-store/,
/oak-common-aspect/,
];
return [
/oak-domain/,
/oak-external-sdk/,
/oak-frontend-base/,
/oak-general-business/,
/oak-memory-tree-store/,
/oak-common-aspect/,
];
};
return {

View File

@ -121,7 +121,7 @@
"ui-extract-webpack-plugin": "^1.0.0",
"uuid": "^8.3.2",
"webpack": "^5.86.0",
"webpack-bundle-analyzer": "^4.5.0",
"webpack-bundle-analyzer": "^4.9.0",
"webpack-dev-server": "^4.15.1",
"webpack-manifest-plugin": "^4.0.2",
"workbox-webpack-plugin": "^6.4.1"

View File

@ -454,20 +454,6 @@ class OakWeChatMpPlugin {
minChunks: 0,
},
oak_frontend_base: {
chunks: 'all',
test: /[\\/]oak-frontend-base[\\/]/,
name: 'oak_frontend_base',
minChunks: 0,
},
// oak_memory_tree_store: {
// chunks: 'all',
// test: /[\\/]oak-memory-tree-store[\\/]/,
// name: 'oak_memory_tree_store',
// minChunks: 0,
// },
echarts: {
chunks: 'all',
test: /[\\/]miniprogram_npm\/ec-canvas\/echarts\.js$/,

View File

@ -1,207 +0,0 @@
const fs = require('fs-extra');
const Path = require('path');
const { merge, get, set, setWith } = require('lodash');
const OakRegex =
/([\\/]*[a-zA-Z0-9_-\w\W]|[\\/]*[a-zA-Z0-9_-\w\W]:)*[\\/](lib|src)([\\/]*[a-zA-Z0-9_-\w\W])*[\\/](pages|components|locales)+[\\/]/;
const PageAndComponentRegex =
/([\\/]*[a-zA-Z0-9_-\w\W]|[\\/]*[a-zA-Z0-9_-\w\W]:)*[\\/](lib|src)([\\/]*[a-zA-Z0-9_-\w\W])*[\\/](pages|components)+[\\/]/;
const DomainRegex =
/([\\/]*[a-zA-Z0-9_-\w\W]|[\\/]*[a-zA-Z0-9_-\w\W]:)*[\\/](lib|src)([\\/]*[a-zA-Z0-9_-\w\W])*[\\/](oak-app-domain)+[\\/]/;
const CommonRegex =
/([\\/]*[a-zA-Z0-9_-\w\W]|[\\/]*[a-zA-Z0-9_-\w\W]:)*[\\/](lib|src)[\\/](locales)+[\\/]/;
function getName(val) {
const name = val.substring(0, 1).toLowerCase() + val.substring(1);
return name;
}
function getNameAndLng(path) {
let name = '';
let lng = '';
const arr = path.split('/').filter((ele) => !!ele);
let isLocales = false;
for (let n of arr) {
if (isLocales) {
lng = n.substring(n, n.indexOf('.'));
} else if (n === 'locales') {
isLocales = true;
} else {
const name2 = getName(n);
name = name ? `${name}-${name2}` : name2;
}
}
return {
name,
lng,
};
}
function buildLocales({
projectPaths,
buildPath,
nodeEnv,
platform,
}) {
const dataJson = {};
projectPaths.forEach((path) => {
findLocaleFiles(dataJson, path, buildPath, nodeEnv, platform);
});
return dataJson;
}
// pages /house/locales/zh-CN.json 或者 /house/list/locales/zh-CN.json 或者 oak-app-domain
function findLocaleFiles(json, path, buildPath, nodeEnv, platform) {
if (!fs.existsSync(path)) {
return;
}
let stats = fs.statSync(path);
if (stats.isDirectory()) {
const files = fs.readdirSync(path);
files
.filter(
(ele) =>
!['.DS_Store', 'package.json'].includes(ele) &&
!/\.(ts|less|jsx|tsx|wxml|js)$/.test(ele) &&
!/general-app-domain/.test(ele)
)
.forEach((val, index) => {
let fPath = Path.resolve(path, val).replace(/\\/g, '/');
let stats = fs.statSync(fPath);
if (stats.isDirectory()) {
// 文件夹
if (val === 'locales') {
//监听locales文件夹
if (nodeEnv !== 'production') {
listenerLocaleFiles(
fPath,
buildPath,
nodeEnv,
platform
);
}
}
findLocaleFiles(json, fPath, buildPath, nodeEnv, platform);
} else {
const fPath = Path.resolve(path, val).replace(/\\/g, '/');
if (/(\/locales\/)/.test(fPath) && /\.(json)$/.test(fPath)) {
let newFilename = '';
if (PageAndComponentRegex.test(fPath)) {
newFilename = fPath.replace(PageAndComponentRegex, '');
} else if (DomainRegex.test(fPath)) {
newFilename = fPath.replace(DomainRegex, '');
} else if (CommonRegex.test(fPath)) {
newFilename = fPath.replace(CommonRegex, '');
}
if (newFilename) {
const { name, lng } = getNameAndLng(newFilename);
const dataJson = fs.readJsonSync(fPath);
const path = name ? `${lng}.${name}` : lng;
const data = get(json, path);
// 处理locales出现相同路径合并下
if (data) {
merge(dataJson, data);
}
setWith(json, path, dataJson, Object);
}
}
}
});
}
else {
//主入口 应该是文件夹
}
}
function listenerLocaleFiles(path, buildPath, nodeEnv, platform) {
// todo linux下不支持recursive监控以后再说
fs.watch(path, process.platform !== 'linux' ? { recursive: true } : {}, (eventType, filename) => {
const fPath = Path.resolve(path, filename).replace(/\\/g, '/');
// console.log('\nThe file', fPath, 'was modified!');
// console.log('The type of change was:', eventType);
if (/(\/locales\/)/.test(filename) && /\.(json)$/.test(filename)) {
if (eventType === 'change') {
//文件内容改变
const newFilename = fPath.replace(OakRegex, '');
const { name, lng } = getNameAndLng(newFilename);
const dataJson = fs.readJsonSync(fPath);
const newJson = {};
setWith(
newJson,
name ? `${lng}.${name}` : lng,
dataJson,
Object
);
if (platform === 'wechatMp') {
mergeMpJsonFiles(newJson, buildPath);
} else {
mergeWebJsonFiles(newJson, buildPath);
}
}
}
});
}
//
function mergeWebJsonFiles(json, buildPath, isMerge = true) {
for (let lng in json) {
// lng生成文件夹
const lngPath = Path.resolve(buildPath, lng);
if (!fs.existsSync(lngPath)) {
fs.mkdirSync(lngPath);
}
const entityJson = json[lng] || {};
// 覆盖entity.json
for (let entity in entityJson) {
const entityPath = Path.resolve(lngPath, `${entity}.json`);
const data = entityJson[entity] || {};
let dataJson = {};
if (isMerge) {
if (fs.existsSync(entityPath)) {
dataJson = fs.readJSONSync(entityPath);
}
}
merge(dataJson, data);
fs.writeFileSync(entityPath, JSON.stringify(dataJson, null, 2));
}
}
}
function mergeMpJsonFiles(json, buildPath, isMerge = true) {
for (let lng in json) {
// lng生成文件夹
const lngPath = Path.resolve(buildPath, `${lng}.json`);
const data = json[lng] || {};
let dataJson = {};
if (isMerge) {
if (fs.existsSync(lngPath)) {
dataJson = fs.readJSONSync(lngPath);
}
}
merge(dataJson, data);
fs.writeFileSync(lngPath, JSON.stringify(dataJson, null, 2));
}
}
module.exports = {
buildLocales,
mergeWebJsonFiles,
mergeMpJsonFiles,
};

View File

@ -1,43 +0,0 @@
const fs = require('fs-extra');
const Path = require('path');
const paths = require('../../config/mp/paths');
const getClientEnvironment = require('../../config/mp/env');
const env = getClientEnvironment();
const { buildLocales, mergeMpJsonFiles } = require('./build-locales');
const chalk = require('chalk');
const consola = require('consola');
const Locales = 'locales';
function copyLocaleFiles() {
//build locales
consola.success(`${chalk.greenBright('读取locales生成json数据')}`);
// locales到mp/dist下
const buildPath = Path.resolve(paths.appSrc, Locales);
const json = buildLocales({
projectPaths: [
paths.appRootSrc,
paths.oakGeneralBusinessPath,
paths.oakFrontendBasePath,
],
buildPath,
nodeEnv: env.raw.NODE_ENV,
platform: env.raw.OAK_PLATFORM,
});
consola.success(`${chalk.greenBright('json数据已生成准备写入json文件')}`);
if (!fs.existsSync(buildPath)) {
fs.mkdirSync(buildPath);
} else {
fs.emptyDirSync(buildPath);
}
mergeMpJsonFiles(json, buildPath);
consola.success(
`${chalk.greenBright(
`json数据已写入文件可以在${buildPath}目录下查看`
)}`
);
}
module.exports = {
copyLocaleFiles,
};

View File

@ -1,51 +0,0 @@
const fs = require('fs-extra');
const Path = require('path');
const paths = require('../../config/web/paths');
const getClientEnvironment = require('../../config/web/env');
const env = getClientEnvironment();
const { buildLocales, mergeWebJsonFiles } = require('./build-locales');
const chalk = require('chalk');
const consola = require('consola');
const Locales = 'locales';
function copyLocaleFiles() {
//build locales
try {
// locales到web/public下
const buildPath = Path.resolve(paths.appPublic, Locales);
consola.success(`${chalk.greenBright('读取locales生成json数据')}`);
const json = buildLocales({
projectPaths: [
paths.appRootSrc,
paths.appSrc,
paths.oakGeneralBusinessPath,
paths.oakFrontendBasePath,
],
buildPath,
nodeEnv: env.raw.NODE_ENV,
platform: env.raw.OAK_PLATFORM,
});
consola.success(
`${chalk.greenBright('json数据已生成准备写入json文件')}`
);
if (!fs.existsSync(buildPath)) {
fs.mkdirSync(buildPath);
} else {
fs.emptyDirSync(buildPath);
}
mergeWebJsonFiles(json, buildPath);
consola.success(
`${chalk.greenBright(
`json数据已写入文件可以在${buildPath}目录下查看`
)}`
);
} catch (e) {
console.log(e);
throw e;
}
}
module.exports = {
copyLocaleFiles,
};