Skip to content
Permalink
Browse files

fix: rollup watching loses file contents (#377)

  • Loading branch information...
tivac committed Dec 13, 2017
1 parent b560651 commit 347ee8d63114b20587df710133f7e82367d2e695
Showing with 141 additions and 17 deletions.
  1. +16 −4 packages/rollup/rollup.js
  2. +24 −0 packages/rollup/test/__snapshots__/rollup.test.js.snap
  3. +101 −13 packages/rollup/test/rollup.test.js
@@ -40,19 +40,31 @@ module.exports = function(opts) {
name : "modular-css",

transform : function(code, id) {
var removed;

if(!filter(id) || id.slice(slice) !== options.ext) {
return null;
}

// If the file is being re-processed we need to remove it to
// avoid cache staleness issues
if(runs) {
processor.remove(id);
removed = processor.remove(id);
} else {
removed = [];
}

// Add the file & its dependencies
return processor.string(id, code).then((result) => {
var classes = output.join(result.exports),
return Promise.all(
// Run current file first since it's already in-memory
[ processor.string(id, code) ].concat(
removed.map((file) =>
processor.file(file)
)
)
)
.then((results) => {
var result = results[0],
classes = output.join(result.exports),
named = Object.keys(classes),
out = [
`export default ${JSON.stringify(classes, null, 4)};`
@@ -92,6 +92,30 @@ console.log(css);
}
`;

exports[`/rollup.js watch should correctly update files within the dependency graph in watch mode when files change 1`] = `
"/* packages/rollup/test/output/one.css */
.mc19ef5610_one {
color: red;
}
/* packages/rollup/test/output/two.css */
.mc32dbcb4b_two {

color: blue;
}"
`;

exports[`/rollup.js watch should correctly update files within the dependency graph in watch mode when files change 2`] = `
"/* packages/rollup/test/output/one.css */
.mc19ef5610_one {
color: green;
}
/* packages/rollup/test/output/two.css */
.mc32dbcb4b_two {

color: blue;
}"
`;

exports[`/rollup.js watch should generate correct builds in watch mode when files change 1`] = `
"/* packages/rollup/test/output/watched.css */
.mc580619a9_one { color: red; }
@@ -2,7 +2,8 @@

var fs = require("fs"),

rollup = require("rollup").rollup,
rollup = require("rollup").rollup,
dedent = require("dedent"),

read = require("test-utils/read.js")(__dirname),
namer = require("test-utils/namer.js"),
@@ -13,6 +14,24 @@ function error(root) {
throw root.error("boom");
}

function watching(cb) {
var count = 0;

return (details) => {
if(details.code === "ERROR" || details.code === "FATAL") {
throw details.error;
}

if(details.code !== "END") {
return;
}

count++;

cb(count, details);
};
}

error.postcssPlugin = "error-plugin";

describe("/rollup.js", () => {
@@ -264,21 +283,20 @@ describe("/rollup.js", () => {
});

describe("watch", () => {
var watcher;
var watch = require("rollup").watch,
watcher;

afterEach(() => watcher.close());

it("should generate correct builds in watch mode when files change", (done) => {
var builds = 0;

// Create v1 of the file
fs.writeFileSync(
"./packages/rollup/test/output/watched.css",
".one { color: red; }"
);

// Start watching (re-requiring rollup because it needs root obj reference)
watcher = require("rollup").watch({
watcher = watch({
input : require.resolve("./specimens/watch.js"),
output : {
file : "./packages/rollup/test/output/watch.js",
@@ -298,15 +316,85 @@ describe("/rollup.js", () => {
".two { color: blue; }"
), 200);

watcher.on("event", (details) => {
/* eslint consistent-return:0 */

if(details.code !== "END") {
return;
watcher.on("event", watching((builds) => {
// First build
if(builds === 1) {
try {
expect(read("watch-output.css")).toMatchSnapshot();
} catch(e) {
return done(e);
}
}

// Second build
if(builds > 1) {
try {
expect(read("watch-output.css")).toMatchSnapshot();
} catch(e) {
return done(e);
}

return done();
}
}));
});

builds++;

it("should correctly update files within the dependency graph in watch mode when files change", (done) => {
// Create v1 of the files
fs.writeFileSync(
"./packages/rollup/test/output/one.css",
dedent(`
.one {
color: red;
}
`)
);

fs.writeFileSync(
"./packages/rollup/test/output/two.css",
dedent(`
.two {
composes: one from "./one.css";
color: blue;
}
`)
);

fs.writeFileSync(
"./packages/rollup/test/output/watch.js",
dedent(`
import css from "./two.css";
console.log(css);
`)
);

// Start watching (re-requiring rollup because it needs root obj reference)
watcher = watch({
input : require.resolve("./output/watch.js"),
output : {
file : "./packages/rollup/test/output/watch-output.js",
format : "es"
},
plugins : [
plugin({
css : "./packages/rollup/test/output/watch-output.css",
map : false
})
]
});

// Create v2 of the file after a bit
setTimeout(() => fs.writeFileSync(
"./packages/rollup/test/output/one.css",
dedent(`
.one {
color: green;
}
`)
), 200);

watcher.on("event", watching((builds) => {
// First build
if(builds === 1) {
try {
@@ -326,7 +414,7 @@ describe("/rollup.js", () => {

return done();
}
});
}));
});
});
});

0 comments on commit 347ee8d

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