Skip to content

Instantly share code, notes, and snippets.

@drwpow
Last active July 27, 2020 18:35
Show Gist options
  • Save drwpow/967d24a95fd39b48c20ba2711b7d31fc to your computer and use it in GitHub Desktop.
Save drwpow/967d24a95fd39b48c20ba2711b7d31fc to your computer and use it in GitHub Desktop.
Snowpacker plugin
// Example of using require.context() plugin
// note: this will run on every `.js` file
module.exports = () => ({
name: 'my awesome plugin',
async transform({
extension, // the file extension
contents // JS code
}) {
if (extension !== '.js') return; // if not JS, ignore
const context = require.context('./controllers'); // grab context
const imports = myFunctionToGenerateImportsFromContext(context); // TODO: generate the necessary import statements
return `${imports}${contents}`; // final code with imports injected at top
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment