|
| 1 | +const async = require('async'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const upath = require('upath'); |
| 5 | + |
| 6 | +const importRegex = /@import\s+?["'](.+?)["'];/g; |
| 7 | + |
| 8 | +/** |
| 9 | + * Parses all of the imports for SCSS files and returns a flat object with the file path as the key and |
| 10 | + * file content as the value. |
| 11 | + * |
| 12 | + * @param {Array|string} cssFiles |
| 13 | + * @param {string} absolutePath |
| 14 | + * @param {string} type - scss | css |
| 15 | + * @param options - options object { bundle: boolean } |
| 16 | + * @param callback |
| 17 | + */ |
| 18 | +function assemble(cssFiles, absolutePath, type, options, callback) { |
| 19 | + const ret = {}; |
| 20 | + const ext = `.${type}`; |
| 21 | + |
| 22 | + const cssFilesArr = Array.isArray(cssFiles) ? cssFiles : [cssFiles]; |
| 23 | + |
| 24 | + const parseImports = (cssFileAlias, parseImportsCallback) => { |
| 25 | + const normalizedCssFileAlias = cssFileAlias.replace(/(\.scss)$/g, ''); |
| 26 | + const typedCssFilePath = path.join(absolutePath, normalizedCssFileAlias + ext); |
| 27 | + const rawCssFilePath = path.join(absolutePath, normalizedCssFileAlias + '.css'); |
| 28 | + const cssFileAliasDir = path.parse(normalizedCssFileAlias).dir; |
| 29 | + |
| 30 | + const fileParts = path.parse(typedCssFilePath); |
| 31 | + const underscoredFilePath = path.join(fileParts.dir, `_${fileParts.base}`); |
| 32 | + let filePath; |
| 33 | + |
| 34 | + if (fs.existsSync(typedCssFilePath)) { |
| 35 | + filePath = typedCssFilePath; |
| 36 | + } else if (fs.existsSync(underscoredFilePath)) { |
| 37 | + filePath = underscoredFilePath; |
| 38 | + } else if (ext === '.scss' && fs.existsSync(rawCssFilePath)) { |
| 39 | + filePath = rawCssFilePath; |
| 40 | + } else { |
| 41 | + // File doesn't exist, just return to skip it |
| 42 | + parseImportsCallback(); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + fs.readFile(filePath, { encoding: 'utf-8' }, (err, content) => { |
| 47 | + const matches = []; |
| 48 | + let match; |
| 49 | + let importCssFileAlias; |
| 50 | + |
| 51 | + if (!err) { |
| 52 | + // Ensure all import paths are Unix paths for prod compat. |
| 53 | + const cssFile = options.bundle |
| 54 | + ? upath.toUnix(normalizedCssFileAlias + ext) |
| 55 | + : normalizedCssFileAlias + ext; |
| 56 | + |
| 57 | + ret[cssFile] = content; |
| 58 | + match = importRegex.exec(content); |
| 59 | + |
| 60 | + while (match !== null) { |
| 61 | + [, importCssFileAlias] = match; |
| 62 | + if (cssFileAliasDir) { |
| 63 | + importCssFileAlias = path.join(cssFileAliasDir, importCssFileAlias); |
| 64 | + } |
| 65 | + |
| 66 | + if (!ret[importCssFileAlias + ext]) { |
| 67 | + matches.push(importCssFileAlias); |
| 68 | + } |
| 69 | + |
| 70 | + match = importRegex.exec(content); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + async.each(matches, parseImports, parseImportsCallback); |
| 75 | + }); |
| 76 | + }; |
| 77 | + |
| 78 | + async.map( |
| 79 | + cssFilesArr, |
| 80 | + (cssFile, mapCallback) => { |
| 81 | + const cssFileParts = path.parse(cssFile); |
| 82 | + const cssFileAlias = path.join(cssFileParts.dir, cssFileParts.name); |
| 83 | + |
| 84 | + parseImports(cssFileAlias, mapCallback); |
| 85 | + }, |
| 86 | + (err) => { |
| 87 | + if (err) { |
| 88 | + callback(err); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + callback(null, ret); |
| 93 | + }, |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +module.exports = { |
| 98 | + assemble, |
| 99 | +}; |
0 commit comments