convert to typescript

This commit is contained in:
Rich Harris
2018-01-21 16:28:02 -05:00
parent fd0dd4fe58
commit 8bad37205d
18 changed files with 177 additions and 119 deletions

42
package-lock.json generated
View File

@@ -10,11 +10,53 @@
"integrity": "sha512-bPBbpu1vqgOOD70aMVG5tgioPdttKXQQFq6xodjZxVbPprtZIcm8NcTEJoB+/1QoH8z1TIqjaEN1Wm3YndnfNQ==",
"dev": true
},
"@types/events": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@types/events/-/events-1.1.0.tgz",
"integrity": "sha512-y3bR98mzYOo0pAZuiLari+cQyiKk3UXRuT45h1RjhfeCzqkjaVsfZJNaxdgtk7/3tzOm1ozLTqEqMP3VbI48jw==",
"dev": true
},
"@types/glob": {
"version": "5.0.34",
"resolved": "https://registry.npmjs.org/@types/glob/-/glob-5.0.34.tgz",
"integrity": "sha512-sUvpieq+HsWTLdkeOI8Mi8u22Ag3AoGuM3sv+XMP1bKtbaIAHpEA2f52K2mz6vK5PVhTa3bFyRZLZMqTxOo2Cw==",
"dev": true,
"requires": {
"@types/events": "1.1.0",
"@types/minimatch": "3.0.3",
"@types/node": "9.3.0"
}
},
"@types/minimatch": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz",
"integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==",
"dev": true
},
"@types/mkdirp": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/@types/mkdirp/-/mkdirp-0.5.2.tgz",
"integrity": "sha512-U5icWpv7YnZYGsN4/cmh3WD2onMY0aJIiTE6+51TwJCttdHvtCYmkBNOobHlXwrJRL0nkH9jH4kD+1FAdMN4Tg==",
"dev": true,
"requires": {
"@types/node": "9.3.0"
}
},
"@types/node": {
"version": "9.3.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-9.3.0.tgz",
"integrity": "sha512-wNBfvNjzsJl4tswIZKXCFQY0lss9nKUyJnG6T94X/eqjRgI2jHZ4evdjhQYBSan/vGtF6XVXPApOmNH2rf0KKw=="
},
"@types/rimraf": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/@types/rimraf/-/rimraf-2.0.2.tgz",
"integrity": "sha512-Hm/bnWq0TCy7jmjeN5bKYij9vw5GrDFWME4IuxV08278NtU/VdGbzsBohcCUJ7+QMqmUq5hpRKB39HeQWJjztQ==",
"dev": true,
"requires": {
"@types/glob": "5.0.34",
"@types/node": "9.3.0"
}
},
"@types/strip-bom": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@types/strip-bom/-/strip-bom-3.0.0.tgz",

View File

