mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-12 19:25:10 +00:00
sapper build defaults to build dir, sapper export defaults to export dir (#133)
This commit is contained in:
@@ -2,18 +2,16 @@ import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import mkdirp from 'mkdirp';
|
||||
import rimraf from 'rimraf';
|
||||
import { create_compilers, create_app, create_routes, create_serviceworker } from 'sapper/core.js';
|
||||
import { create_compilers, create_app, create_routes, create_serviceworker } from 'sapper/core.js'
|
||||
import { src, dest, dev } from '../config';
|
||||
|
||||
export default async function build({ src, dest, dev, entry }: {
|
||||
src: string;
|
||||
dest: string;
|
||||
dev: boolean;
|
||||
entry: { client: string, server: string }
|
||||
}) {
|
||||
mkdirp.sync(dest);
|
||||
rimraf.sync(path.join(dest, '**/*'));
|
||||
export default async function build() {
|
||||
const output = dest();
|
||||
|
||||
const routes = create_routes({ src });
|
||||
mkdirp.sync(output);
|
||||
rimraf.sync(path.join(output, '**/*'));
|
||||
|
||||
const routes = create_routes();
|
||||
|
||||
// create app/manifest/client.js and app/manifest/server.js
|
||||
create_app({ routes, src, dev });
|
||||
@@ -21,7 +19,7 @@ export default async function build({ src, dest, dev, entry }: {
|
||||
const { client, server, serviceworker } = create_compilers();
|
||||
|
||||
const client_stats = await compile(client);
|
||||
fs.writeFileSync(path.join(dest, 'client_info.json'), JSON.stringify(client_stats.toJson()));
|
||||
fs.writeFileSync(path.join(output, 'client_info.json'), JSON.stringify(client_stats.toJson()));
|
||||
|
||||
await compile(server);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import * as http from 'http';
|
||||
import mkdirp from 'mkdirp';
|
||||
import rimraf from 'rimraf';
|
||||
import { wait_for_port } from './utils';
|
||||
import { dest } from '../config';
|
||||
import { create_compilers, create_app, create_routes, create_serviceworker, create_template } from 'sapper/core.js';
|
||||
|
||||
type Deferred = {
|
||||
@@ -66,7 +67,9 @@ function create_hot_update_server(port: number, interval = 10000) {
|
||||
return { send };
|
||||
}
|
||||
|
||||
export default async function dev(src: string, dir: string) {
|
||||
export default async function dev() {
|
||||
const dir = dest();
|
||||
|
||||
rimraf.sync(dir);
|
||||
mkdirp.sync(dir);
|
||||
|
||||
@@ -75,8 +78,8 @@ export default async function dev(src: string, dir: string) {
|
||||
// initial build
|
||||
const dev_port = await require('get-port')(10000);
|
||||
|
||||
const routes = create_routes({ src });
|
||||
create_app({ routes, src, dev: true, dev_port });
|
||||
const routes = create_routes();
|
||||
create_app({ routes, dev_port });
|
||||
|
||||
const hot_update_server = create_hot_update_server(dev_port);
|
||||
|
||||
@@ -94,8 +97,8 @@ export default async function dev(src: string, dir: string) {
|
||||
}
|
||||
|
||||
watch_files('routes/**/*.+(html|js|mjs)', () => {
|
||||
const routes = create_routes({ src });
|
||||
create_app({ routes, src, dev: true, dev_port });
|
||||
const routes = create_routes();
|
||||
create_app({ routes, dev_port });
|
||||
});
|
||||
|
||||
watch_files('app/template.html', () => {
|
||||
@@ -190,9 +193,8 @@ export default async function dev(src: string, dir: string) {
|
||||
});
|
||||
|
||||
create_serviceworker({
|
||||
routes: create_routes({ src }),
|
||||
client_files,
|
||||
src
|
||||
routes: create_routes(),
|
||||
client_files
|
||||
});
|
||||
|
||||
watch_serviceworker();
|
||||
|
||||
@@ -6,8 +6,7 @@ import cheerio from 'cheerio';
|
||||
import URL from 'url-parse';
|
||||
import fetch from 'node-fetch';
|
||||
import { wait_for_port } from './utils';
|
||||
|
||||
const { OUTPUT_DIR = 'dist' } = process.env;
|
||||
import { dest } from '../config';
|
||||
|
||||
const app = express();
|
||||
|
||||
@@ -15,19 +14,24 @@ function read_json(file: string) {
|
||||
return JSON.parse(sander.readFileSync(file, { encoding: 'utf-8' }));
|
||||
}
|
||||
|
||||
export default async function exporter(dir: string) { // dir === '.sapper'
|
||||
// Prep output directory
|
||||
sander.rimrafSync(OUTPUT_DIR);
|
||||
export default async function exporter(export_dir: string) {
|
||||
const build_dir = dest();
|
||||
|
||||
sander.copydirSync('assets').to(OUTPUT_DIR);
|
||||
sander.copydirSync(dir, 'client').to(OUTPUT_DIR, 'client');
|
||||
sander.copyFileSync(dir, 'service-worker.js').to(OUTPUT_DIR, 'service-worker.js');
|
||||
// Prep output directory
|
||||
sander.rimrafSync(export_dir);
|
||||
|
||||
sander.copydirSync('assets').to(export_dir);
|
||||
sander.copydirSync(build_dir, 'client').to(export_dir, 'client');
|
||||
|
||||
if (sander.existsSync(build_dir, 'service-worker.js')) {
|
||||
sander.copyFileSync(build_dir, 'service-worker.js').to(export_dir, 'service-worker.js');
|
||||
}
|
||||
|
||||
const port = await require('get-port')(3000);
|
||||
|
||||
const origin = `http://localhost:${port}`;
|
||||
|
||||
const proc = child_process.fork(path.resolve(`${dir}/server.js`), [], {
|
||||
const proc = child_process.fork(path.resolve(`${build_dir}/server.js`), [], {
|
||||
cwd: process.cwd(),
|
||||
env: {
|
||||
PORT: port,
|
||||
@@ -48,11 +52,11 @@ export default async function exporter(dir: string) { // dir === '.sapper'
|
||||
saved.add(url.pathname);
|
||||
|
||||
if (message.type === 'text/html') {
|
||||
const dest = `${OUTPUT_DIR}/${url.pathname}/index.html`;
|
||||
sander.writeFileSync(dest, message.body);
|
||||
const file = `${export_dir}/${url.pathname}/index.html`;
|
||||
sander.writeFileSync(file, message.body);
|
||||
} else {
|
||||
const dest = `${OUTPUT_DIR}/${url.pathname}`;
|
||||
sander.writeFileSync(dest, message.body);
|
||||
const file = `${export_dir}/${url.pathname}`;
|
||||
sander.writeFileSync(file, message.body);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import build from './build';
|
||||
import exporter from './export';
|
||||
import dev from './dev';
|
||||
import upgrade from './upgrade';
|
||||
import { dest, entry, src } from '../config';
|
||||
import * as pkg from '../../package.json';
|
||||
|
||||
const opts = mri(process.argv.slice(2), {
|
||||
@@ -31,8 +30,9 @@ const start = Date.now();
|
||||
|
||||
if (cmd === 'build') {
|
||||
process.env.NODE_ENV = 'production';
|
||||
process.env.SAPPER_DEST = opts._[1] || 'build';
|
||||
|
||||
build({ dest, dev: false, entry, src })
|
||||
build()
|
||||
.then(() => {
|
||||
const elapsed = Date.now() - start;
|
||||
console.error(`built in ${elapsed}ms`); // TODO beautify this, e.g. 'built in 4.7 seconds'
|
||||
@@ -43,8 +43,10 @@ if (cmd === 'build') {
|
||||
} else if (cmd === 'export') {
|
||||
process.env.NODE_ENV = 'production';
|
||||
|
||||
build({ dest, dev: false, entry, src })
|
||||
.then(() => exporter(dest))
|
||||
const export_dir = opts._[1] || 'export';
|
||||
|
||||
build()
|
||||
.then(() => exporter(export_dir))
|
||||
.then(() => {
|
||||
const elapsed = Date.now() - start;
|
||||
console.error(`extracted in ${elapsed}ms`); // TODO beautify this, e.g. 'built in 4.7 seconds'
|
||||
@@ -53,7 +55,7 @@ if (cmd === 'build') {
|
||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||
});
|
||||
} else if (cmd === 'dev') {
|
||||
dev(src, dest);
|
||||
dev();
|
||||
} else if (cmd === 'upgrade') {
|
||||
upgrade();
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user