Skip to content

Commit

Permalink
Merge pull request #153 from elycruz/fix/issue-#150/empty_style_files
Browse files Browse the repository at this point in the history
Fix/issue #150/ignore empty style files
  • Loading branch information
elycruz authored Aug 18, 2024
2 parents 0425739 + d8058ab commit 5863042
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 27 deletions.
19 changes: 12 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const MATCH_SASS_FILENAME_RE = /\.sass$/,
const moduleUrl = url.slice(1);
const resolveOptions = {
basedir: dirname(prevUrl),
extensions: ['.scss', '.css', '.sass'],
extensions: ['.scss', '.sass'],
};

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

processRenderResponse = (rollupOptions, file, state, inCss) => {
if (!inCss) return;
if (!inCss) return Promise.resolve();

const {processor} = rollupOptions;

return Promise.resolve()
.then(() => !isFunction(processor) ? inCss + '' : processor(inCss, file))

// Gather output requirements
.then(result => {
if (!isObject(result)) {
return [result, ''];
Expand All @@ -97,6 +99,8 @@ const MATCH_SASS_FILENAME_RE = /\.sass$/,
agg + `export const ${name} = ${JSON.stringify(result[name])};\n`, '');
return [outCss, restExports];
})

// Compose output
.then(([resolvedCss, restExports]) => {
const {styleMaps} = state;

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

if (rollupOptions.insert) {
/**
* Add `insertStyle` import for handling "inserting"
* *.css into *.html `head`.
* @see insertStyle.ts for additional information
* Let rollup handle import by processing insertStyle as a module
*/
imports = `import ${insertFnName} from '${__dirname}/insertStyle.js';\n`;
defaultExport = `${insertFnName}(${out});`;
Expand Down Expand Up @@ -189,15 +194,15 @@ export = function plugin(options = {} as RollupPluginSassOptions): RollupPlugin
.then(result => [res, result])
)
.then(([res, codeResult]) => {

// @todo Do we need to filter this call so it only occurs when rollup is in 'watch' mode?
res.stats.includedFiles.forEach(i => {this.addWatchFile(i)});
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
Empty file.
Empty file.
Empty file.
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;
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;}
5 changes: 5 additions & 0 deletions test/fixtures/dependencies/style2.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import './style3.scss'
@import './empty-style3'

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

This file was deleted.

30 changes: 14 additions & 16 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ test('should support processor return type `Promise<{css: string, icssExport: {}
input: 'test/fixtures/processor-promise/with-icss-exports.js',
plugins: [
sass({
processor: (css) => new Promise((resolve, reject) => {
processor: (css) => new Promise((resolve) => {
const pcssRootNodeRslt = postcss.parse(css),
extractedIcss = extractICSS(pcssRootNodeRslt, true),
cleanedCss = pcssRootNodeRslt.toString(),
Expand Down Expand Up @@ -556,25 +556,26 @@ test('module stylesheets graph should be added to watch list', t => {
.then(bundle => {
return Promise.all([
'test/fixtures/dependencies/style1.scss',
'test/fixtures/dependencies/style2.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',
]
.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 @@ -585,11 +586,8 @@ test('module stylesheets graph should be added to watch list', 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 Down

0 comments on commit 5863042

Please sign in to comment.