Skip to content

Commit be8c2f5

Browse files
committed
ensured dynamic imports are asynchronous
this is required in preparation of switching to ESM, where synchronous `require` will have to be replaced with dynamic `import` note that this constitutes a breaking change for existing plugins and developers in general
1 parent c3d9b10 commit be8c2f5

File tree

8 files changed

+85
-79
lines changed

8 files changed

+85
-79
lines changed

bin/faucet

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
let { faucetDispatch } = require("../lib");
55
let { parseCLI } = require("../lib/cli");
66

7-
let { referenceDir, config, options } = parseCLI();
8-
faucetDispatch(referenceDir, config, options);
7+
parseCLI().
8+
then(({ referenceDir, config, options }) => {
9+
faucetDispatch(referenceDir, config, options);
10+
});

lib/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Options:
2727
serve generated files via HTTP with live reloading
2828
`.trim();
2929

30-
exports.parseCLI = function parseCLI() {
30+
exports.parseCLI = async function parseCLI() {
3131
let { values } = parseArgs({
3232
options: {
3333
help: {
@@ -84,6 +84,6 @@ exports.parseCLI = function parseCLI() {
8484
}
8585

8686
let rootDir = process.cwd();
87-
let { referenceDir, config } = readConfig(rootDir, values.config);
87+
let { referenceDir, config } = await readConfig(rootDir, values.config);
8888
return { referenceDir, config, options };
8989
};

lib/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
let path = require("path");
44

5-
exports.readConfig = function readConfig(rootDir, filepath = "faucet.config.js") {
5+
exports.readConfig = async function readConfig(rootDir, filepath = "faucet.config.js") {
66
let configPath = path.resolve(rootDir, filepath);
77
return {
88
referenceDir: path.dirname(configPath),
9-
config: require(configPath)
9+
config: await require(configPath)
1010
};
1111
};

lib/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ exports.faucetDispatch = async function faucetDispatch(referenceDir, config,
2222
browsers = [browsers];
2323
}
2424

25-
let plugins = pluginsByBucket(config);
25+
let plugins = await pluginsByBucket(config);
2626
// initialize plugins with corresponding configuration
2727
let buckets = Object.keys(plugins).reduce((memo, bucket) => {
2828
memo[bucket] = plugins[bucket].map(({ plugin, config }) => {
@@ -40,8 +40,10 @@ exports.faucetDispatch = async function faucetDispatch(referenceDir, config,
4040

4141
if(watch) {
4242
makeWatcher(config.watchDirs, referenceDir).
43-
on("edit", filepaths => {
44-
runner.rerun(filepaths);
43+
then(watcher => {
44+
watcher.on("edit", filepaths => {
45+
runner.rerun(filepaths);
46+
});
4547
});
4648
}
4749

@@ -62,8 +64,8 @@ function buildStep(plugins) {
6264
then(() => files);
6365
}
6466

65-
function makeWatcher(watchDirs, referenceDir) {
66-
let niteOwl = require("nite-owl");
67+
async function makeWatcher(watchDirs, referenceDir) {
68+
let niteOwl = await require("nite-owl");
6769

6870
if(watchDirs) {
6971
watchDirs = watchDirs.map(dir => resolvePath(dir, referenceDir,

lib/plugins.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,47 +41,50 @@ module.exports = {
4141

4242
// returns plugin functions grouped by bucket and filtered by relevance (based
4343
// on configuration)
44-
function pluginsByBucket(config, defaults) {
45-
let plugins = determinePlugins(config.plugins, defaults);
44+
async function pluginsByBucket(config, defaults) {
45+
let plugins = await determinePlugins(config.plugins, defaults);
4646
let buckets = [...BUCKETS].reduce((memo, bucket) => {
4747
memo[bucket] = [];
4848
return memo;
4949
}, {});
50-
Object.entries(plugins).forEach(([key, _plugin]) => {
50+
for(let [key, _plugin] of Object.entries(plugins)) {
5151
let pluginConfig = config[key];
5252
if(!pluginConfig) {
53-
return;
53+
continue;
5454
}
5555

5656
let { bucket, plugin } = _plugin;
57+
if(!plugin.call) {
58+
({ plugin } = await loadPlugin(plugin));
59+
}
5760
buckets[bucket].push({
58-
plugin: plugin.call ? plugin : loadPlugin(plugin).plugin,
61+
plugin,
5962
config: pluginConfig
6063
});
61-
});
64+
}
6265
return buckets;
6366
}
6467

6568
// `plugins` is an array of plugins, each either a package identifier or a
6669
// `{ key, bucket, plugin }` object`, with `key` being the configuration key and
6770
// `plugin` being either a function or a package identifier
68-
function determinePlugins(plugins = [], defaults = DEFAULTS) {
71+
async function determinePlugins(plugins = [], defaults = DEFAULTS) {
6972
let registry = {};
7073
// NB: default plugins are resolved lazily because eager loading would
7174
// result in them becoming a hard dependency rather than a convenience
7275
// preset - however, that requires us to duplicate the respective
7376
// configuration keys and buckets here
74-
defaults.forEach(plugin => {
75-
registerPlugin(registry, plugin, false);
76-
});
77-
plugins.forEach(plugin => {
78-
registerPlugin(registry, plugin, true);
79-
});
77+
for(let plugin of defaults) {
78+
await registerPlugin(registry, plugin, false);
79+
}
80+
for(let plugin of plugins) {
81+
await registerPlugin(registry, plugin, true);
82+
}
8083
return registry;
8184
}
8285

83-
function registerPlugin(registry, _plugin, eager) {
84-
let { key, bucket, plugin } = resolvePlugin(_plugin, eager);
86+
async function registerPlugin(registry, _plugin, eager) {
87+
let { key, bucket, plugin } = await resolvePlugin(_plugin, eager);
8588
// NB: default plugins may be overridden
8689
if(registry[key] && !DEFAULT_KEYS.has(key)) {
8790
abort(`ERROR: duplicate plugin key ${repr(key, false)}`);
@@ -92,14 +95,14 @@ function registerPlugin(registry, _plugin, eager) {
9295
}
9396
}
9497

95-
function resolvePlugin(_plugin, eager) {
98+
async function resolvePlugin(_plugin, eager) {
9699
if(_plugin.substr) { // package identifier
97-
_plugin = loadPlugin(_plugin);
100+
_plugin = await loadPlugin(_plugin);
98101
}
99102

100103
let { key, bucket, plugin } = _plugin;
101104
if(eager && plugin.substr && (!key || !bucket)) { // auto-configuration
102-
let _plugin = loadPlugin(plugin);
105+
let _plugin = await loadPlugin(plugin);
103106
plugin = _plugin.plugin;
104107
// local configuration takes precedence
105108
key = key || _plugin.key;
@@ -112,13 +115,13 @@ function resolvePlugin(_plugin, eager) {
112115
return { key, bucket, plugin };
113116
}
114117

115-
function loadPlugin(pkg) {
118+
async function loadPlugin(pkg) {
116119
let fail = prop => abort(`ERROR: invalid plugin ${repr(pkg)}; ` +
117120
`missing ${repr(prop, false)}`);
118121
let {
119122
key = fail("key"),
120123
bucket = fail("bucket"),
121124
plugin = fail("plugin")
122-
} = loadExtension(pkg, "ERROR: missing plugin");
125+
} = await loadExtension(pkg, "ERROR: missing plugin");
123126
return { key, bucket, plugin };
124127
}

lib/server.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ let DEFAULTS = {
55
port: 3000
66
};
77

8-
exports.static = (config, webroot) => {
9-
let donny = loadExtension("donny", "failed to activate server");
8+
exports.static = async (config, webroot) => {
9+
let donny = await loadExtension("donny", "failed to activate server");
1010
let [host, port] = parseHost(config);
1111

12-
donny({ port, bind: host, webroot }).
13-
then(() => {
14-
console.error(`serving ${repr(webroot)} at http://${host}:${port}`);
15-
});
12+
await donny({ port, bind: host, webroot });
13+
console.error(`serving ${repr(webroot)} at http://${host}:${port}`);
1614
};
1715

18-
exports.live = (config, root) => {
19-
let liveServer = loadExtension("live-server", "failed to activate live-server");
16+
exports.live = async (config, root) => {
17+
let liveServer = await loadExtension("live-server", "failed to activate live-server");
2018
let [host, port] = parseHost(config);
2119

2220
liveServer.start({ port, host, root, open: false });

lib/util/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ exports.reportFileStatus = (filepath, referenceDir, error) => {
1313
console.error(error ? `✗ ${ref}: ${error.message || error}` : `✓ ${ref}`);
1414
};
1515

16-
// attempts to `require` a module, prompting the user to install the
17-
// corresponding package if it is unavailable
18-
exports.loadExtension = (pkg, errorMessage, supplier = pkg) => {
16+
// attempts to load a module, prompting the user to install the corresponding
17+
// package if it is unavailable
18+
exports.loadExtension = async (pkg, errorMessage, supplier = pkg) => {
1919
try {
20-
return require(pkg);
20+
return await require(pkg);
2121
} catch(err) {
2222
if(err.code !== "MODULE_NOT_FOUND") {
2323
throw err;

0 commit comments

Comments
 (0)