Skip to content

Commit

Permalink
feat: issue-#150 - Ignore empty files and css file support
Browse files Browse the repository at this point in the history
- Cleaned up related fixtures.
- Updated 'watch' feature tests to also include
  'empty files' case promise test.
  • Loading branch information
elycruz committed Aug 18, 2024
1 parent 3c5829c commit 81855f3
Show file tree
Hide file tree
Showing 13 changed files with 18 additions and 106 deletions.
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,15 @@ export = function plugin(options = {} as RollupPluginSassOptions): RollupPlugin
.then(result => [res, result])
)
.then(([res, codeResult]) => {

if (!codeResult) return null;

// @todo Do we need to filter this call so it only occurs when rollup is in 'watch' mode?
res.stats.includedFiles.forEach(filePath => {
res.stats.includedFiles.forEach((filePath: string) => {
this.addWatchFile(filePath);
});

return {
code: codeResult,
code: codeResult || '',
map: {mappings: res.map ? res.map.toString() : ''}
};

}); // @note do not `catch` here - let error propagate to rollup level.
},

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions test/fixtures/dependencies/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import style1 from './style1.scss';
import emptyStyle1 from './empty-style1.scss';

export default style1;
Empty file.
3 changes: 0 additions & 3 deletions test/fixtures/dependencies/style1.css

This file was deleted.

4 changes: 0 additions & 4 deletions test/fixtures/dependencies/style1.sass

This file was deleted.

3 changes: 2 additions & 1 deletion test/fixtures/dependencies/style1.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import 'style2.scss';
@import './style2.sass';
@import './empty-style2.sass';

body {color: red;}
3 changes: 0 additions & 3 deletions test/fixtures/dependencies/style2.css

This file was deleted.

3 changes: 2 additions & 1 deletion test/fixtures/dependencies/style2.sass
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import 'style3.scss'
@import './style3.scss'
@import './empty-style3'

body
color: white
3 changes: 0 additions & 3 deletions test/fixtures/dependencies/style2.scss

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/dependencies/style3.css

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixtures/dependencies/style3.sass

This file was deleted.

93 changes: 11 additions & 82 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,71 +541,6 @@ test('When `sourcemap` is set, to `true`, adjacent source map file should be out
test('module stylesheets graph should be added to watch list', t => {
const inputFilePath = 'test/fixtures/dependencies/index.js';

// Bundle our dependencies fixture module
// ----
return rollup({
input: inputFilePath,
plugins: [
sass({
options: sassOptions
})
]
})
// Load nested style sheet contents and return associated list of filename and content tuples
// ----
.then(bundle => {
return Promise.all([
'test/fixtures/dependencies/style1.scss',
'test/fixtures/dependencies/style2.scss',
'test/fixtures/dependencies/style3.scss',
]
.map(filePath => fs.readFile(filePath).then(buf => [filePath, squash(buf.toString())]))
)
// Run tests
// ----
.then(async nestedFilePathsAndContents => {
// Check `watchFiles` count (three above, and 'index.js' module one)
t.true(bundle.watchFiles.length === 4, 'should contain expected number of "watched" files');

// Ensure our initial 'index.js' module is being watched
t.true(bundle.watchFiles[0].endsWith(inputFilePath),
'Expected `bundle.watchFiles[0]` to end with "index.js"');

// Skip 'index.js' file and ensure remaining nested files are also watched.
// ----
bundle.watchFiles.slice(1).forEach((filePath, i) => {
const [expectedTail] = nestedFilePathsAndContents[i];
t.true(filePath.endsWith(expectedTail), `${filePath} should end with ${expectedTail}`);
});

// Get target module.
// ----
const targetModule = bundle?.cache?.modules[0];
t.true(!!targetModule, 'Expected bundle data');

// Ensure target module transform dependencies indeed end with expected file path tails.
// ----
t.true(targetModule.transformDependencies?.every((filePath, i) => {
const [expectedTail] = nestedFilePathsAndContents[i];
const result = filePath.endsWith(expectedTail);
t.true(result, `${filePath} should end with ${expectedTail}`);
return result;
}), '`bundle.cache.modules[0].transformDependencies` entries should' +
' each end with expected file-path tails');

// Test final content output
// ----
const expectedFinalContent = await fs.readFile('test/fixtures/dependencies/expected.js')
.then(x => x.toString());

t.is(targetModule.code.trim(), expectedFinalContent.trim());
});
});
});

test('should ignore empty files', t => {
const inputFilePath = 'test/fixtures/dependencies/index-should-ignore-empties.js';

// Bundle our dependencies fixture module
// ----
return rollup({
Expand All @@ -623,26 +558,24 @@ test('should ignore empty files', t => {
'test/fixtures/dependencies/style1.scss',
'test/fixtures/dependencies/empty-style1.scss',
'test/fixtures/dependencies/style2.sass',
'test/fixtures/dependencies/style3.scss',
'test/fixtures/dependencies/empty-style3.scss',
'test/fixtures/dependencies/empty-style2.sass',
'test/fixtures/dependencies/style3.css',
'test/fixtures/dependencies/empty_style3.css',
]
.map(filePath => fs.readFile(filePath).then(buf => [filePath, squash(buf.toString())]))
)
// Run tests
// ----
.then(async nestedFilePathsAndContents => {
// Check `watchFiles` count (three above, and 'index.js' module one)
t.true(bundle.watchFiles.length === 4, 'should contain expected number of "watched" files');
const expectedWatchedFiles = ['test/fixtures/dependencies/index.js']
.concat(nestedFilePathsAndContents.map(([fp]) => fp));

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

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

Expand All @@ -653,11 +586,8 @@ test('should ignore empty files', t => {

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

Expand All @@ -670,4 +600,3 @@ test('should ignore empty files', t => {
});
});
});

0 comments on commit 81855f3

Please sign in to comment.