-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved 'lume init', removed 'lume import-map'
- Loading branch information
1 parent
c987779
commit a9a7774
Showing
6 changed files
with
121 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,16 @@ | ||
import { brightGreen, gray } from "../deps/colors.ts"; | ||
import { pluginNames } from "../core/utils.ts"; | ||
import importMap from "./import_map.ts"; | ||
import { | ||
pluginNames, | ||
updateLumeVersion, | ||
writeDenoConfig, | ||
} from "../core/utils.ts"; | ||
import { Checkbox, Confirm, Select } from "../deps/cliffy.ts"; | ||
import { outdent } from "../deps/outdent.ts"; | ||
|
||
/** Generate a _config.js file */ | ||
export default function (): Promise<void> { | ||
return init(); | ||
} | ||
|
||
export async function init() { | ||
const plugins = await initConfig(); | ||
if (!plugins) return; | ||
await importMap({ plugins }); | ||
welcome(); | ||
} | ||
import type { DenoConfigResult } from "../core/utils.ts"; | ||
|
||
/** (Re)configure lume config file */ | ||
async function initConfig(): Promise<string[] | undefined> { | ||
/** Init Lume in the current directory */ | ||
export default async function init(): Promise<void> { | ||
const configFile = await getConfigFile(); | ||
|
||
if (!configFile) { | ||
|
@@ -27,6 +20,13 @@ async function initConfig(): Promise<string[] | undefined> { | |
|
||
const plugins = await getPlugins(); | ||
|
||
const denoConfig: DenoConfigResult = { | ||
config: {}, | ||
file: "deno.json", | ||
}; | ||
|
||
initPlugins(plugins, denoConfig); | ||
|
||
// Generate the code for the config file | ||
const code = [`import lume from "lume/mod.ts";`]; | ||
|
||
|
@@ -51,7 +51,11 @@ async function initConfig(): Promise<string[] | undefined> { | |
await Deno.writeTextFile(configFile, code.join("\n")); | ||
console.log(); | ||
console.log("Lume configuration file saved:", gray(configFile)); | ||
return plugins; | ||
|
||
const url = new URL(import.meta.resolve("../")); | ||
updateLumeVersion(url, denoConfig); | ||
writeDenoConfig(denoConfig); | ||
welcome(); | ||
} | ||
|
||
/** | ||
|
@@ -150,3 +154,59 @@ function welcome() { | |
|
||
console.log(message); | ||
} | ||
|
||
function initPlugins(plugins: string[], denoConfig: DenoConfigResult) { | ||
// Ensure that jsx and jsx_preact are not used at the same time and are loaded before mdx | ||
if (plugins.includes("mdx")) { | ||
const jsx = plugins.indexOf("jsx"); | ||
const jsx_preact = plugins.indexOf("jsx_preact"); | ||
|
||
if (jsx !== -1 && jsx_preact !== -1) { | ||
throw new Error( | ||
"You can't use both the jsx and jsx_preact plugins at the same time.", | ||
); | ||
} | ||
|
||
if (jsx !== -1) { | ||
// Ensure jsx is loaded before mdx | ||
plugins.splice(jsx, 1); | ||
plugins.unshift("jsx"); | ||
} else if (jsx_preact !== -1) { | ||
// Ensure jsx_preact is loaded before mdx | ||
plugins.splice(jsx_preact, 1); | ||
plugins.unshift("jsx_preact"); | ||
} else { | ||
// Use jsx by default | ||
plugins.unshift("jsx"); | ||
} | ||
} | ||
|
||
if (plugins.includes("jsx")) { | ||
denoConfig.config.compilerOptions ||= {}; | ||
denoConfig.config.compilerOptions.jsx = "react-jsx"; | ||
denoConfig.config.compilerOptions.jsxImportSource = "react"; | ||
|
||
// Add jsx-runtime import to import_map. | ||
denoConfig.importMap ||= { imports: {} }; | ||
denoConfig.importMap.imports["react/jsx-runtime"] = | ||
"https://esm.sh/[email protected]/jsx-runtime"; | ||
} | ||
|
||
if (plugins.includes("jsx_preact")) { | ||
denoConfig.config.compilerOptions ||= {}; | ||
denoConfig.config.compilerOptions.jsx = "react-jsx"; | ||
denoConfig.config.compilerOptions.jsxImportSource = "npm:preact"; | ||
} | ||
|
||
// Ensure that tailwindcss is loaded before postcss | ||
if (plugins.includes("tailwindcss")) { | ||
const tailwindcss = plugins.indexOf("tailwindcss"); | ||
const postcss = plugins.indexOf("postcss"); | ||
|
||
if (postcss !== -1) { | ||
plugins.splice(postcss, 1); | ||
} | ||
|
||
plugins.splice(tailwindcss, 1, "tailwindcss", "postcss"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,28 +47,6 @@ export const pluginNames = [ | |
"windi_css", | ||
]; | ||
|
||
/** A list of the available plugins with init configurations */ | ||
export const initPlugins: Record< | ||
string, | ||
(denoConfig: DenoConfigResult) => void | ||
> = { | ||
jsx(denoConfig) { | ||
denoConfig.config.compilerOptions ||= {}; | ||
denoConfig.config.compilerOptions.jsx = "react-jsx"; | ||
denoConfig.config.compilerOptions.jsxImportSource = "react"; | ||
|
||
// Add jsx-runtime import to import_map. | ||
denoConfig.importMap ||= { imports: {} }; | ||
denoConfig.importMap.imports["react/jsx-runtime"] = | ||
"https://esm.sh/[email protected]/jsx-runtime"; | ||
}, | ||
jsx_preact(denoConfig) { | ||
denoConfig.config.compilerOptions ||= {}; | ||
denoConfig.config.compilerOptions.jsx = "react-jsx"; | ||
denoConfig.config.compilerOptions.jsxImportSource = "npm:preact"; | ||
}, | ||
}; | ||
|
||
export function log(...lines: (string | undefined)[]) { | ||
console.log("----------------------------------------"); | ||
lines.forEach((line) => line && console.log(line)); | ||
|
@@ -358,6 +336,36 @@ export async function readDenoConfig(): Promise<DenoConfigResult | undefined> { | |
} | ||
} | ||
|
||
export function updateLumeVersion(url: URL, denoConfig: DenoConfigResult) { | ||
denoConfig.importMap ??= { imports: {} }; | ||
|
||
const { config, importMap } = denoConfig; | ||
|
||
// Configure the import map | ||
if (Deno.version.deno < "1.30.0") { | ||
config.importMap ||= "./import_map.json"; | ||
} | ||
|
||
const oldUrl = importMap.imports["lume/"]; | ||
const newUrl = new URL("./", url).href; | ||
importMap.imports["lume/"] = newUrl; | ||
|
||
for (const [specifier, url] of Object.entries(importMap.imports)) { | ||
if (url.startsWith(oldUrl)) { | ||
importMap.imports[specifier] = url.replace(oldUrl, newUrl); | ||
} | ||
} | ||
|
||
// Configure lume tasks | ||
const tasks = config.tasks || {}; | ||
if (!tasks.lume || !tasks.lume.includes(`echo "import 'lume/cli.ts'"`)) { | ||
tasks.lume = `echo "import 'lume/cli.ts'" | deno run --unstable -A -`; | ||
tasks.build = "deno task lume"; | ||
tasks.serve = "deno task lume -s"; | ||
} | ||
config.tasks = tasks; | ||
} | ||
|
||
/** Update the Deno configuration */ | ||
export async function writeDenoConfig(options: DenoConfigResult) { | ||
const { file, config, importMap } = options; | ||
|