diff --git a/lib/importsToResolve.js b/lib/importsToResolve.js index a768d36a..814d71e1 100644 --- a/lib/importsToResolve.js +++ b/lib/importsToResolve.js @@ -5,6 +5,7 @@ const path = require('path'); const utils = require('loader-utils'); const matchModuleImport = /^~([^/]+|@[^/]+[/][^/]+)$/; +const matchScopedNpmPackage = /^@.*\//; /** * When libsass tries to resolve an import, it uses a special algorithm. @@ -25,6 +26,13 @@ function importsToResolve(url) { return [request, url]; } + // In case there is a deep import from a scoped NPM package + // ex: @import '@component-lib/button' + // return nothing and let node-sass handle the import directly + if (matchScopedNpmPackage.test(url)) { + return []; + } + // libsass' import algorithm works like this: // In case there is a file extension... diff --git a/test/index.test.js b/test/index.test.js index 2d3ea956..6c3a4a6f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -154,6 +154,10 @@ implementations.forEach((implementation) => { execTest('bootstrap-sass')); it('should correctly import scoped npm packages', () => execTest('import-from-npm-org-pkg')); + it('should directly import scoped npm packages', () => + execTest('import-directly-from-npm-org-pkg', { + includePaths: [path.join(__dirname, 'node_modules')], + })); it('should resolve aliases', () => execTest( 'import-alias', diff --git a/test/node_modules/@org/baz/qux.scss b/test/node_modules/@org/baz/qux.scss new file mode 100644 index 00000000..3b735ab0 --- /dev/null +++ b/test/node_modules/@org/baz/qux.scss @@ -0,0 +1,3 @@ +.scoped-npm-pkg-qux { + background: purple; +} \ No newline at end of file diff --git a/test/sass/import-directly-from-npm-org-pkg.sass b/test/sass/import-directly-from-npm-org-pkg.sass new file mode 100644 index 00000000..c678cb3e --- /dev/null +++ b/test/sass/import-directly-from-npm-org-pkg.sass @@ -0,0 +1,5 @@ +/* @import ~@org/pkg */ +@import @org/baz/qux + +.foo + background: #000 diff --git a/test/scss/import-directly-from-npm-org-pkg.scss b/test/scss/import-directly-from-npm-org-pkg.scss new file mode 100644 index 00000000..c68e3881 --- /dev/null +++ b/test/scss/import-directly-from-npm-org-pkg.scss @@ -0,0 +1,6 @@ +/* @import "~@org/pkg"; */ +@import "@org/baz/qux"; + +.foo { + background: #000; +} diff --git a/test/tools/createSpec.js b/test/tools/createSpec.js index b23fd8ef..2e1721db 100644 --- a/test/tools/createSpec.js +++ b/test/tools/createSpec.js @@ -18,6 +18,10 @@ function createSpec(ext) { const basePath = path.join(testFolder, ext); const testNodeModules = path.relative(basePath, path.join(testFolder, 'node_modules')) + path.sep; + const testDirectNodeModules = path.relative( + basePath, + path.join(testFolder, 'node_modules', '@') + ); const pathToBootstrap = path.relative( basePath, path.resolve(testFolder, '..', 'node_modules', 'bootstrap-sass') @@ -84,6 +88,7 @@ function createSpec(ext) { .replace(/^~module/, pathToModule) .replace(/^~another/, pathToAnother) .replace(/^~/, testNodeModules) + .replace(/^@/, testDirectNodeModules) .replace(/^path-to-alias/, pathToFooAlias); } return {