From dd3e0f0232ff950fae9a3f496b8277ae486f33f1 Mon Sep 17 00:00:00 2001 From: youbing54 Date: Sat, 9 Nov 2024 15:24:39 +0800 Subject: [PATCH] =?UTF-8?q?IssueNo:=20https://gitee.com/openharmony/develo?= =?UTF-8?q?ptools=5Face=5Fjs2bundle/issues/IB35SC=20describe:=20=E5=8C=85?= =?UTF-8?q?=E5=90=8D=E5=92=8C=E6=A8=A1=E5=9D=97=E5=90=8D=E4=B8=8D=E4=B8=80?= =?UTF-8?q?=E8=87=B4=EF=BC=8C=E5=AF=BC=E5=8C=85=E7=94=A8=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=90=8D=E6=9E=84=E5=BB=BA=E4=B8=8D=E6=8A=A5=E9=94=99=20Featur?= =?UTF-8?q?e=20or=20Bugfix:=20Bugfix=20Binary=20Source:Yes=20Signed-off-by?= =?UTF-8?q?:=20youbing54?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/util.js b/src/util.js index 6a60d17..c8e9eb0 100644 --- a/src/util.js +++ b/src/util.js @@ -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'));