]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler-sfc): add preprocessCustomRequire option
authorEvan You <yyx990803@gmail.com>
Fri, 24 Apr 2020 13:27:51 +0000 (09:27 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 24 Apr 2020 13:28:03 +0000 (09:28 -0400)
packages/compiler-sfc/src/compileStyle.ts
packages/compiler-sfc/src/stylePreprocessors.ts

index 129c8ca3b13e21e8911b037b23bdeb51d160dbe8..a5c513319f04da4867e6abfff3c86ef9ca3acebb 100644 (file)
@@ -1,4 +1,3 @@
-// const postcss = require('postcss')
 import postcss, { ProcessOptions, LazyResult, Result, ResultMap } from 'postcss'
 import trimPlugin from './stylePluginTrim'
 import scopedPlugin from './stylePluginScoped'
@@ -19,6 +18,7 @@ export interface SFCStyleCompileOptions {
   trim?: boolean
   preprocessLang?: PreprocessLang
   preprocessOptions?: any
+  preprocessCustomRequire?: (id: string) => any
   postcssOptions?: any
   postcssPlugins?: any[]
 }
@@ -137,8 +137,13 @@ function preprocess(
   options: SFCStyleCompileOptions,
   preprocessor: StylePreprocessor
 ): StylePreprocessorResults {
-  return preprocessor.render(options.source, options.map, {
-    filename: options.filename,
-    ...options.preprocessOptions
-  })
+  return preprocessor.render(
+    options.source,
+    options.map,
+    {
+      filename: options.filename,
+      ...options.preprocessOptions
+    },
+    options.preprocessCustomRequire
+  )
 }
index b29f0279a758bd2c00586cffd070357061327d3c..c52910d7cd256e343eaf0d502ad197d4c2658ab7 100644 (file)
@@ -1,7 +1,12 @@
 import merge from 'merge-source-map'
 
 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 {
@@ -12,8 +17,8 @@ export interface StylePreprocessorResults {
 
 // .scss/.sass processor
 const scss: StylePreprocessor = {
-  render(source, map, options) {
-    const nodeSass = require('sass')
+  render(source, map, options, load = require) {
+    const nodeSass = load('sass')
     const finalOptions = {
       ...options,
       data: source,
@@ -41,18 +46,23 @@ const scss: StylePreprocessor = {
 }
 
 const sass: StylePreprocessor = {
-  render(source, map, options) {
-    return scss.render(source, map, {
-      ...options,
-      indentedSyntax: true
-    })
+  render(source, map, options, load) {
+    return scss.render(
+      source,
+      map,
+      {
+        ...options,
+        indentedSyntax: true
+      },
+      load
+    )
   }
 }
 
 // .less
 const less: StylePreprocessor = {
-  render(source, map, options) {
-    const nodeLess = require('less')
+  render(source, map, options, load = require) {
+    const nodeLess = load('less')
 
     let result: any
     let error: Error | null = null
@@ -81,8 +91,8 @@ const less: StylePreprocessor = {
 
 // .styl
 const styl: StylePreprocessor = {
-  render(source, map, options) {
-    const nodeStylus = require('stylus')
+  render(source, map, options, load = require) {
+    const nodeStylus = load('stylus')
     try {
       const ref = nodeStylus(source)
       Object.keys(options).forEach(key => ref.set(key, options[key]))