diff --git a/packages/core/core/src/Transformation.js b/packages/core/core/src/Transformation.js index 064d01967c3..2e79c5e26e0 100644 --- a/packages/core/core/src/Transformation.js +++ b/packages/core/core/src/Transformation.js @@ -1,6 +1,7 @@ // @flow strict-local import type { + AST, FilePath, GenerateOutput, Transformer, @@ -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(value.astKey) + : null; + let ast = + value.astKey != null + ? await this.options.cache.getBlob(value.astKey) + : null; + return new UncommittedAsset({ value, options: this.options, - }), + content, + mapBuffer, + ast, + }); + }), ); } diff --git a/packages/core/core/src/UncommittedAsset.js b/packages/core/core/src/UncommittedAsset.js index bae4484a4bd..dbe30906f34 100644 --- a/packages/core/core/src/UncommittedAsset.js +++ b/packages/core/core/src/UncommittedAsset.js @@ -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 { @@ -179,7 +180,7 @@ export default class UncommittedAsset { return (await this.content).toString(); } - return ''; + invariant(false, 'Internal error: missing content'); } async getBuffer(): Promise { diff --git a/packages/core/integration-tests/test/cache.js b/packages/core/integration-tests/test/cache.js index 1cfb01e23cc..95607c5c30c 100644 --- a/packages/core/integration-tests/test/cache.js +++ b/packages/core/integration-tests/test/cache.js @@ -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, { @@ -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); + }); }); diff --git a/packages/core/integration-tests/test/integration/postcss-modules-cjs/foo.module.css.json b/packages/core/integration-tests/test/integration/postcss-modules-cjs/foo.module.css.json deleted file mode 100644 index bbf2cf11ba2..00000000000 --- a/packages/core/integration-tests/test/integration/postcss-modules-cjs/foo.module.css.json +++ /dev/null @@ -1 +0,0 @@ -{"foo":"_foo_1829j_1"} \ No newline at end of file