diff --git a/deps/weex-styler/index.js b/deps/weex-styler/index.js index 35ce806314c2c3ceeb5e7fafab808873659fd18b..4509bcfdeb349baaa4df10bbf7e9d755943952a4 100644 --- a/deps/weex-styler/index.js +++ b/deps/weex-styler/index.js @@ -545,17 +545,22 @@ function parseImport(resourcePath, rule, jsonStyle, log) { resourcePath = resourcePath.substring(0, resourcePath.lastIndexOf(path.sep) + 1); 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}' } @@ -586,6 +591,45 @@ function addPreviewCSS(importPath, resourcePath) { } } +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 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 *