Created
January 11, 2018 21:02
-
-
Save segphault/c084faf4b6dfbeb3ba7d4b7158d17213 to your computer and use it in GitHub Desktop.
Bundling remote dependencies with rollup
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
const http = require("http"); | |
const https = require("https"); | |
const isRemote = path => | |
path.startsWith("http://") || path.startsWith("https://"); | |
const protocol = url => | |
url.startsWith("https://") ? https : http; | |
const fetch = url => | |
new Promise((resolve, reject) => | |
protocol(url).get(url, response => { | |
let output = ""; | |
response.on("data", chunk => output += chunk); | |
response.on("end", () => resolve(output)); | |
}).on("error", err => reject(err))); | |
const resolveURL = { | |
name: "URL Resolver", | |
resolveId(importee) { | |
if (isRemote(importee)) | |
return importee; | |
}, | |
load(id) { | |
if (isRemote(id)) | |
return fetch(id); | |
} | |
}; | |
export default { | |
entry: "plugins/dashboard/public/index.js", | |
dest: "plugins/dashboard/public/bundle.js", | |
format: "es", | |
plugins: [resolveURL] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment