Skip to content

Commit 814f76e

Browse files
committed
refactor(vue2): refactor styleProcessors to a webpack alias
1 parent c4d06ff commit 814f76e

File tree

3 files changed

+21
-34
lines changed

3 files changed

+21
-34
lines changed

src/vue2/styleProcessors.ts renamed to build/vue2StyleProcessors.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,22 @@ export interface StylePreprocessorResults {
2222
errors: Array<Error>
2323
}
2424

25+
const customRequire = (name: string, options?: any) => {
26+
const requireFn = options?.preprocessOptions?.customRequire
27+
if (requireFn) {
28+
return requireFn(name)
29+
}
30+
return require(name)
31+
}
32+
2533
// .scss/.sass processor
2634
const scss: StylePreprocessor = {
2735
render(
2836
source: string,
2937
map: any | null,
30-
options: any,
31-
load: (id: string)=> any = require
38+
options: any
3239
): StylePreprocessorResults {
33-
const nodeSass = load('sass')
40+
const nodeSass = customRequire('sass', options)
3441
const finalOptions = Object.assign({}, options, {
3542
data: source,
3643
file: options.filename,
@@ -61,13 +68,11 @@ const sass = {
6168
source: string,
6269
map: any | null,
6370
options: any,
64-
load: (id: string)=> any = require
6571
): StylePreprocessorResults {
6672
return scss.render(
6773
source,
6874
map,
69-
Object.assign({}, options, { indentedSyntax: true }),
70-
load
75+
Object.assign({}, options, { indentedSyntax: true })
7176
)
7277
}
7378
}
@@ -77,10 +82,9 @@ const less = {
7782
render(
7883
source: string,
7984
map: any | null,
80-
options: any,
81-
load: (id: string)=> any = require
85+
options: any
8286
): StylePreprocessorResults {
83-
const nodeLess = load('less')
87+
const nodeLess = customRequire('less', options)
8488

8589
let result: any
8690
let error: Error | null = null
@@ -112,10 +116,9 @@ const styl = {
112116
render(
113117
source: string,
114118
map: any | null,
115-
options: any,
116-
load: (id: string)=> any = require
119+
options: any
117120
): StylePreprocessorResults {
118-
const nodeStylus = load('stylus')
121+
const nodeStylus = customRequire('stylus', options)
119122
try {
120123
const ref = nodeStylus(source)
121124
Object.keys(options).forEach(key => ref.set(key, options[key]))

build/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ ${ pkg.name } v${ pkg.version }
208208
'less': false,
209209
'prettier': false,
210210
'./buble.js': Path.resolve(__dirname, 'fakeBuble.mjs'), // used by vue-template-es2015-compiler
211+
'./styleProcessors': Path.resolve(__dirname, 'vue2StyleProcessors.ts'), // used by @vue/component-compiler-utils
211212

212213
...!genSourcemap ? {
213214
'source-map': false,

src/createVue2SFCModule.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ import {
4545
CustomBlockCallback
4646
} from './types'
4747

48-
import { processors as styleProcessors } from './vue2/styleProcessors'
49-
5048
/**
5149
* the version of the library (process.env.VERSION is set by webpack, at compile-time)
5250
*/
@@ -225,27 +223,12 @@ export async function createSFCModule(source : string, filename : string, option
225223
filename,
226224
id: scopeId,
227225
scoped: descStyle.scoped,
228-
trim: false
229-
}
230-
231-
if (descStyle.lang) {
232-
const processor = styleProcessors[descStyle.lang]
233-
if (processor) {
234-
const result = processor.render(
235-
compileStyleOptions.source,
236-
genSourcemap,
237-
compileStyleOptions.preprocessOptions,
238-
(id) => moduleCache[id]
239-
)
240-
241-
if (result.errors.length) {
242-
preventCache();
243-
for ( const err of result.errors ) {
244-
log?.('error', 'SFC style', formatError(err.message, filename, descStyle.content));
245-
}
226+
trim: false,
227+
preprocessLang: descStyle.lang,
228+
preprocessOptions: {
229+
preprocessOptions: {
230+
customRequire: (id: string) => moduleCache[id]
246231
}
247-
248-
compileStyleOptions.source = result.code
249232
}
250233
}
251234

0 commit comments

Comments
 (0)