This commit is contained in:
Rich Harris
2018-05-03 23:30:09 -04:00
parent f4eac2515f
commit 4fa5ed5e2c

View File

@@ -21,18 +21,11 @@ prog.command('dev')
prog.command('build [dest]')
.describe('Create a production-ready version of your app')
.option('-l, --launcher', 'Create a launcher file (defaults to start.js)')
.option('-p, --port', 'Default port, if using --launcher')
.example(`build -l`)
.example(`build custom-dir -l custom-launcher.js -p 4567`)
.action(async (dest = 'build', opts: { launcher: string | true, port: string }) => {
.option('-p, --port', 'Default of process.env.PORT', '3000')
.example(`build custom-dir -p 4567`)
.action(async (dest = 'build', opts: { port: string }) => {
console.log(`> Building...`);
if (opts.port && !opts.launcher) {
console.error(`${clorox.bold.red(`You can only use the --port option with --launcher`)}`);
process.exit(1);
}
process.env.NODE_ENV = 'production';
process.env.SAPPER_DEST = dest;
@@ -42,31 +35,19 @@ prog.command('build [dest]')
const { build } = await import('./cli/build');
await build();
let cmd;
const launcher = path.resolve(dest, 'index.js');
if (opts.launcher) {
const launcher = opts.launcher === true ? path.join(dest, 'index.js') : opts.launcher;
const resolvedLauncher = path.resolve(launcher);
const resolvedServer = path.resolve(dest, 'server.js');
const pathToServer = path.relative(path.dirname(resolvedLauncher), resolvedServer);
const pathToDest = path.relative(path.dirname(resolvedLauncher), dest);
fs.writeFileSync(launcher, `
// generated by sapper build at ${new Date().toISOString()}
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
process.env.SAPPER_DEST = __dirname;
process.env.PORT = process.env.PORT || ${opts.port || 3000};
fs.writeFileSync(resolvedLauncher, `
// generated by sapper build at ${new Date().toISOString()}
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
process.env.SAPPER_DEST = ${pathToDest ? `require('path').resolve(__dirname, '${pathToDest}')` : '__dirname'};
process.env.PORT = process.env.PORT || ${opts.port || 3000};
console.log('Starting server on port ' + process.env.PORT);
require('./server.js');
`.replace(/^\t+/gm, '').trim());
console.log('Starting server on port ' + process.env.PORT);
require('${pathToServer[0] === '.' ? pathToServer : `./${pathToServer}`}');
`.replace(/^\t+/gm, '').trim());
cmd = `node ${path.relative(process.cwd(), launcher).replace(/[\/\\]index.js$/, '')}`;
} else {
cmd = dest === 'build' ? 'npx sapper start' : `npx sapper start ${dest}`;
}
console.error(`\n> Finished in ${elapsed(start)}. Type ${clorox.bold.cyan(cmd)} to run the app.`);
console.error(`\n> Finished in ${elapsed(start)}. Type ${clorox.bold.cyan(`node ${dest}`)} to run the app.`);
} catch (err) {
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
}