From 1b6678951ae0b7b9c168f16bda8a27c8963589ba Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Sat, 3 Jun 2023 18:46:40 +1000 Subject: [PATCH] feat: support embedded import map in `deno.json` (#1188) One can now inline the `imports` and `scopes` from the import map directly into deno.json. For new projects, `import_map.json` is not generated anymore. Co-authored-by: Luca Casonato --- .github/workflows/ci.yml | 2 +- demo/deno.json | 9 +++- demo/fresh.gen.ts | 2 - demo/import_map.json | 10 ---- docs/concepts/deployment.md | 2 +- docs/concepts/updating.md | 17 +++--- docs/getting-started/create-a-project.md | 8 ++- init.ts | 16 ++---- src/build/deps.ts | 6 +-- src/build/esbuild.ts | 6 +-- src/dev/mod.ts | 37 +------------ src/server/context.ts | 58 ++++++++++++++++++--- src/server/deps.ts | 3 ++ src/server/mod.ts | 4 +- tests/cli_test.ts | 14 +---- tests/fixture/deno.json | 9 +++- tests/fixture/fresh.gen.ts | 2 - tests/fixture/import_map.json | 10 ---- tests/fixture_error/deno.json | 8 ++- tests/fixture_error/fresh.gen.ts | 2 - tests/fixture_error/import_map.json | 8 --- tests/fixture_jsx_pragma/deno.json | 7 ++- tests/fixture_jsx_pragma/fresh.gen.ts | 2 - tests/fixture_jsx_pragma/import-map.json | 8 --- tests/fixture_plugin/deno.json | 7 ++- tests/fixture_plugin/fresh.gen.ts | 2 - tests/fixture_plugin/import_map.json | 8 --- tests/fixture_twind_hydrate/deno.json | 9 +++- tests/fixture_twind_hydrate/fresh.gen.ts | 2 - tests/fixture_twind_hydrate/import_map.json | 10 ---- update.ts | 29 +++++------ www/deno.json | 13 ++++- www/fresh.gen.ts | 2 - www/import_map.json | 14 ----- 34 files changed, 149 insertions(+), 197 deletions(-) delete mode 100644 demo/import_map.json delete mode 100644 tests/fixture/import_map.json delete mode 100644 tests/fixture_error/import_map.json delete mode 100644 tests/fixture_jsx_pragma/import-map.json delete mode 100644 tests/fixture_plugin/import_map.json delete mode 100644 tests/fixture_twind_hydrate/import_map.json delete mode 100644 www/import_map.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a88e1255..d4173cd8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,7 +38,7 @@ jobs: - name: Cache dependencies if: startsWith(matrix.os, 'ubuntu') && matrix.deno == 'v1.x' - run: deno cache --no-check --import-map=./www/import_map.json src/dev/deps.ts src/server/deps.ts www/main.ts + run: deno cache --config=www/deno.json src/dev/deps.ts src/server/deps.ts www/main.ts - name: Cache Chrome (Linux) if: startsWith(matrix.os, 'ubuntu') diff --git a/demo/deno.json b/demo/deno.json index b691b285..e64bfa96 100644 --- a/demo/deno.json +++ b/demo/deno.json @@ -3,7 +3,14 @@ "tasks": { "start": "deno run -A --watch=static/,routes/ dev.ts" }, - "importMap": "./import_map.json", + "imports": { + "$fresh/": "../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3", + "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", + "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3" + }, "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" diff --git a/demo/fresh.gen.ts b/demo/fresh.gen.ts index 8d5fc01d..d11e969a 100644 --- a/demo/fresh.gen.ts +++ b/demo/fresh.gen.ts @@ -2,7 +2,6 @@ // This file SHOULD be checked into source version control. // This file is automatically updated during development when running `dev.ts`. -import config from "./deno.json" assert { type: "json" }; import * as $0 from "./routes/index.tsx"; import * as $$0 from "./islands/Counter.tsx"; @@ -14,7 +13,6 @@ const manifest = { "./islands/Counter.tsx": $$0, }, baseUrl: import.meta.url, - config, }; export default manifest; diff --git a/demo/import_map.json b/demo/import_map.json deleted file mode 100644 index b0f63712..00000000 --- a/demo/import_map.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "imports": { - "$fresh/": "../", - "preact": "https://esm.sh/preact@10.15.1", - "preact/": "https://esm.sh/preact@10.15.1/", - "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3", - "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", - "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3" - } -} diff --git a/docs/concepts/deployment.md b/docs/concepts/deployment.md index f51bd8ca..051d0d54 100644 --- a/docs/concepts/deployment.md +++ b/docs/concepts/deployment.md @@ -44,7 +44,7 @@ ENV DENO_DEPLOYMENT_ID=${GIT_REVISION} WORKDIR /app COPY . . -RUN deno cache main.ts --import-map=import_map.json +RUN deno cache main.ts EXPOSE 8000 diff --git a/docs/concepts/updating.md b/docs/concepts/updating.md index 15582002..26c27c9f 100644 --- a/docs/concepts/updating.md +++ b/docs/concepts/updating.md @@ -28,14 +28,14 @@ below. To update your dependencies, you have two options: - Run the fresh updater to update your project dependencies. -- Manually update the dependency versions in your `import_map.json` file. +- Manually update the dependency versions in your `deno.json` file. ### Auto updater The auto updater is a command line tool that will update your project's -`import_map.json` file to the latest versions of Fresh and its dependencies. It -may also contain code mods for your project that will update your code to the -latest recommended patterns for Fresh projects. +`deno.json` file to the latest versions of Fresh and its dependencies. It may +also contain code mods for your project that will update your code to the latest +recommended patterns for Fresh projects. To run the auto updater, run the following command from the root of your project: @@ -48,11 +48,10 @@ You will be prompted to confirm the changes that will be made to your project. ### Manual update -To manually update your project's dependencies, you can edit the -`import_map.json` file in the root of your projects directory. Dependency -versions are encoded into the URLs in this file. For example, here is how to -update a project from Fresh 1.0.2 to 1.1.3, and update Preact to the latest -version: +To manually update your project's dependencies, you can edit the `deno.json` +file in the root of your projects directory. Dependency versions are encoded +into the URLs in this file. For example, here is how to update a project from +Fresh 1.0.2 to 1.1.3, and update Preact to the latest version: ```diff { diff --git a/docs/getting-started/create-a-project.md b/docs/getting-started/create-a-project.md index 34f8ef08..7c12e3a5 100644 --- a/docs/getting-started/create-a-project.md +++ b/docs/getting-started/create-a-project.md @@ -30,15 +30,13 @@ files that are strictly necessary to run a fresh project: - **`fresh.gen.ts`**: This is the manifest file that contains information about your routes and islands. This file is automatically generated in development based on your `routes/` and `islands/` folders. -- **`import_map.json`**: This is an [import map][import-map] that is used to - manage dependencies for the project. This allows for easy importing and - updating of dependencies. A **`deno.json`** file is also created in the project directory. This file does two things: -- It tells Deno about the location of the import map, so that it can be loaded - automatically. +- It defines the "imports" field. This is an [import map][import-map] that is + used to manage dependencies for the project. This allows for easy importing + and updating of dependencies. - It registers a "start" [task][task-runner] to run the project without having to type a long `deno run` command. diff --git a/init.ts b/init.ts index 2c382e27..1d6917ab 100644 --- a/init.ts +++ b/init.ts @@ -101,16 +101,6 @@ await Deno.writeTextFile( GITIGNORE, ); -const importMap = { imports: {} as Record }; -freshImports(importMap.imports); -if (useTwind) twindImports(importMap.imports); -dotenvImports(importMap.imports); -const IMPORT_MAP_JSON = JSON.stringify(importMap, null, 2) + "\n"; -await Deno.writeTextFile( - join(resolvedDirectory, "import_map.json"), - IMPORT_MAP_JSON, -); - const ROUTES_INDEX_TSX = `import { Head } from "$fresh/runtime.ts"; import { useSignal } from "@preact/signals"; import Counter from "../islands/Counter.tsx"; @@ -310,12 +300,16 @@ const config = { tasks: { start: "deno run -A --watch=static/,routes/ dev.ts", }, - importMap: "./import_map.json", + imports: {} as Record, compilerOptions: { jsx: "react-jsx", jsxImportSource: "preact", }, }; +freshImports(config.imports); +if (useTwind) twindImports(config.imports); +dotenvImports(config.imports); + const DENO_CONFIG = JSON.stringify(config, null, 2) + "\n"; await Deno.writeTextFile(join(resolvedDirectory, "deno.json"), DENO_CONFIG); diff --git a/src/build/deps.ts b/src/build/deps.ts index e96bc119..c84c9c77 100644 --- a/src/build/deps.ts +++ b/src/build/deps.ts @@ -2,8 +2,8 @@ export { fromFileUrl, toFileUrl, -} from "https://deno.land/std@0.189.0/path/mod.ts"; -export { escape as regexpEscape } from "https://deno.land/std@0.189.0/regexp/escape.ts"; +} from "https://deno.land/std@0.190.0/path/mod.ts"; +export { escape as regexpEscape } from "https://deno.land/std@0.190.0/regexp/escape.ts"; // -- esbuild -- // @deno-types="https://deno.land/x/esbuild@v0.17.11/mod.d.ts" @@ -17,4 +17,4 @@ const esbuild: typeof esbuildWasm = Deno.run === undefined const esbuildWasmURL = new URL("./esbuild_v0.17.11.wasm", import.meta.url).href; export { esbuild, esbuildWasm as esbuildTypes, esbuildWasmURL }; -export { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.7.0/mod.ts"; +export { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.7.2/mod.ts"; diff --git a/src/build/esbuild.ts b/src/build/esbuild.ts index af721152..cb18c9c5 100644 --- a/src/build/esbuild.ts +++ b/src/build/esbuild.ts @@ -16,8 +16,8 @@ export interface EsbuildBuilderOptions { entrypoints: Record; /** Whether or not this is a dev build. */ dev: boolean; - /** The import map URL. */ - importMapURL: URL; + /** The path to the deno.json / deno.jsonc config file. */ + configPath: string; /** The JSX configuration. */ jsxConfig: JSXConfig; } @@ -74,7 +74,7 @@ export class EsbuildBuilder implements Builder { plugins: [ buildIdPlugin(opts.buildID), - ...denoPlugins({ importMapURL: opts.importMapURL.href }), + ...denoPlugins({ configPath: opts.configPath }), ], }); diff --git a/src/dev/mod.ts b/src/dev/mod.ts index b3343f2d..a42c6b63 100644 --- a/src/dev/mod.ts +++ b/src/dev/mod.ts @@ -1,12 +1,4 @@ -import { - dirname, - fromFileUrl, - gte, - join, - relative, - toFileUrl, - walk, -} from "./deps.ts"; +import { dirname, fromFileUrl, gte, join, toFileUrl, walk } from "./deps.ts"; import { error } from "./error.ts"; const MIN_DENO_VERSION = "1.25.0"; @@ -60,31 +52,6 @@ export async function collect(directory: string): Promise { return { routes, islands }; } -async function findDenoConfig(directory: string) { - let dir = directory; - while (true) { - const path = join(dir, "deno.json"); - try { - await Deno.stat(path); - break; - } catch (err) { - if (!(err instanceof Deno.errors.NotFound)) { - throw err; - } - const parent = dirname(dir); - if (parent === dir) { - error( - `Could not find a deno.json file in the current directory or any parent directory.`, - ); - } - dir = parent; - } - } - - // convert into import path, would be cool if there is a stdlib function - return `${relative(directory, dir).replace(/\\/g, "/") || "."}/deno.json`; -} - export async function generate(directory: string, manifest: Manifest) { const { routes, islands } = manifest; @@ -92,7 +59,6 @@ export async function generate(directory: string, manifest: Manifest) { // This file SHOULD be checked into source version control. // This file is automatically updated during development when running \`dev.ts\`. -import config from "${await findDenoConfig(directory)}" assert { type: "json" }; ${ routes.map((file, i) => `import * as $${i} from "./routes${file}";`).join( "\n", @@ -117,7 +83,6 @@ const manifest = { } }, baseUrl: import.meta.url, - config, }; export default manifest; diff --git a/src/server/context.ts b/src/server/context.ts index 82d7f014..5696848e 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -1,7 +1,10 @@ import { ConnInfo, + dirname, extname, fromFileUrl, + join, + JSONC, RequestHandler, Status, toFileUrl, @@ -10,7 +13,7 @@ import { } from "./deps.ts"; import { h } from "preact"; import * as router from "./router.ts"; -import { Manifest } from "./mod.ts"; +import { DenoConfig, Manifest } from "./mod.ts"; import { ALIVE_URL, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts"; import { BUILD_ID } from "./build_id.ts"; import DefaultErrorHandler from "./default_error_page.ts"; @@ -47,6 +50,12 @@ interface RouterState { state: Record; } +function isObject(value: unknown) { + return typeof value === "object" && + !Array.isArray(value) && + value !== null; +} + interface StaticFile { /** The URL to the static file on disk. */ localUrl: URL; @@ -83,7 +92,7 @@ export class ServerContext { notFound: UnknownPage, error: ErrorPage, plugins: Plugin[], - importMapURL: URL, + configPath: string, jsxConfig: JSXConfig, ) { this.#routes = routes; @@ -99,7 +108,7 @@ export class ServerContext { this.#builder = new EsbuildBuilder({ buildID: BUILD_ID, entrypoints: collectEntrypoints(this.#dev, this.#islands, this.#plugins), - importMapURL, + configPath, dev: this.#dev, jsxConfig, }); @@ -115,11 +124,14 @@ export class ServerContext { // Get the manifest' base URL. const baseUrl = new URL("./", manifest.baseUrl).href; - const config = manifest.config || { importMap: "./import_map.json" }; - if (typeof config.importMap !== "string") { - throw new Error("deno.json must contain an 'importMap' property."); + const { config, path: configPath } = await readDenoConfig( + fromFileUrl(baseUrl), + ); + if (typeof config.importMap !== "string" && !isObject(config.imports)) { + throw new Error( + "deno.json must contain an 'importMap' or 'imports' property.", + ); } - const importMapURL = new URL(config.importMap, manifest.baseUrl); config.compilerOptions ??= {}; @@ -318,7 +330,7 @@ export class ServerContext { notFound, error, opts.plugins ?? [], - importMapURL, + configPath, jsxConfig, ); } @@ -937,3 +949,33 @@ function collectEntrypoints( return entryPoints; } + +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; + } +} diff --git a/src/server/deps.ts b/src/server/deps.ts index 5f8a329f..f26516d2 100644 --- a/src/server/deps.ts +++ b/src/server/deps.ts @@ -1,7 +1,9 @@ // -- std -- export { + dirname, extname, fromFileUrl, + join, toFileUrl, } from "https://deno.land/std@0.190.0/path/mod.ts"; export { walk } from "https://deno.land/std@0.190.0/fs/walk.ts"; @@ -17,6 +19,7 @@ export { } from "https://deno.land/std@0.190.0/media_types/mod.ts"; export { toHashString } from "https://deno.land/std@0.190.0/crypto/to_hash_string.ts"; export { escape } from "https://deno.land/std@0.190.0/regexp/escape.ts"; +export * as JSONC from "https://deno.land/std@0.190.0/jsonc/mod.ts"; // -- esbuild -- // @deno-types="https://deno.land/x/esbuild@v0.17.11/mod.d.ts" diff --git a/src/server/mod.ts b/src/server/mod.ts index bb4ba980..149f94a3 100644 --- a/src/server/mod.ts +++ b/src/server/mod.ts @@ -47,11 +47,11 @@ export interface Manifest { >; islands: Record; baseUrl: string; - config?: DenoConfig; } export interface DenoConfig { - importMap: string; + imports?: Record; + importMap?: string; compilerOptions?: { jsx?: string; jsxImportSource?: string; diff --git a/tests/cli_test.ts b/tests/cli_test.ts index 47a02306..8b182a4c 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -65,7 +65,6 @@ Deno.test({ "name": tmpDirName, "contents": [ { "type": "file", "name": "README.md" }, - { "type": "file", "name": "import_map.json" }, { "type": "file", "name": "fresh.gen.ts" }, { "type": "directory", @@ -204,7 +203,6 @@ Deno.test({ "name": tmpDirName, "contents": [ { "type": "file", "name": "README.md" }, - { "type": "file", "name": "import_map.json" }, { "type": "file", "name": "fresh.gen.ts" }, { "type": "file", "name": "twind.config.ts" }, { @@ -422,20 +420,11 @@ Deno.test({ await cliProcess.output(); - // "move deno.json and import_map.json one level up" + // move deno.json one level up await Deno.rename( path.join(tmpDirName, "subdirectory", "subsubdirectory", "deno.json"), path.join(tmpDirName, "deno.json"), ); - await Deno.rename( - path.join( - tmpDirName, - "subdirectory", - "subsubdirectory", - "import_map.json", - ), - path.join(tmpDirName, "import_map.json"), - ); const targetFileTree: FileTree[] = [ { @@ -443,7 +432,6 @@ Deno.test({ "name": tmpDirName, "contents": [ { "type": "file", "name": "deno.json" }, - { "type": "file", "name": "import_map.json" }, { "type": "directory", "name": "subdirectory", diff --git a/tests/fixture/deno.json b/tests/fixture/deno.json index a5d3115d..f6bc377f 100644 --- a/tests/fixture/deno.json +++ b/tests/fixture/deno.json @@ -1,6 +1,13 @@ { "lock": false, - "importMap": "./import_map.json", + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3", + "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", + "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3" + }, "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index 8f7f9efc..9adaa57d 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -2,7 +2,6 @@ // This file SHOULD be checked into source version control. // This file is automatically updated during development when running `dev.ts`. -import config from "./deno.json" assert { type: "json" }; import * as $0 from "./routes/[name].tsx"; import * as $1 from "./routes/_404.tsx"; import * as $2 from "./routes/_500.tsx"; @@ -90,7 +89,6 @@ const manifest = { "./islands/kebab-case-counter-test.tsx": $$6, }, baseUrl: import.meta.url, - config, }; export default manifest; diff --git a/tests/fixture/import_map.json b/tests/fixture/import_map.json deleted file mode 100644 index 11aab501..00000000 --- a/tests/fixture/import_map.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "imports": { - "$fresh/": "../../", - "preact": "https://esm.sh/preact@10.15.1", - "preact/": "https://esm.sh/preact@10.15.1/", - "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3", - "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", - "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3" - } -} diff --git a/tests/fixture_error/deno.json b/tests/fixture_error/deno.json index e9932610..5a3c9a8c 100644 --- a/tests/fixture_error/deno.json +++ b/tests/fixture_error/deno.json @@ -1,5 +1,11 @@ { - "importMap": "./import_map.json", + "lock": false, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3" + }, "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" diff --git a/tests/fixture_error/fresh.gen.ts b/tests/fixture_error/fresh.gen.ts index 39130ea3..e24e6727 100644 --- a/tests/fixture_error/fresh.gen.ts +++ b/tests/fixture_error/fresh.gen.ts @@ -2,7 +2,6 @@ // This file SHOULD be checked into source version control. // This file is automatically updated during development when running `dev.ts`. -import config from "./deno.json" assert { type: "json" }; import * as $0 from "./routes/[...all].ts"; import * as $1 from "./routes/index.tsx"; @@ -13,7 +12,6 @@ const manifest = { }, islands: {}, baseUrl: import.meta.url, - config, }; export default manifest; diff --git a/tests/fixture_error/import_map.json b/tests/fixture_error/import_map.json deleted file mode 100644 index 02b01b6f..00000000 --- a/tests/fixture_error/import_map.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "imports": { - "$fresh/": "../../", - "preact": "https://esm.sh/preact@10.15.1", - "preact/": "https://esm.sh/preact@10.15.1/", - "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3" - } -} diff --git a/tests/fixture_jsx_pragma/deno.json b/tests/fixture_jsx_pragma/deno.json index 28cb918d..c333fe24 100644 --- a/tests/fixture_jsx_pragma/deno.json +++ b/tests/fixture_jsx_pragma/deno.json @@ -1,4 +1,9 @@ { "lock": false, - "importMap": "./import-map.json" + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/preact-render-to-string@6.0.3?external=preact" + } } diff --git a/tests/fixture_jsx_pragma/fresh.gen.ts b/tests/fixture_jsx_pragma/fresh.gen.ts index 9ad34082..221a8cd3 100644 --- a/tests/fixture_jsx_pragma/fresh.gen.ts +++ b/tests/fixture_jsx_pragma/fresh.gen.ts @@ -2,7 +2,6 @@ // This file SHOULD be checked into source version control. // This file is automatically updated during development when running `dev.ts`. -import config from "./deno.json" assert { type: "json" }; import * as $0 from "./routes/index.tsx"; import * as $$0 from "./islands/Island.tsx"; @@ -14,7 +13,6 @@ const manifest = { "./islands/Island.tsx": $$0, }, baseUrl: import.meta.url, - config, }; export default manifest; diff --git a/tests/fixture_jsx_pragma/import-map.json b/tests/fixture_jsx_pragma/import-map.json deleted file mode 100644 index ec023997..00000000 --- a/tests/fixture_jsx_pragma/import-map.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "imports": { - "$fresh/": "../../", - "preact": "https://esm.sh/preact@10.15.1", - "preact/": "https://esm.sh/preact@10.15.1/", - "preact-render-to-string": "https://esm.sh/preact-render-to-string@5.2.6?external=preact" - } -} diff --git a/tests/fixture_plugin/deno.json b/tests/fixture_plugin/deno.json index a5d3115d..5a3c9a8c 100644 --- a/tests/fixture_plugin/deno.json +++ b/tests/fixture_plugin/deno.json @@ -1,6 +1,11 @@ { "lock": false, - "importMap": "./import_map.json", + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3" + }, "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" diff --git a/tests/fixture_plugin/fresh.gen.ts b/tests/fixture_plugin/fresh.gen.ts index 464e32d6..39739cd6 100644 --- a/tests/fixture_plugin/fresh.gen.ts +++ b/tests/fixture_plugin/fresh.gen.ts @@ -2,7 +2,6 @@ // This file SHOULD be checked into source version control. // This file is automatically updated during development when running `dev.ts`. -import config from "./deno.json" assert { type: "json" }; import * as $0 from "./routes/static.tsx"; import * as $1 from "./routes/with-island.tsx"; import * as $$0 from "./islands/Island.tsx"; @@ -16,7 +15,6 @@ const manifest = { "./islands/Island.tsx": $$0, }, baseUrl: import.meta.url, - config, }; export default manifest; diff --git a/tests/fixture_plugin/import_map.json b/tests/fixture_plugin/import_map.json deleted file mode 100644 index 02b01b6f..00000000 --- a/tests/fixture_plugin/import_map.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "imports": { - "$fresh/": "../../", - "preact": "https://esm.sh/preact@10.15.1", - "preact/": "https://esm.sh/preact@10.15.1/", - "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3" - } -} diff --git a/tests/fixture_twind_hydrate/deno.json b/tests/fixture_twind_hydrate/deno.json index b691b285..2a7fcc6e 100644 --- a/tests/fixture_twind_hydrate/deno.json +++ b/tests/fixture_twind_hydrate/deno.json @@ -3,7 +3,14 @@ "tasks": { "start": "deno run -A --watch=static/,routes/ dev.ts" }, - "importMap": "./import_map.json", + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.11.0", + "preact/": "https://esm.sh/preact@10.11.0/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.4", + "@preact/signals": "https://esm.sh/*@preact/signals@1.0.3", + "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.0.1" + }, "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" diff --git a/tests/fixture_twind_hydrate/fresh.gen.ts b/tests/fixture_twind_hydrate/fresh.gen.ts index 521f4ee5..d32cac5d 100644 --- a/tests/fixture_twind_hydrate/fresh.gen.ts +++ b/tests/fixture_twind_hydrate/fresh.gen.ts @@ -2,7 +2,6 @@ // This file SHOULD be checked into source version control. // This file is automatically updated during development when running `dev.ts`. -import config from "./deno.json" assert { type: "json" }; import * as $0 from "./routes/check-duplication.tsx"; import * as $1 from "./routes/insert-cssrules.tsx"; import * as $2 from "./routes/static.tsx"; @@ -20,7 +19,6 @@ const manifest = { "./islands/InsertCssrules.tsx": $$1, }, baseUrl: import.meta.url, - config, }; export default manifest; diff --git a/tests/fixture_twind_hydrate/import_map.json b/tests/fixture_twind_hydrate/import_map.json deleted file mode 100644 index 61be52e8..00000000 --- a/tests/fixture_twind_hydrate/import_map.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "imports": { - "$fresh/": "../../", - "preact": "https://esm.sh/preact@10.11.0", - "preact/": "https://esm.sh/preact@10.11.0/", - "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3", - "@preact/signals": "https://esm.sh/*@preact/signals@1.0.3", - "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.0.1" - } -} diff --git a/update.ts b/update.ts index 52df9800..281e2e11 100644 --- a/update.ts +++ b/update.ts @@ -27,24 +27,21 @@ const unresolvedDirectory = Deno.args[0]; const resolvedDirectory = resolve(unresolvedDirectory); // Update dependencies in the import map. -const IMPORT_MAP_PATH = join(resolvedDirectory, "import_map.json"); -let importMapText = await Deno.readTextFile(IMPORT_MAP_PATH); -const importMap = JSON.parse(importMapText); -freshImports(importMap.imports); -if (importMap.imports["twind"]) { - twindImports(importMap.imports); +const DENO_JSON_PATH = join(resolvedDirectory, "deno.json"); +let denoJsonText = await Deno.readTextFile(DENO_JSON_PATH); +let denoJson = JSON.parse(denoJsonText); +freshImports(denoJson.imports); +if (denoJson.imports["twind"]) { + twindImports(denoJson.imports); } -importMapText = JSON.stringify(importMap, null, 2); -await Deno.writeTextFile(IMPORT_MAP_PATH, importMapText); +denoJsonText = JSON.stringify(denoJson, null, 2); +await Deno.writeTextFile(DENO_JSON_PATH, denoJsonText); // Code mod for classic JSX -> automatic JSX. const JSX_CODEMOD = `This project is using the classic JSX transform. Would you like to update to the automatic JSX transform? This will remove the /** @jsx h */ pragma from your source code and add the jsx: "react-jsx" compiler option to your deno.json file.`; -const DENO_JSON_PATH = join(resolvedDirectory, "deno.json"); -let denoJsonText = await Deno.readTextFile(DENO_JSON_PATH); -const denoJson = JSON.parse(denoJsonText); if (denoJson.compilerOptions?.jsx !== "react-jsx" && confirm(JSX_CODEMOD)) { console.log("Updating config file..."); denoJson.compilerOptions = denoJson.compilerOptions || {}; @@ -88,12 +85,12 @@ const TWIND_CODEMOD = `This project is using an old version of the twind integration. Would you like to update to the new twind plugin? This will remove the 'class={tw\`border\`}' boilerplate from your source code replace it with the simpler 'class="border"'.`; -if (importMap.imports["@twind"] && confirm(TWIND_CODEMOD)) { - await Deno.remove(join(resolvedDirectory, importMap.imports["@twind"])); +if (denoJson.imports["@twind"] && confirm(TWIND_CODEMOD)) { + await Deno.remove(join(resolvedDirectory, denoJson.imports["@twind"])); - delete importMap.imports["@twind"]; - importMapText = JSON.stringify(importMap, null, 2); - await Deno.writeTextFile(IMPORT_MAP_PATH, importMapText); + delete denoJson.imports["@twind"]; + denoJson = JSON.stringify(denoJson, null, 2); + await Deno.writeTextFile(DENO_JSON_PATH, denoJson); const MAIN_TS = `/// /// diff --git a/www/deno.json b/www/deno.json index b691b285..5fec9f11 100644 --- a/www/deno.json +++ b/www/deno.json @@ -3,7 +3,18 @@ "tasks": { "start": "deno run -A --watch=static/,routes/ dev.ts" }, - "importMap": "./import_map.json", + "imports": { + "$fresh/": "../", + "twind": "https://esm.sh/twind@0.16.19", + "twind/": "https://esm.sh/twind@0.16.19/", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3", + "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", + "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3", + "$std/": "https://deno.land/std@0.190.0/", + "$ga4": "https://raw.githubusercontent.com/denoland/ga4/main/mod.ts" + }, "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" diff --git a/www/fresh.gen.ts b/www/fresh.gen.ts index bb028f8c..95c92ee2 100644 --- a/www/fresh.gen.ts +++ b/www/fresh.gen.ts @@ -2,7 +2,6 @@ // This file SHOULD be checked into source version control. // This file is automatically updated during development when running `dev.ts`. -import config from "./deno.json" assert { type: "json" }; import * as $0 from "./routes/_404.tsx"; import * as $1 from "./routes/_500.tsx"; import * as $2 from "./routes/_middleware.ts"; @@ -40,7 +39,6 @@ const manifest = { "./islands/SearchButton.tsx": $$4, }, baseUrl: import.meta.url, - config, }; export default manifest; diff --git a/www/import_map.json b/www/import_map.json deleted file mode 100644 index 3ede032f..00000000 --- a/www/import_map.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "imports": { - "$fresh/": "../", - "twind": "https://esm.sh/twind@0.16.19", - "twind/": "https://esm.sh/twind@0.16.19/", - "preact": "https://esm.sh/preact@10.15.1", - "preact/": "https://esm.sh/preact@10.15.1/", - "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3", - "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", - "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3", - "$std/": "https://deno.land/std@0.190.0/", - "$ga4": "https://raw.githubusercontent.com/denoland/ga4/main/mod.ts" - } -}