Skip to content

Commit

Permalink
feat: Rollup@1.0 support (#532)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: requires rollup@1.0
  • Loading branch information
tivac authored Jan 1, 2019
1 parent ee1da9a commit 1fab777
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 82 deletions.
23 changes: 12 additions & 11 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"devDependencies": {
"@modular-css/test-utils": "file:packages/test-utils",
"@modular-css/website": "file:packages/www",
"@tivac/eslint-config": "^2.2.2",
"@tivac/eslint-config": "^2.3.0",
"browserify": "^16.2.3",
"cli-tester": "2.0.0",
"dedent": "0.7.0",
Expand All @@ -36,7 +36,7 @@
"lerna": "^3.8.0",
"pegjs": "0.10.0",
"read-dir-deep": "1.0.4",
"rollup": "^0.68.1",
"rollup": "^1.0.0",
"rollup-plugin-svelte": "^5.0.0",
"shelljs": "^0.8.3",
"svelte": "^2.16.0",
Expand Down
26 changes: 24 additions & 2 deletions packages/processor/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ class Processor {
return files;
}

// Mark a file and everything that depends on it as invalid so
// it can be overwritten
invalidate(input) {
if(!input) {
throw new Error("invalidate() requires a file argument");
}

// Only want files actually in the array
const source = this._normalize(input);

if(!this._graph.hasNode(source)) {
throw new Error(`Unknown file: ${input}`);
}

const deps = this.dependents(source);

deps.concat(source).forEach((file) => {
this._files[file].valid = false;
});
}

// Get the dependency order for a file or the entire tree
dependencies(file) {
if(file) {
Expand Down Expand Up @@ -345,8 +366,8 @@ class Processor {
// Process files and walk their composition/value dependency tree to find
// new files we need to process
async _walk(name, text) {
// No need to re-process files
if(this._files[name]) {
// No need to re-process files unless they've been marked invalid
if(this._files[name] && this._files[name].valid) {
return;
}

Expand All @@ -358,6 +379,7 @@ class Processor {
text,
exports : false,
values : false,
valid : true,
result : this._before.process(
text,
params(this, {
Expand Down
56 changes: 56 additions & 0 deletions packages/processor/test/__snapshots__/api.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,62 @@ exports[`/processor.js API .file() should process an absolute file 4`] = `
"
`;

exports[`/processor.js API .invalidate() should invalidate a relative file 1`] = `
Array [
Array [
"simple.css",
false,
],
]
`;

exports[`/processor.js API .invalidate() should invalidate all dependents as well 1`] = `
Array [
Array [
"packages/processor/test/specimens/start.css",
false,
],
Array [
"packages/processor/test/specimens/local.css",
false,
],
Array [
"packages/processor/test/specimens/folder/folder.css",
false,
],
]
`;

exports[`/processor.js API .invalidate() should invalidate an absolute file 1`] = `
Array [
Array [
"packages/processor/test/specimens/simple.css",
false,
],
]
`;

exports[`/processor.js API .invalidate() should reprocess invalidated files 1`] = `
Array [
Array [
"packages/processor/test/specimens/start.css",
true,
],
Array [
"packages/processor/test/specimens/local.css",
true,
],
Array [
"packages/processor/test/specimens/folder/folder.css",
true,
],
]
`;

exports[`/processor.js API .invalidate() should throw if an invalid file is passed 1`] = `"Unknown file: nope.css"`;

exports[`/processor.js API .invalidate() should throw if no file is passed 1`] = `"invalidate() requires a file argument"`;

exports[`/processor.js API .output() should allow for seperate source map output 1`] = `
Object {
"file": "to.css",
Expand Down
59 changes: 59 additions & 0 deletions packages/processor/test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,65 @@ describe("/processor.js", () => {
).toMatchSnapshot();
});
});

describe(".invalidate()", () => {
const status = (source) =>
Object.entries(source).map(([ key, value ]) =>
([ relative([ key ])[0], value.valid ])
);

it("should invalidate a relative file", async () => {
await processor.string(
"./simple.css",
".wooga { }"
);

processor.invalidate("./simple.css");

expect(status(processor.files)).toMatchSnapshot();
});

it("should invalidate an absolute file", async () => {
await processor.string(
"./packages/processor/test/specimens/simple.css",
".wooga { }"
);

processor.invalidate(require.resolve("./specimens/simple.css"));

expect(status(processor.files)).toMatchSnapshot();
});

it("should throw if no file is passed", async () => {
await processor.file("./packages/processor/test/specimens/start.css");

expect(() => processor.invalidate()).toThrowErrorMatchingSnapshot();
});

it("should throw if an invalid file is passed", async () => {
await processor.file("./packages/processor/test/specimens/start.css");

expect(() => processor.invalidate("nope.css")).toThrowErrorMatchingSnapshot();
});

it("should invalidate all dependents as well", async () => {
await processor.file("./packages/processor/test/specimens/start.css");

processor.invalidate("./packages/processor/test/specimens/folder/folder.css");

expect(status(processor.files)).toMatchSnapshot();
});

it("should reprocess invalidated files", async () => {
await processor.file("./packages/processor/test/specimens/start.css");

processor.invalidate("./packages/processor/test/specimens/start.css");

await processor.file("./packages/processor/test/specimens/start.css");

expect(status(processor.files)).toMatchSnapshot();
});
});

describe(".dependencies()", () => {
it("should return the dependencies of the specified file", async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"slash": "^2.0.0"
},
"peerDependencies": {
"rollup": ">=0.68"
"rollup": "^1"
}
}
21 changes: 9 additions & 12 deletions packages/rollup/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,8 @@ module.exports = (opts) => {

log("file changed", file);

processor.dependents(file).forEach((dep) =>
processor.remove(dep)
);

processor.remove(file);
// TODO: should the file be removed if it's gone?
processor.invalidate(file);
},

async transform(code, id) {
Expand Down Expand Up @@ -123,10 +120,11 @@ module.exports = (opts) => {
out.push(`export var styles = ${JSON.stringify(details.result.css)};`);
}

processor.dependencies(id).forEach((dependency) => this.addWatchFile(dependency));

return {
code : out.join("\n"),
map : emptyMappings,
dependencies : processor.dependencies(id),
code : out.join("\n"),
map : emptyMappings,
};
},

Expand Down Expand Up @@ -171,9 +169,8 @@ module.exports = (opts) => {
// Keep track of files that are queued to be written
const queued = new Set();


usage.overallOrder().forEach((entry) => {
const { modules, name, fileName } = chunks[entry];
const { modules, name } = chunks[entry];
const css = new Set();
let counter = 1;

Expand Down Expand Up @@ -223,8 +220,6 @@ module.exports = (opts) => {

const id = this.emitAsset(`${name}.css`);

log("css output", id);

/* eslint-disable-next-line no-await-in-loop */
const result = await processor.output({
to : to.replace(/\[(name|extname)\]/g, (match, field) =>
Expand All @@ -233,6 +228,8 @@ module.exports = (opts) => {
files,
});

log("css output", `${name}.css`);

this.setAssetSource(id, result.css);

if(result.map) {
Expand Down
Loading

0 comments on commit 1fab777

Please sign in to comment.