Skip to content

Commit

Permalink
fix: importing file directly from scoped npm package (#450)
Browse files Browse the repository at this point in the history
* fix issue with importing file directly from scoped npm package

* Try to fix appveyor
  • Loading branch information
roman-vanesyan authored and joshwiens committed May 10, 2017
1 parent f01ae22 commit 5d06e9d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
16 changes: 12 additions & 4 deletions lib/importsToResolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ function importsToResolve(request) {
const basename = path.basename(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 = dirname.charAt(0) === "@" && dirname.length > 1 ? true : request.charAt(0) !== "." && dirname === ".";
const hasCssExt = ext === ".css";
const hasSassExt = ext === ".scss" || ext === ".sass";

// a module import is an identifier like 'bootstrap-sass'
// We also need to check for dirname since it might also be a deep import like 'bootstrap-sass/something'
let isModuleImport = request.charAt(0) !== "." && dirname === ".";

if (dirname.charAt(0) === "@") {

This comment has been minimized.

Copy link
@gizm0bill

gizm0bill May 21, 2017

I think this breaks resolving webpack aliases that start with @ character. at least that's what happened to me on 6.0.5 upgrade

This comment has been minimized.

Copy link
@michael-ciniawsky

michael-ciniawsky May 21, 2017

Member

@gizm0bill Could you open an issue with a few examples please :) ?

This comment has been minimized.

Copy link
@gizm0bill

gizm0bill May 24, 2017

sure I'll add an example, it just needs one tbh :)

// Check whether it is a deep import from scoped npm package
// (i.e. @pkg/foo/file), if so, process import as file import;
// otherwise, if we import from root npm scoped package (i.e. @pkg/foo)
// process import as a module import.
isModuleImport = !(dirname.indexOf("/") > -1);
}

return (isModuleImport && [request]) || // Do not modify module imports
(hasCssExt && []) || // Do not import css files
(hasSassExt && [request]) || // Do not modify imports with explicit extensions
Expand Down
3 changes: 3 additions & 0 deletions test/node_modules/@org/bar/_foo.scss

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

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

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

.foo {
background: #000;
Expand Down

0 comments on commit 5d06e9d

Please sign in to comment.