Merge pull request #469 from sveltejs/remove-env-vars

replace magic env vars with documented CLI flags
This commit is contained in:
Rich Harris
2018-10-09 14:59:30 -04:00
committed by GitHub
4 changed files with 95 additions and 39 deletions

View File

@@ -16,19 +16,19 @@ type Opts = {
routes?: string; routes?: string;
dest?: string; dest?: string;
output?: string; output?: string;
static_files?: string; static?: string;
legacy?: boolean; legacy?: boolean;
bundler?: 'rollup' | 'webpack'; bundler?: 'rollup' | 'webpack';
oncompile?: ({ type, result }: { type: string, result: CompileResult }) => void; oncompile?: ({ type, result }: { type: string, result: CompileResult }) => void;
}; };
export async function build({ export async function build({
cwd = process.cwd(), cwd,
src = path.join(cwd, 'src'), src = 'src',
routes = path.join(cwd, 'src/routes'), routes = 'src/routes',
output = path.join(cwd, '__sapper__'), output = '__sapper__',
static_files = path.join(cwd, 'static'), static: static_files = 'static',
dest = path.join(cwd, '__sapper__/build'), dest = '__sapper__/build',
bundler, bundler,
legacy = false, legacy = false,
@@ -36,6 +36,14 @@ export async function build({
}: Opts = {}) { }: Opts = {}) {
bundler = validate_bundler(bundler); bundler = validate_bundler(bundler);
cwd = path.resolve(cwd);
src = path.resolve(cwd, src);
dest = path.resolve(cwd, dest);
routes = path.resolve(cwd, routes);
output = path.resolve(cwd, output);
static_files = path.resolve(cwd, static_files);
dest = path.resolve(cwd, dest);
if (legacy && bundler === 'webpack') { if (legacy && bundler === 'webpack') {
throw new Error(`Legacy builds are not supported for projects using webpack`); throw new Error(`Legacy builds are not supported for projects using webpack`);
} }

View File

@@ -22,7 +22,7 @@ type Opts = {
dest?: string, dest?: string,
routes?: string, routes?: string,
output?: string, output?: string,
static_files?: string, static?: string,
'dev-port'?: number, 'dev-port'?: number,
live?: boolean, live?: boolean,
hot?: boolean, hot?: boolean,
@@ -43,7 +43,7 @@ class Watcher extends EventEmitter {
dest: string; dest: string;
routes: string; routes: string;
output: string; output: string;
static_files: string; static: string;
} }
port: number; port: number;
closed: boolean; closed: boolean;
@@ -69,12 +69,12 @@ class Watcher extends EventEmitter {
} }
constructor({ constructor({
cwd = process.cwd(), cwd = '.',
src = path.join(cwd, 'src'), src = 'src',
routes = path.join(cwd, 'src/routes'), routes = 'src/routes',
output = path.join(cwd, '__sapper__'), output = '__sapper__',
static_files = path.join(cwd, 'static'), static: static_files = 'static',
dest = path.join(cwd, '__sapper__/dev'), dest = '__sapper__/dev',
'dev-port': dev_port, 'dev-port': dev_port,
live, live,
hot, hot,
@@ -84,8 +84,18 @@ class Watcher extends EventEmitter {
}: Opts) { }: Opts) {
super(); super();
cwd = path.resolve(cwd);
this.bundler = validate_bundler(bundler); this.bundler = validate_bundler(bundler);
this.dirs = { cwd, src, dest, routes, output, static_files }; this.dirs = {
cwd,
src: path.resolve(cwd, src),
dest: path.resolve(cwd, dest),
routes: path.resolve(cwd, routes),
output: path.resolve(cwd, output),
static: path.resolve(cwd, static_files)
};
this.port = port; this.port = port;
this.closed = false; this.closed = false;
@@ -133,7 +143,7 @@ class Watcher extends EventEmitter {
this.port = await ports.find(3000); this.port = await ports.find(3000);
} }
const { cwd, src, dest, routes, output, static_files } = this.dirs; const { cwd, src, dest, routes, output, static: static_files } = this.dirs;
rimraf.sync(dest); rimraf.sync(dest);
mkdirp.sync(`${dest}/client`); mkdirp.sync(`${dest}/client`);
if (this.bundler === 'rollup') copy_shimport(dest); if (this.bundler === 'rollup') copy_shimport(dest);

View File

@@ -29,15 +29,20 @@ type URL = url.UrlWithStringQuery;
export { _export as export }; export { _export as export };
async function _export({ async function _export({
cwd = process.cwd(), cwd,
static: static_files = path.join(cwd, 'static'), static: static_files = 'static',
build_dir = path.join(cwd, '__sapper__/build'), build_dir = '__sapper__/build',
export_dir = '__sapper__/export',
basepath = '', basepath = '',
export_dir = path.join(cwd, '__sapper__/export', basepath),
timeout = 5000, timeout = 5000,
oninfo = noop, oninfo = noop,
onfile = noop onfile = noop
}: Opts = {}) { }: Opts = {}) {
cwd = path.resolve(cwd);
static_files = path.resolve(cwd, static_files);
build_dir = path.resolve(cwd, build_dir);
export_dir = path.resolve(cwd, export_dir, basepath);
// Prep output directory // Prep output directory
sander.rimrafSync(export_dir); sander.rimrafSync(export_dir);

View File

@@ -25,21 +25,36 @@ prog.command('dev')
.option('--hot', 'Use hot module replacement (requires webpack)', true) .option('--hot', 'Use hot module replacement (requires webpack)', true)
.option('--live', 'Reload on changes if not using --hot', true) .option('--live', 'Reload on changes if not using --hot', true)
.option('--bundler', 'Specify a bundler (rollup or webpack)') .option('--bundler', 'Specify a bundler (rollup or webpack)')
.option('--cwd', 'Current working directory', '.')
.option('--src', 'Source directory', 'src')
.option('--routes', 'Routes directory', 'src/routes')
.option('--static', 'Static files directory', 'static')
.option('--output', 'Sapper output directory', '__sapper__')
.option('--build-dir', 'Development build directory', '__sapper__/dev')
.action(async (opts: { .action(async (opts: {
port: number, port: number,
open: boolean, open: boolean,
'dev-port': number, 'dev-port': number,
live: boolean, live: boolean,
hot: boolean, hot: boolean,
bundler?: 'rollup' | 'webpack' bundler?: 'rollup' | 'webpack',
cwd: string,
src: string,
routes: string,
static: string,
output: string,
'build-dir': string
}) => { }) => {
const cwd = path.resolve(process.env.SAPPER_BASE || '');
const { dev } = await import('./api/dev'); const { dev } = await import('./api/dev');
try { try {
const watcher = dev({ const watcher = dev({
cwd, cwd: opts.cwd,
src: opts.src,
routes: opts.routes,
static: opts.static,
output: opts.output,
dest: opts['build-dir'],
port: opts.port, port: opts.port,
'dev-port': opts['dev-port'], 'dev-port': opts['dev-port'],
live: opts.live, live: opts.live,
@@ -125,18 +140,24 @@ prog.command('build [dest]')
.option('-p, --port', 'Default of process.env.PORT', '3000') .option('-p, --port', 'Default of process.env.PORT', '3000')
.option('--bundler', 'Specify a bundler (rollup or webpack, blank for auto)') .option('--bundler', 'Specify a bundler (rollup or webpack, blank for auto)')
.option('--legacy', 'Create separate legacy build') .option('--legacy', 'Create separate legacy build')
.option('--cwd', 'Current working directory', '.')
.option('--src', 'Source directory', 'src')
.option('--routes', 'Routes directory', 'src/routes')
.option('--output', 'Sapper output directory', '__sapper__')
.example(`build custom-dir -p 4567`) .example(`build custom-dir -p 4567`)
.action(async (dest = '__sapper__/build', opts: { .action(async (dest = '__sapper__/build', opts: {
port: string, port: string,
legacy: boolean, legacy: boolean,
bundler?: 'rollup' | 'webpack' bundler?: 'rollup' | 'webpack',
cwd: string,
src: string,
routes: string,
output: string
}) => { }) => {
console.log(`> Building...`); console.log(`> Building...`);
const cwd = path.resolve(process.env.SAPPER_BASE || '');
try { try {
await _build(opts.bundler, opts.legacy, cwd, dest); await _build(opts.bundler, opts.legacy, opts.cwd, opts.src, opts.routes, opts.output, dest);
const launcher = path.resolve(dest, 'index.js'); const launcher = path.resolve(dest, 'index.js');
@@ -159,25 +180,33 @@ prog.command('build [dest]')
prog.command('export [dest]') prog.command('export [dest]')
.describe('Export your app as static files (if possible)') .describe('Export your app as static files (if possible)')
.option('--build', '(Re)build app before exporting', true) .option('--build', '(Re)build app before exporting', true)
.option('--build-dir', 'Specify a custom temporary build directory', '__sapper__/build')
.option('--basepath', 'Specify a base path') .option('--basepath', 'Specify a base path')
.option('--timeout', 'Milliseconds to wait for a page (--no-timeout to disable)', 5000) .option('--timeout', 'Milliseconds to wait for a page (--no-timeout to disable)', 5000)
.option('--legacy', 'Create separate legacy build') .option('--legacy', 'Create separate legacy build')
.option('--bundler', 'Specify a bundler (rollup or webpack, blank for auto)') .option('--bundler', 'Specify a bundler (rollup or webpack, blank for auto)')
.option('--cwd', 'Current working directory', '.')
.option('--src', 'Source directory', 'src')
.option('--routes', 'Routes directory', 'src/routes')
.option('--static', 'Static files directory', 'static')
.option('--output', 'Sapper output directory', '__sapper__')
.option('--build-dir', 'Intermediate build directory', '__sapper__/build')
.action(async (dest = '__sapper__/export', opts: { .action(async (dest = '__sapper__/export', opts: {
build: boolean, build: boolean,
legacy: boolean, legacy: boolean,
bundler?: 'rollup' | 'webpack', bundler?: 'rollup' | 'webpack',
'build-dir': string,
basepath?: string, basepath?: string,
timeout: number | false timeout: number | false,
cwd: string,
src: string,
routes: string,
static: string,
output: string,
'build-dir': string,
}) => { }) => {
const cwd = path.resolve(process.env.SAPPER_BASE || '');
try { try {
if (opts.build) { if (opts.build) {
console.log(`> Building...`); console.log(`> Building...`);
await _build(opts.bundler, opts.legacy, cwd, opts['build-dir']); await _build(opts.bundler, opts.legacy, opts.cwd, opts.src, opts.routes, opts.output, opts['build-dir']);
console.error(`\n> Built in ${elapsed(start)}`); console.error(`\n> Built in ${elapsed(start)}`);
} }
@@ -185,7 +214,8 @@ prog.command('export [dest]')
const { default: pb } = await import('pretty-bytes'); const { default: pb } = await import('pretty-bytes');
await _export({ await _export({
static: path.resolve(cwd, process.env.SAPPER_STATIC || 'static'), cwd: opts.cwd,
static: opts.static,
build_dir: opts['build-dir'], build_dir: opts['build-dir'],
export_dir: dest, export_dir: dest,
basepath: opts.basepath, basepath: opts.basepath,
@@ -221,6 +251,9 @@ async function _build(
bundler: 'rollup' | 'webpack', bundler: 'rollup' | 'webpack',
legacy: boolean, legacy: boolean,
cwd: string, cwd: string,
src: string,
routes: string,
output: string,
dest: string dest: string
) { ) {
const { build } = await import('./api/build'); const { build } = await import('./api/build');
@@ -229,9 +262,9 @@ async function _build(
bundler, bundler,
legacy, legacy,
cwd, cwd,
src: path.resolve(cwd, process.env.SAPPER_SRC || 'src'), src,
routes: path.resolve(cwd, process.env.SAPPER_ROUTES || 'src/routes'), routes,
dest: path.resolve(cwd, dest), dest,
oncompile: event => { oncompile: event => {
let banner = `built ${event.type}`; let banner = `built ${event.type}`;