return new Promise((resolve, reject) => { sassConfig.omitSourceMapUrl = true; if (sassConfig.sourceMap) { sassConfig.sourceMap = basename(sassConfig.outFile); sassConfig.sourceMapContents = true; } nodeSassRender(sassConfig, (sassError: SassError, sassResult: Result) => { const diagnostics = runSassDiagnostics(context, sassError); if (diagnostics.length) { printDiagnostics(context, DiagnosticsType.Sass, diagnostics, true, true); // sass render error :( reject(new BuildError()); } else { // sass render success :) renderSassSuccess(context, sassResult, sassConfig).then(outFile => { resolve(outFile); }).catch(err => { reject(new BuildError(err)); }); } }); });
return new Promise((resolve, reject) => { sass.render(sassOpts, (err, result) => { if (err) { reject(err); } else { resolve(result.css.toString()); } }); });
return new Promise((resolve, reject) => { render({ file: inFile, outputStyle: 'nested', outFile: outFile, sourceMap: false, }, (err, result) => { if (err) reject(err); else resolve(result); }); });
return new Promise((resolve, reject) => { sass.render({ file, sourceMap: false }, (err, result) => { if (err) { return reject(`failed to compile ${file}: ${err}`); } const css = result.css.toString(); postcss([autoprefixer({browsers: ['last 2 versions']}), cssnano]) .process(css) .then(postResult => { postResult.warnings().forEach(warn => { console.warn(warn.toString()); }); // do not write to file if output is identical to previous version const productionCss = postResult.css.toString(); if (resourceChecksum.update(checksums, path.basename(outputFile), productionCss)) { console.log(`[scss]:${file}`); fs.writeFile(outputFile, productionCss, werr => { if (werr) { return reject(`faile to write css file: ${outputFile}: ${werr}`); } resolve(`${file} compiled`); }); } else { console.log(`[unchanged]:${file}`); resolve(`${file} unchanged`); } }); }); });
sass.render({ file: '/path/to/myFile.scss', data: 'body{background:blue; a{color:black;}}', importer: function(url, prev, done) { // url is the path in import as is, which libsass encountered. // prev is the previously resolved path. // done is an optional callback, either consume it or return value synchronously. // this.options contains this options hash, this.callback contains the node-style callback someAsyncFunction(url, prev, function(result) { done({ file: result.path, // only one of them is required, see section Sepcial Behaviours. contents: result.data }); }); // OR var result = someSyncFunction(url, prev); return { file: result.path, contents: result.data }; }, includePaths: ['lib/', 'mod/'], outputStyle: 'compressed' }, function(error, result) { // node-style callback from v3.0.0 onwards if (error) { console.log(error.status); // used to be "code" in v2x and below console.log(error.column); console.log(error.message); console.log(error.line); } else { console.log(result.css.toString()); console.log(result.stats); console.log(result.map.toString()); // or better console.log(JSON.stringify(result.map)); // note, JSON.stringify accepts Buffer too } });
return; } accum = accum + $n.getValue(); } done(sass.types.Number(accum, unit)); } }; const asyncFunctions: Record<string, sass.AsyncSassFunction> = {...asyncFunction, ...asyncVarArg}; const functions: sass.FunctionDeclarations = {...syncFunctions, ...asyncFunctions}; sass.render({ file: '/path/to/myFile.scss', data: 'body{background:blue; a{color:black;}}', functions, importer: [anotherAsyncImporter, asyncImporter, syncImporter], includePaths: ['lib/', 'mod/'], outputStyle: 'compressed' }, handleAsyncResult); // OR sass.render({ file: '/path/to/myFile.scss', data: 'body{background:blue; a{color:black;}}', importer: asyncImporter, includePaths: ['lib/', 'mod/'], outputStyle: 'compressed' }, handleAsyncResult); // OR
sass.render({ file: '/path/to/myFile.scss', data: 'body{background:blue; a{color:black;}}', importer: function(url, prev, done) { someAsyncFunction(url, prev, function(result) { if (result == null) { // return null to opt out of handling this path // compiler will fall to next importer in array (or its own default) return null; } // only one of them is required, see section Sepcial Behaviours. done({ file: result.path }); done({ contents: result.data }); }); }, includePaths: ['lib/', 'mod/'], outputStyle: 'compressed' }, function(error, result) { // node-style callback from v3.0.0 onwards if (error) { console.log(error.status, error.column, error.message, error.line); } else { console.log(result.stats); console.log(result.css.toString()); console.log(result.map.toString()); // or better console.log(JSON.stringify(result.map)); // note, JSON.stringify accepts Buffer too } }); // OR
return new Promise((resolve, reject) => { render(opts, (err, res) => { if (err) return reject(err); return resolve(res.css.toString("utf8")); }); });
let html = renderFullPage( css, renderToStaticMarkup(result.root), JSON.stringify(state) ); res.send(html); } else { next(); } }; } sass.render({ file: "./site.scss" }, (error, result) => { if (error !== undefined) { let handleRender = getHandleRender(result.css.toString("utf8")); let app = express(); let port = 3000; app.use(handleRender); app.use("/dist", express.static("dist")); app.listen(port); } }); console.log("Starting up http-server on: http:127.0.0.1:3000");