From 5d06e9d51e82ed41a6034994aef3d3468a27b9c8 Mon Sep 17 00:00:00 2001 From: Roman Vanesyan Date: Wed, 10 May 2017 07:51:20 +0500 Subject: [PATCH] fix: importing file directly from scoped npm package (#450) * fix issue with importing file directly from scoped npm package * Try to fix appveyor --- lib/importsToResolve.js | 16 ++++++++++++---- test/node_modules/@org/bar/_foo.scss | 3 +++ test/sass/import-from-npm-org-pkg.sass | 6 ++++-- test/scss/import-from-npm-org-pkg.scss | 2 ++ 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 test/node_modules/@org/bar/_foo.scss diff --git a/lib/importsToResolve.js b/lib/importsToResolve.js index 0a259f47..595fc4aa 100644 --- a/lib/importsToResolve.js +++ b/lib/importsToResolve.js @@ -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) === "@") { + // 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 diff --git a/test/node_modules/@org/bar/_foo.scss b/test/node_modules/@org/bar/_foo.scss new file mode 100644 index 00000000..7f619bb5 --- /dev/null +++ b/test/node_modules/@org/bar/_foo.scss @@ -0,0 +1,3 @@ +.scoped-npm-pkg-foo { + background: black; +} diff --git a/test/sass/import-from-npm-org-pkg.sass b/test/sass/import-from-npm-org-pkg.sass index e22f2aef..43c22271 100644 --- a/test/sass/import-from-npm-org-pkg.sass +++ b/test/sass/import-from-npm-org-pkg.sass @@ -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; diff --git a/test/scss/import-from-npm-org-pkg.scss b/test/scss/import-from-npm-org-pkg.scss index 29086be4..943cc4a6 100644 --- a/test/scss/import-from-npm-org-pkg.scss +++ b/test/scss/import-from-npm-org-pkg.scss @@ -1,5 +1,7 @@ /* @import "~@org/pkg"; */ @import "~@org/pkg"; +/* @import "~@org/bar/foo"; */ +@import "~@org/bar/foo"; .foo { background: #000;