Skip to content

Commit 3192f0e

Browse files
ArpitMartynas Žilinskas
authored andcommitted
fix: css imports with extension were not bundled in output. (#55)
* fix #48: css imports with extension were not bundled in output. * tests : add test cases * tests : add snapshots of test result * fix: removed sass extension from supported extensions
1 parent fe75f2e commit 3192f0e

8 files changed

Lines changed: 33 additions & 7 deletions

File tree

@types/bundler.d.ts

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

dist/bundler.js

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

src/bundler.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import * as Helpers from "./helpers";
88
const IMPORT_PATTERN = /@import\s+['"](.+)['"];/g;
99
const COMMENT_PATTERN = /\/\/.*$/gm;
1010
const MULTILINE_COMMENT_PATTERN = /\/\*[\s\S]*?\*\//g;
11-
const FILE_EXTENSION = ".scss";
11+
const DEFAULT_FILE_EXTENSION = ".scss";
12+
const ALLOWED_FILE_EXTENSIONS = [".scss", ".css"];
1213
const NODE_MODULES = "node_modules";
1314
const TILDE = "~";
1415

@@ -80,6 +81,9 @@ export class Bundler {
8081
}
8182
}
8283

84+
private isExtensionExists(importName: string): boolean {
85+
return ALLOWED_FILE_EXTENSIONS.some((extension => importName.indexOf(extension) !== -1));
86+
}
8387
private async bundle(
8488
filePath: string,
8589
content: string,
@@ -103,8 +107,8 @@ export class Bundler {
103107
const importsPromises = Helpers.getAllMatches(content, IMPORT_PATTERN).map(async match => {
104108
let importName = match[1];
105109
// Append extension if it's absent
106-
if (importName.indexOf(FILE_EXTENSION) === -1) {
107-
importName += FILE_EXTENSION;
110+
if (!this.isExtensionExists(importName)) {
111+
importName += DEFAULT_FILE_EXTENSION;
108112
}
109113

110114
// Determine if import should be ignored

tests/cases/__tests__/__snapshots__/css-import.test.ts.snap

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

tests/cases/css-import/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!node_modules

tests/cases/css-import/main.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "~styles-package/styles.css";

tests/cases/css-import/node_modules/styles-package/styles.css

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Entry": "main.scss"
3+
}

0 commit comments

Comments
 (0)