处理了编译过程中对t函数的处理
This commit is contained in:
parent
71ed5c94d5
commit
c48cd58de3
|
|
@ -1,11 +1,97 @@
|
|||
const fs = require('fs');
|
||||
const { relative, resolve } = require('path');
|
||||
const { relative, resolve, join } = require('path');
|
||||
const t = require('@babel/types');
|
||||
const { assert } = require('console');
|
||||
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)+[\\/]/;
|
||||
|
||||
|
||||
const ModuleDict = {};
|
||||
|
||||
function parseFileModuleAndNs(cwd, filename) {
|
||||
const relativePath = relative(cwd, filename);
|
||||
|
||||
if (relativePath.startsWith('node_modules') || relativePath.startsWith('..')) { // 在测试环境下是相对路径
|
||||
const moduleRelativePath = relativePath.split('\\').slice(0, 2);
|
||||
const modulePath = join(cwd, ...moduleRelativePath);
|
||||
const moduleDir = moduleRelativePath[1];
|
||||
|
||||
let moduleName = ModuleDict[moduleDir];
|
||||
if (!moduleName) {
|
||||
const { name } = require(join(modulePath, 'package.json'));
|
||||
ModuleDict[moduleDir] = name;
|
||||
moduleName = name;
|
||||
console.log(moduleDir, name);
|
||||
}
|
||||
const rel2paths = relative(modulePath, filename).split('\\');
|
||||
|
||||
let ns;
|
||||
switch (rel2paths[1]) {
|
||||
case 'pages': {
|
||||
ns = `${moduleName}-p-${rel2paths.slice(2, rel2paths.length - 1).join('-')}`;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
assert(rel2paths[1] === 'components', rel2paths.join('//'));
|
||||
ns = `${moduleName}-c-${rel2paths.slice(2, rel2paths.length - 1).join('-')}`;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
moduleName,
|
||||
ns,
|
||||
};
|
||||
}
|
||||
else {
|
||||
let moduleName = ModuleDict['./'];
|
||||
if (!moduleName) {
|
||||
const { name } = require(join(cwd, 'package.json'));
|
||||
ModuleDict['./'] = name;
|
||||
moduleName = name;
|
||||
console.log('./', name);
|
||||
}
|
||||
|
||||
const rel2paths = relative(cwd, filename).split('\\');
|
||||
|
||||
let ns;
|
||||
switch (rel2paths[1]) {
|
||||
case 'pages': {
|
||||
ns = `${moduleName}-p-${rel2paths.slice(2, rel2paths.length - 1).join('-')}`;
|
||||
break;
|
||||
}
|
||||
case 'components': {
|
||||
ns = `${moduleName}-c-${rel2paths.slice(2, rel2paths.length - 1).join('-')}`;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// 处理web/wechatMp中的数据
|
||||
assert(rel2paths[1] === 'src');
|
||||
const p1 = rel2paths[0];
|
||||
if (p1 === 'web') {
|
||||
ns = `${moduleName}-w-${rel2paths.slice(2, rel2paths.length - 1).join('-')}`;
|
||||
}
|
||||
else if (p1 === 'wechatMp') {
|
||||
ns = `${moduleName}-wmp-${rel2paths.slice(2, rel2paths.length - 1).join('-')}`;
|
||||
}
|
||||
else {
|
||||
assert(p1.startsWith('wechatMp'));
|
||||
const iter = parseInt(p1.slice(8), 10);
|
||||
ns = `${moduleName}-wmp${iter}-${rel2paths.slice(2, rel2paths.length - 1).join('-')}`;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
moduleName,
|
||||
ns,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (babel) => {
|
||||
return {
|
||||
visitor: {
|
||||
|
|
@ -16,111 +102,99 @@ module.exports = (babel) => {
|
|||
// t('common:detail') 不需要处理 t('detail') 需要处理;
|
||||
// t(`${common}:${cc}`) 不需要处理 t(`${common}cc`) 需要处理
|
||||
// 只支持t的参数为字符串或模版字符串
|
||||
if (
|
||||
/(pages|components)[\w|\W]+(.tsx|.ts|.jsx|.js)$/.test(res)
|
||||
) {
|
||||
const { node } = path;
|
||||
if (
|
||||
node &&
|
||||
node.callee &&
|
||||
((t.isIdentifier(node.callee) &&
|
||||
node.callee.name === 't') ||
|
||||
(t.isMemberExpression(node.callee) &&
|
||||
t.isIdentifier(node.callee.property) &&
|
||||
node.callee.property.name === 't'))
|
||||
) {
|
||||
const p = res.replace(Regex, '');
|
||||
const eP = p.substring(0, p.lastIndexOf('/'));
|
||||
const ns = eP
|
||||
.split('/')
|
||||
.filter((ele) => !!ele)
|
||||
.join('-');
|
||||
const arguments = node.arguments;
|
||||
arguments &&
|
||||
arguments.forEach((node2, index) => {
|
||||
if (
|
||||
index === 0 &&
|
||||
t.isLiteral(node2) &&
|
||||
node2.value &&
|
||||
node2.value.indexOf(':') === -1
|
||||
) {
|
||||
// t('d')
|
||||
arguments.splice(
|
||||
index,
|
||||
1,
|
||||
t.stringLiteral(ns + ':' + node2.value)
|
||||
);
|
||||
} else if (
|
||||
index === 0 &&
|
||||
t.isTemplateLiteral(node2) &&
|
||||
node2.quasis &&
|
||||
!node2.quasis.find(
|
||||
(node3) =>
|
||||
node3 &&
|
||||
node3.value &&
|
||||
node3.value.raw &&
|
||||
node3.value.raw.indexOf(':') !== -1
|
||||
)
|
||||
) {
|
||||
// t(`ele`)
|
||||
node2.quasis.splice(
|
||||
0,
|
||||
1,
|
||||
t.templateElement({
|
||||
raw:
|
||||
ns +
|
||||
':' +
|
||||
node2.quasis[0].value.raw,
|
||||
cooked:
|
||||
ns +
|
||||
':' +
|
||||
node2.quasis[0].value.cooked,
|
||||
})
|
||||
);
|
||||
}
|
||||
// else if (
|
||||
// index === 0 &&
|
||||
// t.isIdentifier(node2) &&
|
||||
// node2.name &&
|
||||
// node2.name.indexOf(':') === -1
|
||||
// ) {
|
||||
// // t(ele)
|
||||
// arguments.splice(
|
||||
// index,
|
||||
// 1,
|
||||
// t.binaryExpression(
|
||||
// '+',
|
||||
// t.stringLiteral(ns + ':'),
|
||||
// t.identifier(node2.name)
|
||||
// )
|
||||
// );
|
||||
// } else if (
|
||||
// index === 0 &&
|
||||
// t.isMemberExpression(node2) &&
|
||||
// node2.object.name &&
|
||||
// node2.object.name.indexOf(':') === -1
|
||||
// ) {
|
||||
// // t(ele.label)
|
||||
// arguments.splice(
|
||||
// index,
|
||||
// 1,
|
||||
// t.binaryExpression(
|
||||
// '+',
|
||||
// t.stringLiteral(ns + ':'),
|
||||
// t.memberExpression(
|
||||
// t.identifier(
|
||||
// node2.object.name
|
||||
// ),
|
||||
// t.identifier(
|
||||
// node2.property.name
|
||||
// )
|
||||
// )
|
||||
// )
|
||||
// );
|
||||
// }
|
||||
});
|
||||
}
|
||||
}
|
||||
if (
|
||||
/(pages|components)[\w|\W]+(.tsx|.ts|.jsx|.js)$/.test(res)
|
||||
) {
|
||||
const { node } = path;
|
||||
if (
|
||||
node &&
|
||||
node.callee &&
|
||||
((t.isIdentifier(node.callee) &&
|
||||
node.callee.name === 't') ||
|
||||
(t.isMemberExpression(node.callee) &&
|
||||
t.isIdentifier(node.callee.property) &&
|
||||
node.callee.property.name === 't'))
|
||||
) {
|
||||
const { moduleName, ns } = parseFileModuleAndNs(cwd, filename);
|
||||
const arguments = node.arguments;
|
||||
const argu0 = arguments && arguments[0];
|
||||
if (t.isStringLiteral(argu0)) {
|
||||
const { value } = argu0;
|
||||
if (!value.includes(':')) {
|
||||
// 是自己namespace下,加上ns
|
||||
if (!value.startsWith(ns)) {
|
||||
arguments.splice(
|
||||
0,
|
||||
1,
|
||||
t.stringLiteral(ns + '.' + value)
|
||||
);
|
||||
}
|
||||
}
|
||||
else if (value.includes('::')) {
|
||||
// 公共namespace下,加上moduleName-l
|
||||
arguments.splice(
|
||||
0,
|
||||
1,
|
||||
t.stringLiteral((moduleName + '-l-' + value).replace('::', '.'))
|
||||
);
|
||||
}
|
||||
else {
|
||||
// 是entity namespace下,改成.就行了
|
||||
arguments.splice(
|
||||
0,
|
||||
1,
|
||||
t.stringLiteral(value.replace(':', '.'))
|
||||
);
|
||||
}
|
||||
}
|
||||
else if (t.isTemplateLiteral(argu0)) {
|
||||
assert (argu0.quasis);
|
||||
const namespaceQuasis = argu0.quasis.find(
|
||||
ele => ele.value.raw && ele.value.raw.includes(':')
|
||||
);
|
||||
if (namespaceQuasis) {
|
||||
if (namespaceQuasis.value.raw.includes('::')) {
|
||||
// 公共ns,改成.并在头上加上moduleName-l-
|
||||
namespaceQuasis.value.raw = namespaceQuasis.value.raw.replace('::', '.');
|
||||
argu0.quasis[0].value.raw = moduleName + '-l-' + argu0.quasis[0].value.raw || '';
|
||||
}
|
||||
else {
|
||||
// entity的ns,改成.
|
||||
namespaceQuasis.value.raw = namespaceQuasis.value.raw.replace(':', '.');
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 自身ns
|
||||
if (!argu0.quasis[0].value.raw || !argu0.quasis[0].value.raw.startsWith(ns)) {
|
||||
argu0.quasis[0].value.raw = ns + '.' + argu0.quasis[0].value.raw || '';
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (t.isIdentifier(argu0) || t.isExpression(argu0)) {
|
||||
// 是变量或表达式,一定是自己的namespace下的
|
||||
arguments.splice(
|
||||
0,
|
||||
1,
|
||||
t.templateLiteral(
|
||||
[
|
||||
t.templateElement({
|
||||
raw: ns + '.'
|
||||
}),
|
||||
t.templateElement({
|
||||
raw: ''
|
||||
})
|
||||
],
|
||||
[
|
||||
argu0
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
else {
|
||||
assert(false, 't函数调用的第一个参数只能是字符串、模板或表达式');
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,11 +3,14 @@ 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"));
|
||||
const makeLocale_1 = tslib_1.__importDefault(require("./makeLocale"));
|
||||
async function build(cmd) {
|
||||
if (!cmd.target) {
|
||||
(0, tip_style_1.Error)(`${(0, tip_style_1.error)(`Please add --target web or --target mp or --target wechatMp to he command`)}`);
|
||||
return;
|
||||
}
|
||||
// 先makeLocale
|
||||
(0, makeLocale_1.default)();
|
||||
//ts类型检查 waring 还是error,
|
||||
//主要web受影响,error级别的话 控制台和网页都报错,warning级别的话 控制台报错
|
||||
const TSC_COMPILE_ON_ERROR = cmd.check !== 'error';
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
export default function make(): Promise<void>;
|
||||
export default function make(watch?: boolean): Promise<void>;
|
||||
|
|
|
|||
|
|
@ -3,10 +3,14 @@ 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() {
|
||||
async function make(watch) {
|
||||
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`make locales`)}`);
|
||||
// ts-node scripts/build-app-domain & npm link ./app-domain
|
||||
const result = cross_spawn_1.default.sync('ts-node', [require.resolve('../scripts/' + 'make-locale.js')], {
|
||||
const args = [require.resolve('../scripts/' + 'make-locale.js')];
|
||||
if (watch) {
|
||||
args.push('true');
|
||||
}
|
||||
const result = cross_spawn_1.default.sync('ts-node', args, {
|
||||
stdio: 'inherit',
|
||||
shell: true,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
const watch = process.argv[2];
|
||||
const LocaleBuilder = require('oak-domain/lib/compiler/localeBuilder').default;
|
||||
const builder = new LocaleBuilder(false);
|
||||
builder.build();
|
||||
builder.build(!!watch);
|
||||
|
|
@ -8,6 +8,7 @@ import {
|
|||
Warn,
|
||||
} from './tip-style';
|
||||
import spawn from 'cross-spawn';
|
||||
import makeLocale from './makeLocale';
|
||||
|
||||
export default async function build(cmd: any) {
|
||||
if (!cmd.target) {
|
||||
|
|
@ -18,6 +19,8 @@ export default async function build(cmd: any) {
|
|||
);
|
||||
return;
|
||||
}
|
||||
// 先makeLocale
|
||||
makeLocale();
|
||||
//ts类型检查 waring 还是error,
|
||||
//主要web受影响,error级别的话 控制台和网页都报错,warning级别的话 控制台报错
|
||||
const TSC_COMPILE_ON_ERROR = cmd.check !== 'error';
|
||||
|
|
|
|||
|
|
@ -9,12 +9,16 @@ import {
|
|||
} from './tip-style';
|
||||
import spawn from 'cross-spawn';
|
||||
|
||||
export default async function make() {
|
||||
export default async function make(watch?: boolean) {
|
||||
Success(`${success(`make locales`)}`);
|
||||
// ts-node scripts/build-app-domain & npm link ./app-domain
|
||||
const args = [require.resolve('../scripts/' + 'make-locale.js')];
|
||||
if (watch) {
|
||||
args.push('true');
|
||||
}
|
||||
const result = spawn.sync(
|
||||
'ts-node',
|
||||
[require.resolve('../scripts/' + 'make-locale.js')],
|
||||
args,
|
||||
{
|
||||
stdio: 'inherit',
|
||||
shell: true,
|
||||
|
|
|
|||
Loading…
Reference in New Issue