Skip to content
Permalink
Browse files

feat: Use rollup@0.65.0's watchChange event (#511)

  • Loading branch information...
tivac committed Aug 28, 2018
1 parent fd9d4f0 commit 510b662cde8ffa6369c7c0ec7c4ce201b019bf9b

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -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",
@@ -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

@@ -22,5 +22,8 @@
"mkdirp": "^0.5.1",
"modular-css-core": "file:../core",
"rollup-pluginutils": "^2.0.1"
},
"peerDependencies": {
"rollup":"^0.65.0"
}
}
@@ -1,7 +1,6 @@
/* eslint max-statements: [ 1, 20 ] */
"use strict";

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

const { keyword } = require("esutils");
@@ -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",

@@ -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);
@@ -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",

0 comments on commit 510b662

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