Skip to content

Commit

Permalink
Deal with assets that do not exist in the output (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
agubler authored Mar 21, 2019
1 parent 83bd45e commit 16237e5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/missing-assets/assetOne.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function myFunction() {}
4 changes: 4 additions & 0 deletions tests/fixtures/missing-assets/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"assetOne.js": "assetOne.js",
"assetTwo.js": "assetTwo.js"
}
55 changes: 55 additions & 0 deletions tests/unit/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 16237e5

Please sign in to comment.