Skip to content
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

Rework rollup support #531

Merged
merged 13 commits into from
Dec 22, 2018
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"lerna": "^3.4.3",
"pegjs": "0.10.0",
"read-dir-deep": "1.0.4",
"rollup": "^0.67.3",
"rollup": "^0.68.0",
"rollup-plugin-svelte": "^4.3.2",
"shelljs": "^0.8.3",
"svelte": "^2.15.3",
Expand Down
32 changes: 16 additions & 16 deletions packages/browserify/browserify.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = (browserify, opts) => {

function depReducer(curr, next) {
curr[prefixed(options.cwd, next)] = next;

return curr;
}

Expand Down Expand Up @@ -84,19 +84,19 @@ module.exports = (browserify, opts) => {
processor.dependencies(result.id).forEach((dep) =>
browserify.emit("file", dep, dep)
);

push(outputs(result));

done();
},

(error) => {
// Thrown from the current bundler instance, NOT the main browserify
// instance. This is so that watchify won't explode.
bundler.emit("error", error);

push(buffer);

done();
}
);
Expand All @@ -108,32 +108,32 @@ module.exports = (browserify, opts) => {
if(path.extname(row.file) !== options.ext) {
return done(null, row);
}

handled[row.id] = true;

// Ensure that browserify knows about the CSS dependency tree by updating
// any referenced entries w/ their dependencies
row.deps = processor.dependencies(row.file).reduce(depReducer, {});

return done(null, row);
}, function(done) {
// Ensure that any CSS dependencies not directly referenced are
// injected into the stream of files being managed
const push = this.push.bind(this);

processor.dependencies().forEach((dep) => {
if(dep in handled) {
return;
}

push({
id : path.resolve(options.cwd, dep),
file : path.resolve(options.cwd, dep),
source : outputs(processor.files[dep]),
deps : processor.dependencies(dep).reduce(depReducer, {}),
});
});

done();
}));

Expand Down Expand Up @@ -169,24 +169,24 @@ module.exports = (browserify, opts) => {
// in case things have changed out from under us, like when using watchify
bundles = {};
handled = {};

// cache set to false means we need to create a new Processor each run-through
if(!options.cache) {
processor = new Processor(options);
}

bundler = current;

// Listen for bundling to finish
bundler.on("end", () => {
const bundling = Object.keys(bundles).length > 0;

if(options.json) {
mkdirp.sync(path.dirname(options.json));

fs.writeFileSync(
options.json,
JSON.stringify(output.compositions(options.cwd, processor), null, 4)
JSON.stringify(output.compositions(processor), null, 4)
);
}

Expand Down Expand Up @@ -231,7 +231,7 @@ module.exports = (browserify, opts) => {
if(!common.length && !options.empty) {
return Promise.resolve();
}

return write(bundling && common, options.css);
});
});
Expand Down
7 changes: 4 additions & 3 deletions packages/processor/lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ exports.join = (output) =>
classes.toString()
));

exports.compositions = (cwd, { files }) => {
exports.compositions = ({ options, files }) => {
const { cwd } = options;
const json = {};

Object.keys(files)
.sort()
.forEach((file) =>
(json[relative(cwd, file)] = exports.join(files[file].exports))
);

return json;
};
19 changes: 10 additions & 9 deletions packages/processor/plugins/values-replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
const selector = require("postcss-selector-parser");
const value = require("postcss-value-parser");
const escape = require("escape-string-regexp");
const each = require("lodash/forEach");
const get = require("lodash/get");
const Graph = require("dependency-graph").DepGraph;

const namespaced = require("./values-namespaced.js");

module.exports = (css, { opts, messages }) => {
Expand All @@ -22,8 +21,10 @@ module.exports = (css, { opts, messages }) => {
messages
.filter(({ plugin }) => plugin === namespaced.postcssPlugin)
.forEach((msg) =>
each(msg.values, (children, ns) =>
each(children, (details, child) => (values[`${ns}.${child}`] = details))
Object.entries(msg.values).forEach(([ ns, children ]) =>
Object.entries(children).forEach(([ child, details ]) =>
(values[`${ns}.${child}`] = details)
)
)
);

Expand Down Expand Up @@ -54,17 +55,17 @@ module.exports = (css, { opts, messages }) => {
const replacer = (prop) =>
(thing) => {
const parsed = value(thing[prop]);

parsed.walk((node) => {
if(node.type !== "word") {
return;
}

// Replace any value instances
node.value = node.value.replace(matchRegex, (match) => {
// Source map support
thing.source = values[match].source;

return values[match].value;
});
});
Expand All @@ -73,7 +74,7 @@ module.exports = (css, { opts, messages }) => {
};

// Walk through all values & build dependency graph
each(values, (details, name) => {
Object.entries(values).forEach(([ name, details ]) => {
graph.addNode(name);

value(details.value).walk((node) => {
Expand All @@ -89,7 +90,7 @@ module.exports = (css, { opts, messages }) => {
// Walk through values in dependency order & update any inter-dependent values
graph.overallOrder().forEach((name) => {
const parsed = value(values[name].value);

parsed.walk((node) => {
if(node.type !== "word" || !values[node.value]) {
return;
Expand Down
Loading