mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-20 22:35:09 +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/app/manifest
|
||||||
test/app/export
|
test/app/export
|
||||||
test/app/build
|
test/app/build
|
||||||
|
*.js
|
||||||
|
*.js.map
|
||||||
*.ts.js
|
*.ts.js
|
||||||
*.ts.js.map
|
*.ts.js.map
|
||||||
!rollup.config.js
|
!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",
|
"description": "Military-grade apps, engineered by Svelte",
|
||||||
"main": "middleware.js",
|
"main": "middleware.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
"sapper": "sapper"
|
"sapper": "./sapper"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"*.js",
|
"*.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 sade from 'sade';
|
||||||
import * as clorox from 'clorox';
|
import * as clorox from 'clorox';
|
||||||
import prettyMs from 'pretty-ms';
|
import prettyMs from 'pretty-ms';
|
||||||
import help from './cli/help.md';
|
// import upgrade from './cli/upgrade';
|
||||||
import build from './cli/build';
|
|
||||||
import exporter from './cli/export';
|
|
||||||
import dev from './cli/dev';
|
|
||||||
import upgrade from './cli/upgrade';
|
|
||||||
import * as ports from 'port-authority';
|
import * as ports from 'port-authority';
|
||||||
import * as pkg from '../package.json';
|
import * as pkg from '../package.json';
|
||||||
|
|
||||||
@@ -18,23 +14,13 @@ prog.command('dev')
|
|||||||
.describe('Start a development server')
|
.describe('Start a development server')
|
||||||
.option('-p, --port', 'Specify a port')
|
.option('-p, --port', 'Specify a port')
|
||||||
.action(async (opts: { port: number }) => {
|
.action(async (opts: { port: number }) => {
|
||||||
let port = opts.port || +process.env.PORT;
|
const { dev } = await import('./cli/dev');
|
||||||
|
dev(opts);
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
prog.command('build [dest]')
|
prog.command('build [dest]')
|
||||||
.describe('Create a production-ready version of your app')
|
.describe('Create a production-ready version of your app')
|
||||||
.action((dest = 'build') => {
|
.action(async (dest = 'build') => {
|
||||||
console.log(`> Building...`);
|
console.log(`> Building...`);
|
||||||
|
|
||||||
process.env.NODE_ENV = 'production';
|
process.env.NODE_ENV = 'production';
|
||||||
@@ -42,52 +28,26 @@ prog.command('build [dest]')
|
|||||||
|
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|
||||||
build()
|
try {
|
||||||
.then(() => {
|
const { build } = await import('./cli/build');
|
||||||
const elapsed = Date.now() - start;
|
await build();
|
||||||
console.error(`\n> Finished in ${prettyMs(elapsed)}. Type ${clorox.bold.cyan(dest === 'build' ? 'npx sapper start' : `npx sapper start ${dest}`)} to run the app.`);
|
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) {
|
||||||
.catch(err => {
|
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
}
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
prog.command('start [dir]')
|
prog.command('start [dir]')
|
||||||
.describe('Start your app')
|
.describe('Start your app')
|
||||||
.option('-p, --port', 'Specify a port')
|
.option('-p, --port', 'Specify a port')
|
||||||
.action(async (dir = 'build', opts: { port: number }) => {
|
.action(async (dir = 'build', opts: { port: number }) => {
|
||||||
let port = opts.port || +process.env.PORT;
|
const { start } = await import('./cli/start');
|
||||||
|
start(dir, opts);
|
||||||
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)
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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)')
|
||||||
.action((dest = 'export') => {
|
.action(async (dest = 'export') => {
|
||||||
console.log(`> Building...`);
|
console.log(`> Building...`);
|
||||||
|
|
||||||
process.env.NODE_ENV = 'production';
|
process.env.NODE_ENV = 'production';
|
||||||
@@ -95,21 +55,23 @@ prog.command('export [dest]')
|
|||||||
|
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|
||||||
build()
|
try {
|
||||||
.then(() => {
|
const { build } = await import('./cli/build');
|
||||||
const elapsed = Date.now() - start;
|
await build();
|
||||||
console.error(`\n> Built in ${prettyMs(elapsed)}. Exporting...`);
|
console.error(`\n> Built in ${elapsed(start)}. Exporting...`);
|
||||||
})
|
|
||||||
.then(() => exporter(dest))
|
const { exporter } = await import('./cli/export');
|
||||||
.then(() => {
|
await exporter(dest);
|
||||||
const elapsed = Date.now() - start;
|
console.error(`\n> Finished in ${elapsed(start)}. Type ${clorox.bold.cyan(`npx serve ${dest}`)} to run the app.`);
|
||||||
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');
|
||||||
.catch(err => {
|
}
|
||||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO upgrade
|
// TODO upgrade
|
||||||
|
|
||||||
prog.parse(process.argv);
|
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 { create_compilers, create_app, create_routes, create_serviceworker } from '../core'
|
||||||
import { src, dest, dev } from '../config';
|
import { src, dest, dev } from '../config';
|
||||||
|
|
||||||
export default async function build() {
|
export async function build() {
|
||||||
const output = dest();
|
const output = dest();
|
||||||
|
|
||||||
mkdirp.sync(output);
|
mkdirp.sync(output);
|
||||||
|
|||||||
@@ -70,9 +70,20 @@ function create_hot_update_server(port: number, interval = 10000) {
|
|||||||
return { send };
|
return { send };
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function dev(port: number) {
|
export async function dev(opts: { port: number }) {
|
||||||
process.env.NODE_ENV = 'development';
|
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();
|
const dir = dest();
|
||||||
rimraf.sync(dir);
|
rimraf.sync(dir);
|
||||||
mkdirp.sync(dir);
|
mkdirp.sync(dir);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { dest } from '../config';
|
|||||||
|
|
||||||
const app = polka();
|
const app = polka();
|
||||||
|
|
||||||
export default async function exporter(export_dir: string) {
|
export async function exporter(export_dir: string) {
|
||||||
const build_dir = dest();
|
const build_dir = dest();
|
||||||
|
|
||||||
// Prep output directory
|
// 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