-
Notifications
You must be signed in to change notification settings - Fork 40
/
render.ts
150 lines (136 loc) · 4.73 KB
/
render.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {dirname, resolve, sep} from 'path'
import fs, {readFileSync} from 'fs'
import {fileSyntax, sourceMappingURL} from './utils'
import * as sass from 'sass'
import {ImporterResult} from 'sass'
import {pathToFileURL} from 'url'
import {SassPluginOptions} from './index'
export type RenderSync = (path: string) => RenderResult
export type RenderResult = {
css: string
watchFiles: string[]
}
export function createRenderer(options: SassPluginOptions = {}, sourcemap: boolean): RenderSync {
const loadPaths = options.loadPaths!
/**
* NOTE: we're deliberately ignoring sass recommendation to avoid sacrificing speed here!
* - we prefer fragment attempt over syntax attempt
* - we prefer .scss and .css over .sass
* - we don't throw exceptions if the URL is ambiguous
*/
function resolveImport(pathname: string, ext?: string): string | null {
if (ext) {
let filename = pathname + ext
if (fs.existsSync(filename)) {
return filename
}
const index = filename.lastIndexOf(sep)
filename = index >= 0 ? filename.slice(0, index) + sep + '_' + filename.slice(index + 1) : '_' + filename
if (fs.existsSync(filename)) {
return filename
}
return null
} else {
if (!fs.existsSync(dirname(pathname))) {
return null
}
return resolveImport(pathname, '.scss')
|| resolveImport(pathname, '.css')
|| resolveImport(pathname, '.sass')
|| resolveImport(pathname + sep + 'index')
}
}
function resolveRelativeImport(loadPath: string, filename: string): string | null {
const absolute = resolve(loadPath, filename)
let ext = absolute.lastIndexOf('.')
if (ext >= 0) {
return resolveImport(absolute.slice(0, ext), absolute.slice(ext))
} else {
return resolveImport(absolute)
}
}
const requireOptions = {paths: ['.', ...loadPaths]}
/**
* renderSync
*/
return function (path: string): RenderResult {
const basedir = dirname(path)
let source = fs.readFileSync(path, 'utf8')
if (options.precompile) {
source = options.precompile(source, path)
}
const syntax = fileSyntax(path)
if (syntax === 'css') {
return {css: readFileSync(path, 'utf-8'), watchFiles: [path]}
}
const {
css,
loadedUrls,
sourceMap
} = sass.compileString(source, {
...options,
syntax,
importer: {
load(canonicalUrl: URL): ImporterResult | null {
const filename = sep === '/' ? canonicalUrl.pathname : canonicalUrl.pathname.slice(1)
let contents = fs.readFileSync(filename, 'utf8')
if (options.precompile) {
contents = options.precompile(contents, filename)
}
return {
contents,
syntax: fileSyntax(filename),
sourceMapUrl: sourcemap ? pathToFileURL(filename) : undefined
}
},
canonicalize(url: string): URL | null {
let filename
if (url.startsWith('~')) {
filename = url.slice(1)
try {
requireOptions.paths[0] = basedir
filename = require.resolve(filename, requireOptions)
} catch (ignored) {
}
} else if (url.startsWith('file://')) {
filename = sep === '/' ? url.slice(7) : url.slice(8)
// ================================================ patch for: https://github.com/sass/dart-sass/issues/1581
let joint = filename.lastIndexOf('/~')
if (joint >= 0) {
const basedir = filename.slice(0, joint)
filename = filename.slice(joint + 2)
try {
requireOptions.paths[0] = basedir
filename = require.resolve(filename, requireOptions)
} catch (ignored) {
}
}
// =========================================================================================================
} else {
filename = url
}
if (options.importMapper) {
filename = options.importMapper(filename)
}
let resolved = resolveRelativeImport(basedir, filename)
if (resolved) {
return pathToFileURL(resolved)
}
for (const loadPath of loadPaths) {
resolved = resolveRelativeImport(loadPath, filename)
if (resolved) {
return pathToFileURL(resolved)
}
}
return null
}
},
sourceMap: sourcemap
})
const cssText = css.toString()
return {
css: sourcemap ? `${cssText}\n${sourceMappingURL(sourceMap)}` : cssText,
watchFiles: [path, ...loadedUrls.map(url => sep === '/' ? url.pathname : url.pathname.slice(1))]
}
}
}