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