Skip to content

Commit

Permalink
feat: issue-#150 - ignore empty files
Browse files Browse the repository at this point in the history
- Scaffolding for feature
  and tests.
  • Loading branch information
elycruz committed Aug 7, 2024
1 parent 0425739 commit 3c5829c
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 All @@ -123,7 +128,7 @@ const MATCH_SASS_FILENAME_RE = /\.sass$/,
}); // @note do not `catch` here - let error propagate to rollup level
},

defaultIncludes = ['**/*.sass', '**/*.scss'],
defaultIncludes = ['**/*.sass', '**/*.scss', '**/*.css'],

defaultExcludes = 'node_modules/**';

Expand Down Expand Up @@ -190,8 +195,12 @@ export = function plugin(options = {} as RollupPluginSassOptions): RollupPlugin
)
.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(i => {this.addWatchFile(i)});
res.stats.includedFiles.forEach(filePath => {
this.addWatchFile(filePath);
});

return {
code: codeResult,
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/dependencies/style1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import url(style2.css);

body {color: red;}
4 changes: 4 additions & 0 deletions test/fixtures/dependencies/style1.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import 'style2.scss'

body
color: red
3 changes: 3 additions & 0 deletions test/fixtures/dependencies/style2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import url(style3.css);

body {color: white;}
4 changes: 4 additions & 0 deletions test/fixtures/dependencies/style2.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import 'style3.scss'

body
color: white
1 change: 1 addition & 0 deletions test/fixtures/dependencies/style3.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body {color:blue;}
2 changes: 2 additions & 0 deletions test/fixtures/dependencies/style3.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
body
color: blue
71 changes: 70 additions & 1 deletion 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 @@ -602,3 +602,72 @@ test('module stylesheets graph should be added to watch list', t => {
});
});
});

test('should ignore empty files', t => {
const inputFilePath = 'test/fixtures/dependencies/index-should-ignore-empties.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/empty-style1.scss',
'test/fixtures/dependencies/style2.sass',
'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');

// 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());
});
});
});

0 comments on commit 3c5829c

Please sign in to comment.