-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmr.config.mjs
66 lines (56 loc) · 1.69 KB
/
wmr.config.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import path from "path";
import { defineConfig } from 'wmr';
import { promises as fs } from "fs";
// @NOTE
// - Eventually this will be PR'd to the official directory plugin
// - https://github.com/preactjs/wmr/issues/957
/**
* @param {import("wmr").Options} options
* @param {string} options.root
* @returns {import('rollup').Plugin}
*/
function directoryPlugin(options) {
const PREFIX = "dir:";
const INTERNAL = "\0dir:";
options.plugins.push({
name: "directory",
async resolveId(id, importer) {
if (!id.startsWith(PREFIX)) return;
id = id.slice(PREFIX.length);
let resolved = await this.resolve(id, importer, { skipSelf: true });
if (!resolved) {
const r = path.join(path.dirname(importer), id);
const stats = await fs.stat(r);
if (!stats.isDirectory()) throw Error(`Not a directory.`);
resolved = { id: r };
}
return INTERNAL + (resolved ? resolved.id : id);
},
async load(id) {
if (!id.startsWith(INTERNAL)) return;
// remove the "\dir:" prefix and convert to an absolute path:
id = id.slice(INTERNAL.length);
let dir = id.split(path.posix.sep).join(path.sep);
if (!options.prod) {
dir = path.join(options.root, dir);
}
// watch the directory for changes:
this.addWatchFile(dir);
// generate a module that exports the directory contents as an Array:
const files = (await fs.readdir(dir)).filter((d) => d[0] != ".");
return `export default ${JSON.stringify(files)}`;
},
});
}
// Full list of options: https://wmr.dev/docs/configuration
export default defineConfig(config => {
directoryPlugin(config);
return {
port: 8080,
host: '0.0.0.0',
alias: {
react: 'preact/compat',
'react-dom': 'preact/compat',
}
}
});