Skip to content

Commit

Permalink
support addWatchFile
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Aug 11, 2018
1 parent 9f25bc0 commit 0193bb3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export interface PluginCache {

export interface PluginContext {
watcher: Watcher;
addWatchFile: (id: string) => void;
cache: PluginCache;
resolveId: ResolveIdHook;
isExternal: IsExternal;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/pluginDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { createAssetPluginHooks, EmitAsset } from './assetHooks';
import { getRollupDefaultPlugin } from './default-plugin';
import error from './error';
import { resolve } from './path';

export interface PluginDriver {
emitAsset: EmitAsset;
Expand Down Expand Up @@ -84,6 +85,9 @@ export function createPluginDriver(

const context: PluginContext = {
watcher,
addWatchFile(id: string) {
graph.watchFiles.push(resolve(id));
},
cache: cacheInstance,
isExternal(id: string, parentId: string, isResolved = false) {
return graph.isExternal(id, parentId, isResolved);
Expand Down
7 changes: 4 additions & 3 deletions src/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function transform(

function transformReducer(code: string, result: any, plugin: Plugin) {
// track which plugins use the custom this.cache to opt-out of transform caching
if (trackedPluginCache.used) customTransformCache = true;
if (!customTransformCache && trackedPluginCache.used) customTransformCache = true;
if (customTransformCache) {
if (result && Array.isArray(result.dependencies)) {
for (const dep of result.dependencies) {
Expand Down Expand Up @@ -103,13 +103,14 @@ export default function transform(
[curSource, id],
transformReducer,
(pluginContext, plugin) => {
trackedPluginCache = trackPluginCache(pluginContext.cache);
if (plugin.cacheKey) customTransformCache = true;
else trackedPluginCache = trackPluginCache(pluginContext.cache);

let emitAsset: EmitAsset;
({ assets, emitAsset } = createTransformEmitAsset(graph.assetsById, baseEmitAsset));
return {
...pluginContext,
cache: trackedPluginCache.cache,
cache: trackedPluginCache ? trackedPluginCache.cache : pluginContext.cache,
warn(warning: RollupWarning | string, pos?: { line: number; column: number }) {
if (typeof warning === 'string') warning = { message: warning };
if (pos) augmentCodeLocation(warning, pos, curSource, id);
Expand Down
46 changes: 46 additions & 0 deletions test/watch/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require('assert');
const sander = require('sander');
const rollup = require('../../dist/rollup');
const path = require('path');

const cwd = process.cwd();

Expand Down Expand Up @@ -354,6 +355,51 @@ describe('rollup.watch', () => {
});
});

it('supports transform cache opt-out via cacheKey and custom watchFiles', () => {
return sander
.copydir('test/watch/samples/transform-dependencies')
.to('test/_tmp/input')
.then(() => {
const watcher = rollup.watch({
input: 'test/_tmp/input/main.js',
output: {
file: 'test/_tmp/output/bundle.js',
format: 'cjs'
},
plugins: [
{
cacheKey: 'asdf',
transform(code) {
this.addWatchFile('test/_tmp/input/asdf');
const text = sander.readFileSync('test/_tmp/input/asdf').toString();
return `export default "${text}"`;
}
}
],
watch: { chokidar }
});

return sequence(watcher, [
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.equal(run('../_tmp/output/bundle.js'), 'asdf');
sander.unlinkSync('test/_tmp/input/asdf');
sander.writeFileSync('test/_tmp/input/asdf', 'next');
},
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.equal(run('../_tmp/output/bundle.js'), 'next');
}
]);
});
});

it('throws if transform dependency doesnt exist', () => {
return sander
.copydir('test/watch/samples/transform-dependencies')
Expand Down

0 comments on commit 0193bb3

Please sign in to comment.