-
Notifications
You must be signed in to change notification settings - Fork 40
/
render.ts
204 lines (183 loc) · 6.36 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {dirname, parse, relative, resolve, sep} from 'path'
import fs, {readFileSync} from 'fs'
import {createResolver, fileSyntax, sourceMappingURL, DEFAULT_FILTER} from './utils'
import {PartialMessage} from 'esbuild'
import {fileURLToPath, pathToFileURL} from 'url'
import {SassPluginOptions} from './index'
import {ImporterResult} from 'sass'
import {StringOptions} from 'sass/types/options'
import {CompileResult} from 'sass/types/compile'
export type RenderSass = (path: string) => Promise<RenderResult>
export type RenderResult = {
cssText: string
watchFiles: string[]
warnings?: PartialMessage[]
}
export type Compiler = (source: string, options?: StringOptions<any>) => CompileResult | Promise<CompileResult>
async function createCompiler(embedded:boolean|undefined, onDispose: (callback: () => void) => void): Promise<Compiler> {
if (embedded) {
const {initAsyncCompiler} = await import('sass-embedded')
const compiler = await initAsyncCompiler();
onDispose(compiler.dispose.bind(compiler))
return compiler.compileStringAsync.bind(compiler) as Compiler
} else {
const {compileString} = await import('sass')
return compileString
}
}
export async function createRenderer(
options: SassPluginOptions = {},
sourcemap: boolean,
onDispose: (callback: () => void) => void
): Promise<RenderSass> {
const loadPaths = options.loadPaths!
const resolveModule = createResolver(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)
const pathParts = parse(absolute)
if (DEFAULT_FILTER.test(pathParts.ext)) {
return resolveImport(pathParts.dir + sep + pathParts.name, pathParts.ext)
} else {
return resolveImport(absolute)
}
}
const sepTilde = `${sep}~`
const compileString = await createCompiler(options.embedded, onDispose)
/**
* renderAsync
*/
return async function (path: string): Promise<RenderResult> {
const basedir = dirname(path)
let source = fs.readFileSync(path, 'utf-8')
if (options.precompile) {
source = options.precompile(source, path, true)
}
const syntax = fileSyntax(path)
if (syntax === 'css') {
return {cssText: readFileSync(path, 'utf-8'), watchFiles: [path]}
}
if (options.quietDeps) {
options.url = pathToFileURL(path)
}
const warnings: PartialMessage[] = []
const logger = options.logger ?? {
warn: function (message, opts) {
if (!opts.span) {
warnings.push({text: `sass warning: ${message}`})
} else {
const filename = opts.span.url?.pathname ?? path
const esbuildMsg = {
text: message,
location: {
file: filename,
line: opts.span.start.line,
column: opts.span.start.column,
lineText: opts.span.text
},
detail: {
deprecation: opts.deprecation,
stack: opts.stack
}
}
warnings.push(esbuildMsg)
}
}
}
const {
css,
loadedUrls,
sourceMap
} = await compileString(source, {
sourceMapIncludeSources: true,
...options,
logger,
syntax,
importer: {
load(canonicalUrl: URL): ImporterResult | null {
const pathname = fileURLToPath(canonicalUrl)
let contents = fs.readFileSync(pathname, 'utf8')
if (options.precompile) {
contents = options.precompile(contents, pathname, false)
}
return {
contents,
syntax: fileSyntax(pathname),
sourceMapUrl: sourcemap ? canonicalUrl : undefined
}
},
canonicalize(url: string): URL | null {
let filename: string
if (url.startsWith('~')) {
filename = resolveModule(decodeURI(url.slice(1)), basedir)
} else if (url.startsWith('file://')) {
filename = fileURLToPath(url)
// ================================================ patch for: https://github.com/sass/dart-sass/issues/1581
let joint = filename.lastIndexOf(sepTilde)
if (joint >= 0) {
filename = resolveModule(filename.slice(joint + 2), filename.slice(0, joint))
}
// =========================================================================================================
} else {
filename = decodeURI(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
})
let cssText = css.toString()
if (sourceMap) {
sourceMap.sourceRoot = basedir
sourceMap.sources = sourceMap.sources.map(source => {
return relative(basedir, source.startsWith('data:') ? path : fileURLToPath(source))
})
cssText += '\n' + sourceMappingURL(sourceMap)
}
return {
cssText,
warnings: warnings,
watchFiles: [path, ...loadedUrls.map(fileURLToPath)]
}
}
}