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 committed 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
Expand Up @@ -35,7 +35,7 @@
"lerna": "^3.1.4", "lerna": "^3.1.4",
"pegjs": "0.10.0", "pegjs": "0.10.0",
"read-dir-deep": "1.0.4", "read-dir-deep": "1.0.4",
"rollup": "0.64.1", "rollup": "^0.65.0",
"rollup-plugin-svelte": "4.2.1", "rollup-plugin-svelte": "4.2.1",
"shelljs": "0.8.2", "shelljs": "0.8.2",
"svelte": "^2.13.0", "svelte": "^2.13.0",
Expand Down
9 changes: 7 additions & 2 deletions packages/rollup/README.md
Expand Up @@ -10,9 +10,14 @@ Rollup support for [`modular-css`](https://github.com/tivac/modular-css).


`$ npm i modular-css-rollup` `$ 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 ### API


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


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


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


const processor = options.processor || new Processor(options); 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 { return {
name : "modular-css-rollup", name : "modular-css-rollup",


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


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


// Is this file being processed on a watch update? processor.dependents(file).forEach((dep) =>
if(runs++ && (id in processor.files)) { processor.remove(dep)
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.remove(file); processor.remove(file);
}); },


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


const { details, exports } = await processor.string(id, code); const { details, exports } = await processor.string(id, code);
Expand Down
1 change: 1 addition & 0 deletions packages/rollup/test/__snapshots__/rollup.test.js.snap
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`] = ` exports[`/rollup.js should warn & not export individual keys when they are not valid identifiers 1`] = `
Object { Object {
"code": "PLUGIN_WARNING", "code": "PLUGIN_WARNING",
"hook": "transform",
"id": Any<String>, "id": Any<String>,
"message": "Invalid JS identifier \\"fooga-wooga\\", unable to export", "message": "Invalid JS identifier \\"fooga-wooga\\", unable to export",
"plugin": "modular-css-rollup", "plugin": "modular-css-rollup",
Expand Down

0 comments on commit 510b662

Please sign in to comment.