Skip to content

Commit

Permalink
fix: Resolving of scoped npm packages (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-vanesyan authored and joshwiens committed May 9, 2017
1 parent 1e6cd4d commit 8d37244
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/importsToResolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ function importsToResolve(request) {
const dirname = path.dirname(request);
const startsWithUnderscore = basename.charAt(0) === "_";
// a module import is an identifier like 'bootstrap-sass'
// Firstly check whether we importing scoped npm package (i.e. "@org/pkg")
// We also need to check for dirname since it might also be a deep import like 'bootstrap-sass/something'
const isModuleImport = request.charAt(0) !== "." && dirname === ".";
const isModuleImport = dirname.charAt(0) === "@" && dirname.length > 1 ? true : request.charAt(0) !== "." && dirname === ".";
const hasCssExt = ext === ".css";
const hasSassExt = ext === ".scss" || ext === ".sass";

Expand Down
1 change: 1 addition & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ syntaxStyles.forEach(ext => {
}));
it("should not resolve CSS imports", () => execTest("import-css"));
it("should compile bootstrap-sass without errors", () => execTest("bootstrap-sass"));
it("should correctly import scoped npm packages", () => execTest("import-from-npm-org-pkg"));
});
describe("custom importers", () => {
it("should use custom importer", () => execTest("custom-importer", {
Expand Down
3 changes: 3 additions & 0 deletions test/node_modules/@org/pkg/index.scss

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/node_modules/@org/pkg/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/sass/import-from-npm-org-pkg.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* @import "~@org/pkg"; */
@import "~@org/pkg";

.foo
background: #000;
6 changes: 6 additions & 0 deletions test/scss/import-from-npm-org-pkg.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* @import "~@org/pkg"; */
@import "~@org/pkg";

.foo {
background: #000;
}
2 changes: 2 additions & 0 deletions test/tools/createSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function createSpec(ext) {
const basePath = path.join(testFolder, ext);
const testNodeModules = path.relative(basePath, path.join(testFolder, "node_modules")) + path.sep;
const pathToBootstrap = path.relative(basePath, path.resolve(testFolder, "..", "node_modules", "bootstrap-sass"));
const pathToScopedNpmPkg = path.relative(basePath, path.resolve(testFolder, "node_modules", "@org", "pkg", "./index.scss"));

fs.readdirSync(path.join(testFolder, ext))
.filter((file) => {
Expand All @@ -30,6 +31,7 @@ function createSpec(ext) {
if (/\.css$/.test(url) === false) { // Do not transform css imports
url = url
.replace(/^~bootstrap-sass/, pathToBootstrap)
.replace(/^~@org\/pkg/, pathToScopedNpmPkg)
.replace(/^~/, testNodeModules);
}
return {
Expand Down

0 comments on commit 8d37244

Please sign in to comment.