From ae976dc1a85cb3e51b07711ae4d30e3f8f56997d Mon Sep 17 00:00:00 2001 From: Gilad Gray Date: Wed, 23 Nov 2016 00:00:55 -0800 Subject: [PATCH] add tests for sync and async importers --- node-sass/node-sass-tests.ts | 54 +++++++++++++----------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/node-sass/node-sass-tests.ts b/node-sass/node-sass-tests.ts index 33ce90ec67..4de32d7d06 100644 --- a/node-sass/node-sass-tests.ts +++ b/node-sass/node-sass-tests.ts @@ -1,63 +1,47 @@ import * as sass from 'node-sass'; + 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 - }); + 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 }); }); - // 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); + console.log(error.status, error.column, error.message, error.line); } else { - console.log(result.css.toString()); - 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 -var result = sass.renderSync({ +const result = sass.renderSync({ file: '/path/to/file.scss', data: 'body{background:blue; a{color:black;}}', outputStyle: 'compressed', outFile: '/to/my/output.css', sourceMap: true, // or an absolute or relative (to outFile) path sourceMapRoot: '.', - 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 - 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 }; + importer: function(url, prev) { + if (url.startsWith('!')) { + return new Error('Cannot accept this file'); + } + return { file: [prev, url].join('/') }; }, }); @@ -65,7 +49,7 @@ console.log(result.css); console.log(result.map); console.log(result.stats); -function someAsyncFunction(url: string, prev: string, callback: (result: { path: string; data: string }) => void): void {} -function someSyncFunction(url: string, prev: string): { path: string; data: string} { +function someAsyncFunction(url: string, prev: string, callback: (result: { path: string; data: string }) => void): void { } +function someSyncFunction(url: string, prev: string): { path: string; data: string } { return null; }