@@ -39,6 +39,9 @@
},
"devDependencies": {
"@std/esm": "^0.19.7",
"@types/glob": "^5.0.34",
"@types/mkdirp": "^0.5.2",
"@types/rimraf": "^2.0.2",
"css-loader": "^0.28.7",
"eslint": "^4.13.1",
"eslint-plugin-import": "^2.8.0",

View File

@@ -14,7 +14,7 @@ const paths = {
export default [
// cli.js
{
input: 'src/cli/index.js',
input: 'src/cli/index.ts',
output: {
file: 'cli.js',
format: 'cjs',
@@ -32,7 +32,7 @@ export default [
// core.js
{
input: 'src/core/index.js',
input: 'src/core/index.ts',
output: {
file: 'core.js',
format: 'cjs',
@@ -50,7 +50,7 @@ export default [
// middleware.js
{
input: 'src/middleware/index.js',
input: 'src/middleware/index.ts',
output: {
file: 'middleware.js',
format: 'cjs',
@@ -84,7 +84,7 @@ export default [
// webpack/config.js
{
input: 'src/webpack/index.js',
input: 'src/webpack/index.ts',
output: {
file: 'webpack/config.js',
format: 'cjs',

View File

@@ -1,5 +1,5 @@
import { build, export as exporter } from 'sapper/core.js';
import { dest, dev, entry, src } from '../config.js';
import { dest, dev, entry, src } from '../config';
const cmd = process.argv[2];
const start = Date.now();

View File

@@ -6,7 +6,17 @@ import create_compilers from './create_compilers.js';
import create_app from './create_app.js';
import create_assets from './create_assets.js';
export default function build({ dest, dev, entry, src }) {
export default 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, '**/*'));
@@ -31,12 +41,18 @@ export default function build({ dest, dev, entry, src }) {
client.run((err, client_stats) => {
handleErrors(err, client_stats);
const client_info = client_stats.toJson();
fs.writeFileSync(path.join(dest, 'stats.client.json'), JSON.stringify(client_info, null, ' '));
fs.writeFileSync(
path.join(dest, 'stats.client.json'),
JSON.stringify(client_info, null, ' ')
);
server.run((err, server_stats) => {
handleErrors(err, server_stats);
const server_info = server_stats.toJson();
fs.writeFileSync(path.join(dest, 'stats.server.json'), JSON.stringify(server_info, null, ' '));
fs.writeFileSync(
path.join(dest, 'stats.server.json'),
JSON.stringify(server_info, null, ' ')
);
create_assets({ src, dest, dev, client_info, server_info });
fulfil();

View File

@@ -1,94 +0,0 @@
import * as fs from 'fs';
import * as path from 'path';
import create_routes from './create_routes.js';
function posixify(file) {
return file.replace(/[/\\]/g, '/');
}
function fudge_mtime(file) {
// need to fudge the mtime so that webpack doesn't go doolally
const { atime, mtime } = fs.statSync(file);
fs.utimesSync(file, new Date(atime.getTime() - 999999), new Date(mtime.getTime() - 999999));
}
function create_app({ src, dev, entry }) {
const routes = create_routes({ src });
function create_client_main() {
const code = `[${
routes
.filter(route => route.type === 'page')
.map(route => {
const params = route.dynamic.length === 0 ?
'{}' :
`{ ${route.dynamic.map((part, i) => `${part}: match[${i + 1}]`).join(', ') } }`;
const file = posixify(`${src}/${route.file}`);
return `{ pattern: ${route.pattern}, params: match => (${params}), load: () => import(/* webpackChunkName: "${route.id}" */ '${file}') }`
})
.join(', ')
}]`;
let main = fs.readFileSync('templates/main.js', 'utf-8')
.replace(/__app__/g, posixify(path.resolve(__dirname, '../../runtime/app.js')))
.replace(/__routes__/g, code)
.replace(/__dev__/g, String(dev));
if (dev) {
const hmr_client = posixify(require.resolve(`webpack-hot-middleware/client`));
main += `\n\nimport('${hmr_client}?path=/__webpack_hmr&timeout=20000'); if (module.hot) module.hot.accept();`
}
fs.writeFileSync(entry.client, main);
fudge_mtime(entry.client);
}
function create_server_routes() {
const imports = routes
.map(route => {
const file = posixify(`${src}/${route.file}`);
return route.type === 'page' ?
`import ${route.id} from '${file}';` :
`import * as ${route.id} from '${file}';`;
})
.join('\n');
const exports = `export { ${routes.map(route => route.id)} };`;
fs.writeFileSync(entry.server, `${imports}\n\n${exports}`);
fudge_mtime(entry.server);
}
create_client_main();
create_server_routes();
}
// export function start_watching({ src }) {
// const chokidar = require('chokidar');
// const watch = (glob, callback) => {
// const watcher = chokidar.watch(glob, {
// ignoreInitial: true,
// persistent: false
// });
// watcher.on('add', callback);
// watcher.on('change', callback);
// watcher.on('unlink', callback);
// };
// watch('templates/main.js', create_app);
// watch('routes/**/*.+(html|js|mjs)', () => {
// route_manager.update({ src });
// create_app();
// });
// watch('templates/**.html', () => {
// create_templates();
// // TODO reload current page?
// });
// }
export default create_app;

90
src/core/create_app.ts Normal file
View File

@@ -0,0 +1,90 @@
import * as fs from 'fs';
import * as path from 'path';
import create_routes from './create_routes';
function posixify(file: string) {
return file.replace(/[/\\]/g, '/');
}
function fudge_mtime(file: string) {
// need to fudge the mtime so that webpack doesn't go doolally
const { atime, mtime } = fs.statSync(file);
fs.utimesSync(
file,
new Date(atime.getTime() - 999999),
new Date(mtime.getTime() - 999999)
);
}
function create_app({
src,
dev,
entry
}: {
src: string;
dev: boolean;
entry: { client: string; server: string };
}) {
const routes = create_routes({ src });
function create_client_main() {
const code = `[${routes
.filter(route => route.type === 'page')
.map(route => {
const params =
route.dynamic.length === 0
? '{}'
: `{ ${route.dynamic
.map((part, i) => `${part}: match[${i + 1}]`)
.join(', ')} }`;
const file = posixify(`${src}/${route.file}`);
return `{ pattern: ${
route.pattern
}, params: match => (${params}), load: () => import(/* webpackChunkName: "${
route.id
}" */ '${file}') }`;
})
.join(', ')}]`;
let main = fs
.readFileSync('templates/main.js', 'utf-8')
.replace(
/__app__/g,
posixify(path.resolve(__dirname, '../../runtime/app.js'))
)
.replace(/__routes__/g, code)
.replace(/__dev__/g, String(dev));
if (dev) {
const hmr_client = posixify(
require.resolve(`webpack-hot-middleware/client`)
);
main += `\n\nimport('${hmr_client}?path=/__webpack_hmr&timeout=20000'); if (module.hot) module.hot.accept();`;
}
fs.writeFileSync(entry.client, main);
fudge_mtime(entry.client);
}
function create_server_routes() {
const imports = routes
.map(route => {
const file = posixify(`${src}/${route.file}`);
return route.type === 'page'
? `import ${route.id} from '${file}';`
: `import * as ${route.id} from '${file}';`;
})
.join('\n');
const exports = `export { ${routes.map(route => route.id)} };`;
fs.writeFileSync(entry.server, `${imports}\n\n${exports}`);
fudge_mtime(entry.server);
}
create_client_main();
create_server_routes();
}
export default create_app;

View File

@@ -1,8 +1,8 @@
import * as fs from 'fs';
import * as path from 'path';
import glob from 'glob';
import { create_templates, render } from './templates.js';
import create_routes from './create_routes.js';
import { create_templates, render } from './templates';
import create_routes from './create_routes';
function ensure_array(thing) {
return Array.isArray(thing) ? thing : [thing]; // omg webpack what the HELL are you doing

View File

@@ -1,10 +0,0 @@
import * as templates from './templates.js'; // TODO templates is an anomaly... fix post-#91
export { default as build } from './build.js';
export { default as export } from './export.js';
export { default as create_assets } from './create_assets.js';
export { default as create_compilers } from './create_compilers.js';
export { default as create_routes } from './create_routes.js';
export { default as create_app } from './create_app.js';
export { templates };

11
src/core/index.ts Normal file
View File

@@ -0,0 +1,11 @@
import { create_templates, render, stream } from './templates'; // TODO templates is an anomaly... fix post-#91
export { default as build } from './build';
export { default as export } from './export.js';
export { default as create_app } from './create_app';
export { default as create_assets } from './create_assets';
export { default as create_compilers } from './create_compilers';
export { default as create_routes } from './create_routes';
export const templates = { create_templates, render, stream };

View File

@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import chalk from 'chalk';
import { create_app, create_assets, create_routes, create_templates } from 'sapper/core.js';
import { create_app, create_assets, create_routes, templates } from 'sapper/core.js';
import { dest } from '../config.js';
function deferred() {
@@ -84,7 +84,7 @@ export default function create_watcher({ compilers, dev, entry, src, onroutes })
});
watch_files('templates/**.html', () => {
create_templates();
templates.create_templates();
// TODO reload current page?
});

View File

@@ -5,8 +5,8 @@ import rimraf from 'rimraf';
import serialize from 'serialize-javascript';
import escape_html from 'escape-html';
import { create_routes, templates, create_compilers, create_assets } from 'sapper/core.js';
import create_watcher from './create_watcher.js';
import { dest, dev, entry, src } from '../config.js';
import create_watcher from './create_watcher';
import { dest, dev, entry, src } from '../config';
function connect_dev() {
mkdirp.sync(dest);

View File

@@ -1,4 +1,4 @@
import { dest, dev, entry } from '../config.js';
import { dest, dev, entry } from '../config';
export default {
dev,