By moving folks from `twind` over to the `tailwind` plugin we have traded support questions about "how to get twind autocompletion working?" with "my styles are not showing up in automatic mode". The tailwind plugin has a hard requirement on having a build step. We have documented this at various places and have a guide on how to set everything up, but it's much better if we float those steps from Fresh itself when we detect this. With this PR we will prepare a `.github/workflows/deploy.yml` file already where the user only needs to swap out the project name. When Fresh is started in production mode, the tailwind plugin is active and no snapshot files were loaded, then we print a helpful hint to the console: ```sh No pre-compiled tailwind styles found. Did you forget to run "deno task build" prior to starting the production server? ``` When we are running inside Deno Deploy this PR goes one step further. Since it is the first time many users might use Deno Deploy we'll also show a simple page that shows how to finish the tailwind setup. I've intentionally tried to not present this as a warning but rather as a "finishing" step to avoid scaring users off. It's not the prettiest page, but it's good enough for now. <img width="892" alt="Screenshot 2024-01-09 at 23 41 27" src="https://github.com/denoland/fresh/assets/1062408/9bf2a61b-5551-4d8e-bdf8-b498072f8e98"> Fixes https://github.com/denoland/fresh/issues/2223
144 lines
3.8 KiB
TypeScript
144 lines
3.8 KiB
TypeScript
import { dirname, fromFileUrl, isAbsolute, join, JSONC } from "./deps.ts";
|
|
import { FromManifestConfig, Manifest } from "./mod.ts";
|
|
import { DEFAULT_RENDER_FN } from "./render.ts";
|
|
import {
|
|
DenoConfig,
|
|
InternalFreshState,
|
|
ResolvedFreshConfig,
|
|
} from "./types.ts";
|
|
|
|
export async function readDenoConfig(
|
|
directory: string,
|
|
): Promise<{ config: DenoConfig; path: string }> {
|
|
let dir = directory;
|
|
while (true) {
|
|
for (const name of ["deno.json", "deno.jsonc"]) {
|
|
const path = join(dir, name);
|
|
try {
|
|
const file = await Deno.readTextFile(path);
|
|
if (name.endsWith(".jsonc")) {
|
|
return { config: JSONC.parse(file) as DenoConfig, path };
|
|
} else {
|
|
return { config: JSON.parse(file), path };
|
|
}
|
|
} catch (err) {
|
|
if (!(err instanceof Deno.errors.NotFound)) {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
const parent = dirname(dir);
|
|
if (parent === dir) {
|
|
throw new Error(
|
|
`Could not find a deno.json file in the current directory or any parent directory.`,
|
|
);
|
|
}
|
|
dir = parent;
|
|
}
|
|
}
|
|
|
|
function isObject(value: unknown) {
|
|
return value !== null && typeof value === "object" &&
|
|
!Array.isArray(value);
|
|
}
|
|
|
|
export async function getInternalFreshState(
|
|
manifest: Manifest,
|
|
config: FromManifestConfig,
|
|
): Promise<InternalFreshState> {
|
|
const base = dirname(fromFileUrl(manifest.baseUrl));
|
|
const { config: denoJson, path: denoJsonPath } = await readDenoConfig(base);
|
|
|
|
if (typeof denoJson.importMap !== "string" && !isObject(denoJson.imports)) {
|
|
throw new Error(
|
|
"deno.json must contain an 'importMap' or 'imports' property.",
|
|
);
|
|
}
|
|
|
|
const isLegacyDev = Deno.env.get("__FRSH_LEGACY_DEV") === "true";
|
|
|
|
/**
|
|
* assume a basePath is declared like this:
|
|
* basePath: "/foo/bar"
|
|
* it must start with a slash, and not have a trailing slash
|
|
*/
|
|
|
|
let basePath = "";
|
|
if (config.router?.basePath) {
|
|
basePath = config.router?.basePath;
|
|
if (!basePath.startsWith("/")) {
|
|
throw new TypeError(
|
|
`"basePath" option must start with "/". Received: "${basePath}"`,
|
|
);
|
|
}
|
|
if (basePath.endsWith("/")) {
|
|
throw new TypeError(
|
|
`"basePath" option must not end with "/". Received: "${basePath}"`,
|
|
);
|
|
}
|
|
}
|
|
|
|
const internalConfig: ResolvedFreshConfig = {
|
|
dev: isLegacyDev || Boolean(config.dev),
|
|
build: {
|
|
outDir: config.build?.outDir
|
|
? parseFileOrUrl(config.build.outDir, base)
|
|
: join(base, "_fresh"),
|
|
target: config.build?.target ?? ["chrome99", "firefox99", "safari15"],
|
|
},
|
|
plugins: config.plugins ?? [],
|
|
staticDir: config.staticDir
|
|
? parseFileOrUrl(config.staticDir, base)
|
|
: join(base, "static"),
|
|
render: config.render ?? DEFAULT_RENDER_FN,
|
|
router: config.router,
|
|
server: config.server ?? {},
|
|
basePath,
|
|
};
|
|
|
|
if (config.cert) {
|
|
internalConfig.server.cert = config.cert;
|
|
}
|
|
if (config.hostname) {
|
|
internalConfig.server.hostname = config.hostname;
|
|
}
|
|
if (config.key) {
|
|
internalConfig.server.key = config.key;
|
|
}
|
|
if (config.onError) {
|
|
internalConfig.server.onError = config.onError;
|
|
}
|
|
if (config.onListen) {
|
|
internalConfig.server.onListen = config.onListen;
|
|
}
|
|
if (config.port) {
|
|
internalConfig.server.port = config.port;
|
|
}
|
|
if (config.reusePort) {
|
|
internalConfig.server.reusePort = config.reusePort;
|
|
}
|
|
if (config.signal) {
|
|
internalConfig.server.signal = config.signal;
|
|
}
|
|
|
|
return {
|
|
config: internalConfig,
|
|
manifest,
|
|
loadSnapshot: !isLegacyDev && !config.dev,
|
|
didLoadSnapshot: false,
|
|
denoJsonPath,
|
|
denoJson,
|
|
build: false,
|
|
};
|
|
}
|
|
|
|
function parseFileOrUrl(input: string, base: string) {
|
|
if (input.startsWith("file://")) {
|
|
return fromFileUrl(input);
|
|
} else if (!isAbsolute(input)) {
|
|
return join(base, input);
|
|
}
|
|
|
|
return input;
|
|
}
|