| 1 |
import {OnLoadResult, Plugin} from 'esbuild' |
| 2 |
import {useCache} from './cache' |
| 3 |
import {dirname} from 'path' |
| 4 |
import {SassPluginOptions} from './index' |
| 5 |
import {createRenderer} from './render' |
| 6 |
import {DEFAULT_FILTER, getContext, makeModule, modulesPaths, parseNonce, posixRelative, safeExport} from './utils' |
| 7 |
|
| 8 |
let nextId = 0 |
| 9 |
|
| 10 |
/** |
| 11 |
* |
| 12 |
* @param options |
| 13 |
*/ |
| 14 |
export function sassPlugin(options: SassPluginOptions = {}): Plugin { |
| 15 |
|
| 16 |
if (!options.basedir) { |
| 17 |
options.basedir = process.cwd() |
| 18 |
} |
| 19 |
|
| 20 |
if (options.includePaths) { |
| 21 |
console.log(`'includePaths' option is deprecated, please use 'loadPaths' instead`) |
| 22 |
} |
| 23 |
|
| 24 |
const type = options.type ?? 'css' |
| 25 |
|
| 26 |
if (options['picomatch'] || options['exclude'] || typeof type !== 'string' && typeof type !== 'function') { |
| 27 |
console.log('The type array, exclude and picomatch options are no longer supported, please refer to the README for alternatives.') |
| 28 |
} |
| 29 |
|
| 30 |
const nonce = parseNonce(options.nonce) |
| 31 |
|
| 32 |
return { |
| 33 |
name: 'sass-plugin', |
| 34 |
async setup({initialOptions, onResolve, onLoad, resolve, onStart, onDispose}) { |
| 35 |
|
| 36 |
options.loadPaths = Array.from(new Set([ |
| 37 |
...options.loadPaths || modulesPaths(initialOptions.absWorkingDir), |
| 38 |
...options.includePaths || [] |
| 39 |
])) |
| 40 |
|
| 41 |
const { |
| 42 |
sourcemap, |
| 43 |
watched |
| 44 |
} = getContext(initialOptions) |
| 45 |
|
| 46 |
if (options.cssImports) { |
| 47 |
onResolve({filter: /^~.*\.css$/}, ({path, importer, resolveDir}) => { |
| 48 |
return resolve(path.slice(1), {importer, resolveDir, kind: 'import-rule'}) |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
const transform = options.transform ? options.transform.bind(options) : null |
| 53 |
const namedExports = options.namedExports === 'safe' ? safeExport : options.namedExports |
| 54 |
|
| 55 |
const cssChunks: Record<string, string | Uint8Array | undefined> = {} |
| 56 |
const pluginSuffix = nextId ? `-${nextId++}` : (nextId = 1) && '' |
| 57 |
const cssChunkPrefix = 'css-chunk' + pluginSuffix |
| 58 |
|
| 59 |
if (transform) { |
| 60 |
const namespace = 'esbuild-sass-plugin' + pluginSuffix |
| 61 |
|
| 62 |
onResolve({filter: new RegExp(`^${cssChunkPrefix}`)}, ({path, resolveDir}) => ({ |
| 63 |
path, |
| 64 |
namespace, |
| 65 |
pluginData: {resolveDir} |
| 66 |
})) |
| 67 |
|
| 68 |
onLoad({filter: /./, namespace}, ({path, pluginData: {resolveDir}}) => ({ |
| 69 |
contents: cssChunks[path], |
| 70 |
resolveDir, |
| 71 |
loader: 'css' |
| 72 |
})) |
| 73 |
} |
| 74 |
|
| 75 |
const renderSass = await createRenderer(options, options.sourceMap ?? sourcemap, onDispose) |
| 76 |
|
| 77 |
const [cached, resetCache] = useCache(options) |
| 78 |
|
| 79 |
onStart(resetCache) |
| 80 |
|
| 81 |
onLoad({filter: options.filter ?? DEFAULT_FILTER}, cached(async ({path}) => { |
| 82 |
try { |
| 83 |
let {cssText, watchFiles, warnings} = await renderSass(path) |
| 84 |
if (!warnings) { |
| 85 |
warnings = [] |
| 86 |
} |
| 87 |
|
| 88 |
watched[path] = watchFiles |
| 89 |
|
| 90 |
const resolveDir = dirname(path) |
| 91 |
|
| 92 |
if (transform) { |
| 93 |
const out: string | OnLoadResult = await transform(cssText, resolveDir, path) |
| 94 |
if (typeof out !== 'string') { |
| 95 |
if (out.loader && out.loader !== 'js') { |
| 96 |
return { |
| 97 |
...out, |
| 98 |
resolveDir, |
| 99 |
watchFiles: [...watchFiles, ...(out.watchFiles || [])], |
| 100 |
watchDirs: out.watchDirs || [] |
| 101 |
} |
| 102 |
} |
| 103 |
let {contents, pluginData} = out |
| 104 |
if (type === 'css') { |
| 105 |
let name = cssChunkPrefix + posixRelative(path) |
| 106 |
cssChunks[name] = contents |
| 107 |
contents = `import '${name}';` |
| 108 |
} else if (type === 'style') { |
| 109 |
contents = makeModule(String(contents), 'style', nonce) |
| 110 |
} else { |
| 111 |
return { |
| 112 |
errors: [{text: `unsupported type '${type}' for postCSS modules`}] |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if (namedExports) { |
| 117 |
const json = JSON.parse(pluginData.exports) |
| 118 |
const keys = Object.keys(json) |
| 119 |
if (typeof namedExports === 'function') { |
| 120 |
const vars: Array<[string, string]> = [] |
| 121 |
for (const name of keys) { |
| 122 |
const safe = namedExports(name) |
| 123 |
if (safe) { |
| 124 |
if (safe !== name) { |
| 125 |
if (json[safe]) { |
| 126 |
return {errors: [{text: `clash detected in safe named export '${safe}'`}]} |
| 127 |
} |
| 128 |
console.log(`exported '${name}' as '${safe}' in '${path}'`) |
| 129 |
} |
| 130 |
vars.push([safe, name]) |
| 131 |
} |
| 132 |
} |
| 133 |
if (vars.length > 0) { |
| 134 |
contents += `export const ${ |
| 135 |
vars.map(([name, key]) => { |
| 136 |
const assignment = `${name}=${JSON.stringify(json[key])}` |
| 137 |
json[key] = name |
| 138 |
return assignment |
| 139 |
}).join(',')};` |
| 140 |
} |
| 141 |
} else { |
| 142 |
contents += `export const ${ |
| 143 |
keys.map(key => { |
| 144 |
const assignment = `${key}=${JSON.stringify(json[key])}` |
| 145 |
json[key] = key |
| 146 |
return assignment |
| 147 |
}).join(',')};` |
| 148 |
} |
| 149 |
contents += `export default {${ |
| 150 |
keys.map(key => `${JSON.stringify(key)}:${json[key]}`).join(',') |
| 151 |
}};` |
| 152 |
} else { |
| 153 |
contents += `export default ${pluginData.exports};` |
| 154 |
} |
| 155 |
|
| 156 |
return { |
| 157 |
contents, |
| 158 |
loader: 'js', |
| 159 |
resolveDir, |
| 160 |
watchFiles: [...watchFiles, ...(out.watchFiles || [])], |
| 161 |
watchDirs: out.watchDirs || [] |
| 162 |
} |
| 163 |
} else { |
| 164 |
cssText = out |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return type === 'css' || type === 'local-css' ? { |
| 169 |
contents: cssText, |
| 170 |
loader: type, |
| 171 |
resolveDir, |
| 172 |
warnings, |
| 173 |
watchFiles |
| 174 |
} : { |
| 175 |
contents: makeModule(cssText, type, nonce), |
| 176 |
loader: 'js', |
| 177 |
resolveDir, |
| 178 |
warnings, |
| 179 |
watchFiles |
| 180 |
} |
| 181 |
|
| 182 |
} catch (err: any) { |
| 183 |
return { |
| 184 |
errors: [{text: err.message}], |
| 185 |
watchFiles: watched[path] ?? [path] |
| 186 |
} |
| 187 |
} |
| 188 |
})) |
| 189 |
} |
| 190 |
} |
| 191 |
} |