8000 feat: add --static-dist-prefix option by kt3k · Pull Request #30 · kt3k/packup · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add --static-dist-prefix option #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ Options:
-p, --port <port> Sets the port to serve on. Default is 1234.
--livereload-port Sets the port for live reloading. Default is 35729.
-s, --static-dir <dir> The directory for static files. The files here are served as is.
-o, --open Automatically opens in specified browser.
--static-dist-prefix <prefix> The prefix for static files in the destination.
--public-url <prefix> The path prefix for urls. Default is ".".
-o, --open Automatically opens in specified browser.
TODO --https Serves files over HTTPS.
TODO --cert <path> The path to certificate to use with HTTPS.
TODO --key <path> The path to private key to use with HTTPS.
Expand All @@ -64,6 +65,7 @@ bundles for production
Options:
--dist-dir <dir> Output directory to write to when unspecified by targets
-s, --static-dir <dir> The directory for static files. The files here are copied to dist as is.
--static-dist-prefix <prefix> The prefix for static files in the destination.
--public-url <prefix> The path prefix for urls. Default is ".".
-L, --log-level <level> Set the log level (choices: "none", "error", "warn", "info", "verbose")
-h, --help Display help for command
Expand All @@ -81,6 +83,7 @@ type CliArgs = {
port: string;
"public-url": string;
"static-dir": string;
"static-dist-prefix": string;
};

/**
Expand All @@ -93,6 +96,7 @@ export async function main(cliArgs: string[] = Deno.args): Promise<number> {
help,
"dist-dir": distDir = "dist",
"static-dir": staticDir = "static",
"static-dist-prefix": staticDistPrefix = "",
"log-level": logLevel = "info",
open = false,
port = "1234",
Expand Down Expand Up @@ -169,7 +173,12 @@ export async function main(cliArgs: string[] = Deno.args): Promise<number> {
usageBuild();
return 1;
}
await build(entrypoints, { distDir, staticDir, publicUrl });
await build(entrypoints, {
distDir,
staticDir,
publicUrl,
staticDistPrefix,
});
return 0;
}

Expand All @@ -194,12 +203,14 @@ export async function main(cliArgs: string[] = Deno.args): Promise<number> {
livereloadPort: +livereloadPort,
staticDir,
publicUrl,
staticDistPrefix,
});
return 0;
}

type BuildAndServeCommonOptions = {
staticDir: string;
staticDistPrefix: string;
publicUrl: string;
};

Expand All @@ -212,14 +223,18 @@ type BuildOptions = {
*/
async function build(
paths: string[],
{ distDir, staticDir, publicUrl }: BuildOptions & BuildAndServeCommonOptions,
{ distDir, staticDir, publicUrl, staticDistPrefix }:
& BuildOptions
& BuildAndServeCommonOptions,
) {
checkUniqueEntrypoints(paths);

logger.log(`Writing the assets to ${distDir}`);
await ensureDir(distDir);

const staticAssets = generateStaticAssets(staticDir);
const staticAssets = generateStaticAssets(staticDir, {
distPrefix: staticDistPrefix,
});
const allAssets: AsyncGenerator<File, void, void>[] = [];
for (const path of paths) {
const [assets] = await generateAssets(path, { publicUrl });
Expand Down Expand Up @@ -247,7 +262,7 @@ type ServeOptions = {
*/
async function serve(
paths: string[],
{ open, port, livereloadPort, staticDir, publicUrl }:
{ open, port, livereloadPort, staticDir, publicUrl, staticDistPrefix }:
& ServeOptions
& BuildAndServeCommonOptions,
) {
Expand All @@ -274,7 +289,9 @@ async function serve(
});
allAssets.push(assets);
}
const staticAssets = watchAndGenStaticAssets(staticDir);
const staticAssets = watchAndGenStaticAssets(staticDir, {
distPrefix: staticDistPrefix,
});

const { addr } = serveIterable(mux(...allAssets, staticAssets), { port });
if (addr.transport === "tcp") {
Expand Down
4 changes: 3 additions & 1 deletion docs/getting-started/cli-command.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ Options:
-p, --port <port> Sets the port to serve on. Default is 1234.
--livereload-port Sets the port for live reloading. Default is 35729.
-s, --static-dir <dir> The directory for static files. The files here are served as is.
-o, --open Automatically opens in specified browser.
--static-dist-prefix <prefix> The prefix for static files in the destination.
--public-url <prefix> The path prefix for urls. Default is ".".
-o, --open Automatically opens in specified browser.
--log-level <level> Sets the log level. "error", "warn", "info", "debug" or "trace". Default is "info".
-h, --help Displays help for command.
```
Expand Down Expand Up @@ -62,6 +63,7 @@ bundles for production
Options:
--dist-dir <dir> Output directory to write to when unspecified by targets
-s, --static-dir <dir> The directory for static files. The files here are copied to dist as is.
--static-dist-prefix <prefix> The prefix for static files in the destination.
--public-url <prefix> The path prefix for urls. Default is ".".
-L, --log-level <level> Set the log level (choices: "none", "error", "warn", "info", "verbose")
-h, --help Display help for command
Expand Down
23 changes: 17 additions & 6 deletions generate_static_assets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { relative, walk } from "./deps.ts";
import { join, relative, walk } from "./deps.ts";
import { logger } from "./logger_util.ts";

type File = Blob & { name: string };
Expand All @@ -23,28 +23,36 @@ export async function checkStaticDir(dir: string): Promise<boolean> {
}
}

export async function* generateStaticAssets(dir: string): AsyncIterable<File> {
type GenerateStaticAssetsOptions = {
distPrefix: string;
};

export async function* generateStaticAssets(
dir: string,
opts: GenerateStaticAssetsOptions,
): AsyncIterable<File> {
if (!await checkStaticDir(dir)) {
return;
}

for await (const entry of walk(dir)) {
if (!entry.isDirectory) {
yield createStaticAssetFromPath(entry.path, dir);
yield createStaticAssetFromPath(entry.path, dir, opts.distPrefix);
}
}
}

export async function* watchAndGenStaticAssets(
dir: string,
opts: GenerateStaticAssetsOptions,
): AsyncIterable<File> {
if (!await checkStaticDir(dir)) {
return;
}

for await (const entry of walk(dir)) {
if (!entry.isDirectory) {
yield createStaticAssetFromPath(entry.path, dir);
yield createStaticAssetFromPath(entry.path, dir, opts.distPrefix);
}
}

Expand All @@ -56,7 +64,7 @@ export async function* watchAndGenStaticAssets(
if (stat.isDirectory) {
continue;
}
yield await createStaticAssetFromPath(path, dir);
yield await createStaticAssetFromPath(path, dir, opts.distPrefix);
} catch (e) {
logger.error(e);
}
Expand All @@ -67,8 +75,11 @@ export async function* watchAndGenStaticAssets(
async function createStaticAssetFromPath(
path: string,
root: string,
distPrefix: string,
): Promise<File> {
logger.debug("Reading", path);
const bytes = await Deno.readFile(path);
return Object.assign(new Blob([bytes]), { name: relative(root, path) });
return Object.assign(new Blob([bytes]), {
name: join(distPrefix, relative(root, path)),
});
}
0