!175 包名和模块名不一致,导包用模块名构建不报错

Merge pull request !175 from youbing54/master
This commit is contained in:
openharmony_ci 2024-11-12 10:52:39 +00:00 committed by Gitee
commit ad48487677
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -21,6 +21,7 @@ import path from 'path'
import fs from 'fs'
import loaderUtils from 'loader-utils'
const crypto = require("crypto")
const JSON5 = require('json5');
import {
SourceMapGenerator,
SourceMapConsumer
@ -285,6 +286,7 @@ export function parseRequireModule (source, resourcePath) {
if (requireStatements && requireStatements.length) {
for (let requireStatement of requireStatements) {
const requireStatementExec = /\((\"|\')(.+)(\"|\')\)/.exec(requireStatement);
checkModuleIsVaild(requireStatementExec, resourcePath);
if (requireStatement.match(REG_SYSTEM) && requireStatementExec && requireStatementExec.length > 3) {
if (systemModules.length === 0 || systemModules.includes(requireStatementExec[2] + '.d.ts') ||
process.env.DEVICE_LEVEL === 'lite') {
@ -388,6 +390,48 @@ function copyFile(inputFile, outputFile) {
}
}
function isPathUnderBase(fullPath, basePath) {
const normalizedFullPath = fullPath.replace(/\\/g, '/').toLowerCase();
const normalizedBasePath = basePath.replace(/\\/g, '/').toLowerCase() + '/';
return normalizedFullPath.startsWith(normalizedBasePath);
}
function checkModuleIsVaild(requireStatementExec, resourcePath) {
if (process.env.DEVICE_LEVEL !== 'lite' || !requireStatementExec || requireStatementExec.length <= 3) {
return;
}
const appJSPath = path.dirname(path.resolve(process.env.projectPath, 'app.js'));
if (!isPathUnderBase(resourcePath, appJSPath)) {
return;
}
const json5Path = path.join(process.env.projectPath, '../../../../', 'oh-package.json5');
const dependencies = [];
if (fs.existsSync(json5Path)) {
const json5Content = fs.readFileSync(json5Path, 'utf8');
const content = JSON5.parse(json5Content);
if (content['dependencies']) {
Object.keys(content['dependencies']).forEach(element =>{
dependencies.push(element);
})
}
}
if (dependencies.length === 0) {
return;
}
const moduleName = requireStatementExec[2];
if (moduleName.includes('../')) {
return;
}
if (!dependencies.includes(moduleName)) {
throw new Error(`Cannot find module ${moduleName} or its corresponding type declarations.`);
}
}
function copyJsonFile(inputFile, outputFile) {
try {
const contentInput = JSON.parse(fs.readFileSync(inputFile, 'utf-8'));