feat(compiler-sfc): add preprocessCustomRequire option

This commit is contained in:
Evan You 2020-04-24 09:27:51 -04:00
parent d1e6932edc
commit 20d425fb19
2 changed files with 32 additions and 17 deletions

View File

@ -1,4 +1,3 @@
// const postcss = require('postcss')
import postcss, { ProcessOptions, LazyResult, Result, ResultMap } from 'postcss' import postcss, { ProcessOptions, LazyResult, Result, ResultMap } from 'postcss'
import trimPlugin from './stylePluginTrim' import trimPlugin from './stylePluginTrim'
import scopedPlugin from './stylePluginScoped' import scopedPlugin from './stylePluginScoped'
@ -19,6 +18,7 @@ export interface SFCStyleCompileOptions {
trim?: boolean trim?: boolean
preprocessLang?: PreprocessLang preprocessLang?: PreprocessLang
preprocessOptions?: any preprocessOptions?: any
preprocessCustomRequire?: (id: string) => any
postcssOptions?: any postcssOptions?: any
postcssPlugins?: any[] postcssPlugins?: any[]
} }
@ -137,8 +137,13 @@ function preprocess(
options: SFCStyleCompileOptions, options: SFCStyleCompileOptions,
preprocessor: StylePreprocessor preprocessor: StylePreprocessor
): StylePreprocessorResults { ): StylePreprocessorResults {
return preprocessor.render(options.source, options.map, { return preprocessor.render(
options.source,
options.map,
{
filename: options.filename, filename: options.filename,
...options.preprocessOptions ...options.preprocessOptions
}) },
options.preprocessCustomRequire
)
} }

View File

@ -1,7 +1,12 @@
import merge from 'merge-source-map' import merge from 'merge-source-map'
export interface StylePreprocessor { export interface StylePreprocessor {
render(source: string, map?: object, options?: any): StylePreprocessorResults render(
source: string,
map?: object,
options?: any,
customRequire?: (id: string) => any
): StylePreprocessorResults
} }
export interface StylePreprocessorResults { export interface StylePreprocessorResults {
@ -12,8 +17,8 @@ export interface StylePreprocessorResults {
// .scss/.sass processor // .scss/.sass processor
const scss: StylePreprocessor = { const scss: StylePreprocessor = {
render(source, map, options) { render(source, map, options, load = require) {
const nodeSass = require('sass') const nodeSass = load('sass')
const finalOptions = { const finalOptions = {
...options, ...options,
data: source, data: source,
@ -41,18 +46,23 @@ const scss: StylePreprocessor = {
} }
const sass: StylePreprocessor = { const sass: StylePreprocessor = {
render(source, map, options) { render(source, map, options, load) {
return scss.render(source, map, { return scss.render(
source,
map,
{
...options, ...options,
indentedSyntax: true indentedSyntax: true
}) },
load
)
} }
} }
// .less // .less
const less: StylePreprocessor = { const less: StylePreprocessor = {
render(source, map, options) { render(source, map, options, load = require) {
const nodeLess = require('less') const nodeLess = load('less')
let result: any let result: any
let error: Error | null = null let error: Error | null = null
@ -81,8 +91,8 @@ const less: StylePreprocessor = {
// .styl // .styl
const styl: StylePreprocessor = { const styl: StylePreprocessor = {
render(source, map, options) { render(source, map, options, load = require) {
const nodeStylus = require('stylus') const nodeStylus = load('stylus')
try { try {
const ref = nodeStylus(source) const ref = nodeStylus(source)
Object.keys(options).forEach(key => ref.set(key, options[key])) Object.keys(options).forEach(key => ref.set(key, options[key]))