vue3-yuanma/packages/compiler-sfc/src/compileStyle.ts

199 lines
5.0 KiB
TypeScript
Raw Normal View History

2019-11-07 02:58:15 +00:00
import postcss, { ProcessOptions, LazyResult, Result, ResultMap } from 'postcss'
import trimPlugin from './stylePluginTrim'
import scopedPlugin from './stylePluginScoped'
import {
processors,
StylePreprocessor,
StylePreprocessorResults,
PreprocessLang
2019-11-07 02:58:15 +00:00
} from './stylePreprocessors'
2019-12-11 18:33:45 +00:00
import { RawSourceMap } from 'source-map'
2019-11-07 02:58:15 +00:00
2019-12-13 16:24:09 +00:00
export interface SFCStyleCompileOptions {
2019-11-07 02:58:15 +00:00
source: string
filename: string
id: string
2019-12-11 18:33:45 +00:00
map?: RawSourceMap
2019-11-07 02:58:15 +00:00
scoped?: boolean
vars?: boolean
2019-11-07 02:58:15 +00:00
trim?: boolean
preprocessLang?: PreprocessLang
2019-11-07 02:58:15 +00:00
preprocessOptions?: any
preprocessCustomRequire?: (id: string) => any
2019-11-07 02:58:15 +00:00
postcssOptions?: any
postcssPlugins?: any[]
}
2019-12-13 16:24:09 +00:00
export interface SFCAsyncStyleCompileOptions extends SFCStyleCompileOptions {
2019-11-07 02:58:15 +00:00
isAsync?: boolean
// css modules support, note this requires async so that we can get the
// resulting json
modules?: boolean
// maps to postcss-modules options
// https://github.com/css-modules/postcss-modules
modulesOptions?: {
scopeBehaviour?: 'global' | 'local'
globalModulePaths?: string[]
generateScopedName?:
| string
| ((name: string, filename: string, css: string) => string)
hashPrefix?: string
localsConvention?: 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly'
}
2019-11-07 02:58:15 +00:00
}
2019-12-13 16:24:09 +00:00
export interface SFCStyleCompileResults {
2019-11-07 02:58:15 +00:00
code: string
2019-12-11 18:33:45 +00:00
map: RawSourceMap | undefined
2019-11-07 02:58:15 +00:00
rawResult: LazyResult | Result | undefined
2019-12-11 18:33:45 +00:00
errors: Error[]
modules?: Record<string, string>
2019-11-07 02:58:15 +00:00
}
export function compileStyle(
2019-12-13 16:24:09 +00:00
options: SFCStyleCompileOptions
): SFCStyleCompileResults {
return doCompileStyle({
...options,
isAsync: false
}) as SFCStyleCompileResults
2019-11-07 02:58:15 +00:00
}
export function compileStyleAsync(
options: SFCAsyncStyleCompileOptions
2019-12-13 16:24:09 +00:00
): Promise<SFCStyleCompileResults> {
2019-11-07 02:58:15 +00:00
return doCompileStyle({ ...options, isAsync: true }) as Promise<
2019-12-13 16:24:09 +00:00
SFCStyleCompileResults
2019-11-07 02:58:15 +00:00
>
}
export function doCompileStyle(
2019-12-13 16:24:09 +00:00
options: SFCAsyncStyleCompileOptions
): SFCStyleCompileResults | Promise<SFCStyleCompileResults> {
2019-11-07 02:58:15 +00:00
const {
filename,
id,
scoped = false,
vars = false,
2019-11-07 02:58:15 +00:00
trim = true,
modules = false,
modulesOptions = {},
2019-11-07 02:58:15 +00:00
preprocessLang,
postcssOptions,
postcssPlugins
} = options
const preprocessor = preprocessLang && processors[preprocessLang]
const preProcessedSource = preprocessor && preprocess(options, preprocessor)
const map = preProcessedSource ? preProcessedSource.map : options.map
const source = preProcessedSource ? preProcessedSource.code : options.source
const plugins = (postcssPlugins || []).slice()
if (trim) {
plugins.push(trimPlugin())
}
if (scoped) {
plugins.push(scopedPlugin({ id, vars }))
2019-11-07 02:58:15 +00:00
}
let cssModules: Record<string, string> | undefined
if (modules) {
if (__GLOBAL__ || __ESM_BROWSER__) {
throw new Error(
'[@vue/compiler-sfc] `modules` option is not supported in the browser build.'
)
}
if (!options.isAsync) {
throw new Error(
'[@vue/compiler-sfc] `modules` option can only be used with compileStyleAsync().'
)
}
plugins.push(
require('postcss-modules')({
...modulesOptions,
getJSON: (_cssFileName: string, json: Record<string, string>) => {
cssModules = json
}
})
)
}
2019-11-07 02:58:15 +00:00
const postCSSOptions: ProcessOptions = {
...postcssOptions,
to: filename,
from: filename
}
if (map) {
postCSSOptions.map = {
inline: false,
annotation: false,
prev: map
}
}
let result: LazyResult | undefined
let code: string | undefined
let outMap: ResultMap | undefined
2019-12-11 18:33:45 +00:00
const errors: Error[] = []
2019-11-07 02:58:15 +00:00
if (preProcessedSource && preProcessedSource.errors.length) {
errors.push(...preProcessedSource.errors)
}
try {
result = postcss(plugins).process(source, postCSSOptions)
// In async mode, return a promise.
if (options.isAsync) {
return result
.then(result => ({
code: result.css || '',
2019-12-11 18:33:45 +00:00
map: result.map && (result.map.toJSON() as any),
2019-11-07 02:58:15 +00:00
errors,
modules: cssModules,
2019-11-07 02:58:15 +00:00
rawResult: result
}))
.catch(error => ({
code: '',
map: undefined,
2019-12-11 18:33:45 +00:00
errors: [...errors, error],
2019-11-07 02:58:15 +00:00
rawResult: undefined
}))
}
// force synchronous transform (we know we only have sync plugins)
code = result.css
outMap = result.map
} catch (e) {
errors.push(e)
}
return {
code: code || ``,
2019-12-11 18:33:45 +00:00
map: outMap && (outMap.toJSON() as any),
2019-11-07 02:58:15 +00:00
errors,
rawResult: result
}
}
function preprocess(
2019-12-13 16:24:09 +00:00
options: SFCStyleCompileOptions,
2019-11-07 02:58:15 +00:00
preprocessor: StylePreprocessor
): StylePreprocessorResults {
if ((__ESM_BROWSER__ || __GLOBAL__) && !options.preprocessCustomRequire) {
throw new Error(
`[@vue/compiler-sfc] Style preprocessing in the browser build must ` +
`provide the \`preprocessCustomRequire\` option to return the in-browser ` +
`version of the preprocessor.`
)
}
return preprocessor.render(
options.source,
options.map,
{
filename: options.filename,
...options.preprocessOptions
},
options.preprocessCustomRequire
)
2019-11-07 02:58:15 +00:00
}