Skip to content
This repository has been archived by the owner on May 1, 2022. It is now read-only.

Commit

Permalink
Pass through snowpack log colors and show stats during production bui…
Browse files Browse the repository at this point in the history
…lds (#56)

* Pass through snowpack log colors to stdio

* Upgrade snowpack and show stats during production builds

* Always exit if initial build fails, even in dev mode

* Upgrade to snowpack v1.5.1
  • Loading branch information
jacobdeichert authored Mar 2, 2020
1 parent a8389df commit b2aaea1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"p-limit": "^2.2.2",
"rimraf": "^3.0.2",
"servor": "^3.1.0",
"snowpack": "1.4.0",
"snowpack": "1.5.1",
"terser": "^4.6.3"
},
"devDependencies": {
Expand Down
45 changes: 29 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-console */
import * as util from 'util';
import { exec as execSync } from 'child_process';
import { spawn } from 'child_process';
import { promises as fs, existsSync } from 'fs';
import * as path from 'path';
import * as svelte from 'svelte/compiler';
Expand All @@ -14,7 +13,6 @@ import servor from 'servor';
import rimraf from 'rimraf';
import { init as initEsModuleLexer, parse } from 'es-module-lexer';
import throttle from 'lodash.throttle';
const exec = util.promisify(execSync);

const IS_PRODUCTION_MODE = process.env.NODE_ENV === 'production';
const BABEL_CONFIG = loadBabelConfig();
Expand Down Expand Up @@ -170,7 +168,7 @@ async function transform(
await snowpack(destPath);
} catch (err) {
console.error('\n\nFailed to build with snowpack');
console.error(err.stderr || err);
err && console.error(err.stderr || err);
// Don't continue building...
return;
}
Expand Down Expand Up @@ -238,22 +236,38 @@ async function checkForNewWebModules(
// in dev mode, snowpack will be ran again.
async function snowpack(includeFiles: string): Promise<void> {
const maybeOptimize = IS_PRODUCTION_MODE ? '--optimize' : '';
const maybeStats = IS_PRODUCTION_MODE ? '--stat' : '';

console.info(`\nBuilding web_modules with snowpack...`);
console.info(`\n\nBuilding web_modules with snowpack...`);

const snowpackLocation = path.resolve(
require.resolve('snowpack'),
'../index.bin.js'
);

const { stdout, stderr } = await exec(
`node ${snowpackLocation} --include '${includeFiles}' --dest dist/web_modules ${maybeOptimize}`
);

// TODO: hide behind --verbose flag
// Show any output from snowpack...
stdout && console.info(stdout);
stderr && console.info(stderr);
await new Promise((resolve, reject) => {
const proc = spawn(
'node',
[
snowpackLocation,
'--include',
includeFiles,
'--dest',
'dist/web_modules',
maybeOptimize,
maybeStats,
],
{
// Inherit so snowpack's log coloring is passed through
stdio: 'inherit',
}
);
proc.on('exit', (code: number) => {
if (code > 0) return reject();
resolve();
});
});
console.log('\n'); // Just add some spacing...
}

async function initialBuild(): Promise<void> {
Expand Down Expand Up @@ -294,10 +308,9 @@ async function initialBuild(): Promise<void> {
await snowpack('dist/**/*');
} catch (err) {
console.error('\n\nFailed to build with snowpack');
console.error(err.stderr || err);
err && console.error(err.stderr || err);
// Don't continue building...
if (IS_PRODUCTION_MODE) process.exit(1);
return;
process.exit(1);
}

// Transform all generated js files with babel.
Expand Down

0 comments on commit b2aaea1

Please sign in to comment.