Skip to content

Commit

Permalink
feat: Add verbose option to rollup & svelte (#521)
Browse files Browse the repository at this point in the history
Fixes #520
  • Loading branch information
tivac committed Oct 12, 2018
1 parent 64e8e15 commit 0706e3d
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 44 deletions.
49 changes: 24 additions & 25 deletions packages/processor/processor.js
Expand Up @@ -33,50 +33,49 @@ function params(processor, args) {

class Processor {
constructor(opts) {
/* eslint max-statements: [ "warn", 20 ] */
this._options = Object.assign(
/* eslint max-statements: [ "warn", 25 ] */
const options = Object.assign(
Object.create(null),
{
cwd : process.cwd(),
map : false,
rewrite : true,
verbose : false,
cwd : process.cwd(),
map : false,
rewrite : true,
verbose : false,
resolvers : [],
},
opts
);

if(!path.isAbsolute(this._options.cwd)) {
this._options.cwd = path.resolve(this._options.cwd);
}
this._options = options;

if(typeof this._options.namer === "string") {
this._options.namer = require(this._options.namer)();
if(!path.isAbsolute(options.cwd)) {
options.cwd = path.resolve(options.cwd);
}

if(typeof this._options.namer !== "function") {
this._options.namer = (file, selector) =>
`mc${slug(relative(this._options.cwd, file))}_${selector}`;
if(typeof options.namer === "string") {
options.namer = require(options.namer)();
}

if(!Array.isArray(this._options.resolvers)) {
this._options.resolvers = [];
if(typeof options.namer !== "function") {
options.namer = (file, selector) =>
`mc${slug(relative(options.cwd, file))}_${selector}`;
}

this._log = this._options.verbose ?
this._log = options.verbose ?
// eslint-disable-next-line no-console
console.log.bind(console, "[processor]") :
// eslint-disable-next-line no-empty-function
() => {};

this._resolve = resolve.resolvers(this._options.resolvers);
this._resolve = resolve.resolvers(options.resolvers);

this._absolute = (file) => (path.isAbsolute(file) ? file : path.join(this._options.cwd, file));
this._absolute = (file) => (path.isAbsolute(file) ? file : path.join(options.cwd, file));

this._files = Object.create(null);
this._graph = new Graph();

this._before = postcss([
...(this._options.before || []),
...(options.before || []),
require("./plugins/values-local.js"),
require("./plugins/values-export.js"),
require("./plugins/values-replace.js"),
Expand All @@ -93,17 +92,17 @@ class Processor {
require("./plugins/externals.js"),
require("./plugins/composition.js"),
require("./plugins/keyframes.js"),
...(this._options.processing || []),
...(options.processing || []),
]);

this._after = postcss(this._options.after || [ noop ]);
this._after = postcss(options.after || [ noop ]);

// Add postcss-url to the afters if requested
if(this._options.rewrite) {
this._after.use(require("postcss-url")(this._options.rewrite));
if(options.rewrite) {
this._after.use(require("postcss-url")(options.rewrite));
}

this._done = postcss(this._options.done || [ noop ]);
this._done = postcss(options.done || [ noop ]);
}

// Add a file on disk to the dependency graph
Expand Down
70 changes: 70 additions & 0 deletions packages/processor/test/__snapshots__/options.test.js.snap
Expand Up @@ -74,6 +74,76 @@ exports[`/processor.js options lifecycle options processing should run sync post
a {}"
`;

exports[`/processor.js options lifecycle options verbose should output debugging messages when verbose mode is enabled 1`] = `
Array [
Array [
"[processor]",
"file()",
"packages/processor/test/specimens/start.css",
],
Array [
"[processor]",
"string()",
"packages/processor/test/specimens/start.css",
],
Array [
"[processor]",
"processing",
"packages/processor/test/specimens/folder/folder.css",
],
Array [
"[processor]",
"processed",
"packages/processor/test/specimens/folder/folder.css",
],
Array [
"[processor]",
"processing",
"packages/processor/test/specimens/local.css",
],
Array [
"[processor]",
"processed",
"packages/processor/test/specimens/local.css",
],
Array [
"[processor]",
"processing",
"packages/processor/test/specimens/start.css",
],
Array [
"[processor]",
"processed",
"packages/processor/test/specimens/start.css",
],
Array [
"[processor]",
"string() done",
"packages/processor/test/specimens/start.css",
],
Array [
"[processor]",
"string()",
"packages/processor/test/specimens/string.css",
],
Array [
"[processor]",
"processing",
"packages/processor/test/specimens/string.css",
],
Array [
"[processor]",
"processed",
"packages/processor/test/specimens/string.css",
],
Array [
"[processor]",
"string() done",
"packages/processor/test/specimens/string.css",
],
]
`;

exports[`/processor.js options map should generate external source maps 1`] = `
"/* packages/processor/test/specimens/folder/folder.css */
.folder { margin: 2px; }
Expand Down
22 changes: 22 additions & 0 deletions packages/processor/test/options.test.js
Expand Up @@ -5,6 +5,7 @@ const path = require("path");
const dedent = require("dedent");
const namer = require("@modular-css/test-utils/namer.js");
const relative = require("@modular-css/test-utils/relative.js");
const logs = require("@modular-css/test-utils/logs.js");

const Processor = require("../processor.js");

Expand Down Expand Up @@ -370,6 +371,27 @@ describe("/processor.js", () => {
.then((result) => expect(result.css).toMatchSnapshot());
});
});

describe("verbose", () => {
it("should output debugging messages when verbose mode is enabled", async () => {
const { logSnapshot } = logs();

const processor = new Processor({
namer,
verbose : true,
});

await processor.file("./packages/processor/test/specimens/start.css");
await processor.string(
"packages/processor/test/specimens/string.css",
".foo { color: fuschia; }"
);

await processor.output();

logSnapshot();
});
});
});
});
});
22 changes: 20 additions & 2 deletions packages/rollup/rollup.js
Expand Up @@ -35,11 +35,15 @@ module.exports = function(opts) {
namedExports : true,
styleExport : false,
dev : false,
verbose : false,
}, opts);

const filter = utils.createFilter(options.include, options.exclude);

const { styleExport, done, map, dev } = options;
const { styleExport, done, map, dev, verbose } = options;

// eslint-disable-next-line no-console, no-empty-function
const log = verbose ? console.log.bind(console, "[rollup]") : () => {};

if(typeof map === "undefined") {
// Sourcemaps don't make much sense in styleExport mode
Expand All @@ -53,6 +57,8 @@ module.exports = function(opts) {
name : "@modular-css/rollup",

buildStart() {
log("build start");

// done lifecycle won't ever be called on per-component styles since
// it only happens at bundle compilation time
// Need to do this on buildStart so it has access to this.warn() o_O
Expand All @@ -68,6 +74,8 @@ module.exports = function(opts) {
return;
}

log("file changed", file);

processor.dependents(file).forEach((dep) =>
processor.remove(dep)
);
Expand All @@ -80,6 +88,8 @@ module.exports = function(opts) {
return null;
}

log("transform", id);

const { details, exports } = await processor.string(id, code);

const exported = output.join(exports);
Expand Down Expand Up @@ -226,6 +236,8 @@ module.exports = function(opts) {
.map(async ({ base, name, css }, idx) => {
const id = this.emitAsset(`${base}.css`);

log("css output", id);

const result = await processor.output({
to : to.replace(/\[(name|extname)\]/g, (match, field) =>
(field === "name" ? name : ".css")
Expand All @@ -240,11 +252,17 @@ module.exports = function(opts) {
if(options.json && idx === 0) {
const file = typeof options.json === "string" ? options.json : "exports.json";

log("json output", file);

this.emitAsset(file, JSON.stringify(result.compositions, null, 4));
}

if(result.map) {
this.emitAsset(`${base}.css.map`, result.map.toString());
const file = `${base}.css.map`;

log("map output", file);

this.emitAsset(file, result.map.toString());
}
})
);
Expand Down
104 changes: 104 additions & 0 deletions packages/rollup/test/__snapshots__/rollup.test.js.snap
Expand Up @@ -110,6 +110,110 @@ exports[`/rollup.js should handle assetFileNames being undefined 1`] = `
"
`;
exports[`/rollup.js should log in verbose mode 1`] = `
Array [
Array [
"[rollup]",
"build start",
],
Array [
"[rollup]",
"transform",
"packages/rollup/test/specimens/simple.css",
],
Array [
"[processor]",
"string()",
"packages/rollup/test/specimens/simple.css",
],
Array [
"[processor]",
"processing",
"packages/rollup/test/specimens/simple.css",
],
Array [
"[processor]",
"processed",
"packages/rollup/test/specimens/simple.css",
],
Array [
"[processor]",
"string() done",
"packages/rollup/test/specimens/simple.css",
],
Array [
"[rollup]",
"css output",
"40a365e7",
],
Array [
"[processor]",
"file()",
"packages/processor/test/specimens/start.css",
],
Array [
"[processor]",
"string()",
"packages/processor/test/specimens/start.css",
],
Array [
"[processor]",
"processing",
"packages/processor/test/specimens/folder/folder.css",
],
Array [
"[processor]",
"processed",
"packages/processor/test/specimens/folder/folder.css",
],
Array [
"[processor]",
"processing",
"packages/processor/test/specimens/local.css",
],
Array [
"[processor]",
"processed",
"packages/processor/test/specimens/local.css",
],
Array [
"[processor]",
"processing",
"packages/processor/test/specimens/start.css",
],
Array [
"[processor]",
"processed",
"packages/processor/test/specimens/start.css",
],
Array [
"[processor]",
"string() done",
"packages/processor/test/specimens/start.css",
],
Array [
"[processor]",
"string()",
"packages/processor/test/specimens/string.css",
],
Array [
"[processor]",
"processing",
"packages/processor/test/specimens/string.css",
],
Array [
"[processor]",
"processed",
"packages/processor/test/specimens/string.css",
],
Array [
"[processor]",
"string() done",
"packages/processor/test/specimens/string.css",
],
]
`;
exports[`/rollup.js should not output sourcemaps when they are disabled 1`] = `
"/* packages/rollup/test/specimens/simple.css */
.fooga {
Expand Down

0 comments on commit 0706e3d

Please sign in to comment.