|
| 1 | +let { abort } = require("faucet-pipeline-core/lib/util"); |
| 2 | +let path = require("path"); |
| 3 | +let makePostCSS = require("./make-postcss"); |
| 4 | + |
| 5 | +module.exports = (config, assetManager, { browsers, compact, sourcemaps } = {}) => { |
| 6 | + let bundlers = config.map(bundleConfig => |
| 7 | + makeBundler(bundleConfig, assetManager, { browsers, compact, sourcemaps })); |
| 8 | + |
| 9 | + return filepaths => Promise.all(bundlers.map(bundle => bundle(filepaths))); |
| 10 | +}; |
| 11 | + |
| 12 | +function makeBundler(config, assetManager, { browsers, compact, sourcemaps } = {}) { |
| 13 | + let { browserslist, fingerprint } = config; |
| 14 | + |
| 15 | + if(!config.source || !config.target) { |
| 16 | + abort("ERROR: CSS configuration requires both target and source"); |
| 17 | + } |
| 18 | + |
| 19 | + let source = assetManager.resolvePath(config.source); |
| 20 | + let target = assetManager.resolvePath(config.target, { enforceRelative: true }); |
| 21 | + |
| 22 | + if(browserslist === false) { |
| 23 | + browsers = null; |
| 24 | + } else if(browserslist) { |
| 25 | + browsers = browsers[browserslist]; |
| 26 | + } else { |
| 27 | + browsers = browsers.defaults; |
| 28 | + } |
| 29 | + |
| 30 | + let postCSS = makePostCSS(source, target, assetManager, sourcemaps, browsers); |
| 31 | + |
| 32 | + let previouslyIncludedFiles; |
| 33 | + |
| 34 | + return filepaths => { |
| 35 | + // If this is the first run or the changed file is one of the |
| 36 | + // previously included ones, run the compiler |
| 37 | + if(previouslyIncluded(filepaths, previouslyIncludedFiles)) { |
| 38 | + postCSS(). |
| 39 | + then(result => { |
| 40 | + previouslyIncludedFiles = result.stats.includedFiles. |
| 41 | + map(filepath => path.normalize(filepath)); |
| 42 | + |
| 43 | + let options = {}; |
| 44 | + if(fingerprint !== undefined) { |
| 45 | + options.fingerprint = fingerprint; |
| 46 | + } |
| 47 | + assetManager.writeFile(target, result.css, options); |
| 48 | + }). |
| 49 | + catch(error => { |
| 50 | + let options = { error }; |
| 51 | + if(fingerprint !== undefined) { |
| 52 | + options.fingerprint = fingerprint; |
| 53 | + } |
| 54 | + assetManager.writeFile(target, errorOutput(error.message, |
| 55 | + assetManager.referenceDir), |
| 56 | + options); |
| 57 | + }); |
| 58 | + } |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +function previouslyIncluded(filepaths, previouslyIncludedFiles) { |
| 63 | + return previouslyIncludedFiles === undefined || |
| 64 | + filepaths.some(filepath => { |
| 65 | + return previouslyIncludedFiles. |
| 66 | + find(candidate => candidate === path.normalize(filepath)); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +let MESSAGE_PATTERN = /^(\S+)(:\d+:\d+:)\s+(.+)$/; |
| 71 | + |
| 72 | +function errorOutput(message, referenceDir) { |
| 73 | + // eslint-disable-next-line no-unused-vars |
| 74 | + let [_, file, locator, description] = MESSAGE_PATTERN.exec(message); |
| 75 | + let error = `${path.relative(referenceDir, file)}${locator} ${description}`; |
| 76 | + |
| 77 | + return `body:before { |
| 78 | + content: "\\26a0 CSS Error: ${error.replace(/"/g, "'").replace(/\s+/g, " ")}"; |
| 79 | + font-weight: bold; |
| 80 | + display: block; |
| 81 | + border: 5px solid red; |
| 82 | + padding: 5px; |
| 83 | +}\n`; |
| 84 | +} |
0 commit comments