diff --git a/README.md b/README.md index 3d3182f4..1c9724f4 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,14 @@ module.exports = { If you want to view the original Sass files inside Chrome and even edit it, [there's a good blog post](https://medium.com/@toolmantim/getting-started-with-css-sourcemaps-and-in-browser-sass-editing-b4daab987fb0). Checkout [test/sourceMap](https://github.com/jtangelder/sass-loader/tree/master/test) for a running example. Make sure to serve the content with an HTTP server. +## Contribution + +### How to run test +```bash + mkdir -p test/node_modules/ && touch test/node_modules/some-module.s{a,c}ss && touch test/node_modules/underscore.s{a,c}ss + npm test +``` + ## License MIT (http://www.opensource.org/licenses/mit-license.php) diff --git a/index.js b/index.js index 0788a663..39814b96 100644 --- a/index.js +++ b/index.js @@ -104,6 +104,19 @@ module.exports = function (content) { return path.dirname(context); } + // add result files to loader + function addNodeSassResult2WebpackDep(loader, result) { + if (!loader || !result || !result.stats + || !result.stats.includedFiles || 1 > result.stats.includedFiles.length) { + return; + } + + var depFn = loader.addDependency || loader.dependency; + for (var i in result.stats.includedFiles) { + depFn(result.stats.includedFiles[i]); + } + } + this.cacheable(); opt = utils.parseQuery(this.query); @@ -140,7 +153,10 @@ module.exports = function (content) { // start the actual rendering if (isSync) { try { - return sass.renderSync(opt).css.toString(); + + var ret = sass.renderSync(opt); + addNodeSassResult2WebpackDep(self, ret); + return ret.css.toString(); } catch (err) { formatSassError(err); throw err; @@ -163,6 +179,8 @@ module.exports = function (content) { result.map = null; } + addNodeSassResult2WebpackDep(self, result); + callback(null, result.css.toString(), result.map); }); }; @@ -207,17 +225,17 @@ function syncResolve(loaderContext, url, context) { try { filename = loaderContext.resolveSync(context, url); - loaderContext.dependency && loaderContext.dependency(filename); } catch (err) { basename = path.basename(url); if (requiresLookupForUnderscoreModule(err, basename)) { url = addUnderscoreToBasename(url, basename); return syncResolve(loaderContext, url, context); } - // Unfortunately we can't return an error inside a custom importer yet - // @see https://github.com/sass/node-sass/issues/651#issuecomment-73317319 - filename = url; + + // let the libsass do the rest job, e.g. search module in includePaths + filename = path.join(path.dirname(url), removeUnderscoreFromBasename(basename)); } + return { file: filename }; @@ -242,11 +260,9 @@ function asyncResolve(loaderContext, url, context, done) { url = addUnderscoreToBasename(url, basename); return asyncResolve(loaderContext, url, context, done); } - // Unfortunately we can't return an error inside a custom importer yet - // @see https://github.com/sass/node-sass/issues/651#issuecomment-73317319 - filename = url; - } else { - loaderContext.dependency && loaderContext.dependency(filename); + + // let the libsass do the rest job, e.g. search module in includePaths + filename = path.join(path.dirname(url), removeUnderscoreFromBasename(basename)); } // Use self.loadModule() before calling done() to make imported files available to @@ -277,3 +293,7 @@ function requiresLookupForUnderscoreModule(err, basename) { function addUnderscoreToBasename(url, basename) { return url.slice(0, -basename.length) + '_' + basename; } + +function removeUnderscoreFromBasename(basename) { + return !basename ? basename : ("_" === basename[0] ? basename.substring(1) : basename); +} diff --git a/test/another/sass/_underscoreIncPathStyle.sass b/test/another/sass/_underscoreIncPathStyle.sass new file mode 100644 index 00000000..b960b372 --- /dev/null +++ b/test/another/sass/_underscoreIncPathStyle.sass @@ -0,0 +1 @@ +// just empty for includePaths feature diff --git a/test/another/sass/incPathsStyle.sass b/test/another/sass/incPathsStyle.sass new file mode 100644 index 00000000..b960b372 --- /dev/null +++ b/test/another/sass/incPathsStyle.sass @@ -0,0 +1 @@ +// just empty for includePaths feature diff --git a/test/another/scss/_underscoreIncPathStyle.scss b/test/another/scss/_underscoreIncPathStyle.scss new file mode 100644 index 00000000..b960b372 --- /dev/null +++ b/test/another/scss/_underscoreIncPathStyle.scss @@ -0,0 +1 @@ +// just empty for includePaths feature diff --git a/test/another/scss/incPathsStyle.scss b/test/another/scss/incPathsStyle.scss new file mode 100644 index 00000000..b960b372 --- /dev/null +++ b/test/another/scss/incPathsStyle.scss @@ -0,0 +1 @@ +// just empty for includePaths feature diff --git a/test/index.test.js b/test/index.test.js index 9a377740..9c76bf84 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -38,6 +38,9 @@ describe('sass-loader', function () { testSync('should resolve imports from other language style correctly (sync)', 'import-other-style'); testAsync('should resolve imports from other language style correctly (async)', 'import-other-style'); + // test for includepath not under context + testSync('should resolve imports from another directory declared by includePaths correctly(sync)', 'import-include-paths'); + testAsync('should resolve imports from another directory declared by includePaths correctly(async)', 'import-include-paths'); }); describe('errors', function () { @@ -72,7 +75,7 @@ describe('sass-loader', function () { } catch (err) { // check for file excerpt err.message.should.match(/@import "does-not-exist";/); - err.message.should.match(/File to import not found or unreadable: \.\/_does-not-exist\.scss/); + err.message.should.match(/File to import not found or unreadable: does-not-exist\.scss/); err.message.should.match(/\(line 1, column 9\)/); err.message.indexOf(pathToErrorFileNotFound).should.not.equal(-1); } @@ -140,6 +143,6 @@ function testSync(name, id) { function pathToSassFile(ext, id) { return 'raw!' + pathToSassLoader + '?' + - (ext === 'sass'? '&indentedSyntax' : '') + '!' + + (ext === 'sass'? '&indentedSyntax&' : '') + 'includePaths[]=' + path.join(__dirname, 'another', ext) + '!' + path.join(__dirname, ext, id + '.' + ext); } diff --git a/test/prepare.js b/test/prepare.js index 7ed4a144..614d5d0a 100644 --- a/test/prepare.js +++ b/test/prepare.js @@ -36,7 +36,8 @@ var filesWithTildeImports = [ css = sass.renderSync({ file: fileName, includePaths: [ - path.join(__dirname, ext, 'another') + path.join(__dirname, ext, 'another'), + path.join(__dirname, 'another', ext) ] }).css; diff --git a/test/sass/import-include-paths.sass b/test/sass/import-include-paths.sass new file mode 100644 index 00000000..01adb54c --- /dev/null +++ b/test/sass/import-include-paths.sass @@ -0,0 +1,2 @@ +@import incPathsStyle +@import underscoreIncPathStyle diff --git a/test/scss/import-include-paths.scss b/test/scss/import-include-paths.scss new file mode 100644 index 00000000..c9345f38 --- /dev/null +++ b/test/scss/import-include-paths.scss @@ -0,0 +1,2 @@ +@import "incPathsStyle"; +@import "underscoreIncPathStyle";