mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-12 03:05:12 +00:00
lazy-load stuff
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,6 +7,8 @@ test/app/.sapper
|
||||
test/app/app/manifest
|
||||
test/app/export
|
||||
test/app/build
|
||||
*.js
|
||||
*.js.map
|
||||
*.ts.js
|
||||
*.ts.js.map
|
||||
!rollup.config.js
|
||||
12
chunk1.js
12
chunk1.js
@@ -1,12 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
var path = require('path');
|
||||
|
||||
var dev = function () { return process.env.NODE_ENV !== 'production'; };
|
||||
var src = function () { return path.resolve(process.env.SAPPER_ROUTES || 'routes'); };
|
||||
var dest = function () { return path.resolve(process.env.SAPPER_DEST || '.sapper'); };
|
||||
|
||||
exports.dev = dev;
|
||||
exports.src = src;
|
||||
exports.dest = dest;
|
||||
//# sourceMappingURL=./chunk1.js.map
|
||||
@@ -1 +0,0 @@
|
||||
{"version":3,"file":"chunk1.js","sources":["src/config.ts"],"sourcesContent":["import * as path from 'path';\n\nexport const dev = () => process.env.NODE_ENV !== 'production';\nexport const src = () => path.resolve(process.env.SAPPER_ROUTES || 'routes');\nexport const dest = () => path.resolve(process.env.SAPPER_DEST || '.sapper');\n"],"names":["path.resolve"],"mappings":";;;;IAEa,GAAG,GAAG,cAAM,OAAA,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,GAAA,CAAC;AAC/D,IAAa,GAAG,GAAG,cAAM,OAAAA,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,IAAI,QAAQ,CAAC,GAAA,CAAC;AAC7E,IAAa,IAAI,GAAG,cAAM,OAAAA,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,SAAS,CAAC,GAAA;;;;;;"}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -4,7 +4,7 @@
|
||||
"description": "Military-grade apps, engineered by Svelte",
|
||||
"main": "middleware.js",
|
||||
"bin": {
|
||||
"sapper": "sapper"
|
||||
"sapper": "./sapper"
|
||||
},
|
||||
"files": [
|
||||
"*.js",
|
||||
|
||||
96
src/cli.ts
96
src/cli.ts
@@ -4,11 +4,7 @@ import * as child_process from 'child_process';
|
||||
import sade from 'sade';
|
||||
import * as clorox from 'clorox';
|
||||
import prettyMs from 'pretty-ms';
|
||||
import help from './cli/help.md';
|
||||
import build from './cli/build';
|
||||
import exporter from './cli/export';
|
||||
import dev from './cli/dev';
|
||||
import upgrade from './cli/upgrade';
|
||||
// import upgrade from './cli/upgrade';
|
||||
import * as ports from 'port-authority';
|
||||
import * as pkg from '../package.json';
|
||||
|
||||
@@ -18,23 +14,13 @@ prog.command('dev')
|
||||
.describe('Start a development server')
|
||||
.option('-p, --port', 'Specify a port')
|
||||
.action(async (opts: { port: number }) => {
|
||||
let port = opts.port || +process.env.PORT;
|
||||
|
||||
if (port) {
|
||||
if (!await ports.check(port)) {
|
||||
console.log(clorox.bold.red(`> Port ${port} is unavailable`));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
port = await ports.find(3000);
|
||||
}
|
||||
|
||||
dev(port);
|
||||
const { dev } = await import('./cli/dev');
|
||||
dev(opts);
|
||||
});
|
||||
|
||||
prog.command('build [dest]')
|
||||
.describe('Create a production-ready version of your app')
|
||||
.action((dest = 'build') => {
|
||||
.action(async (dest = 'build') => {
|
||||
console.log(`> Building...`);
|
||||
|
||||
process.env.NODE_ENV = 'production';
|
||||
@@ -42,52 +28,26 @@ prog.command('build [dest]')
|
||||
|
||||
const start = Date.now();
|
||||
|
||||
build()
|
||||
.then(() => {
|
||||
const elapsed = Date.now() - start;
|
||||
console.error(`\n> Finished in ${prettyMs(elapsed)}. Type ${clorox.bold.cyan(dest === 'build' ? 'npx sapper start' : `npx sapper start ${dest}`)} to run the app.`);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||
});
|
||||
try {
|
||||
const { build } = await import('./cli/build');
|
||||
await build();
|
||||
console.error(`\n> Finished in ${elapsed(start)}. Type ${clorox.bold.cyan(dest === 'build' ? 'npx sapper start' : `npx sapper start ${dest}`)} to run the app.`);
|
||||
} catch (err) {
|
||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||
}
|
||||
});
|
||||
|
||||
prog.command('start [dir]')
|
||||
.describe('Start your app')
|
||||
.option('-p, --port', 'Specify a port')
|
||||
.action(async (dir = 'build', opts: { port: number }) => {
|
||||
let port = opts.port || +process.env.PORT;
|
||||
|
||||
const resolved = path.resolve(dir);
|
||||
const server = path.resolve(dir, 'server.js');
|
||||
|
||||
if (!fs.existsSync(server)) {
|
||||
console.log(clorox.bold.red(`> ${dir}/server.js does not exist — type ${clorox.bold.cyan(dir === 'build' ? `npx sapper build` : `npx sapper build ${dir}`)} to create it`));
|
||||
return;
|
||||
}
|
||||
|
||||
if (port) {
|
||||
if (!await ports.check(port)) {
|
||||
console.log(clorox.bold.red(`> Port ${port} is unavailable`));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
port = await ports.find(3000);
|
||||
}
|
||||
|
||||
child_process.fork(server, [], {
|
||||
cwd: process.cwd(),
|
||||
env: Object.assign({
|
||||
NODE_ENV: 'production',
|
||||
PORT: port,
|
||||
SAPPER_DEST: dir
|
||||
}, process.env)
|
||||
});
|
||||
const { start } = await import('./cli/start');
|
||||
start(dir, opts);
|
||||
});
|
||||
|
||||
prog.command('export [dest]')
|
||||
.describe('Export your app as static files (if possible)')
|
||||
.action((dest = 'export') => {
|
||||
.action(async (dest = 'export') => {
|
||||
console.log(`> Building...`);
|
||||
|
||||
process.env.NODE_ENV = 'production';
|
||||
@@ -95,21 +55,23 @@ prog.command('export [dest]')
|
||||
|
||||
const start = Date.now();
|
||||
|
||||
build()
|
||||
.then(() => {
|
||||
const elapsed = Date.now() - start;
|
||||
console.error(`\n> Built in ${prettyMs(elapsed)}. Exporting...`);
|
||||
})
|
||||
.then(() => exporter(dest))
|
||||
.then(() => {
|
||||
const elapsed = Date.now() - start;
|
||||
console.error(`\n> Finished in ${prettyMs(elapsed)}. Type ${clorox.bold.cyan(`npx serve ${dest}`)} to run the app.`);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||
});
|
||||
try {
|
||||
const { build } = await import('./cli/build');
|
||||
await build();
|
||||
console.error(`\n> Built in ${elapsed(start)}. Exporting...`);
|
||||
|
||||
const { exporter } = await import('./cli/export');
|
||||
await exporter(dest);
|
||||
console.error(`\n> Finished in ${elapsed(start)}. Type ${clorox.bold.cyan(`npx serve ${dest}`)} to run the app.`);
|
||||
} catch (err) {
|
||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||
}
|
||||
});
|
||||
|
||||
// TODO upgrade
|
||||
|
||||
prog.parse(process.argv);
|
||||
|
||||
function elapsed(start: number) {
|
||||
return prettyMs(Date.now() - start);
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import rimraf from 'rimraf';
|
||||
import { create_compilers, create_app, create_routes, create_serviceworker } from '../core'
|
||||
import { src, dest, dev } from '../config';
|
||||
|
||||
export default async function build() {
|
||||
export async function build() {
|
||||
const output = dest();
|
||||
|
||||
mkdirp.sync(output);
|
||||
|
||||
@@ -70,9 +70,20 @@ function create_hot_update_server(port: number, interval = 10000) {
|
||||
return { send };
|
||||
}
|
||||
|
||||
export default async function dev(port: number) {
|
||||
export async function dev(opts: { port: number }) {
|
||||
process.env.NODE_ENV = 'development';
|
||||
|
||||
let port = opts.port || +process.env.PORT;
|
||||
|
||||
if (port) {
|
||||
if (!await ports.check(port)) {
|
||||
console.log(clorox.bold.red(`> Port ${port} is unavailable`));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
port = await ports.find(3000);
|
||||
}
|
||||
|
||||
const dir = dest();
|
||||
rimraf.sync(dir);
|
||||
mkdirp.sync(dir);
|
||||
|
||||
@@ -10,7 +10,7 @@ import { dest } from '../config';
|
||||
|
||||
const app = polka();
|
||||
|
||||
export default async function exporter(export_dir: string) {
|
||||
export async function exporter(export_dir: string) {
|
||||
const build_dir = dest();
|
||||
|
||||
// Prep output directory
|
||||
|
||||
35
src/cli/start.ts
Normal file
35
src/cli/start.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as child_process from 'child_process';
|
||||
import * as clorox from 'clorox';
|
||||
import * as ports from 'port-authority';
|
||||
|
||||
export async function start(dir: string, opts: { port: number }) {
|
||||
let port = opts.port || +process.env.PORT;
|
||||
|
||||
const resolved = path.resolve(dir);
|
||||
const server = path.resolve(dir, 'server.js');
|
||||
|
||||
if (!fs.existsSync(server)) {
|
||||
console.log(clorox.bold.red(`> ${dir}/server.js does not exist — type ${clorox.bold.cyan(dir === 'build' ? `npx sapper build` : `npx sapper build ${dir}`)} to create it`));
|
||||
return;
|
||||
}
|
||||
|
||||
if (port) {
|
||||
if (!await ports.check(port)) {
|
||||
console.log(clorox.bold.red(`> Port ${port} is unavailable`));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
port = await ports.find(3000);
|
||||
}
|
||||
|
||||
child_process.fork(server, [], {
|
||||
cwd: process.cwd(),
|
||||
env: Object.assign({
|
||||
NODE_ENV: 'production',
|
||||
PORT: port,
|
||||
SAPPER_DEST: dir
|
||||
}, process.env)
|
||||
});
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
{"version":3,"file":"webpack.ts.js","sources":["src/webpack.ts"],"sourcesContent":["import { dest, dev } from './config';\n\nexport default {\n\tdev: dev(),\n\n\tclient: {\n\t\tentry: () => {\n\t\t\treturn {\n\t\t\t\tmain: './app/client'\n\t\t\t};\n\t\t},\n\n\t\toutput: () => {\n\t\t\treturn {\n\t\t\t\tpath: `${dest()}/client`,\n\t\t\t\tfilename: '[hash]/[name].js',\n\t\t\t\tchunkFilename: '[hash]/[name].[id].js',\n\t\t\t\tpublicPath: '/client/'\n\t\t\t};\n\t\t}\n\t},\n\n\tserver: {\n\t\tentry: () => {\n\t\t\treturn {\n\t\t\t\tserver: './app/server'\n\t\t\t};\n\t\t},\n\n\t\toutput: () => {\n\t\t\treturn {\n\t\t\t\tpath: dest(),\n\t\t\t\tfilename: '[name].js',\n\t\t\t\tchunkFilename: '[hash]/[name].[id].js',\n\t\t\t\tlibraryTarget: 'commonjs2'\n\t\t\t};\n\t\t}\n\t},\n\n\tserviceworker: {\n\t\tentry: () => {\n\t\t\treturn {\n\t\t\t\t'service-worker': './app/service-worker'\n\t\t\t};\n\t\t},\n\n\t\toutput: () => {\n\t\t\treturn {\n\t\t\t\tpath: dest(),\n\t\t\t\tfilename: '[name].js',\n\t\t\t\tchunkFilename: '[name].[id].[hash].js'\n\t\t\t}\n\t\t}\n\t}\n};"],"names":["dev","dest"],"mappings":";;;;AAEA,cAAe;IACd,GAAG,EAAEA,eAAG,EAAE;IAEV,MAAM,EAAE;QACP,KAAK,EAAE;YACN,OAAO;gBACN,IAAI,EAAE,cAAc;aACpB,CAAC;SACF;QAED,MAAM,EAAE;YACP,OAAO;gBACN,IAAI,EAAKC,gBAAI,EAAE,YAAS;gBACxB,QAAQ,EAAE,kBAAkB;gBAC5B,aAAa,EAAE,uBAAuB;gBACtC,UAAU,EAAE,UAAU;aACtB,CAAC;SACF;KACD;IAED,MAAM,EAAE;QACP,KAAK,EAAE;YACN,OAAO;gBACN,MAAM,EAAE,cAAc;aACtB,CAAC;SACF;QAED,MAAM,EAAE;YACP,OAAO;gBACN,IAAI,EAAEA,gBAAI,EAAE;gBACZ,QAAQ,EAAE,WAAW;gBACrB,aAAa,EAAE,uBAAuB;gBACtC,aAAa,EAAE,WAAW;aAC1B,CAAC;SACF;KACD;IAED,aAAa,EAAE;QACd,KAAK,EAAE;YACN,OAAO;gBACN,gBAAgB,EAAE,sBAAsB;aACxC,CAAC;SACF;QAED,MAAM,EAAE;YACP,OAAO;gBACN,IAAI,EAAEA,gBAAI,EAAE;gBACZ,QAAQ,EAAE,WAAW;gBACrB,aAAa,EAAE,uBAAuB;aACtC,CAAA;SACD;KACD;CACD,CAAC;;;;"}
|
||||
Reference in New Issue
Block a user