mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-12 03:05:12 +00:00
Add support for custom route file extensions.
This commit is contained in:
@@ -19,6 +19,7 @@ type Opts = {
|
||||
static?: string;
|
||||
legacy?: boolean;
|
||||
bundler?: 'rollup' | 'webpack';
|
||||
ext?: string;
|
||||
oncompile?: ({ type, result }: { type: string, result: CompileResult }) => void;
|
||||
};
|
||||
|
||||
@@ -32,6 +33,7 @@ export async function build({
|
||||
|
||||
bundler,
|
||||
legacy = false,
|
||||
ext,
|
||||
oncompile = noop
|
||||
}: Opts = {}) {
|
||||
bundler = validate_bundler(bundler);
|
||||
@@ -68,7 +70,7 @@ export async function build({
|
||||
|
||||
fs.writeFileSync(`${dest}/template.html`, minify_html(template));
|
||||
|
||||
const manifest_data = create_manifest_data(routes);
|
||||
const manifest_data = create_manifest_data(routes, ext);
|
||||
|
||||
// create src/node_modules/@sapper/app.mjs and server.mjs
|
||||
create_app({
|
||||
|
||||
@@ -28,7 +28,8 @@ type Opts = {
|
||||
hot?: boolean,
|
||||
'devtools-port'?: number,
|
||||
bundler?: 'rollup' | 'webpack',
|
||||
port?: number
|
||||
port?: number,
|
||||
ext: string
|
||||
};
|
||||
|
||||
export function dev(opts: Opts) {
|
||||
@@ -47,7 +48,7 @@ class Watcher extends EventEmitter {
|
||||
}
|
||||
port: number;
|
||||
closed: boolean;
|
||||
|
||||
|
||||
dev_port: number;
|
||||
live: boolean;
|
||||
hot: boolean;
|
||||
@@ -67,6 +68,7 @@ class Watcher extends EventEmitter {
|
||||
unique_warnings: Set<string>;
|
||||
unique_errors: Set<string>;
|
||||
}
|
||||
ext: string;
|
||||
|
||||
constructor({
|
||||
cwd = '.',
|
||||
@@ -80,7 +82,8 @@ class Watcher extends EventEmitter {
|
||||
hot,
|
||||
'devtools-port': devtools_port,
|
||||
bundler,
|
||||
port = +process.env.PORT
|
||||
port = +process.env.PORT,
|
||||
ext
|
||||
}: Opts) {
|
||||
super();
|
||||
|
||||
@@ -95,7 +98,7 @@ class Watcher extends EventEmitter {
|
||||
output: path.resolve(cwd, output),
|
||||
static: path.resolve(cwd, static_files)
|
||||
};
|
||||
|
||||
this.ext = ext;
|
||||
this.port = port;
|
||||
this.closed = false;
|
||||
|
||||
@@ -161,7 +164,7 @@ class Watcher extends EventEmitter {
|
||||
let manifest_data: ManifestData;
|
||||
|
||||
try {
|
||||
manifest_data = create_manifest_data(routes);
|
||||
manifest_data = create_manifest_data(routes, this.ext);
|
||||
create_app({
|
||||
bundler: this.bundler,
|
||||
manifest_data,
|
||||
@@ -189,7 +192,7 @@ class Watcher extends EventEmitter {
|
||||
},
|
||||
() => {
|
||||
try {
|
||||
const new_manifest_data = create_manifest_data(routes);
|
||||
const new_manifest_data = create_manifest_data(routes, this.ext);
|
||||
create_app({
|
||||
bundler: this.bundler,
|
||||
manifest_data, // TODO is this right? not new_manifest_data?
|
||||
|
||||
22
src/cli.ts
22
src/cli.ts
@@ -31,6 +31,7 @@ prog.command('dev')
|
||||
.option('--static', 'Static files directory', 'static')
|
||||
.option('--output', 'Sapper output directory', 'src/node_modules/@sapper')
|
||||
.option('--build-dir', 'Development build directory', '__sapper__/dev')
|
||||
.option('--ext', 'Custom Route Extension', '.svelte .html')
|
||||
.action(async (opts: {
|
||||
port: number,
|
||||
open: boolean,
|
||||
@@ -43,7 +44,8 @@ prog.command('dev')
|
||||
routes: string,
|
||||
static: string,
|
||||
output: string,
|
||||
'build-dir': string
|
||||
'build-dir': string,
|
||||
ext: string
|
||||
}) => {
|
||||
const { dev } = await import('./api/dev');
|
||||
|
||||
@@ -59,7 +61,8 @@ prog.command('dev')
|
||||
'dev-port': opts['dev-port'],
|
||||
live: opts.live,
|
||||
hot: opts.hot,
|
||||
bundler: opts.bundler
|
||||
bundler: opts.bundler,
|
||||
ext: opts.ext
|
||||
});
|
||||
|
||||
let first = true;
|
||||
@@ -151,6 +154,7 @@ prog.command('build [dest]')
|
||||
.option('--src', 'Source directory', 'src')
|
||||
.option('--routes', 'Routes directory', 'src/routes')
|
||||
.option('--output', 'Sapper output directory', 'src/node_modules/@sapper')
|
||||
.option('--ext', 'Custom Route Extension', '.svelte .html')
|
||||
.example(`build custom-dir -p 4567`)
|
||||
.action(async (dest = '__sapper__/build', opts: {
|
||||
port: string,
|
||||
@@ -159,12 +163,13 @@ prog.command('build [dest]')
|
||||
cwd: string,
|
||||
src: string,
|
||||
routes: string,
|
||||
output: string
|
||||
output: string,
|
||||
ext: string
|
||||
}) => {
|
||||
console.log(`> Building...`);
|
||||
|
||||
try {
|
||||
await _build(opts.bundler, opts.legacy, opts.cwd, opts.src, opts.routes, opts.output, dest);
|
||||
await _build(opts.bundler, opts.legacy, opts.cwd, opts.src, opts.routes, opts.output, dest, opts.ext);
|
||||
|
||||
const launcher = path.resolve(dest, 'index.js');
|
||||
|
||||
@@ -199,6 +204,7 @@ prog.command('export [dest]')
|
||||
.option('--static', 'Static files directory', 'static')
|
||||
.option('--output', 'Sapper output directory', 'src/node_modules/@sapper')
|
||||
.option('--build-dir', 'Intermediate build directory', '__sapper__/build')
|
||||
.option('--ext', 'Custom Route Extension', '.svelte .html')
|
||||
.action(async (dest = '__sapper__/export', opts: {
|
||||
build: boolean,
|
||||
legacy: boolean,
|
||||
@@ -212,11 +218,12 @@ prog.command('export [dest]')
|
||||
static: string,
|
||||
output: string,
|
||||
'build-dir': string,
|
||||
ext: string
|
||||
}) => {
|
||||
try {
|
||||
if (opts.build) {
|
||||
console.log(`> Building...`);
|
||||
await _build(opts.bundler, opts.legacy, opts.cwd, opts.src, opts.routes, opts.output, opts['build-dir']);
|
||||
await _build(opts.bundler, opts.legacy, opts.cwd, opts.src, opts.routes, opts.output, opts['build-dir'], opts.ext);
|
||||
console.error(`\n> Built in ${elapsed(start)}`);
|
||||
}
|
||||
|
||||
@@ -265,7 +272,8 @@ async function _build(
|
||||
src: string,
|
||||
routes: string,
|
||||
output: string,
|
||||
dest: string
|
||||
dest: string,
|
||||
ext: string
|
||||
) {
|
||||
const { build } = await import('./api/build');
|
||||
|
||||
@@ -276,7 +284,7 @@ async function _build(
|
||||
src,
|
||||
routes,
|
||||
dest,
|
||||
|
||||
ext,
|
||||
oncompile: event => {
|
||||
let banner = `built ${event.type}`;
|
||||
let c = (txt: string) => colors.cyan(txt);
|
||||
|
||||
@@ -4,9 +4,10 @@ import svelte from 'svelte/compiler';
|
||||
import { Page, PageComponent, ServerRoute, ManifestData } from '../interfaces';
|
||||
import { posixify, reserved_words } from '../utils';
|
||||
|
||||
const component_extensions = ['.svelte', '.html']; // TODO make this configurable (to include e.g. .svelte.md?)
|
||||
export default function create_manifest_data(cwd: string, extensions: string = '.svelte .html'): ManifestData {
|
||||
|
||||
const component_extensions = extensions.split(' ');
|
||||
|
||||
export default function create_manifest_data(cwd: string): ManifestData {
|
||||
// TODO remove in a future version
|
||||
if (!fs.existsSync(cwd)) {
|
||||
throw new Error(`As of Sapper 0.21, the routes/ directory should become src/routes/`);
|
||||
|
||||
Reference in New Issue
Block a user