From 04b10890938c5112e00eac7feecacf137080cfe4 Mon Sep 17 00:00:00 2001 From: lizhouze Date: Thu, 26 May 2022 17:09:49 +0800 Subject: [PATCH] lizhouze@huawei.com Signed-off-by: lizhouze --- deps/weex-styler/index.js | 86 +++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 21 deletions(-) diff --git a/deps/weex-styler/index.js b/deps/weex-styler/index.js index 35ce806..3fcfab1 100644 --- a/deps/weex-styler/index.js +++ b/deps/weex-styler/index.js @@ -531,33 +531,38 @@ 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 { + writeErrorOption(); + 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 +570,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) { @@ -586,6 +622,14 @@ function addPreviewCSS(importPath, resourcePath) { } } +function writeErrorOption() { + if (fs.existsSync(process.env.watchCSSFiles)) { + const content = JSON.parse(fs.readFileSync(process.env.watchCSSFiles)); + content['clear'] = true; + fs.writeFileSync(process.env.watchCSSFiles, JSON.stringify(content, null, 2)); + } +} + /** * Validate a JSON Object and log errors & warnings * -- Gitee