Skip to content

Fix duplicate rules #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,30 @@ function normalizeManifestPaths (tokensByFile, rootDir) {
return output;
}

function dedupeSources (sources) {
var foundHashes = {}
Object.keys(sources).forEach(function (key) {
var hash = stringHash(sources[key]);
if (foundHashes[hash]) {
delete sources[key];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion: This is silly, but using delete forces V8 to use un-optimized objects, resulting in a speed loss. It's a minor thing that we might not even notice, but an alternative way to write this might be:

function dedeupSources (loader) {
var dedupedSources = {}
loader.sources = Object.keys(loader.sources).reduce(function (sources, key) {
  var hash = stringHash(loader.sources[key])
  if (dedeupedSources[hash]) {
    return sources
  }
  else {
   dedupedSources[hash] = true
   sources[key] = loader.sources[key]
   return sources
  }
})

It's a small optimization, but might matter in large code bases.

}
else {
foundHashes[hash] = true;
}
})
}

var cssExt = /\.css$/;

// caches
//
// persist these for as long as the process is running. #32

// keep track of css files visited
var filenames = [];

// keep track of all tokens so we can avoid duplicates
var tokensByFile = {};

// keep track of all source files for later builds: when
// using watchify, not all files will be caught on subsequent
// bundles
var sourceByFile = {};
// we need a separate loader for each entry point
var loadersByFile = {};

module.exports = function (browserify, options) {
options = options || {};
Expand All @@ -103,9 +111,10 @@ module.exports = function (browserify, options) {
var jsonOutFilename = options.json || options.jsonOutput;
var sourceKey = cssOutFilename;

// keying our source caches by the name of our output file means we can
// isolate css compilation of seperate bundles that are running in parallel
sourceByFile[sourceKey] = sourceByFile[sourceKey] || {};
var loader = loadersByFile[sourceKey];
if (!loader) {
loader = loadersByFile[sourceKey] = new FileSystemLoader(rootDir, plugins);
}

// PostCSS plugins passed to FileSystemLoader
var plugins = options.use || options.u;
Expand Down Expand Up @@ -158,10 +167,6 @@ module.exports = function (browserify, options) {
return through();
}

// collect visited filenames
filenames.push(filename);

var loader = new FileSystemLoader(rootDir, plugins);
return through(function noop () {}, function end () {
var self = this;

Expand All @@ -170,11 +175,6 @@ module.exports = function (browserify, options) {

assign(tokensByFile, loader.tokensByFile);

// store this file's source to be written out to disk later
sourceByFile[sourceKey][filename] = loader.finalSource;

compiledCssStream.push(loader.finalSource);

self.queue(output);
self.queue(null);
}, function (err) {
Expand All @@ -195,19 +195,19 @@ module.exports = function (browserify, options) {
bundle.emit('css stream', compiledCssStream);

bundle.on('end', function () {
// under certain conditions (eg. with shared libraries) we can end up with
// multiple occurrences of the same rule, so we need to remove duplicates
dedupeSources(loader.sources)

// Combine the collected sources for a single bundle into a single CSS file
var files = Object.keys(sourceByFile[sourceKey]);
var css;
var css = loader.finalSource;

// end the output stream
compiledCssStream.push(css);
compiledCssStream.push(null);

// write the css file
if (cssOutFilename) {
css = files.map(function (file) {
return sourceByFile[sourceKey][file];
}).join('\n');

fs.writeFile(cssOutFilename, css, function (err) {
if (err) {
browserify.emit('error', err);
Expand Down
9 changes: 9 additions & 0 deletions tests/cases/compose-from-shared/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
._shared__shared {
background: #000;
}
._styles_1__foo {
color: #F00;
}
._styles_2__bar {
background: #BAA;
}
2 changes: 2 additions & 0 deletions tests/cases/compose-from-shared/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./styles-1.css');
require('./styles-2.css');
3 changes: 3 additions & 0 deletions tests/cases/compose-from-shared/shared.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.shared {
background: #000;
}
4 changes: 4 additions & 0 deletions tests/cases/compose-from-shared/styles-1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.foo {
composes: shared from "./shared.css";
color: #F00;
}
4 changes: 4 additions & 0 deletions tests/cases/compose-from-shared/styles-2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.bar {
composes: shared from "./shared.css";
background: #BAA;
}