Skip to content

Commit

Permalink
Rehydrate UncommitedAsset in Transformation#readFromCache (#5570)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Jan 21, 2021
1 parent ac6a0cb commit 0ed50b5
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
25 changes: 21 additions & 4 deletions packages/core/core/src/Transformation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow strict-local

import type {
AST,
FilePath,
GenerateOutput,
Transformer,
Expand Down Expand Up @@ -397,12 +398,28 @@ export default class Transformation {
return null;
}

return cachedAssets.map(
(value: AssetValue) =>
new UncommittedAsset({
return Promise.all(
cachedAssets.map(async (value: AssetValue) => {
let content =
value.contentKey != null
? this.options.cache.getStream(value.contentKey)
: null;
let mapBuffer =
value.astKey != null
? await this.options.cache.getBlob<Buffer>(value.astKey)
: null;
let ast =
value.astKey != null
? await this.options.cache.getBlob<AST>(value.astKey)
: null;
return new UncommittedAsset({
value,
options: this.options,
}),
content,
mapBuffer,
ast,
});
}),
);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/src/UncommittedAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
} from './types';

import v8 from 'v8';
import invariant from 'assert';
import {Readable} from 'stream';
import SourceMap from '@parcel/source-map';
import {
Expand Down Expand Up @@ -179,7 +180,7 @@ export default class UncommittedAsset {
return (await this.content).toString();
}

return '';
invariant(false, 'Internal error: missing content');
}

async getBuffer(): Promise<Buffer> {
Expand Down
94 changes: 93 additions & 1 deletion packages/core/integration-tests/test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let inputDir: string;

function runBundle(entries = 'src/index.js', opts) {
entries = (Array.isArray(entries) ? entries : [entries]).map(entry =>
path.join(inputDir, entry),
path.resolve(inputDir, entry),
);

return bundler(entries, {
Expand Down Expand Up @@ -2636,4 +2636,96 @@ describe('cache', function() {
assert.equal(bundles.length, 4);
});
});

it('should correctly read additional child assets from cache', async function() {
await ncp(
path.join(__dirname, '/integration/postcss-modules-cjs'),
path.join(inputDir),
);

let entries = 'index.js';

let b = await runBundle(entries, {minify: false});
let result1 = (await run(b.bundleGraph))();

b = await runBundle(entries, {minify: true});
let result2 = (await run(b.bundleGraph))();

b = await runBundle(entries, {minify: false});
let result3 = (await run(b.bundleGraph))();

assert(typeof result1 === 'string' && result1.includes('foo'));
assert.strictEqual(result1, result2);
assert.strictEqual(result1, result3);
});

it('should correctly read additional child assets from cache 2', async function() {
await ncp(
path.join(__dirname, '/integration/postcss-modules-cjs'),
path.join(inputDir),
);

let entries = 'index.js';

await overlayFS.writeFile(
path.join(inputDir, 'foo.module.css'),
`.foo {
color: red;
}`,
);

let b = await runBundle(entries);
let result1 = (await run(b.bundleGraph))();

await overlayFS.writeFile(
path.join(inputDir, 'foo.module.css'),
`.foo {
color: blue;
}`,
);

b = await runBundle(entries);
let result2 = (await run(b.bundleGraph))();

await overlayFS.writeFile(
path.join(inputDir, 'foo.module.css'),
`.foo {
color: red;
}`,
);

b = await runBundle(entries);
let result3 = (await run(b.bundleGraph))();

assert(typeof result1 === 'string' && result1.includes('foo'));
assert.strictEqual(result1, result2);
assert.strictEqual(result1, result3);
});

it('should correctly reuse intermediate pipeline results when transforming', async function() {
await ncp(path.join(__dirname, '/integration/json'), path.join(inputDir));

let entry = path.join(inputDir, 'index.js');
let original = await overlayFS.readFile(entry, 'utf8');

let b = await runBundle(entry);
let result1 = (await run(b.bundleGraph))();

await overlayFS.writeFile(
entry,
'module.exports = function(){ return 10; }',
);

b = await runBundle(entry);
let result2 = (await run(b.bundleGraph))();

await overlayFS.writeFile(entry, original);

b = await runBundle(entry);
let result3 = (await run(b.bundleGraph))();

assert.strictEqual(result1, 3);
assert.strictEqual(result2, 10);
assert.strictEqual(result3, 3);
});
});

This file was deleted.

0 comments on commit 0ed50b5

Please sign in to comment.