This commit is contained in:
Wang Kejun 2022-08-19 01:30:21 +08:00
parent dd0335057f
commit 89cb0c9a8a
9 changed files with 208 additions and 43 deletions

View File

@ -4,7 +4,7 @@ const t = require('@babel/types');
const { assert } = require('console');
const oakRegex =
/(\/*[a-zA-Z0-9_-])*\/app\/(pages|components)\/|(\\*[a-zA-Z0-9_-])*\\app\\(pages|components)\\/;
/(\/*[a-zA-Z0-9_-])*\/lib\/(pages|components)\/|(\\*[a-zA-Z0-9_-])*\\lib\\(pages|components)\\/;
const localRegex =
/(\/*[a-zA-Z0-9_-])*\/src\/(pages|components)+\/|(\\*[a-zA-Z0-9_-])*\\src\/(pages|components)+\\/;
@ -18,15 +18,16 @@ module.exports = (babel) => {
// t('common:detail') 不需要处理 t('detail') 需要处理;
// t(`${common}:${cc}`) 不需要处理 t(`${common}cc`) 需要处理
if (
/(pages|components)[\w|\W]+(.tsx|.ts)$/.test(
res
)
/(pages|components)[\w|\W]+(.tsx|.ts|.jsx|.js)$/.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 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 &&
@ -51,8 +52,7 @@ module.exports = (babel) => {
1,
t.stringLiteral(ns + ':' + node2.value)
);
}
else if (
} else if (
index === 0 &&
t.isTemplateLiteral(node2) &&
node2.quasis &&
@ -64,15 +64,20 @@ module.exports = (babel) => {
node3.value.raw.indexOf(':') !== -1
)
) {
node2.quasis.splice(
0,
1,
t.templateElement({
raw: ns + ':' + node2.quasis[0].value.raw,
cooked: ns + ':' + node2.quasis[0].value.cooked,
})
);
node2.quasis.splice(
0,
1,
t.templateElement({
raw:
ns +
':' +
node2.quasis[0].value.raw,
cooked:
ns +
':' +
node2.quasis[0].value.cooked,
})
);
}
});
}

View File

@ -54,7 +54,7 @@ module.exports = (babel) => {
const { cwd, filename } = state;
const rel = relative(cwd, filename).replace(/\\/g, '/');
if (
/(pages|components)[\w|\W]+(index\.tsx|index\.pc\.tsx)$/.test(
/(pages|components)[\w|\W]+(index\.tsx|index\.pc\.tsx|web\.tsx|web\.pc\.tsx|web\.jsx|web\.pc\.jsx)$/.test(
rel
)
) {

View File

@ -9,13 +9,33 @@ module.exports = (babel) => {
Program(path, state) {
const { cwd, filename } = state;
const rel = relative(cwd, filename).replace(/\\/g, '/');
const tsPage = /pages|components[\w|\W]+index\.(web.ts|ts)$/.test(rel);
const jsPage = /pages|components[\w|\W]+index\.(web.js|js)$/.test(rel)
const tsPage = (/oak-general-business\/lib/.test(rel) && /pages|components[\w|\W]+index\.(web.ts|ts)$/.test(rel)) ||
(!/node_modules/.test(rel) && /pages|components[\w|\W]+index\.(web.ts|ts)$/.test(rel));
const jsPage = (/oak-general-business\/lib/.test(rel) && /pages|components[\w|\W]+index\.(web.js|js)$/.test(rel)) ||
(!/node_modules/.test(rel) && /pages|components[\w|\W]+index\.(web.js|js)$/.test(rel));
if (tsPage || jsPage) {
const tsxFile = filename.replace(/index\.(web.ts|ts)$/, tsPage ? 'web.tsx': 'web.jsx');
const tsxFile = filename.replace(
/index\.(web.ts|ts|web.js|js)$/,
tsPage ? 'web.tsx' : 'web.jsx'
);
const jsFile = filename.replace(
/index\.(web.ts|ts|web.js|js)$/,
'web.js'
);
const tsxFileExists = fs.existsSync(tsxFile);
const pcTsxFile = filename.replace(/index\.(web.ts|ts)$/, tsPage ? 'web.pc.tsx' : 'web.pc.jsx');
const jsFileExists = fs.existsSync(jsFile);
const pcTsxFile = filename.replace(
/index\.(web.ts|ts|web.js|js)$/,
tsPage ? 'web.pc.tsx' : 'web.pc.jsx'
);
const pcJsFile = filename.replace(
/index\.(web.ts|ts|web.js|js)$/,
'web.pc.js'
);
const pcTsxFileExists = fs.existsSync(pcTsxFile);
const pcJsFileExists = fs.existsSync(pcJsFile);
/** tsxrender
* if (this.props.width === 'xs') {
const renderMobile = require('./web.tsx').default;
@ -26,13 +46,15 @@ module.exports = (babel) => {
return renderScreen.call(this);
}
*/
const renderStatements = [
const renderTsxStatements = [
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('render'),
t.memberExpression(
t.callExpression(t.identifier('require'), [
t.stringLiteral(`./web.${tsPage ? 'tsx' : 'jsx'}`),
t.stringLiteral(
`./web.${tsPage ? 'tsx' : 'jsx'}`
),
]),
t.identifier('default')
)
@ -48,13 +70,13 @@ module.exports = (babel) => {
)
),
];
const renderPcStatements = [
const renderJsStatements = [
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('render'),
t.memberExpression(
t.callExpression(t.identifier('require'), [
t.stringLiteral(`./web.pc.${tsPage ? 'tsx' : 'jsx'}`),
t.stringLiteral('./web.js'),
]),
t.identifier('default')
)
@ -70,6 +92,52 @@ module.exports = (babel) => {
)
),
];
const renderPcTsxStatements = [
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('render'),
t.memberExpression(
t.callExpression(t.identifier('require'), [
t.stringLiteral(
`./web.pc.${tsPage ? 'tsx' : 'jsx'}`
),
]),
t.identifier('default')
)
),
]),
t.returnStatement(
t.callExpression(
t.memberExpression(
t.identifier('render'),
t.identifier('call')
),
[t.thisExpression()]
)
),
];
const renderPcJsStatements = [
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('render'),
t.memberExpression(
t.callExpression(t.identifier('require'), [
t.stringLiteral('./web.pc.js'),
]),
t.identifier('default')
)
),
]),
t.returnStatement(
t.callExpression(
t.memberExpression(
t.identifier('render'),
t.identifier('call')
),
[t.thisExpression()]
)
),
];
const statements = [];
if (tsxFileExists && pcTsxFileExists) {
statements.push(
@ -85,23 +153,83 @@ module.exports = (babel) => {
),
t.stringLiteral('xs')
),
t.blockStatement(renderStatements),
t.blockStatement(renderPcStatements)
t.blockStatement(renderTsxStatements),
t.blockStatement(renderPcTsxStatements)
)
);
}
else if (jsFileExists && pcJsFileExists) {
statements.push(
t.ifStatement(
t.binaryExpression(
'===',
t.memberExpression(
t.memberExpression(
t.thisExpression(),
t.identifier('props')
),
t.identifier('width')
),
t.stringLiteral('xs')
),
t.blockStatement(renderJsStatements),
t.blockStatement(renderPcJsStatements)
)
);
} else if (jsFileExists && pcTsxFileExists) {
statements.push(
t.ifStatement(
t.binaryExpression(
'===',
t.memberExpression(
t.memberExpression(
t.thisExpression(),
t.identifier('props')
),
t.identifier('width')
),
t.stringLiteral('xs')
),
t.blockStatement(renderJsStatements),
t.blockStatement(renderPcTsxStatements)
)
);
} else if (tsxFileExists && pcJsFileExists) {
statements.push(
t.ifStatement(
t.binaryExpression(
'===',
t.memberExpression(
t.memberExpression(
t.thisExpression(),
t.identifier('props')
),
t.identifier('width')
),
t.stringLiteral('xs')
),
t.blockStatement(renderTsxStatements),
t.blockStatement(renderPcJsStatements)
)
);
} else if (tsxFileExists) {
statements.push(...renderStatements);
statements.push(...renderTsxStatements);
} else if (pcTsxFileExists) {
statements.push(...renderPcStatements);
statements.push(...renderPcTsxStatements);
} else if (jsFileExists) {
statements.push(...renderJsStatements);
} else if (pcJsFileExists) {
statements.push(...renderPcJsStatements);
} else {
assert(
false,
`${filename}文件中不存在index.tsx或者index.pc.tsx`
`${filename}文件中不存在web.tsx或者web.pc.tsx`
);
}
const node = path.node;
const body = node.body;
body.forEach((node2) => {
// export default OakPage({})、export default OakComponent({})
if (
node2 &&
node2.declaration &&
@ -124,6 +252,31 @@ module.exports = (babel) => {
}
});
}
// exports.default = OakPage({})、exports.default = OakComponent({})
if (
node2 &&
node2.expression &&
node2.expression.right &&
(node2.expression.right.callee.name === 'OakPage' ||
node2.expression.right.callee.name ===
'OakComponent')
) {
node2.expression.right.arguments.forEach((node3) => {
if (t.isObjectExpression(node3)) {
const propertyRender = t.objectProperty(
t.identifier('render'),
t.functionExpression(
null,
[],
t.blockStatement(statements)
)
);
node3.properties.unshift(propertyRender);
}
});
}
});
}
},

