Skip to content

Commit

Permalink
feat: Use rollup@0.65.0's watchChange event (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
tivac authored Aug 28, 2018
1 parent fd9d4f0 commit 510b662
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 55 deletions.
12 changes: 6 additions & 6 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 @@ -35,7 +35,7 @@
"lerna": "^3.1.4",
"pegjs": "0.10.0",
"read-dir-deep": "1.0.4",
"rollup": "0.64.1",
"rollup": "^0.65.0",
"rollup-plugin-svelte": "4.2.1",
"shelljs": "0.8.2",
"svelte": "^2.13.0",
Expand Down
9 changes: 7 additions & 2 deletions packages/rollup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ Rollup support for [`modular-css`](https://github.com/tivac/modular-css).

`$ npm i modular-css-rollup`

## Usage
## ⚠️Rollup Version support⚠️

Due to API changes, certain major versions of this plugin will require a specific minimum rollup version.

⚠️ As of `modular-css-rollup@11` this plugin will only work with `rollup@0.60.0` or higher due to plugin API changes ⚠️
- `modular-css-rollup@11` requires `rollup@0.60.0`
- `modular-css-rollup@15` requires `rollup@0.65.0`

## Usage

### API

Expand Down
3 changes: 3 additions & 0 deletions packages/rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
"mkdirp": "^0.5.1",
"modular-css-core": "file:../core",
"rollup-pluginutils": "^2.0.1"
},
"peerDependencies": {
"rollup":"^0.65.0"
}
}
57 changes: 11 additions & 46 deletions packages/rollup/rollup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint max-statements: [ 1, 20 ] */
"use strict";

const fs = require("fs");
const path = require("path");

const { keyword } = require("esutils");
Expand Down Expand Up @@ -48,11 +47,6 @@ module.exports = function(opts) {

const processor = options.processor || new Processor(options);

let runs = 0;

// Track which files were changed for which run to avoid excessive re-processing
const updated = new Map();

return {
name : "modular-css-rollup",

Expand All @@ -67,50 +61,21 @@ module.exports = function(opts) {
}
},

async transform(code, id) {
if(!filter(id)) {
return null;
watchChange(file) {
if(!processor.files[file]) {
return;
}

// Is this file being processed on a watch update?
if(runs++ && (id in processor.files)) {
const files = [];

// Watching will call transform w/ the same entry file, even if it
// was one of its dependencies that changed. We need to fork the logic
// here to handle that case.
if(processor.files[id].text === code) {
// figure out exactly which dependency changed
processor.dependencies(id).forEach((dep) => {
if(fs.readFileSync(dep, "utf8") === processor.files[dep].text) {
return;
}

files.push(dep, ...processor.dependents(dep));
});
} else {
// Entry file changed, remove all its dependents and
// then re-process them
files.push(id, ...processor.dependents(id));
}

files.forEach((file) => {
// Don't do the remove/re-add dance for any files that
// were already processed in this run
if(updated.has(file) && updated.get(file) === runs) {
return;
}

updated.set(file, runs);
processor.dependents(file).forEach((dep) =>
processor.remove(dep)
);

processor.remove(file);
});
processor.remove(file);
},

// Can't filter this using updated, it's already been updated
// by the remove loop up above but the processing still needs to happen
await Promise.all([ ...files ].map((dep) =>
processor.file(dep)
));
async transform(code, id) {
if(!filter(id)) {
return null;
}

const { details, exports } = await processor.string(id, code);
Expand Down
1 change: 1 addition & 0 deletions packages/rollup/test/__snapshots__/rollup.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ exports[`/rollup.js should respect the CSS dependency tree 2`] = `
exports[`/rollup.js should warn & not export individual keys when they are not valid identifiers 1`] = `
Object {
"code": "PLUGIN_WARNING",
"hook": "transform",
"id": Any<String>,
"message": "Invalid JS identifier \\"fooga-wooga\\", unable to export",
"plugin": "modular-css-rollup",
Expand Down

0 comments on commit 510b662

Please sign in to comment.