| 1 |
"use strict"; |
| 2 |
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 3 |
if (k2 === undefined) k2 = k; |
| 4 |
var desc = Object.getOwnPropertyDescriptor(m, k); |
| 5 |
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
| 6 |
desc = { enumerable: true, get: function() { return m[k]; } }; |
| 7 |
} |
| 8 |
Object.defineProperty(o, k2, desc); |
| 9 |
}) : (function(o, m, k, k2) { |
| 10 |
if (k2 === undefined) k2 = k; |
| 11 |
o[k2] = m[k]; |
| 12 |
})); |
| 13 |
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 14 |
Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 15 |
}) : function(o, v) { |
| 16 |
o["default"] = v; |
| 17 |
}); |
| 18 |
var __importStar = (this && this.__importStar) || (function () { |
| 19 |
var ownKeys = function(o) { |
| 20 |
ownKeys = Object.getOwnPropertyNames || function (o) { |
| 21 |
var ar = []; |
| 22 |
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; |
| 23 |
return ar; |
| 24 |
}; |
| 25 |
return ownKeys(o); |
| 26 |
}; |
| 27 |
return function (mod) { |
| 28 |
if (mod && mod.__esModule) return mod; |
| 29 |
var result = {}; |
| 30 |
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); |
| 31 |
__setModuleDefault(result, mod); |
| 32 |
return result; |
| 33 |
}; |
| 34 |
})(); |
| 35 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 36 |
exports.createRenderer = createRenderer; |
| 37 |
const path_1 = require("path"); |
| 38 |
const fs_1 = __importStar(require("fs")); |
| 39 |
const utils_1 = require("./utils"); |
| 40 |
const url_1 = require("url"); |
| 41 |
async function createCompiler(embedded, onDispose) { |
| 42 |
if (embedded) { |
| 43 |
const { initAsyncCompiler } = await Promise.resolve().then(() => __importStar(require('sass-embedded'))); |
| 44 |
const compiler = await initAsyncCompiler(); |
| 45 |
onDispose(compiler.dispose.bind(compiler)); |
| 46 |
return compiler.compileStringAsync.bind(compiler); |
| 47 |
} |
| 48 |
else { |
| 49 |
const { compileString } = await Promise.resolve().then(() => __importStar(require('sass'))); |
| 50 |
return compileString; |
| 51 |
} |
| 52 |
} |
| 53 |
async function createRenderer(options = {}, sourcemap, onDispose) { |
| 54 |
const loadPaths = options.loadPaths; |
| 55 |
const resolveModule = (0, utils_1.createResolver)(options, loadPaths); |
| 56 |
function resolveImport(pathname, ext) { |
| 57 |
if (ext) { |
| 58 |
let filename = pathname + ext; |
| 59 |
if (fs_1.default.existsSync(filename)) { |
| 60 |
return filename; |
| 61 |
} |
| 62 |
const index = filename.lastIndexOf(path_1.sep); |
| 63 |
filename = index >= 0 ? filename.slice(0, index) + path_1.sep + '_' + filename.slice(index + 1) : '_' + filename; |
| 64 |
if (fs_1.default.existsSync(filename)) { |
| 65 |
return filename; |
| 66 |
} |
| 67 |
return null; |
| 68 |
} |
| 69 |
else { |
| 70 |
if (!fs_1.default.existsSync((0, path_1.dirname)(pathname))) { |
| 71 |
return null; |
| 72 |
} |
| 73 |
return resolveImport(pathname, '.scss') |
| 74 |
|| resolveImport(pathname, '.css') |
| 75 |
|| resolveImport(pathname, '.sass') |
| 76 |
|| resolveImport(pathname + path_1.sep + 'index'); |
| 77 |
} |
| 78 |
} |
| 79 |
function resolveRelativeImport(loadPath, filename) { |
| 80 |
const absolute = (0, path_1.resolve)(loadPath, filename); |
| 81 |
const pathParts = (0, path_1.parse)(absolute); |
| 82 |
if (utils_1.DEFAULT_FILTER.test(pathParts.ext)) { |
| 83 |
return resolveImport(pathParts.dir + path_1.sep + pathParts.name, pathParts.ext); |
| 84 |
} |
| 85 |
else { |
| 86 |
return resolveImport(absolute); |
| 87 |
} |
| 88 |
} |
| 89 |
const sepTilde = `${path_1.sep}~`; |
| 90 |
const compileString = await createCompiler(options.embedded, onDispose); |
| 91 |
return async function (path) { |
| 92 |
var _a; |
| 93 |
const basedir = (0, path_1.dirname)(path); |
| 94 |
let source = fs_1.default.readFileSync(path, 'utf-8'); |
| 95 |
if (options.precompile) { |
| 96 |
source = options.precompile(source, path, true); |
| 97 |
} |
| 98 |
const syntax = (0, utils_1.fileSyntax)(path); |
| 99 |
if (syntax === 'css') { |
| 100 |
return { cssText: (0, fs_1.readFileSync)(path, 'utf-8'), watchFiles: [path] }; |
| 101 |
} |
| 102 |
if (options.quietDeps) { |
| 103 |
options.url = (0, url_1.pathToFileURL)(path); |
| 104 |
} |
| 105 |
const warnings = []; |
| 106 |
const logger = (_a = options.logger) !== null && _a !== void 0 ? _a : { |
| 107 |
warn: function (message, opts) { |
| 108 |
var _a, _b; |
| 109 |
if (!opts.span) { |
| 110 |
warnings.push({ text: `sass warning: ${message}` }); |
| 111 |
} |
| 112 |
else { |
| 113 |
const filename = (_b = (_a = opts.span.url) === null || _a === void 0 ? void 0 : _a.pathname) !== null && _b !== void 0 ? _b : path; |
| 114 |
const esbuildMsg = { |
| 115 |
text: message, |
| 116 |
location: { |
| 117 |
file: filename, |
| 118 |
line: opts.span.start.line, |
| 119 |
column: opts.span.start.column, |
| 120 |
lineText: opts.span.text |
| 121 |
}, |
| 122 |
detail: { |
| 123 |
deprecation: opts.deprecation, |
| 124 |
stack: opts.stack |
| 125 |
} |
| 126 |
}; |
| 127 |
warnings.push(esbuildMsg); |
| 128 |
} |
| 129 |
} |
| 130 |
}; |
| 131 |
const { css, loadedUrls, sourceMap } = await compileString(source, { |
| 132 |
sourceMapIncludeSources: true, |
| 133 |
...options, |
| 134 |
logger, |
| 135 |
syntax, |
| 136 |
importer: { |
| 137 |
load(canonicalUrl) { |
| 138 |
const pathname = (0, url_1.fileURLToPath)(canonicalUrl); |
| 139 |
let contents = fs_1.default.readFileSync(pathname, 'utf8'); |
| 140 |
if (options.precompile) { |
| 141 |
contents = options.precompile(contents, pathname, false); |
| 142 |
} |
| 143 |
return { |
| 144 |
contents, |
| 145 |
syntax: (0, utils_1.fileSyntax)(pathname), |
| 146 |
sourceMapUrl: sourcemap ? canonicalUrl : undefined |
| 147 |
}; |
| 148 |
}, |
| 149 |
canonicalize(url) { |
| 150 |
let filename; |
| 151 |
if (url.startsWith('~')) { |
| 152 |
filename = resolveModule(decodeURI(url.slice(1)), basedir); |
| 153 |
} |
| 154 |
else if (url.startsWith('file://')) { |
| 155 |
filename = (0, url_1.fileURLToPath)(url); |
| 156 |
let joint = filename.lastIndexOf(sepTilde); |
| 157 |
if (joint >= 0) { |
| 158 |
filename = resolveModule(filename.slice(joint + 2), filename.slice(0, joint)); |
| 159 |
} |
| 160 |
} |
| 161 |
else { |
| 162 |
filename = decodeURI(url); |
| 163 |
} |
| 164 |
if (options.importMapper) { |
| 165 |
filename = options.importMapper(filename); |
| 166 |
} |
| 167 |
let resolved = resolveRelativeImport(basedir, filename); |
| 168 |
if (resolved) { |
| 169 |
return (0, url_1.pathToFileURL)(resolved); |
| 170 |
} |
| 171 |
for (const loadPath of loadPaths) { |
| 172 |
resolved = resolveRelativeImport(loadPath, filename); |
| 173 |
if (resolved) { |
| 174 |
return (0, url_1.pathToFileURL)(resolved); |
| 175 |
} |
| 176 |
} |
| 177 |
if (!filename.startsWith('.') && !(0, path_1.isAbsolute)(filename)) { |
| 178 |
const moduleResolved = resolveModule(filename, basedir); |
| 179 |
if (moduleResolved !== filename && fs_1.default.existsSync(moduleResolved)) { |
| 180 |
return (0, url_1.pathToFileURL)(moduleResolved); |
| 181 |
} |
| 182 |
} |
| 183 |
return null; |
| 184 |
} |
| 185 |
}, |
| 186 |
sourceMap: sourcemap |
| 187 |
}); |
| 188 |
let cssText = css.toString(); |
| 189 |
if (sourceMap) { |
| 190 |
delete sourceMap.sourceRoot; |
| 191 |
sourceMap.sources = sourceMap.sources.map(source => { |
| 192 |
return (0, path_1.relative)(basedir, source.startsWith('data:') ? path : (0, url_1.fileURLToPath)(source)); |
| 193 |
}); |
| 194 |
cssText += '\n' + (0, utils_1.sourceMappingURL)(sourceMap); |
| 195 |
} |
| 196 |
return { |
| 197 |
cssText, |
| 198 |
warnings: warnings, |
| 199 |
watchFiles: [path, ...loadedUrls.map(url_1.fileURLToPath)] |
| 200 |
}; |
| 201 |
}; |
| 202 |
} |
| 203 |
//# sourceMappingURL=render.js.map |