-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from jthemphill/vite
Add Vite support
- Loading branch information
Showing
9 changed files
with
1,620 additions
and
824 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* character_icon.tsx contains a glob import that only works in Parcel. | ||
* vite.config.ts replaces that Parcel glob import by loading this file instead. | ||
* This file contains a glob import that only works in Vite. | ||
*/ | ||
|
||
// Return a map of the form { "Icon_<id>": <url> }. | ||
const images = Object.fromEntries( | ||
Object.entries( | ||
// `import.meta.glob` is a Vite-specific feature. | ||
// @ts-expect-error TODO: Can use "vite/client" types after removing conflicting definition in `assets/globals.d.ts` | ||
import.meta.glob("../../assets/icons/*.webp", { | ||
eager: true, | ||
query: "?url", | ||
import: "default", | ||
}), | ||
) | ||
.map(([path, module]) => { | ||
const iconName = path.match(/(Icon_.*)\.webp$/); | ||
if (!iconName || !iconName[1]) { | ||
return null; | ||
} | ||
return [iconName[1], module]; | ||
}) | ||
.filter((entry) => entry !== null), | ||
); | ||
export default images; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
../../icons |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
../manifest.webmanifest |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
../../screenshots |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
../../assets/static/scripts.json |
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
import react from "@vitejs/plugin-react"; | ||
import posthtml from "posthtml"; | ||
import type { | ||
AliasOptions, | ||
IndexHtmlTransformHook, | ||
IndexHtmlTransformResult, | ||
} from "vite"; | ||
import { defineConfig } from "vite"; | ||
|
||
const alias: AliasOptions = [ | ||
// `.parcelrc` uses the `parcel-resolver-ts-base-url` plugin so imports can be relative to `./src/js` instead of the current file. | ||
// The equivalent in Vite is to declare an alias for every file and directory in `./src/js`. | ||
...fs.readdirSync("./src/js").map((filename) => { | ||
const name = filename.replace(/\.(j|t)sx?$/, ""); | ||
return { | ||
find: name, | ||
replacement: path.resolve(__dirname, "src", "js", name), | ||
}; | ||
}), | ||
{ | ||
// Parcel imports our character icons using a Parcel-specific glob syntax. | ||
// Redirect to character_icons_vite.ts, where we'll use a Vite-specific glob syntax instead. | ||
find: /^.*\/assets\/icons\/\*\.webp$/, | ||
replacement: path.resolve(__dirname, "src", "js", "character_icons_vite"), | ||
}, | ||
]; | ||
|
||
// We run PostHTML on our `index.html` and `script.html` files to process an `<include>` tag. | ||
function postHtmlPlugin() { | ||
return { | ||
name: "posthtml-transform", | ||
async transformIndexHtml(html: string): Promise<IndexHtmlTransformResult> { | ||
const postHtmlResult = await posthtml() | ||
.use(require("posthtml-include")()) | ||
.process(html); | ||
return postHtmlResult.html; | ||
}, | ||
}; | ||
} | ||
|
||
export default defineConfig({ | ||
assetsInclude: ["icons/touch-icon.png"], | ||
build: { | ||
emptyOutDir: true, | ||
outDir: "../dist", | ||
rollupOptions: { | ||
input: { | ||
main: path.resolve(__dirname, "src", "index.html"), | ||
script: path.resolve(__dirname, "src", "script.html"), | ||
}, | ||
}, | ||
}, | ||
plugins: [ | ||
react({ | ||
babel: { | ||
babelrc: true, | ||
}, | ||
}), | ||
postHtmlPlugin(), | ||
], | ||
resolve: { | ||
alias, | ||
}, | ||
root: "src", | ||
}); |
Oops, something went wrong.