describe: 包名和模块名不一致,导包用模块名构建不报错
Feature or Bugfix: Bugfix
Binary Source:Yes
Signed-off-by: youbing54<youbing3@huawei.com>
This commit is contained in:
youbing54 2024-11-09 15:24:39 +08:00
parent 88c88e8e7b
commit dd3e0f0232

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'));