From 6dd68954273ef7dc87dbc75824e28e004c01ff60 Mon Sep 17 00:00:00 2001 From: lizhouze Date: Thu, 23 Jun 2022 18:58:39 +0800 Subject: [PATCH] fixed 71d968f from https://gitee.com/kage1/third_party_weex-loader/pulls/74 lizhouze@huawei.com Signed-off-by: lizhouze --- src/loader.js | 26 +++++++++++++++++++++----- src/util.js | 1 + 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/loader.js b/src/loader.js index bf83674..664168c 100644 --- a/src/loader.js +++ b/src/loader.js @@ -31,7 +31,8 @@ import { getRequireString, stringifyLoaders, logWarn, - loadBabelModule + loadBabelModule, + elements } from './util' import { isReservedTag } from './templater/component_validator' @@ -180,6 +181,11 @@ function loader (source) { const isEntry = resourceQuery.entry const dirName = path.parse(this.resourcePath) const name = isEntry ? dirName.name : resourceQuery.name || getNameByPath(this.resourcePath) + const parentName = resourceQuery.parentName || name; + if (isEntry) { + elements[name] = elements[name] || {}; + elements[name][name] = true; + } if (isReservedTag(name) && process.env.abilityType === 'page') { logWarn(this, [{ reason: 'ERROR: The file name cannot contain reserved tag name: ' + name @@ -189,7 +195,7 @@ function loader (source) { let output = '' // import app.js output += loadApp(this, name, isEntry, customLang, source) - output += loadPage(this, name, isEntry, customLang, source) + output += loadPage(this, name, isEntry, customLang, source, parentName); return output } @@ -262,7 +268,7 @@ function loadApp (_this, name, isEntry, customLang, source) { } } -function loadPage (_this, name, isEntry, customLang, source) { +function loadPage (_this, name, isEntry, customLang, source, parentName) { let output = '' if (path.extname(_this.resourcePath).match(/\.hml/)) { const filename = _this.resourcePath.replace(path.extname(_this.resourcePath).toString(), '') @@ -272,7 +278,8 @@ function loadPage (_this, name, isEntry, customLang, source) { const frag = parseFragment(source) const elementNames = [] const elementLength = frag.element.length - output += loadPageCheckElementLength(_this, elementLength, frag, elementNames, resourcePath, customLang) + output += loadPageCheckElementLength(_this, elementLength, frag, elementNames, resourcePath, + customLang, parentName); output += 'var $app_template$ = ' + getRequireString(_this, getLoaderString('template', { customLang, @@ -299,7 +306,8 @@ function loadPage (_this, name, isEntry, customLang, source) { return output } -function loadPageCheckElementLength (_this, elementLength, frag, elementNames, resourcePath, customLang) { +function loadPageCheckElementLength (_this, elementLength, frag, elementNames, resourcePath, + customLang, parentName) { let output = '' if (elementLength) { for (let i = 0; i < elementLength; i++) { @@ -320,6 +328,14 @@ function loadPageCheckElementLength (_this, elementLength, frag, elementNames, r if (!element.name) { element.name = path.parse(src).name } + elements[parentName] = elements[parentName] || {}; + if (elements[parentName][element.name]) { + logWarn(_this, [{ + reason: `ERROR: The element name ${element.name} can not be repeated.` + }]); + } else { + elements[parentName][element.name] = true; + } checkEntry(_this, filePath, element.src) } else { diff --git a/src/util.js b/src/util.js index 92c9fc8..18e8930 100644 --- a/src/util.js +++ b/src/util.js @@ -28,6 +28,7 @@ import { const { DEVICE_LEVEL } = require('./lite/lite-enum') export const useOSFiles = new Set(); +export const elements = {}; export function getNameByPath (resourcePath) { return path.basename(resourcePath).replace(/\..*$/, '') -- Gitee