From 16237e56f991c222dac6e94d5025766aae6a2dba Mon Sep 17 00:00:00 2001 From: Anthony Gubler Date: Thu, 21 Mar 2019 17:13:00 +0000 Subject: [PATCH] Deal with assets that do not exist in the output (#258) --- src/logger.ts | 15 +++--- tests/fixtures/missing-assets/assetOne.js | 1 + tests/fixtures/missing-assets/manifest.json | 4 ++ tests/unit/logger.ts | 55 +++++++++++++++++++++ 4 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/missing-assets/assetOne.js create mode 100644 tests/fixtures/missing-assets/manifest.json diff --git a/src/logger.ts b/src/logger.ts index f28e6fd0..5d02e34a 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -21,12 +21,15 @@ export default function logger(stats: any, config: any, runningMessage: string = assets = Object.keys(manifestContent).map((item) => { const assetName = manifestContent[item]; const filePath = path.join(config.output.path, assetName); - const fileStats = fs.statSync(filePath); - const size = (fileStats.size / 1000).toFixed(2); - const assetInfo = `${assetName} ${chalk.yellow(`(${size}kb)`)}`; - const content = fs.readFileSync(filePath, 'utf8'); - const compressedSize = (gzipSize.sync(content) / 1000).toFixed(2); - return `${assetInfo} / ${chalk.blue(`(${compressedSize}kb gz)`)}`; + if (fs.existsSync(filePath)) { + const fileStats = fs.statSync(filePath); + const size = (fileStats.size / 1000).toFixed(2); + const assetInfo = `${assetName} ${chalk.yellow(`(${size}kb)`)}`; + const content = fs.readFileSync(filePath, 'utf8'); + const compressedSize = (gzipSize.sync(content) / 1000).toFixed(2); + return `${assetInfo} / ${chalk.blue(`(${compressedSize}kb gz)`)}`; + } + return ''; }); chunks = stats.chunks.map((chunk: any) => { diff --git a/tests/fixtures/missing-assets/assetOne.js b/tests/fixtures/missing-assets/assetOne.js new file mode 100644 index 00000000..1cd4470c --- /dev/null +++ b/tests/fixtures/missing-assets/assetOne.js @@ -0,0 +1 @@ +function myFunction() {} diff --git a/tests/fixtures/missing-assets/manifest.json b/tests/fixtures/missing-assets/manifest.json new file mode 100644 index 00000000..02ba075d --- /dev/null +++ b/tests/fixtures/missing-assets/manifest.json @@ -0,0 +1,4 @@ +{ + "assetOne.js": "assetOne.js", + "assetTwo.js": "assetTwo.js" +} diff --git a/tests/unit/logger.ts b/tests/unit/logger.ts index 9bbfea27..08ff136a 100644 --- a/tests/unit/logger.ts +++ b/tests/unit/logger.ts @@ -166,4 +166,59 @@ ${chalk.red('The build completed with errors.')} assert.isTrue(mockedLogUpdate.calledWith(expectedLog)); assert.isTrue(hasErrors); }); + + it('should skip assets that do not exist', () => { + const logger = mockModule.getModuleUnderTest().default; + const hasErrors = logger( + { + hash: 'hash', + assets: [ + { + name: 'assetOne.js', + size: 1000 + }, + { + name: 'assetTwo.js', + size: 1000 + } + ], + chunks: [ + { + names: ['chunkOne'] + } + ], + errors: [], + warnings: [] + }, + { + output: { + path: path.join(__dirname, '..', 'fixtures', 'missing-assets') + } + } + ); + + const expectedLog = ` +${logSymbols.info} cli-build-app: 9.9.9 +${logSymbols.info} typescript: 1.1.1 +${logSymbols.success} hash: hash +${logSymbols.error} errors: 0 +${logSymbols.warning} warnings: 0 +${''}${''} +${chalk.yellow('chunks:')} +${columns(['chunkOne'])} +${chalk.yellow('assets:')} +${columns([`assetOne.js ${chalk.yellow('(0.03kb)')} / ${chalk.blue('(0.04kb gz)')}`])} +${chalk.yellow( + `output at: ${chalk.cyan( + chalk.underline(`file:///${path.join(__dirname, '..', 'fixtures', 'missing-assets')}`) + )}` + )} + +${chalk.green('The build completed successfully.')} + `; + const mockedLogUpdate = mockModule.getMock('log-update').ctor; + assert.strictEqual(mockedLogUpdate.firstCall.args[0], expectedLog); + assert.isTrue(mockedLogUpdate.calledWith(expectedLog)); + assert.isFalse(hasErrors); + }); });