|
| 1 | +const postcss = require('postcss') |
| 2 | +import { ProcessOptions, LazyResult } from 'postcss' |
| 3 | +import trimPlugin from './stylePlugins/trim' |
| 4 | +import scopedPlugin from './stylePlugins/scoped' |
| 5 | +import { |
| 6 | + processors, |
| 7 | + StylePreprocessor, |
| 8 | + StylePreprocessorResults |
| 9 | +} from './stylePreprocessors' |
| 10 | + |
| 11 | +export interface StyleCompileOptions { |
| 12 | + source: string |
| 13 | + filename: string |
| 14 | + id: string |
| 15 | + map?: any |
| 16 | + scoped?: boolean |
| 17 | + trim?: boolean |
| 18 | + preprocessLang?: string |
| 19 | + preprocessOptions?: any |
| 20 | + postcssOptions?: any |
| 21 | + postcssPlugins?: any[] |
| 22 | +} |
| 23 | + |
| 24 | +export interface AsyncStyleCompileOptions extends StyleCompileOptions { |
| 25 | + isAsync?: boolean |
| 26 | +} |
| 27 | + |
| 28 | +export interface StyleCompileResults { |
| 29 | + code: string |
| 30 | + map: any | void |
| 31 | + rawResult: LazyResult | void |
| 32 | + errors: string[] |
| 33 | +} |
| 34 | + |
| 35 | +export function compileStyle( |
| 36 | + options: StyleCompileOptions |
| 37 | +): StyleCompileResults { |
| 38 | + return doCompileStyle({ ...options, isAsync: false }) |
| 39 | +} |
| 40 | + |
| 41 | +export function compileStyleAsync( |
| 42 | + options: StyleCompileOptions |
| 43 | +): Promise<StyleCompileResults> { |
| 44 | + return Promise.resolve(doCompileStyle({ ...options, isAsync: true })) |
| 45 | +} |
| 46 | + |
| 47 | +export function doCompileStyle( |
| 48 | + options: AsyncStyleCompileOptions |
| 49 | +): StyleCompileResults { |
| 50 | + const { |
| 51 | + filename, |
| 52 | + id, |
| 53 | + scoped = true, |
| 54 | + trim = true, |
| 55 | + preprocessLang, |
| 56 | + postcssOptions, |
| 57 | + postcssPlugins |
| 58 | + } = options |
| 59 | + const preprocessor = preprocessLang && processors[preprocessLang] |
| 60 | + const preProcessedSource = preprocessor && preprocess(options, preprocessor) |
| 61 | + const map = preProcessedSource ? preProcessedSource.map : options.map |
| 62 | + const source = preProcessedSource ? preProcessedSource.code : options.source |
| 63 | + |
| 64 | + const plugins = (postcssPlugins || []).slice() |
| 65 | + if (trim) { |
| 66 | + plugins.push(trimPlugin()) |
| 67 | + } |
| 68 | + if (scoped) { |
| 69 | + plugins.push(scopedPlugin(id)) |
| 70 | + } |
| 71 | + |
| 72 | + const postCSSOptions: ProcessOptions = { |
| 73 | + ...postcssOptions, |
| 74 | + to: filename, |
| 75 | + from: filename |
| 76 | + } |
| 77 | + if (map) { |
| 78 | + postCSSOptions.map = { |
| 79 | + inline: false, |
| 80 | + annotation: false, |
| 81 | + prev: map |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + let result, code, outMap |
| 86 | + const errors: any[] = [] |
| 87 | + if (preProcessedSource && preProcessedSource.errors.length) { |
| 88 | + errors.push(...preProcessedSource.errors) |
| 89 | + } |
| 90 | + try { |
| 91 | + result = postcss(plugins).process(source, postCSSOptions) |
| 92 | + |
| 93 | + // In async mode, return a promise. |
| 94 | + if (options.isAsync) { |
| 95 | + return result |
| 96 | + .then( |
| 97 | + (result: LazyResult): StyleCompileResults => ({ |
| 98 | + code: result.css || '', |
| 99 | + map: result.map && result.map.toJSON(), |
| 100 | + errors, |
| 101 | + rawResult: result |
| 102 | + }) |
| 103 | + ) |
| 104 | + .catch( |
| 105 | + (error: Error): StyleCompileResults => ({ |
| 106 | + code: '', |
| 107 | + map: undefined, |
| 108 | + errors: [...errors, error.message], |
| 109 | + rawResult: undefined |
| 110 | + }) |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + // force synchronous transform (we know we only have sync plugins) |
| 115 | + code = result.css |
| 116 | + outMap = result.map |
| 117 | + } catch (e) { |
| 118 | + errors.push(e) |
| 119 | + } |
| 120 | + |
| 121 | + return { |
| 122 | + code: code || ``, |
| 123 | + map: outMap && outMap.toJSON(), |
| 124 | + errors, |
| 125 | + rawResult: result |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function preprocess( |
| 130 | + options: StyleCompileOptions, |
| 131 | + preprocessor: StylePreprocessor |
| 132 | +): StylePreprocessorResults { |
| 133 | + return preprocessor( |
| 134 | + options.source, |
| 135 | + options.map, |
| 136 | + Object.assign( |
| 137 | + { |
| 138 | + filename: options.filename |
| 139 | + }, |
| 140 | + options.preprocessOptions |
| 141 | + ) |
| 142 | + ) |
| 143 | +} |
0 commit comments