From 52451e4feb1b61c9887acbde87979ad4da7670d5 Mon Sep 17 00:00:00 2001 From: lizhouze Date: Fri, 29 Apr 2022 16:44:39 +0800 Subject: [PATCH 1/2] lizhouze@huawei.com Signed-off-by: lizhouze --- deps/weex-styler/index.js | 77 ++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/deps/weex-styler/index.js b/deps/weex-styler/index.js index 35ce806..8cb5e63 100644 --- a/deps/weex-styler/index.js +++ b/deps/weex-styler/index.js @@ -531,33 +531,37 @@ function parseImport(resourcePath, rule, jsonStyle, log) { if(!resourcePath) { return } - const resourcePath_ = resourcePath - let importString = rule.import - let importPath - let mediaString = '' - let source = '' + const resourcePath_ = resourcePath; + let importString = rule.import; + let importPath; + let mediaString = ''; + let source = ''; if (importString.match(IMPORT_MATCHER)) { - let filePath = importString.match(/['"]([^()]+?)['"]/) - importPath = filePath[1] - mediaString = importString.replace(importPath, '').replace(/['"]/g, '') + let filePath = importString.match(/['"]([^()]+?)['"]/); + importPath = filePath[1]; + mediaString = importString.replace(importPath, '').replace(/['"]/g, ''); } if(/^(\.)|(\.\.)\//.test(importPath)) { resourcePath = resourcePath.substring(0, resourcePath.lastIndexOf(path.sep) + 1); - importPath = path.resolve(resourcePath, importPath) + importPath = path.resolve(resourcePath, importPath); } - if (fs.existsSync(importPath)) { - source = fs.readFileSync(importPath).toString() - addPreviewCSS(importPath, resourcePath_) - } else { - log.push({ - line: rule.position.start.line, - column: rule.position.start.column, - reason: 'ERROR: no such file or directory, open ' + importPath - }) - return + if (!fs.existsSync(importPath)) { + const fileSearch = findFile(importPath); + if (fileSearch.result == true) { + importPath = fileSearch.filePath; + } else { + log.push({ + line: rule.position.start.line, + column: rule.position.start.column, + reason: 'ERROR: no such file or directory, open ' + importPath + }); + return; + } } + source = fs.readFileSync(importPath).toString(); + addPreviewCSS(importPath, resourcePath_); if (mediaString.length !== 0) { - source = '@media ' + mediaString + '{\n' + source + '\n}' + source = '@media ' + mediaString + '{\n' + source + '\n}'; } parse(source, (err, obj) => { if (err) { @@ -565,7 +569,38 @@ function parseImport(resourcePath, rule, jsonStyle, log) { } else { jsonStyle = Object.assign(jsonStyle, obj.jsonStyle) } - }, importPath) + }, importPath); +} + +function findFile(importPath) { + const resultObject = { + result: false + }; + if (!importPath || !process.env.resolveModules) { + return resultObject; + } + try { + const modules = JSON.parse(process.env.resolveModules); + modules.forEach(item => { + if (fs.existsSync(item)) { + if (fs.existsSync(path.join(item, importPath))) { + resultObject.result = true; + resultObject.filePath = path.join(item, importPath); + return resultObject; + } + } else { + const resolveItem = path.resolve(__dirname, item); + if (fs.existsSync(resolveItem)) { + resultObject.result = true; + resultObject.filePath = path.join(resolveItem, importPath); + return resultObject; + } + } + }); + } catch (error) { + resultObject.result = false; + } + return resultObject; } function addPreviewCSS(importPath, resourcePath) { -- Gitee From f83a4a693c409688db753b4ebb23740be1a40faf Mon Sep 17 00:00:00 2001 From: lizhouze Date: Tue, 24 May 2022 15:20:41 +0800 Subject: [PATCH 2/2] lizhouze@huawei.com Signed-off-by: lizhouze --- src/loader.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/loader.js b/src/loader.js index bf83674..56cadb3 100644 --- a/src/loader.js +++ b/src/loader.js @@ -351,7 +351,7 @@ function loadPageFindCss (_this, filename, customLang) { element: undefined, elementName: undefined, source: cssFileName - }), cssFileName) + }), filename) } else { // find less @@ -364,7 +364,7 @@ function loadPageFindCss (_this, filename, customLang) { element: undefined, elementName: undefined, source: lessFileName - }), lessFileName) + }), filename) } else { // find scss @@ -377,7 +377,7 @@ function loadPageFindCss (_this, filename, customLang) { element: undefined, elementName: undefined, source: scssFileName - }), scssFileName) + }), filename) } else { // find sass @@ -390,7 +390,7 @@ function loadPageFindCss (_this, filename, customLang) { element: undefined, elementName: undefined, source: sassFileName - }), sassFileName) + }), filename) } else { extcss = false -- Gitee