Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 0b76f12

Browse files
authored
Merge pull request #469 from sveltejs/remove-env-vars
replace magic env vars with documented CLI flags
2 parents 52f40f9 + 6ae9a5e commit 0b76f12

File tree

4 files changed

+95
-39
lines changed

4 files changed

+95
-39
lines changed

src/api/build.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,34 @@ type Opts = {
1616
routes?: string;
1717
dest?: string;
1818
output?: string;
19-
static_files?: string;
19+
static?: string;
2020
legacy?: boolean;
2121
bundler?: 'rollup' | 'webpack';
2222
oncompile?: ({ type, result }: { type: string, result: CompileResult }) => void;
2323
};
2424

2525
export async function build({
26-
cwd = process.cwd(),
27-
src = path.join(cwd, 'src'),
28-
routes = path.join(cwd, 'src/routes'),
29-
output = path.join(cwd, '__sapper__'),
30-
static_files = path.join(cwd, 'static'),
31-
dest = path.join(cwd, '__sapper__/build'),
26+
cwd,
27+
src = 'src',
28+
routes = 'src/routes',
29+
output = '__sapper__',
30+
static: static_files = 'static',
31+
dest = '__sapper__/build',
3232

3333
bundler,
3434
legacy = false,
3535
oncompile = noop
3636
}: Opts = {}) {
3737
bundler = validate_bundler(bundler);
3838

39+
cwd = path.resolve(cwd);
40+
src = path.resolve(cwd, src);
41+
dest = path.resolve(cwd, dest);
42+
routes = path.resolve(cwd, routes);
43+
output = path.resolve(cwd, output);
44+
static_files = path.resolve(cwd, static_files);
45+
dest = path.resolve(cwd, dest);
46+
3947
if (legacy && bundler === 'webpack') {
4048
throw new Error(`Legacy builds are not supported for projects using webpack`);
4149
}

src/api/dev.ts

+20-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Opts = {
2222
dest?: string,
2323
routes?: string,
2424
output?: string,
25-
static_files?: string,
25+
static?: string,
2626
'dev-port'?: number,
2727
live?: boolean,
2828
hot?: boolean,
@@ -43,7 +43,7 @@ class Watcher extends EventEmitter {
4343
dest: string;
4444
routes: string;
4545
output: string;
46-
static_files: string;
46+
static: string;
4747
}
4848
port: number;
4949
closed: boolean;
@@ -69,12 +69,12 @@ class Watcher extends EventEmitter {
6969
}
7070

7171
constructor({
72-
cwd = process.cwd(),
73-
src = path.join(cwd, 'src'),
74-
routes = path.join(cwd, 'src/routes'),
75-
output = path.join(cwd, '__sapper__'),
76-
static_files = path.join(cwd, 'static'),
77-
dest = path.join(cwd, '__sapper__/dev'),
72+
cwd = '.',
73+
src = 'src',
74+
routes = 'src/routes',
75+
output = '__sapper__',
76+
static: static_files = 'static',
77+
dest = '__sapper__/dev',
7878
'dev-port': dev_port,
7979
live,
8080
hot,
@@ -84,8 +84,18 @@ class Watcher extends EventEmitter {
8484
}: Opts) {
8585
super();
8686

87+
cwd = path.resolve(cwd);
88+
8789
this.bundler = validate_bundler(bundler);
88-
this.dirs = { cwd, src, dest, routes, output, static_files };
90+
this.dirs = {
91+
cwd,
92+
src: path.resolve(cwd, src),
93+
dest: path.resolve(cwd, dest),
94+
routes: path.resolve(cwd, routes),
95+
output: path.resolve(cwd, output),
96+
static: path.resolve(cwd, static_files)
97+
};
98+
8999
this.port = port;
90100
this.closed = false;
91101

@@ -133,7 +143,7 @@ class Watcher extends EventEmitter {
133143
this.port = await ports.find(3000);
134144
}
135145

136-
const { cwd, src, dest, routes, output, static_files } = this.dirs;
146+
const { cwd, src, dest, routes, output, static: static_files } = this.dirs;
137147
rimraf.sync(dest);
138148
mkdirp.sync(`${dest}/client`);
139149
if (this.bundler === 'rollup') copy_shimport(dest);

src/api/export.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,20 @@ type URL = url.UrlWithStringQuery;
2929
export { _export as export };
3030

3131
async function _export({
32-
cwd = process.cwd(),
33-
static: static_files = path.join(cwd, 'static'),
34-
build_dir = path.join(cwd, '__sapper__/build'),
32+
cwd,
33+
static: static_files = 'static',
34+
build_dir = '__sapper__/build',
35+
export_dir = '__sapper__/export',
3536
basepath = '',
36-
export_dir = path.join(cwd, '__sapper__/export', basepath),
3737
timeout = 5000,
3838
oninfo = noop,
3939
onfile = noop
4040
}: Opts = {}) {
41+
cwd = path.resolve(cwd);
42+
static_files = path.resolve(cwd, static_files);
43+
build_dir = path.resolve(cwd, build_dir);
44+
export_dir = path.resolve(cwd, export_dir, basepath);
45+
4146
// Prep output directory
4247
sander.rimrafSync(export_dir);
4348

src/cli.ts

+51-18
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,36 @@ prog.command('dev')
2525
.option('--hot', 'Use hot module replacement (requires webpack)', true)
2626
.option('--live', 'Reload on changes if not using --hot', true)
2727
.option('--bundler', 'Specify a bundler (rollup or webpack)')
28+
.option('--cwd', 'Current working directory', '.')
29+
.option('--src', 'Source directory', 'src')
30+
.option('--routes', 'Routes directory', 'src/routes')
31+
.option('--static', 'Static files directory', 'static')
32+
.option('--output', 'Sapper output directory', '__sapper__')
33+
.option('--build-dir', 'Development build directory', '__sapper__/dev')
2834
.action(async (opts: {
2935
port: number,
3036
open: boolean,
3137
'dev-port': number,
3238
live: boolean,
3339
hot: boolean,
34-
bundler?: 'rollup' | 'webpack'
40+
bundler?: 'rollup' | 'webpack',
41+
cwd: string,
42+
src: string,
43+
routes: string,
44+
static: string,
45+
output: string,
46+
'build-dir': string
3547
}) => {
36-
const cwd = path.resolve(process.env.SAPPER_BASE || '');
37-
3848
const { dev } = await import('./api/dev');
3949

4050
try {
4151
const watcher = dev({
42-
cwd,
52+
cwd: opts.cwd,
53+
src: opts.src,
54+
routes: opts.routes,
55+
static: opts.static,
56+
output: opts.output,
57+
dest: opts['build-dir'],
4358
port: opts.port,
4459
'dev-port': opts['dev-port'],
4560
live: opts.live,
@@ -125,18 +140,24 @@ prog.command('build [dest]')
125140
.option('-p, --port', 'Default of process.env.PORT', '3000')
126141
.option('--bundler', 'Specify a bundler (rollup or webpack, blank for auto)')
127142
.option('--legacy', 'Create separate legacy build')
143+
.option('--cwd', 'Current working directory', '.')
144+
.option('--src', 'Source directory', 'src')
145+
.option('--routes', 'Routes directory', 'src/routes')
146+
.option('--output', 'Sapper output directory', '__sapper__')
128147
.example(`build custom-dir -p 4567`)
129148
.action(async (dest = '__sapper__/build', opts: {
130149
port: string,
131150
legacy: boolean,
132-
bundler?: 'rollup' | 'webpack'
151+
bundler?: 'rollup' | 'webpack',
152+
cwd: string,
153+
src: string,
154+
routes: string,
155+
output: string
133156
}) => {
134157
console.log(`> Building...`);
135158

136-
const cwd = path.resolve(process.env.SAPPER_BASE || '');
137-
138159
try {
139-
await _build(opts.bundler, opts.legacy, cwd, dest);
160+
await _build(opts.bundler, opts.legacy, opts.cwd, opts.src, opts.routes, opts.output, dest);
140161

141162
const launcher = path.resolve(dest, 'index.js');
142163

@@ -159,33 +180,42 @@ prog.command('build [dest]')
159180
prog.command('export [dest]')
160181
.describe('Export your app as static files (if possible)')
161182
.option('--build', '(Re)build app before exporting', true)
162-
.option('--build-dir', 'Specify a custom temporary build directory', '__sapper__/build')
163183
.option('--basepath', 'Specify a base path')
164184
.option('--timeout', 'Milliseconds to wait for a page (--no-timeout to disable)', 5000)
165185
.option('--legacy', 'Create separate legacy build')
166186
.option('--bundler', 'Specify a bundler (rollup or webpack, blank for auto)')
187+
.option('--cwd', 'Current working directory', '.')
188+
.option('--src', 'Source directory', 'src')
189+
.option('--routes', 'Routes directory', 'src/routes')
190+
.option('--static', 'Static files directory', 'static')
191+
.option('--output', 'Sapper output directory', '__sapper__')
192+
.option('--build-dir', 'Intermediate build directory', '__sapper__/build')
167193
.action(async (dest = '__sapper__/export', opts: {
168194
build: boolean,
169195
legacy: boolean,
170196
bundler?: 'rollup' | 'webpack',
171-
'build-dir': string,
172197
basepath?: string,
173-
timeout: number | false
198+
timeout: number | false,
199+
cwd: string,
200+
src: string,
201+
routes: string,
202+
static: string,
203+
output: string,
204+
'build-dir': string,
174205
}) => {
175-
const cwd = path.resolve(process.env.SAPPER_BASE || '');
176-
177206
try {
178207
if (opts.build) {
179208
console.log(`> Building...`);
180-
await _build(opts.bundler, opts.legacy, cwd, opts['build-dir']);
209+
await _build(opts.bundler, opts.legacy, opts.cwd, opts.src, opts.routes, opts.output, opts['build-dir']);
181210
console.error(`\n> Built in ${elapsed(start)}`);
182211
}
183212

184213
const { export: _export } = await import('./api/export');
185214
const { default: pb } = await import('pretty-bytes');
186215

187216
await _export({
188-
static: path.resolve(cwd, process.env.SAPPER_STATIC || 'static'),
217+
cwd: opts.cwd,
218+
static: opts.static,
189219
build_dir: opts['build-dir'],
190220
export_dir: dest,
191221
basepath: opts.basepath,
@@ -221,6 +251,9 @@ async function _build(
221251
bundler: 'rollup' | 'webpack',
222252
legacy: boolean,
223253
cwd: string,
254+
src: string,
255+
routes: string,
256+
output: string,
224257
dest: string
225258
) {
226259
const { build } = await import('./api/build');
@@ -229,9 +262,9 @@ async function _build(
229262
bundler,
230263
legacy,
231264
cwd,
232-
src: path.resolve(cwd, process.env.SAPPER_SRC || 'src'),
233-
routes: path.resolve(cwd, process.env.SAPPER_ROUTES || 'src/routes'),
234-
dest: path.resolve(cwd, dest),
265+
src,
266+
routes,
267+
dest,
235268

236269
oncompile: event => {
237270
let banner = `built ${event.type}`;

0 commit comments

Comments
 (0)