From be63ea7c9692b97229535546372811c9fda6898a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Mar 2018 13:29:39 -0400 Subject: [PATCH] add SAPPER_BASE and SAPPER_APP environment variables --- src/cli/build.ts | 4 ++-- src/cli/dev.ts | 4 ++-- src/cli/export.ts | 4 ++-- src/config.ts | 9 +++++++-- src/core/create_routes.ts | 4 ++-- src/middleware.ts | 6 +++--- src/webpack.ts | 8 ++++---- 7 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/cli/build.ts b/src/cli/build.ts index b9a8f08..b128714 100644 --- a/src/cli/build.ts +++ b/src/cli/build.ts @@ -4,10 +4,10 @@ import * as clorox from 'clorox'; import mkdirp from 'mkdirp'; import rimraf from 'rimraf'; import { create_compilers, create_main_manifests, create_routes, create_serviceworker_manifest } from '../core' -import { dest } from '../config'; +import { locations } from '../config'; export async function build() { - const output = dest(); + const output = locations.dest(); mkdirp.sync(output); rimraf.sync(path.join(output, '**/*')); diff --git a/src/cli/dev.ts b/src/cli/dev.ts index f936f38..9f1e096 100644 --- a/src/cli/dev.ts +++ b/src/cli/dev.ts @@ -9,7 +9,7 @@ import rimraf from 'rimraf'; import format_messages from 'webpack-format-messages'; import prettyMs from 'pretty-ms'; import * as ports from 'port-authority'; -import { dest } from '../config'; +import { locations } from '../config'; import { create_compilers, create_main_manifests, create_routes, create_serviceworker_manifest } from '../core'; type Deferred = { @@ -84,7 +84,7 @@ export async function dev(opts: { port: number }) { port = await ports.find(3000); } - const dir = dest(); + const dir = locations.dest(); rimraf.sync(dir); mkdirp.sync(dir); diff --git a/src/cli/export.ts b/src/cli/export.ts index 6f2efb6..91a5c80 100644 --- a/src/cli/export.ts +++ b/src/cli/export.ts @@ -5,10 +5,10 @@ import cheerio from 'cheerio'; import URL from 'url-parse'; import fetch from 'node-fetch'; import * as ports from 'port-authority'; -import { dest } from '../config'; +import { locations } from '../config'; export async function exporter(export_dir: string) { - const build_dir = dest(); + const build_dir = locations.dest(); // Prep output directory sander.rimrafSync(export_dir); diff --git a/src/config.ts b/src/config.ts index db59d02..8f98481 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,10 @@ import * as path from 'path'; export const dev = () => process.env.NODE_ENV !== 'production'; -export const src = () => path.resolve(process.env.SAPPER_ROUTES || 'routes'); -export const dest = () => path.resolve(process.env.SAPPER_DEST || '.sapper'); + +export const locations = { + base: () => path.resolve(process.env.SAPPER_BASE || ''), + app: () => path.resolve(process.env.SAPPER_BASE || '', process.env.SAPPER_APP || 'app'), + routes: () => path.resolve(process.env.SAPPER_BASE || '', process.env.SAPPER_ROUTES || 'routes'), + dest: () => path.resolve(process.env.SAPPER_BASE || '', process.env.SAPPER_DEST || '.sapper') +}; \ No newline at end of file diff --git a/src/core/create_routes.ts b/src/core/create_routes.ts index 0a247c0..133b865 100644 --- a/src/core/create_routes.ts +++ b/src/core/create_routes.ts @@ -1,9 +1,9 @@ import * as path from 'path'; import glob from 'glob'; -import { src } from '../config'; +import { locations } from '../config'; import { Route } from '../interfaces'; -export default function create_routes({ files } = { files: glob.sync('**/*.*', { cwd: src(), nodir: true }) }) { +export default function create_routes({ files } = { files: glob.sync('**/*.*', { cwd: locations.routes(), nodir: true }) }) { const routes: Route[] = files .map((file: string) => { if (/(^|\/|\\)_/.test(file)) return; diff --git a/src/middleware.ts b/src/middleware.ts index 8ed1542..184818f 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -7,7 +7,7 @@ import devalue from 'devalue'; import { lookup } from './middleware/mime'; import { minify_html } from './middleware/minify_html'; import { create_routes, create_compilers } from './core'; -import { dest, dev } from './config'; +import { locations, dev } from './config'; import { Route, Template } from './interfaces'; import sourceMapSupport from 'source-map-support'; @@ -41,7 +41,7 @@ interface Req extends ClientRequest { export default function middleware({ routes }: { routes: RouteObject[] }) { - const output = dest(); + const output = locations.dest(); const client_info = JSON.parse(fs.readFileSync(path.join(output, 'client_info.json'), 'utf-8')); @@ -81,7 +81,7 @@ function serve({ prefix, pathname, cache_control }: { ? (req: Req) => req.pathname === pathname : (req: Req) => req.pathname.startsWith(prefix); - const output = dest(); + const output = locations.dest(); const cache: Map = new Map(); diff --git a/src/webpack.ts b/src/webpack.ts index 7fd3e86..f20ea79 100644 --- a/src/webpack.ts +++ b/src/webpack.ts @@ -1,4 +1,4 @@ -import { dest, dev } from './config'; +import { locations, dev } from './config'; export default { dev: dev(), @@ -12,7 +12,7 @@ export default { output: () => { return { - path: `${dest()}/client`, + path: `${locations.dest()}/client`, filename: '[hash]/[name].js', chunkFilename: '[hash]/[name].[id].js', publicPath: '/client/' @@ -29,7 +29,7 @@ export default { output: () => { return { - path: dest(), + path: locations.dest(), filename: '[name].js', chunkFilename: '[hash]/[name].[id].js', libraryTarget: 'commonjs2' @@ -46,7 +46,7 @@ export default { output: () => { return { - path: dest(), + path: locations.dest(), filename: '[name].js', chunkFilename: '[name].[id].[hash].js' }