Signed-off-by: puyajun <puyajun@huawei.com>
This commit is contained in:
puyajun
2022-03-24 13:20:26 +08:00
parent e4b058a64e
commit 81aa9e32fc
+48 -4
View File
@@ -18,19 +18,23 @@ const path = require('path');
const chai = require('chai');
const mocha = require('mocha');
const expect = chai.expect;
const { processUISyntax } = require('../lib/process_ui_syntax');
const {
validateUISyntax,
preprocessExtend,
resetComponentCollection,
sourceReplace,
componentCollection
} = require('../lib/validate_ui_syntax');
const { processUISyntax } = require('../lib/process_ui_syntax');
const {
componentInfo,
readFile
} = require('../lib/utils');
const { BUILD_ON } = require('../lib/pre_define');
const {
BUILD_ON,
OHOS_PLUGIN,
NATIVE_MODULE,
SYSTEM_PLUGIN
} = require('../lib/pre_define');
function expectActual(name, filePath) {
const content = require(filePath);
@@ -65,3 +69,43 @@ mocha.describe('compiler', () => {
})
})
})
function sourceReplace(source) {
let content = source;
const log = [];
content = preprocessExtend(content);
content = processSystemApi(content);
return {
content: content,
log: log
};
}
function processSystemApi(content) {
const REG_SYSTEM =
/import\s+(.+)\s+from\s+['"]@(system|ohos)\.(\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)/g;
const REG_LIB_SO =
/import\s+(.+)\s+from\s+['"]lib(\S+)\.so['"]|import\s+(.+)\s*=\s*require\(\s*['"]lib(\S+)\.so['"]\s*\)/g;
const newContent = content.replace(REG_LIB_SO, (_, item1, item2, item3, item4) => {
const libSoValue = item1 || item3;
const libSoKey = item2 || item4;
return `var ${libSoValue} = globalThis.requireNapi("${libSoKey}", true);`;
}).replace(REG_SYSTEM, (item, item1, item2, item3, item4, item5, item6, item7) => {
let moduleType = item2 || item5;
let systemKey = item3 || item6;
let systemValue = item1 || item4;
if (NATIVE_MODULE.has(`${moduleType}.${systemKey}`)) {
item = `var ${systemValue} = globalThis.requireNativeModule('${moduleType}.${systemKey}')`;
} else if (moduleType === SYSTEM_PLUGIN) {
item = `var ${systemValue} = isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ? ` +
`globalThis.systemplugin.${systemKey} : globalThis.requireNapi('${systemKey}')`;
} else if (moduleType === OHOS_PLUGIN) {
item = `var ${systemValue} = globalThis.requireNapi('${systemKey}') || ` +
`(isSystemplugin('${systemKey}', '${OHOS_PLUGIN}') ? ` +
`globalThis.ohosplugin.${systemKey} : isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ` +
`? globalThis.systemplugin.${systemKey} : undefined)`;
}
return item;
});
return newContent;
}