Skip to content

Commit 9204f16

Browse files
znckyyx990803
authored andcommitted
feat: preprocess scss/sass styles with node-sass (#4)
1 parent b43acdb commit 9204f16

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

lib/compileStyle.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ProcessOptions, LazyResult } from 'postcss'
22
import postcss = require('postcss')
33
import trimPlugin from './stylePlugins/trim'
44
import scopedPlugin from './stylePlugins/scoped'
5+
import { processors, StylePreprocessor, StylePreprocessorResults } from './styleProcessors'
56

67
export interface StyleCompileOptions {
78
source: string
@@ -10,6 +11,8 @@ export interface StyleCompileOptions {
1011
map?: any
1112
scoped?: boolean
1213
trim?: boolean
14+
preprocessLang?: string
15+
preprocessOptions?: any
1316
}
1417

1518
export interface StyleCompileResults {
@@ -23,13 +26,16 @@ export function compileStyle (
2326
options: StyleCompileOptions
2427
): StyleCompileResults {
2528
const {
26-
source,
2729
filename,
2830
id,
29-
map,
3031
scoped = true,
31-
trim = true
32+
trim = true,
33+
preprocessLang
3234
} = options
35+
const preprocessor = preprocessLang && processors[preprocessLang]
36+
const preProcessedSource = preprocessor && preprocess(options, preprocessor)
37+
const map = preProcessedSource ? preProcessedSource.map : options.map
38+
const source = preProcessedSource ? preProcessedSource.code : options.source
3339

3440
const plugins = []
3541
if (trim) {
@@ -69,3 +75,12 @@ export function compileStyle (
6975
rawResult: result
7076
}
7177
}
78+
79+
function preprocess(
80+
options: StyleCompileOptions,
81+
preprocessor: StylePreprocessor
82+
): StylePreprocessorResults {
83+
return preprocessor.render(options.source, options.map, Object.assign({
84+
filename: options.filename
85+
}, options.preprocessOptions))
86+
}

lib/styleProcessors/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import merge from 'merge-source-map'
2+
3+
export interface StylePreprocessor {
4+
render(
5+
source: string,
6+
map: any | null,
7+
options: any
8+
): StylePreprocessorResults
9+
}
10+
11+
export interface StylePreprocessorResults {
12+
code: string
13+
map?: any
14+
}
15+
16+
// .scss/.sass processor
17+
const scss: StylePreprocessor = {
18+
render(
19+
source: string,
20+
map: any | null,
21+
options: any
22+
): StylePreprocessorResults {
23+
const nodeSass = require('node-sass')
24+
const finalOptions = Object.assign({}, options, {
25+
data: source,
26+
file: options.filename,
27+
sourceMap: !!map
28+
})
29+
30+
const result = nodeSass.renderSync(finalOptions)
31+
32+
if (map) {
33+
return {
34+
code: result.css.toString(),
35+
map: merge(map, JSON.parse(result.map.toString()))
36+
}
37+
}
38+
39+
return { code: result.css.toString() }
40+
}
41+
}
42+
43+
const sass = {
44+
render(
45+
source: string,
46+
map: any | null,
47+
options: any
48+
): StylePreprocessorResults {
49+
return scss.render(
50+
source,
51+
map,
52+
Object.assign({}, options, { indentedSyntax: true })
53+
)
54+
}
55+
}
56+
57+
export const processors: { [key: string]: StylePreprocessor } = {
58+
scss,
59+
sass
60+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"consolidate": "^0.15.1",
3838
"hash-sum": "^1.0.2",
3939
"lru-cache": "^4.1.2",
40+
"merge-source-map": "^1.1.0",
4041
"postcss": "^6.0.20",
4142
"postcss-selector-parser": "^3.1.1",
4243
"prettier": "^1.11.1",

yarn.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,12 @@ mem@^1.1.0:
22192219
dependencies:
22202220
mimic-fn "^1.0.0"
22212221

2222+
merge-source-map@^1.1.0:
2223+
version "1.1.0"
2224+
resolved "https://registry.yarnpkg.com/merge-source-map/-/merge-source-map-1.1.0.tgz#2fdde7e6020939f70906a68f2d7ae685e4c8c646"
2225+
dependencies:
2226+
source-map "^0.6.1"
2227+
22222228
merge-stream@^1.0.1:
22232229
version "1.0.1"
22242230
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-1.0.1.tgz#4041202d508a342ba00174008df0c251b8c135e1"

0 commit comments

Comments
 (0)