Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not import module which locates at includePaths directories #113

Merged
merged 4 commits into from
Jul 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
40 changes: 30 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -163,6 +179,8 @@ module.exports = function (content) {
result.map = null;
}

addNodeSassResult2WebpackDep(self, result);

callback(null, result.css.toString(), result.map);
});
};
Expand Down Expand Up @@ -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
};
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions test/another/sass/_underscoreIncPathStyle.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// just empty for includePaths feature
1 change: 1 addition & 0 deletions test/another/sass/incPathsStyle.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// just empty for includePaths feature
1 change: 1 addition & 0 deletions test/another/scss/_underscoreIncPathStyle.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// just empty for includePaths feature
1 change: 1 addition & 0 deletions test/another/scss/incPathsStyle.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// just empty for includePaths feature
7 changes: 5 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion test/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions test/sass/import-include-paths.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import incPathsStyle
@import underscoreIncPathStyle
2 changes: 2 additions & 0 deletions test/scss/import-include-paths.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "incPathsStyle";
@import "underscoreIncPathStyle";