Skip to content

Instantly share code, notes, and snippets.

@brainysmurf
Created March 30, 2021 02:27
Show Gist options
  • Save brainysmurf/c166f9bd2f914ab1d620d3b36ec642b7 to your computer and use it in GitHub Desktop.
Save brainysmurf/c166f9bd2f914ab1d620d3b36ec642b7 to your computer and use it in GitHub Desktop.
BundlerPattern for appsscripts libraries
/**
* Should be listed first in the list. Creates the `Import` object and adds properties to it
*/
// create a simple object where we can add namespaces
const Import = Object.create(null);
(function (exports) {
// define your stuff in an iife …
// … a class
class Namespace {
constructor (id) {
this.id = id;
}
}
// … methods, or whatver
const method = () => {};
// add them to the exports variable
exports.Namespace = Namespace;
exports.method = method;
})(Import);
// invoke the iife with the `Import` global
/**
* This is where you write functions where other devs can interact with your library
* You're able to "import" stuff from the bundle
*/
// Since Bundle is the first file, you'll have the `Import` object:
// "import" stuff you need throughout this file here
const {Namespace} = Import;
function fromId(id) {
return Namespace.fromId(id);
}
function doSomething (id) {
const ns = Namespace.fromId(id);
// import stuff from the bundle that you need for just this function here
const {method} = Import;
return method(id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment