Skip to content

Instantly share code, notes, and snippets.

@mohdovais
Created August 5, 2019 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohdovais/462ccada3c2ac2057f60331ed1bd543c to your computer and use it in GitHub Desktop.
Save mohdovais/462ccada3c2ac2057f60331ed1bd543c to your computer and use it in GitHub Desktop.
Rollup plugin to create custom UMD package; the module is always exposed as global variable
const { parse } = require("acorn");
const expressionStatement = node => node.type === "ExpressionStatement";
function chainedProperty(subject, chainString) {
const chain = Array.isArray(chainString)
? chainString
: chainString.split(".");
const count = chain.length;
let index = 0;
let property = subject;
for (; index < count; index++) {
property = property[chain[index]];
if (property === null || property === undefined) {
break;
}
}
return property;
}
function toArray(subject) {
return Array.isArray(subject)
? subject
: subject !== null && subject !== undefined
? [subject]
: [];
}
function firstExpressionStatement(node) {
return toArray(node).filter(expressionStatement)[0];
}
function getIIFECallee(node) {
return firstExpressionStatement(
chainedProperty(
firstExpressionStatement(node.body),
"expression.callee.body.body"
)
);
}
function getExpressionLastAlternate(node) {
return chainedProperty(node, "expression.alternate.alternate");
}
function transform(code) {
const ast = parse(code);
const iifeCalleeExpression = getIIFECallee(ast);
if (iifeCalleeExpression === undefined) {
throw new Error('Could not find IIFE callee expression');
}
const alternateExpression = getExpressionLastAlternate(iifeCalleeExpression);
if (alternateExpression === undefined) {
throw new Error('Could not find last alternate to condition expression');
}
const {
start: sequenceExpressionStart,
end: sequenceExpressionEnd
} = alternateExpression;
const { start: iifeCalleeStart, end: iifeCalleeEnd } = iifeCalleeExpression;
const sequenceExpressionString = code.substring(
sequenceExpressionStart - 1,
sequenceExpressionEnd + 1
);
return (
code.substring(0, sequenceExpressionStart) +
"0" +
code.substring(sequenceExpressionEnd, iifeCalleeEnd) +
sequenceExpressionString +
code.substring(iifeCalleeEnd)
);
}
export default function globalUMD() {
return {
name: "custom-umd",
generateBundle: function(options, bundles, isWrite) {
Object.keys(bundles).forEach(function(bundleName) {
const bundle = bundles[bundleName];
const { code, isAsset, isEntry } = bundle;
if (options.format === "umd" && !isAsset && isEntry && isWrite) {
bundle.code = transform(code);
}
});
}
};
}
import customUMD from "./rollup-plugin-custom-umd";
const name = "org.my-app-name";
const moduleName = name.split(".").pop();
export default {
input: "src/main.js",
output: {
file: `dist/${moduleName}.js`,
format: "umd",
name,
amd: {
id: moduleName
},
},
plugins: [customUMD()]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment