oakI18nPlugin 编译t
This commit is contained in:
parent
eee3e7a66f
commit
905aea17a8
|
|
@ -0,0 +1,61 @@
|
||||||
|
const fs = require('fs');
|
||||||
|
const { relative, resolve } = require('path');
|
||||||
|
const t = require('@babel/types');
|
||||||
|
const pull = require('lodash/pull');
|
||||||
|
const { assert } = require('console');
|
||||||
|
|
||||||
|
const oakRegex =
|
||||||
|
/(\/*[a-zA-Z0-9_-])*\/app\/(pages|components)\/|(\\*[a-zA-Z0-9_-])*\\app\\(pages|components)\\/;
|
||||||
|
const localRegex =
|
||||||
|
/(\/*[a-zA-Z0-9_-])*\/src\/(pages|components)+\/|(\\*[a-zA-Z0-9_-])*\\src\/(pages|components)+\\/;
|
||||||
|
|
||||||
|
module.exports = (babel) => {
|
||||||
|
return {
|
||||||
|
visitor: {
|
||||||
|
CallExpression(path, state) {
|
||||||
|
const { cwd, filename } = state;
|
||||||
|
const res = resolve(cwd, filename).replace(/\\/g, '/');
|
||||||
|
// this.props.t/this.t/t
|
||||||
|
// t('common:detail') 不需要处理 t('detail') 需要处理;
|
||||||
|
// t(`${common}:${cc}`) 不需要处理 t(`${common}cc`) 需要处理
|
||||||
|
if (
|
||||||
|
/(pages|components)[\w|\W]+(.tsx|.ts)$/.test(
|
||||||
|
res
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
const p = res
|
||||||
|
.replace(oakRegex, '')
|
||||||
|
.replace(localRegex, '');
|
||||||
|
const eP = p.substring(0, p.lastIndexOf('/'));
|
||||||
|
const ns = eP.split('/').filter(ele => !!ele).join('-');
|
||||||
|
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 arguments = node.arguments;
|
||||||
|
arguments &&
|
||||||
|
arguments.forEach((node2, index) => {
|
||||||
|
if (
|
||||||
|
index === 0 &&
|
||||||
|
t.isLiteral(node2) &&
|
||||||
|
node2.value.indexOf(':') === -1
|
||||||
|
) {
|
||||||
|
arguments.splice(
|
||||||
|
index,
|
||||||
|
1,
|
||||||
|
t.stringLiteral(ns + ':' + node2.value)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
@ -4,11 +4,6 @@ const t = require('@babel/types');
|
||||||
const pull = require('lodash/pull');
|
const pull = require('lodash/pull');
|
||||||
const { assert } = require('console');
|
const { assert } = require('console');
|
||||||
|
|
||||||
const oakRegex =
|
|
||||||
/(\/*[a-zA-Z0-9_-])*\/app\/(pages|components)\/|(\\*[a-zA-Z0-9_-])*\\app\\(pages|components)\\/;
|
|
||||||
const localRegex =
|
|
||||||
/(\/*[a-zA-Z0-9_-])*\/src\/(pages|components)+\/|(\\*[a-zA-Z0-9_-])*\\src\/(pages|components)+\\/;
|
|
||||||
|
|
||||||
function isOakNamespaceIdentifier(node, name) {
|
function isOakNamespaceIdentifier(node, name) {
|
||||||
if (t.isJSXNamespacedName(node) && t.isJSXIdentifier(node.namespace) && node.namespace.name === 'oak'
|
if (t.isJSXNamespacedName(node) && t.isJSXIdentifier(node.namespace) && node.namespace.name === 'oak'
|
||||||
&& t.isJSXIdentifier(node.name) && node.name.name === name) {
|
&& t.isJSXIdentifier(node.name) && node.name.name === name) {
|
||||||
|
|
@ -164,50 +159,6 @@ module.exports = (babel) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
CallExpression(path, state) {
|
|
||||||
const { cwd, filename } = state;
|
|
||||||
const res = resolve(cwd, filename).replace(/\\/g, '/');
|
|
||||||
// this.props.t/this.t/t
|
|
||||||
// t('common:detail') 不需要处理 t('detail') 需要处理;
|
|
||||||
// t(`${common}:${cc}`) 不需要处理 t(`${common}cc`) 需要处理
|
|
||||||
if (
|
|
||||||
/(pages|components)[\w|\W]+(.tsx|.ts)$/.test(
|
|
||||||
res
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
const p = res
|
|
||||||
.replace(oakRegex, '')
|
|
||||||
.replace(localRegex, '');
|
|
||||||
const eP = p.substring(0, p.lastIndexOf('/'));
|
|
||||||
const ns = eP.split('/').filter(ele => !!ele).join('-');
|
|
||||||
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 arguments = node.arguments;
|
|
||||||
arguments &&
|
|
||||||
arguments.forEach((node2, index) => {
|
|
||||||
if (
|
|
||||||
index === 0 &&
|
|
||||||
t.isLiteral(node2) &&
|
|
||||||
node2.value.indexOf(':') === -1
|
|
||||||
) {
|
|
||||||
arguments.splice(
|
|
||||||
index,
|
|
||||||
1,
|
|
||||||
t.stringLiteral(ns + ':' + node2.value)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
@ -4,12 +4,6 @@ const t = require('@babel/types');
|
||||||
const pull = require('lodash/pull');
|
const pull = require('lodash/pull');
|
||||||
const { assert } = require('console');
|
const { assert } = require('console');
|
||||||
|
|
||||||
const oakRegex =
|
|
||||||
/(\/*[a-zA-Z0-9_-])*\/app\/(pages|components)\/|(\\*[a-zA-Z0-9_-])*\\app\\(pages|components)\\/;
|
|
||||||
const localRegex =
|
|
||||||
/(\/*[a-zA-Z0-9_-])*\/src\/(pages|components)+\/|(\\*[a-zA-Z0-9_-])*\\src\/(pages|components)+\\/;
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = (babel) => {
|
module.exports = (babel) => {
|
||||||
return {
|
return {
|
||||||
visitor: {
|
visitor: {
|
||||||
|
|
@ -132,47 +126,6 @@ module.exports = (babel) => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
CallExpression(path, state) {
|
|
||||||
const { cwd, filename } = state;
|
|
||||||
const res = resolve(cwd, filename).replace(/\\/g, '/');
|
|
||||||
// this.props.t/this.t/t
|
|
||||||
// t('common:detail') 不需要处理 t('detail') 需要处理;
|
|
||||||
// t(`${common}:${cc}`) 不需要处理 t(`${common}cc`) 需要处理
|
|
||||||
if (/(pages|components)[\w|\W]+(.tsx|.ts)$/.test(res)) {
|
|
||||||
const p = res.replace(oakRegex, '').replace(localRegex, '');
|
|
||||||
const eP = p.substring(0, p.lastIndexOf('/'));
|
|
||||||
const ns = eP
|
|
||||||
.split('/')
|
|
||||||
.filter((ele) => !!ele)
|
|
||||||
.join('-');
|
|
||||||
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 arguments = node.arguments;
|
|
||||||
arguments &&
|
|
||||||
arguments.forEach((node2, index) => {
|
|
||||||
if (
|
|
||||||
index === 0 &&
|
|
||||||
t.isLiteral(node2) &&
|
|
||||||
node2.value.indexOf(':') === -1
|
|
||||||
) {
|
|
||||||
arguments.splice(
|
|
||||||
index,
|
|
||||||
1,
|
|
||||||
t.stringLiteral(ns + ':' + node2.value)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,9 @@ const pkg = require(paths.appPackageJson);
|
||||||
// Check if TypeScript is setup
|
// Check if TypeScript is setup
|
||||||
const useTypeScript = fs.existsSync(paths.appTsConfig);
|
const useTypeScript = fs.existsSync(paths.appTsConfig);
|
||||||
const createEnvironmentHash = require('./webpack/persistentCache/createEnvironmentHash');
|
const createEnvironmentHash = require('./webpack/persistentCache/createEnvironmentHash');
|
||||||
|
|
||||||
|
const oakI18nPlugin = require('../babel-plugin/oakI18n');
|
||||||
|
|
||||||
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
|
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
|
||||||
|
|
||||||
const copyPatterns = [].concat(pkg.copyWebpack || []).map((pattern) =>
|
const copyPatterns = [].concat(pkg.copyWebpack || []).map((pattern) =>
|
||||||
|
|
@ -270,6 +273,9 @@ module.exports = function (webpackEnv) {
|
||||||
),
|
),
|
||||||
exclude: /node_modules/,
|
exclude: /node_modules/,
|
||||||
loader: 'babel-loader',
|
loader: 'babel-loader',
|
||||||
|
options: {
|
||||||
|
plugins: [oakI18nPlugin],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
test: /\.((?!tsx)ts)$/,
|
test: /\.((?!tsx)ts)$/,
|
||||||
|
|
@ -277,12 +283,22 @@ module.exports = function (webpackEnv) {
|
||||||
getOakInclude()
|
getOakInclude()
|
||||||
),
|
),
|
||||||
exclude: /node_modules/,
|
exclude: /node_modules/,
|
||||||
loader: 'ts-loader',
|
use: [
|
||||||
options: {
|
{
|
||||||
configFile: paths.appTsConfig,
|
loader: 'babel-loader',
|
||||||
context: paths.appRootPath,
|
options: {
|
||||||
transpileOnly: true,
|
plugins: [oakI18nPlugin],
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
loader: 'ts-loader',
|
||||||
|
options: {
|
||||||
|
configFile: paths.appTsConfig,
|
||||||
|
context: paths.appRootPath,
|
||||||
|
transpileOnly: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
// {
|
// {
|
||||||
// test: /\.json$/,
|
// test: /\.json$/,
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ const createEnvironmentHash = require('./webpack/persistentCache/createEnvironme
|
||||||
const oakPathTsxPlugin = require('../babel-plugin/oakPath');
|
const oakPathTsxPlugin = require('../babel-plugin/oakPath');
|
||||||
const oakRenderTsxPlugin = require('../babel-plugin/oakRender');
|
const oakRenderTsxPlugin = require('../babel-plugin/oakRender');
|
||||||
const oakRouterPlugin = require('../babel-plugin/router');
|
const oakRouterPlugin = require('../babel-plugin/router');
|
||||||
|
const oakI18nPlugin = require('../babel-plugin/oakI18n');
|
||||||
const oakStylePlugin = require('../babel-plugin/oakStyle');
|
const oakStylePlugin = require('../babel-plugin/oakStyle');
|
||||||
const oakRpxToPxPlugin = require('../postcss-plugin/oakRpxToPx');
|
const oakRpxToPxPlugin = require('../postcss-plugin/oakRpxToPx');
|
||||||
|
|
||||||
|
|
@ -493,6 +494,7 @@ module.exports = function (webpackEnv) {
|
||||||
oakPathTsxPlugin,
|
oakPathTsxPlugin,
|
||||||
oakRenderTsxPlugin,
|
oakRenderTsxPlugin,
|
||||||
oakRouterPlugin,
|
oakRouterPlugin,
|
||||||
|
oakI18nPlugin,
|
||||||
// oakStylePlugin,
|
// oakStylePlugin,
|
||||||
],
|
],
|
||||||
// This is a feature of `babel-loader` for webpack (not Babel itself).
|
// This is a feature of `babel-loader` for webpack (not Babel itself).
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue