Skip to content

Commit 8d37244

Browse files
segfaultmedaddyjoshwiens
authored andcommitted
fix: Resolving of scoped npm packages (#447)
1 parent 1e6cd4d commit 8d37244

File tree

7 files changed

+23
-1
lines changed

7 files changed

+23
-1
lines changed

lib/importsToResolve.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ function importsToResolve(request) {
2929
const dirname = path.dirname(request);
3030
const startsWithUnderscore = basename.charAt(0) === "_";
3131
// a module import is an identifier like 'bootstrap-sass'
32+
// Firstly check whether we importing scoped npm package (i.e. "@org/pkg")
3233
// We also need to check for dirname since it might also be a deep import like 'bootstrap-sass/something'
33-
const isModuleImport = request.charAt(0) !== "." && dirname === ".";
34+
const isModuleImport = dirname.charAt(0) === "@" && dirname.length > 1 ? true : request.charAt(0) !== "." && dirname === ".";
3435
const hasCssExt = ext === ".css";
3536
const hasSassExt = ext === ".scss" || ext === ".sass";
3637

test/index.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ syntaxStyles.forEach(ext => {
7777
}));
7878
it("should not resolve CSS imports", () => execTest("import-css"));
7979
it("should compile bootstrap-sass without errors", () => execTest("bootstrap-sass"));
80+
it("should correctly import scoped npm packages", () => execTest("import-from-npm-org-pkg"));
8081
});
8182
describe("custom importers", () => {
8283
it("should use custom importer", () => execTest("custom-importer", {

test/node_modules/@org/pkg/index.scss

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/@org/pkg/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* @import "~@org/pkg"; */
2+
@import "~@org/pkg";
3+
4+
.foo
5+
background: #000;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* @import "~@org/pkg"; */
2+
@import "~@org/pkg";
3+
4+
.foo {
5+
background: #000;
6+
}

test/tools/createSpec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function createSpec(ext) {
1414
const basePath = path.join(testFolder, ext);
1515
const testNodeModules = path.relative(basePath, path.join(testFolder, "node_modules")) + path.sep;
1616
const pathToBootstrap = path.relative(basePath, path.resolve(testFolder, "..", "node_modules", "bootstrap-sass"));
17+
const pathToScopedNpmPkg = path.relative(basePath, path.resolve(testFolder, "node_modules", "@org", "pkg", "./index.scss"));
1718

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

0 commit comments

Comments
 (0)