0.0.3 • Published 3 years ago
@webreflection/hydra v0.0.3
hydra
An utility to create SSR friendly stream-able modules.
API
The module exports a single hydra(config)
entry that can be used to .transform()
modules as tool, or to .map()
all modules as {name, code, module}
strings that can be injected, or used, as needed.
import {hydra} from '@webreflection/hydra';
const config = {
// optional: where to store the cache on transform()
// if not provided, no file is stored
output: '/path/to/transformed.json',
// optional: all default options per each module
defaults: {
// if false, no transpilation happens
babel: true,
// if false, no minification happens
minify: true,
// if empty, modules are defined globally as var
// otherwise it can be any reachable namespace
namespace: 'window',
// used to prefix unique IDs/names per each module
prefix: '_M',
// an object with import "names" => "/different/path.js"
// useful to avoid repeating imports of shared modules
replace: {
'nowhere': '/path/to/somewhere.js',
'@ping/pong': '/path/to/pong-ping.js'
},
},
// a list of modules by name and details, where
// names will be available through the map
modules: {
Default: {
// mandatory: where is the module
input: '/path/to/module.js',
// optional: a callback to invoke once the module is live
code(module, uniqueId) {
// the module is like an import(path) resolved
// it has all the exports by name, including default
// the uniqueId is the "private" module name in the namespace
// so that there are no conflicts in the wild and it can be used
// for DOM related operations (i.e. element hydration on the fly)
}
},
Export: {
// all defaults can be overridden
babel: false,
// if the namespace is empty, modules will be defined
// as unique IDs as variable (usually global scope)
namespace: '',
input: '/path/to/module2.js',
code: ({Test}, uid) => {
console.log(Test, uid);
}
}
}
};
const {map, transform} = hydra(config);
// transform all modules and save these in the json output
await transform();
// returns a map with all modules details to be used on demand
const modules = map();
modules.get('Default');
// shows {name, code, module} strings