Skip to content

Commit

Permalink
wip disk caching
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Aug 12, 2018
1 parent f649f17 commit 042aca2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
43 changes: 36 additions & 7 deletions bin/src/run/build.ts
Expand Up @@ -7,6 +7,8 @@ import {
RollupBuild,
RollupSingleFileBuild
} from '../../../src/rollup/types';
import { readFile, writeFile } from '../../../src/utils/fs';
import { resolve } from '../../../src/utils/path';
import relativeId from '../../../src/utils/relativeId';
import { handleError, stderr } from '../logging';
import SOURCEMAPPING_URL from '../sourceMappingUrl';
Expand All @@ -17,7 +19,8 @@ export default function build(
inputOptions: InputOptions,
outputOptions: OutputOptions[],
warnings: BatchWarnings,
silent = false
silent: boolean,
configFile: string
) {
const useStdout =
outputOptions.length === 1 &&
Expand All @@ -42,9 +45,18 @@ export default function build(
stderr(tc.cyan(`\n${tc.bold(inputFiles)}${tc.bold(files.join(', '))}...`));
}

return rollup
.rollup(inputOptions)
.then((bundle: RollupSingleFileBuild | RollupBuild) => {
return readFile(resolve(configFile, '../.cache/rollup'))
.then(
pluginCache => {
const plugins = JSON.parse(pluginCache.toString());
inputOptions.cache = { plugins };
},
() => {}
)
.then(() => {
return rollup.rollup(inputOptions);
})
.then(bundle => {
if (useStdout) {
const output = outputOptions[0];
if (output.sourcemap && output.sourcemap !== 'inline') {
Expand All @@ -64,9 +76,26 @@ export default function build(
});
}

return Promise.all(outputOptions.map(output => <Promise<any>>bundle.write(output))).then(
() => bundle
);
let usedCache = false;
if (bundle.cache && bundle.cache.plugins) {
Object.keys(bundle.cache.plugins).forEach(cacheKey => {
for (const _p in bundle.cache.plugins[cacheKey]) {
usedCache = true;
return;
}
});
}

const cacheWritePromise = usedCache
? writeFile(resolve(configFile, '../.cache/rollup'), JSON.stringify(bundle.cache.plugins))
: Promise.resolve();
return Promise.all(
outputOptions.map(output => {
return <Promise<any>>bundle.write(output);
})
)
.then(() => cacheWritePromise)
.then(() => bundle);
})
.then((bundle?: RollupSingleFileBuild | RollupBuild) => {
warnings.flush();
Expand Down
1 change: 1 addition & 0 deletions browser/fs.ts
Expand Up @@ -5,3 +5,4 @@ export const readdirSync = nope('readdirSync');
export const readFileSync = nope('readFileSync');
export const realpathSync = nope('realpathSync');
export const writeFile = nope('writeFile');
export const readFile = nope('readFile');
4 changes: 2 additions & 2 deletions src/utils/default-plugin.ts
@@ -1,14 +1,14 @@
import { InputOptions, Plugin } from '../rollup/types';
import error from './error';
import { lstatSync, readdirSync, readFileSync, realpathSync } from './fs'; // eslint-disable-line
import { lstatSync, readdirSync, readFile, realpathSync } from './fs'; // eslint-disable-line
import { basename, dirname, isAbsolute, resolve } from './path';

export function getRollupDefaultPlugin(options: InputOptions): Plugin {
return {
name: 'Rollup Core',
resolveId: createResolveId(options),
load(id) {
return readFileSync(id, 'utf-8');
return readFile(id, 'utf-8').then(source => source.toString());
},
resolveDynamicImport(specifier, parentId) {
if (typeof specifier === 'string')
Expand Down
45 changes: 20 additions & 25 deletions src/utils/fs.ts
Expand Up @@ -3,32 +3,27 @@ import { dirname } from './path';

export * from 'fs';

function mkdirpath(path: string) {
const dir = dirname(path);
try {
fs.readdirSync(dir);
} catch (err) {
mkdirpath(dir);
try {
fs.mkdirSync(dir);
} catch (err2) {
if (err2.code !== 'EEXIST') {
throw err2;
}
}
}
function mkdirp(dir: string): Promise<void> {
return new Promise<void>((resolve, reject) =>
fs.mkdir(dir, err => (err ? reject(err) : resolve()))
).catch(err => {
if (err.code === 'ENOENT') return mkdirp(dirname(dir)).then(() => mkdirp(dir));
else if (err.code === 'EEXIST') return;
throw err;
});
}

export function writeFile(dest: string, data: string | Buffer) {
return new Promise<void>((fulfil, reject) => {
mkdirpath(dest);
export function writeFile(path: string, data: string | Buffer) {
return mkdirp(dirname(path)).then(
() =>
new Promise<void>((resolve, reject) =>
fs.writeFile(path, data, err => (err ? reject(err) : resolve()))
)
);
}

fs.writeFile(dest, data, err => {
if (err) {
reject(err);
} else {
fulfil();
}
});
});
export function readFile(path: string, encoding = 'utf-8'): Promise<Buffer> {
return new Promise((resolve, reject) =>
fs.readFile(path, encoding, (err, source) => (err ? reject(err) : resolve(<any>source)))
);
}

0 comments on commit 042aca2

Please sign in to comment.