View File

@ -85,10 +85,10 @@ const isSrc = (name) => name === 'src';
const isDynamicSrc = (src) => /\{\{/.test(src);
const oakMessage = 'oak-message';
const oakDebugPanel = 'oak-debugPanel';
const oakRegex = /(\/*[a-zA-Z0-9_-])*\/app\/|(\\*[a-zA-Z0-9_-])*\\app\\/;
const oakRegex = /(\/*[a-zA-Z0-9_-])*\/lib\/|(\\*[a-zA-Z0-9_-])*\\lib\\/;
const localRegex = /(\/*[a-zA-Z0-9_-])*\/src+\/|(\\*[a-zA-Z0-9_-])*\\src+\\/;
const oakPagesOrComponentsRegex =
/(\/*[a-zA-Z0-9_-])*\/app\/(pages|components)\/|(\\*[a-zA-Z0-9_-])*\\app\\(pages|components)\\/;
/(\/*[a-zA-Z0-9_-])*\/lib\/(pages|components)\/|(\\*[a-zA-Z0-9_-])*\\lib\\(pages|components)\\/;
const localPagesOrComponentsRegex =
/(\/*[a-zA-Z0-9_-])*\/src\/(pages|components)+\/|(\\*[a-zA-Z0-9_-])*\\src\/(pages|components)+\\/;

View File

@ -66,7 +66,7 @@ module.exports = {
appRootPath: resolveRoot('.'),
oakConfigJson: resolveApp('src/oak.config.json'),
oakGeneralBusinessAppPath: resolveRoot(
'node_modules/oak-general-business/app'
'node_modules/oak-general-business/lib'
),
};

View File

@ -41,7 +41,7 @@ const copyPatterns = [].concat(pkg.copyWebpack || []).map((pattern) =>
}
: pattern
);
const oakRegex = /(\/*[a-zA-Z0-9_-])*\/app\/|(\\*[a-zA-Z0-9_-])*\\app\\/;
const oakRegex = /(\/*[a-zA-Z0-9_-])*\/lib\/|(\\*[a-zA-Z0-9_-])*\\lib\\/;
const localRegex = /(\/*[a-zA-Z0-9_-])*\/src+\/|(\\*[a-zA-Z0-9_-])*\\src+\\/;
module.exports = function (webpackEnv) {
@ -275,7 +275,7 @@ module.exports = function (webpackEnv) {
include: [paths.appSrc, paths.appRootSrc].concat(
getOakInclude()
),
exclude: /node_modules/,
//exclude: /node_modules/,
loader: 'babel-loader',
options: {
plugins: [oakI18nPlugin],
@ -397,7 +397,7 @@ module.exports = function (webpackEnv) {
),
}),
// TypeScript type checking
useTypeScript &&
false &&
new ForkTsCheckerWebpackPlugin({
async: isEnvDevelopment,
typescript: {

View File

@ -506,6 +506,12 @@ module.exports = function (webpackEnv) {
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
compact: isEnvProduction,
// overrides: [
// {
// include: /oak-general-business/,
// sourceType: 'unambiguous',
// },
// ],
},
},
],
@ -812,7 +818,7 @@ module.exports = function (webpackEnv) {
maximumFileSizeToCacheInBytes: 5 * 1024 * 1024,
}),
// TypeScript type checking
useTypeScript &&
false &&
new ForkTsCheckerWebpackPlugin({
async: isEnvDevelopment,
typescript: {

View File

@ -110,6 +110,7 @@
"tailwindcss": "^3.0.2",
"terser-webpack-plugin": "^5.2.5",
"ts-node": "^10.8.1",
"tslib": "^2.4.0",
"ui-extract-webpack-plugin": "^1.0.0",
"uuid": "^8.3.2",
"webpack": "^5.72.0",

View File

@ -13,7 +13,7 @@ const requiredPath = require('required-path');
const pluginName = 'OakWeChatMpPlugin';
const oakRegex = /(^@)(?!project)([a-zA-Z0-9_-])+\/{1}/i;
const oakPageRegex = /node_modules\/[a-zA-Z0-9_-]+\/app\//;
const oakPageRegex = /node_modules\/[a-zA-Z0-9_-]+\/lib\//;
const localRegex = /^@project\/{1}/i;
const localPageRegex = /[a-zA-Z0-9_-]*src\//;
@ -42,7 +42,7 @@ function getProjectName(str) {
function getOakPage(page) {
const name = getProjectName(page); //截取项目名
const oakPage = `node_modules/${name}/app/` + replaceOakPrefix(page);
const oakPage = `node_modules/${name}/lib/` + replaceOakPrefix(page);
return oakPage;
}