mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-22 15:15:19 +00:00
Merge branch 'master' of https://github.com/freedmand/sapper into freedmand-master
This commit is contained in:
26
cli/index.js
26
cli/index.js
@@ -1,11 +1,29 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const build = require('../lib/build.js');
|
||||||
|
|
||||||
const cmd = process.argv[2];
|
const cmd = process.argv[2];
|
||||||
|
const start = Date.now();
|
||||||
|
|
||||||
if (cmd === 'build') {
|
if (cmd === 'build') {
|
||||||
process.env.NODE_ENV = 'production';
|
build()
|
||||||
require('../lib/build.js')();
|
.then(() => {
|
||||||
|
const elapsed = Date.now() - start;
|
||||||
|
console.error(`built in ${elapsed}ms`); // TODO beautify this, e.g. 'built in 4.7 seconds'
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||||
|
});
|
||||||
} else if (cmd === 'extract') {
|
} else if (cmd === 'extract') {
|
||||||
process.env.NODE_ENV = 'production';
|
const start = Date.now();
|
||||||
require('../lib/utils/extract.js')();
|
|
||||||
|
build()
|
||||||
|
.then(() => require('../lib/utils/extract.js')())
|
||||||
|
.then(() => {
|
||||||
|
const elapsed = Date.now() - start;
|
||||||
|
console.error(`extracted in ${elapsed}ms`); // TODO beautify this, e.g. 'built in 4.7 seconds'
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
45
lib/build.js
45
lib/build.js
@@ -1,3 +1,5 @@
|
|||||||
|
process.env.NODE_ENV = 'production';
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const mkdirp = require('mkdirp');
|
const mkdirp = require('mkdirp');
|
||||||
@@ -14,29 +16,32 @@ module.exports = () => {
|
|||||||
// create main.js and server-routes.js
|
// create main.js and server-routes.js
|
||||||
create_app();
|
create_app();
|
||||||
|
|
||||||
function handleErrors(err, stats) {
|
return new Promise((fulfil, reject) => {
|
||||||
if (err) {
|
function handleErrors(err, stats) {
|
||||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
if (err) {
|
||||||
process.exit(1);
|
reject(err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stats.hasErrors()) {
|
||||||
|
console.error(stats.toString({ colors: true }));
|
||||||
|
reject(new Error(`Encountered errors while building app`));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stats.hasErrors()) {
|
client.run((err, clientStats) => {
|
||||||
console.log(stats.toString({ colors: true }));
|
handleErrors(err, clientStats);
|
||||||
process.exit(1);
|
const clientInfo = clientStats.toJson();
|
||||||
}
|
fs.writeFileSync(path.join(dest, 'stats.client.json'), JSON.stringify(clientInfo, null, ' '));
|
||||||
}
|
|
||||||
|
|
||||||
client.run((err, clientStats) => {
|
server.run((err, serverStats) => {
|
||||||
handleErrors(err, clientStats);
|
handleErrors(err, serverStats);
|
||||||
const clientInfo = clientStats.toJson();
|
const serverInfo = serverStats.toJson();
|
||||||
fs.writeFileSync(path.join(dest, 'stats.client.json'), JSON.stringify(clientInfo, null, ' '));
|
fs.writeFileSync(path.join(dest, 'stats.server.json'), JSON.stringify(serverInfo, null, ' '));
|
||||||
|
|
||||||
server.run((err, serverStats) => {
|
generate_asset_cache(clientInfo, serverInfo);
|
||||||
handleErrors(err, serverStats);
|
fulfil();
|
||||||
const serverInfo = serverStats.toJson();
|
});
|
||||||
fs.writeFileSync(path.join(dest, 'stats.server.json'), JSON.stringify(serverInfo, null, ' '));
|
|
||||||
|
|
||||||
generate_asset_cache(clientInfo, serverInfo);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const app = require('express')();
|
const app = require('express')();
|
||||||
const compression = require('compression');
|
const compression = require('compression');
|
||||||
|
const mkdirp = require('mkdirp');
|
||||||
const sapper = require('../index.js');
|
const sapper = require('../index.js');
|
||||||
const static = require('serve-static');
|
const serve = require('serve-static');
|
||||||
const Spider = require('node-spider');
|
const Spider = require('node-spider');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
@@ -106,6 +107,12 @@ function getChunkFiles() {
|
|||||||
*/
|
*/
|
||||||
module.exports = function(includeUrls = null, excludeUrls = null,
|
module.exports = function(includeUrls = null, excludeUrls = null,
|
||||||
apiPrefix = '/api', extractionDir = OUTPUT_DIR) {
|
apiPrefix = '/api', extractionDir = OUTPUT_DIR) {
|
||||||
|
|
||||||
|
// Clean the output directory and copy assets in.
|
||||||
|
fs.removeSync(extractionDir);
|
||||||
|
mkdirp.sync(extractionDir);
|
||||||
|
fs.copySync('assets', extractionDir);
|
||||||
|
|
||||||
// Set up the server.
|
// Set up the server.
|
||||||
|
|
||||||
// this allows us to do e.g. `fetch('/api/blog')` on the server
|
// this allows us to do e.g. `fetch('/api/blog')` on the server
|
||||||
@@ -117,14 +124,10 @@ module.exports = function(includeUrls = null, excludeUrls = null,
|
|||||||
|
|
||||||
app.use(compression({ threshold: 0 }));
|
app.use(compression({ threshold: 0 }));
|
||||||
|
|
||||||
app.use(static('assets'));
|
app.use(serve('assets'));
|
||||||
|
|
||||||
app.use(sapper());
|
app.use(sapper());
|
||||||
|
|
||||||
// Clean the output directory and copy assets in.
|
|
||||||
fs.removeSync(extractionDir);
|
|
||||||
fs.copySync('assets', extractionDir);
|
|
||||||
|
|
||||||
// If exclude URLs are set, normalize them.
|
// If exclude URLs are set, normalize them.
|
||||||
if (excludeUrls == null) excludeUrls = [];
|
if (excludeUrls == null) excludeUrls = [];
|
||||||
excludeUrls = excludeUrls.map((url) => getFullUrl(url));
|
excludeUrls = excludeUrls.map((url) => getFullUrl(url));
|
||||||
@@ -133,9 +136,11 @@ module.exports = function(includeUrls = null, excludeUrls = null,
|
|||||||
// scraper. The program automatically exits after all the static pages have
|
// scraper. The program automatically exits after all the static pages have
|
||||||
// been scraped from the server that are accessible from the root page (`/`).
|
// been scraped from the server that are accessible from the root page (`/`).
|
||||||
const extractedFiles = []; // keep track of extracted files.
|
const extractedFiles = []; // keep track of extracted files.
|
||||||
const server = app.listen(PORT, () => {
|
|
||||||
console.log(`listening on port ${PORT} and beginning extraction`);
|
return new Promise((resolve, reject) => {
|
||||||
return new Promise((resolve, reject) => {
|
const server = app.listen(PORT, () => {
|
||||||
|
console.log(`listening on port ${PORT} and beginning extraction`);
|
||||||
|
|
||||||
const spider = new Spider({
|
const spider = new Spider({
|
||||||
concurrent: 5,
|
concurrent: 5,
|
||||||
delay: 0,
|
delay: 0,
|
||||||
@@ -228,4 +233,4 @@ module.exports = function(includeUrls = null, excludeUrls = null,
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -409,13 +409,10 @@ function run(env) {
|
|||||||
function exec(cmd) {
|
function exec(cmd) {
|
||||||
return new Promise((fulfil, reject) => {
|
return new Promise((fulfil, reject) => {
|
||||||
require('child_process').exec(cmd, (err, stdout, stderr) => {
|
require('child_process').exec(cmd, (err, stdout, stderr) => {
|
||||||
if (err) {
|
process.stdout.write(stdout);
|
||||||
process.stdout.write(stdout);
|
process.stderr.write(stderr);
|
||||||
process.stderr.write(stderr);
|
|
||||||
|
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (err) return reject(err);
|
||||||
fulfil();
|
fulfil();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user