Skip to content

Commit 5863042

Browse files
authored
Merge pull request #153 from elycruz/fix/issue-#150/empty_style_files
Fix/issue #150/ignore empty style files
2 parents 0425739 + d8058ab commit 5863042

File tree

9 files changed

+34
-27
lines changed

9 files changed

+34
-27
lines changed

src/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const MATCH_SASS_FILENAME_RE = /\.sass$/,
5151
const moduleUrl = url.slice(1);
5252
const resolveOptions = {
5353
basedir: dirname(prevUrl),
54-
extensions: ['.scss', '.css', '.sass'],
54+
extensions: ['.scss', '.sass'],
5555
};
5656

5757
// @todo This block should run as a promise instead, will help ensure we're not blocking the thread it is
@@ -77,12 +77,14 @@ const MATCH_SASS_FILENAME_RE = /\.sass$/,
7777
},
7878

7979
processRenderResponse = (rollupOptions, file, state, inCss) => {
80-
if (!inCss) return;
80+
if (!inCss) return Promise.resolve();
8181

8282
const {processor} = rollupOptions;
8383

8484
return Promise.resolve()
8585
.then(() => !isFunction(processor) ? inCss + '' : processor(inCss, file))
86+
87+
// Gather output requirements
8688
.then(result => {
8789
if (!isObject(result)) {
8890
return [result, ''];
@@ -97,6 +99,8 @@ const MATCH_SASS_FILENAME_RE = /\.sass$/,
9799
agg + `export const ${name} = ${JSON.stringify(result[name])};\n`, '');
98100
return [outCss, restExports];
99101
})
102+
103+
// Compose output
100104
.then(([resolvedCss, restExports]) => {
101105
const {styleMaps} = state;
102106

@@ -110,8 +114,9 @@ const MATCH_SASS_FILENAME_RE = /\.sass$/,
110114

111115
if (rollupOptions.insert) {
112116
/**
117+
* Add `insertStyle` import for handling "inserting"
118+
* *.css into *.html `head`.
113119
* @see insertStyle.ts for additional information
114-
* Let rollup handle import by processing insertStyle as a module
115120
*/
116121
imports = `import ${insertFnName} from '${__dirname}/insertStyle.js';\n`;
117122
defaultExport = `${insertFnName}(${out});`;
@@ -189,15 +194,15 @@ export = function plugin(options = {} as RollupPluginSassOptions): RollupPlugin
189194
.then(result => [res, result])
190195
)
191196
.then(([res, codeResult]) => {
192-
193197
// @todo Do we need to filter this call so it only occurs when rollup is in 'watch' mode?
194-
res.stats.includedFiles.forEach(i => {this.addWatchFile(i)});
198+
res.stats.includedFiles.forEach((filePath: string) => {
199+
this.addWatchFile(filePath);
200+
});
195201

196202
return {
197-
code: codeResult,
203+
code: codeResult || '',
198204
map: {mappings: res.map ? res.map.toString() : ''}
199205
};
200-
201206
}); // @note do not `catch` here - let error propagate to rollup level.
202207
},
203208

test/fixtures/dependencies/empty-style1.scss

Whitespace-only changes.

test/fixtures/dependencies/empty-style2.sass

Whitespace-only changes.

test/fixtures/dependencies/empty-style3.scss

Whitespace-only changes.

test/fixtures/dependencies/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import style1 from './style1.scss';
2+
import emptyStyle1 from './empty-style1.scss';
23

34
export default style1;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
@import 'style2.scss';
1+
@import './style2.sass';
2+
@import './empty-style2.sass';
23

34
body {color: red;}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import './style3.scss'
2+
@import './empty-style3'
3+
4+
body
5+
color: white

test/fixtures/dependencies/style2.scss

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/index.test.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ test('should support processor return type `Promise<{css: string, icssExport: {}
354354
input: 'test/fixtures/processor-promise/with-icss-exports.js',
355355
plugins: [
356356
sass({
357-
processor: (css) => new Promise((resolve, reject) => {
357+
processor: (css) => new Promise((resolve) => {
358358
const pcssRootNodeRslt = postcss.parse(css),
359359
extractedIcss = extractICSS(pcssRootNodeRslt, true),
360360
cleanedCss = pcssRootNodeRslt.toString(),
@@ -556,25 +556,26 @@ test('module stylesheets graph should be added to watch list', t => {
556556
.then(bundle => {
557557
return Promise.all([
558558
'test/fixtures/dependencies/style1.scss',
559-
'test/fixtures/dependencies/style2.scss',
559+
'test/fixtures/dependencies/empty-style1.scss',
560+
'test/fixtures/dependencies/style2.sass',
560561
'test/fixtures/dependencies/style3.scss',
562+
'test/fixtures/dependencies/empty-style3.scss',
563+
'test/fixtures/dependencies/empty-style2.sass',
561564
]
562565
.map(filePath => fs.readFile(filePath).then(buf => [filePath, squash(buf.toString())]))
563566
)
564567
// Run tests
565568
// ----
566569
.then(async nestedFilePathsAndContents => {
567-
// Check `watchFiles` count (three above, and 'index.js' module one)
568-
t.true(bundle.watchFiles.length === 4, 'should contain expected number of "watched" files');
570+
const expectedWatchedFiles = ['test/fixtures/dependencies/index.js']
571+
.concat(nestedFilePathsAndContents.map(([fp]) => fp));
569572

570-
// Ensure our initial 'index.js' module is being watched
571-
t.true(bundle.watchFiles[0].endsWith(inputFilePath),
572-
'Expected `bundle.watchFiles[0]` to end with "index.js"');
573+
// Check `watchFiles` count (watched ones plus 'index.js' one)
574+
t.deepEqual(bundle.watchFiles.length, expectedWatchedFiles.length, 'should contain expected number of "watched" files');
573575

574-
// Skip 'index.js' file and ensure remaining nested files are also watched.
575-
// ----
576-
bundle.watchFiles.slice(1).forEach((filePath, i) => {
577-
const [expectedTail] = nestedFilePathsAndContents[i];
576+
// Ensure 'index.js' module, and other files in dep tree are watched
577+
bundle.watchFiles.forEach((filePath, i) => {
578+
const expectedTail = expectedWatchedFiles[i];
578579
t.true(filePath.endsWith(expectedTail), `${filePath} should end with ${expectedTail}`);
579580
});
580581

@@ -585,11 +586,8 @@ test('module stylesheets graph should be added to watch list', t => {
585586

586587
// Ensure target module transform dependencies indeed end with expected file path tails.
587588
// ----
588-
t.true(targetModule.transformDependencies?.every((filePath, i) => {
589-
const [expectedTail] = nestedFilePathsAndContents[i];
590-
const result = filePath.endsWith(expectedTail);
591-
t.true(result, `${filePath} should end with ${expectedTail}`);
592-
return result;
589+
t.true(targetModule.transformDependencies?.every(filePath => {
590+
return !!expectedWatchedFiles.find(fp => filePath.endsWith(fp));
593591
}), '`bundle.cache.modules[0].transformDependencies` entries should' +
594592
' each end with expected file-path tails');
595593

0 commit comments

Comments
 (0)