Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import relativeId from './utils/relativeId';
import error from './utils/error';
import { isAbsolute, isRelative, normalize, relative, resolve } from './utils/path';
import {
CachedChunk,
CachedChunkSet,
InputOptions,
IsExternalHook,
MissingExportHook,
Expand Down Expand Up @@ -86,9 +88,18 @@ export default class Graph {
constructor(options: InputOptions) {
this.cachedModules = new Map();
if (options.cache) {
options.cache.modules.forEach(module => {
this.cachedModules.set(module.id, module);
});
if ((<CachedChunk>options.cache).modules) {
(<CachedChunk>options.cache).modules.forEach(module => {
this.cachedModules.set(module.id, module);
})
} else {
const chunks = (<CachedChunkSet>options.cache).chunks;
for (const chunkName in chunks) {
chunks[chunkName].modules.forEach(module => {
this.cachedModules.set(module.id, module);
});
}
}
}
delete options.cache; // TODO not deleting it here causes a memory leak; needs further investigation

Expand Down
29 changes: 20 additions & 9 deletions src/rollup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ export interface TreeshakingOptions {
export type ExternalOption = string[] | IsExternalHook;
export type GlobalsOption = { [name: string]: string } | ((name: string) => string);

export type CachedChunk = { modules: ModuleJSON[] };
export type CachedChunkSet = { chunks: { [chunkName: string]: CachedChunk } };
export interface InputOptions {
input: string | string[];
external?: ExternalOption;
plugins?: Plugin[];

onwarn?: WarningHandler;
cache?: {
modules: ModuleJSON[];
};
cache?: CachedChunk | CachedChunkSet;

acorn?: {};
acornInjectPlugins?: Function[];
Expand Down Expand Up @@ -242,8 +242,21 @@ export interface OutputChunk {
write: (options: OutputOptions) => Promise<void>;
}

export default function rollup(rawInputOptions: InputOptions): Promise<OutputChunk>;
export default function rollup(rawInputOptions: GenericConfigObject) {
export interface OutputChunkSet {
chunks: {
[chunkName: string]: {
name: string,
imports: string[],
exports: string[],
modules: ModuleJSON[]
}
};
generate: (outputOptions: OutputOptions) => Promise<{ [chunkName: string]: SourceDescription }>;
write: (options: OutputOptions) => Promise<void>;
}

export default function rollup (rawInputOptions: InputOptions): Promise<OutputChunk | OutputChunkSet>;
export default function rollup (rawInputOptions: GenericConfigObject): Promise<OutputChunk | OutputChunkSet> {
try {
if (!rawInputOptions) {
throw new Error('You must supply an options object to rollup');
Expand Down Expand Up @@ -479,7 +492,7 @@ export default function rollup(rawInputOptions: GenericConfigObject) {
return {
chunks: chunks,
generate,
write(outputOptions: OutputOptions) {
write (outputOptions: OutputOptions): Promise<void> {
if (!outputOptions || !outputOptions.dir) {
error({
code: 'MISSING_OPTION',
Expand Down Expand Up @@ -522,11 +535,9 @@ export default function rollup(rawInputOptions: GenericConfigObject) {
)
);
})
// ensures return isn't void[]
.then(() => {})
);
})
);
).then(() => {}); // ensures return void and not void[][]
});
}
};
Expand Down
63 changes: 39 additions & 24 deletions src/watch/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import { EventEmitter } from 'events';
import createFilter from 'rollup-pluginutils/src/createFilter.js';
import rollup, { InputOptions, OutputOptions, OutputChunk } from '../rollup/index';
import rollup, { CachedChunk, CachedChunkSet, InputOptions, OutputOptions, OutputChunk, OutputChunkSet } from '../rollup/index';
import ensureArray from '../utils/ensureArray';
import { mapSequence } from '../utils/promise';
import { addTask, deleteTask } from './fileWatchers';
Expand Down Expand Up @@ -100,7 +100,7 @@ export class Task {
closed: boolean;
watched: Set<string>;
inputOptions: InputOptions;
cache: OutputChunk;
cache: CachedChunk | CachedChunkSet;

chokidarOptions: WatchOptions;
chokidarOptionsHash: string;
Expand All @@ -126,10 +126,7 @@ export class Task {

this.outputs = outputOptions;
this.outputFiles = this.outputs.map(output => {
if (!output.file) {
throw new Error(`watch is currently only supported for a single output.file`);
}
return path.resolve(output.file);
return path.resolve(output.file || output.dir);
});

const watchOptions = inputOptions.watch || {};
Expand Down Expand Up @@ -195,25 +192,34 @@ export class Task {
}

return rollup(options)
.then((chunk: OutputChunk) => {
.then((result: OutputChunk | OutputChunkSet) => {
if (this.closed) return;

this.cache = chunk;
const watched = this.watched = new Set();

const watched = new Set();

chunk.modules.forEach((module: ModuleJSON) => {
watched.add(module.id);
this.watchFile(module.id);
});
const watchChunk = (chunk: {modules: ModuleJSON[]}) => {
chunk.modules.forEach((module: ModuleJSON) => {
watched.add(module.id);
this.watchFile(module.id);
});

this.watched.forEach(id => {
if (!watched.has(id)) deleteTask(id, this, this.chokidarOptionsHash);
});
this.watched.forEach(id => {
if (!watched.has(id)) deleteTask(id, this, this.chokidarOptionsHash);
});
}

this.watched = watched;
this.cache = result;
if ((<OutputChunkSet>result).chunks) {
const chunks = (<OutputChunkSet>result).chunks;
for (const chunkName in chunks) {
watchChunk(chunks[chunkName]);
}
} else {
const chunk = (<OutputChunk>result);
watchChunk(chunk)
}

return Promise.all(this.outputs.map(output => chunk.write(output)));
return Promise.all(this.outputs.map(output => result.write(output)));
})
.then(() => {
this.watcher.emit('event', {
Expand All @@ -227,11 +233,20 @@ export class Task {
if (this.closed) return;

if (this.cache) {
this.cache.modules.forEach(module => {
// this is necessary to ensure that any 'renamed' files
// continue to be watched following an error
this.watchFile(module.id);
});
// this is necessary to ensure that any 'renamed' files
// continue to be watched following an error
if ((<CachedChunk>this.cache).modules) {
(<CachedChunk>this.cache).modules.forEach(module => {
this.watchFile(module.id);
})
} else {
const chunks = (<CachedChunkSet>this.cache).chunks;
for (const chunkName in chunks) {
chunks[chunkName].modules.forEach(module => {
this.watchFile(module.id);
});
}
}
}
throw error;
});
Expand Down
44 changes: 44 additions & 0 deletions test/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('rollup.watch', () => {
}

function runTests(chokidar) {

it('watches a file', () => {
return sander
.copydir('test/watch/samples/basic')
Expand Down Expand Up @@ -99,6 +100,49 @@ describe('rollup.watch', () => {
});
});

it('watches a file in code-splitting mode', () => {
return sander
.copydir('test/watch/samples/code-splitting')
.to('test/_tmp/input')
.then(() => {
const watcher = rollup.watch({
input: ['test/_tmp/input/main1.js', 'test/_tmp/input/main2.js'],
output: {
dir: 'test/_tmp/output',
format: 'cjs'
},
watch: { chokidar },
experimentalCodeSplitting: true
});

return sequence(watcher, [
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
delete require.cache[require.resolve('../_tmp/output/chunk1.js')];
assert.equal(run('../_tmp/output/main1.js'), 21);
assert.equal(run('../_tmp/output/main2.js'), 42);
sander.writeFileSync(
'test/_tmp/input/shared.js',
'export const value = 22;'
);
},
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
delete require.cache[require.resolve('../_tmp/output/chunk1.js')];
assert.equal(run('../_tmp/output/main1.js'), 22);
assert.equal(run('../_tmp/output/main2.js'), 44);
watcher.close();
}
]);
});
});

it('recovers from an error', () => {
return sander
.copydir('test/watch/samples/basic')
Expand Down
2 changes: 2 additions & 0 deletions test/watch/samples/code-splitting/main1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import {value} from './shared';
export default value;
2 changes: 2 additions & 0 deletions test/watch/samples/code-splitting/main2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import {value} from './shared';
export default value * 2;
1 change: 1 addition & 0 deletions test/watch/samples/code-splitting/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 21;