-
Notifications
You must be signed in to change notification settings - Fork 13
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 #197 from eecs485staff/april-fools-languages
[Plugin] April Fools Languages joke
- Loading branch information
Showing
17 changed files
with
940 additions
and
60 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,33 +1,39 @@ | ||
import { initialize as initializeHalloweenPlugin } from './halloween.plugin'; | ||
/** | ||
* Conditional plugins are loaded asynchronously and are intentionally | ||
* isolated from the rest of Primer Spec. This is because this conditional | ||
* plugin framework was designed to build temporary pranks and jokes! (We don't | ||
* want these jokes to affect the page load time and the spec-reading | ||
* experience.) | ||
* | ||
* Plugins run based on conditions defined in the `shouldLoadPlugin()` method. | ||
* They can also be force-enabled by inserting | ||
* `?enable_<plugin_id>=1` in the URL. | ||
*/ | ||
|
||
import type { ConditionalPluginInput } from './types.d'; | ||
import { shouldLoadPlugin } from './should_load_plugin'; | ||
import { loadPlugin } from './load_plugin'; | ||
|
||
const PLUGINS = [initializeHalloweenPlugin()] | ||
.filter((pluginDefinition) => { | ||
const forceEnableOption = pluginForceEnableOption(pluginDefinition.id); | ||
if (forceEnableOption !== null) { | ||
return forceEnableOption; | ||
} | ||
return pluginDefinition.shouldRun(); | ||
}) | ||
.map((pluginDefinition) => pluginDefinition.plugin); | ||
/** | ||
* When adding a new Plugin: | ||
* 1. Add the plugin definition to `./plugins/[your-plugin].plugin.ts` | ||
* 2. Choose a plugin ID, then add it to this list | ||
* 3. Add a condition to `shouldLoadPlugin()` for this plugin ID | ||
* 4. Update `loadPlugin()` to load the plugin definition from (1) | ||
*/ | ||
const PLUGIN_IDS = ['halloween', 'april_fools_languages']; | ||
|
||
const pluginsPromises = PLUGIN_IDS.filter((pluginId) => | ||
shouldLoadPlugin(pluginId), | ||
).map((pluginId) => loadPlugin(pluginId)); | ||
|
||
export async function executePlugins( | ||
input: ConditionalPluginInput, | ||
): Promise<void> { | ||
const plugins = await Promise.all(pluginsPromises); | ||
await Promise.all( | ||
PLUGINS.map(async (plugin) => { | ||
await plugin(input); | ||
plugins.map(async (plugin) => { | ||
await plugin?.(input); | ||
}), | ||
); | ||
} | ||
|
||
function pluginForceEnableOption(pluginId: string): boolean | null { | ||
const match = window.location.search.match( | ||
new RegExp(`enable_${pluginId}=([0|1])`), | ||
); | ||
if (match) { | ||
return match[1] === '1'; | ||
} | ||
return null; | ||
} |
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,23 @@ | ||
import { Plugin } from './types.d'; | ||
|
||
/** | ||
* Given a plugin ID, lazy-load the appropriate JS module containing the plugin | ||
* definition and return the plugin. | ||
* | ||
* Notice that we use the dynamic `import()` syntax. Webpack identifies this as | ||
* an opportunity to split the JS bundle, hence decreasing the size of the main | ||
* Primer Spec JS bundle. Additionally, we won't download the JS code for all | ||
* plugins, only the ones that need to run. | ||
*/ | ||
export async function loadPlugin(pluginId: string): Promise<Plugin | null> { | ||
let plugin: Plugin | null = null; | ||
switch (pluginId) { | ||
case 'halloween': | ||
plugin = (await import('./plugins/halloween.plugin')).default; | ||
break; | ||
case 'april_fools_languages': | ||
plugin = (await import('./plugins/april_fools_languages.plugin')).default; | ||
break; | ||
} | ||
return plugin; | ||
} |
Oops, something went wrong.