Skip to content
Permalink
Browse files

feat: Add verbose option to rollup & svelte (#521)

Fixes #520
  • Loading branch information...
tivac committed Oct 12, 2018
1 parent 64e8e15 commit 0706e3d41a0f033dbb974808c1da351de860ed73
@@ -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"),
@@ -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
@@ -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; }
@@ -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");

@@ -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();
});
});
});
});
});
@@ -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
@@ -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
@@ -68,6 +74,8 @@ module.exports = function(opts) {
return;
}

log("file changed", file);

processor.dependents(file).forEach((dep) =>
processor.remove(dep)
);
@@ -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);
@@ -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")
@@ -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());
}
})
);
@@ -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 {
Oops, something went wrong.

0 comments on commit 0706e3d

Please sign in to comment.
You can’t perform that action at this time.