diff --git a/README.md b/README.md index a2dd6b28..8944356f 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ Append a `/` at the end of a URL to view a listing of all the files in a package
?module
Expands all ["bare" `import` specifiers](https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier) in JavaScript modules to unpkg URLs. This feature is *very experimental*
+ +
?bundle
+
Returns the specified bundle, or the default one if it is not available
### Cache Behavior diff --git a/docs/home.md b/docs/home.md index 4af3ba27..db70eac7 100644 --- a/docs/home.md +++ b/docs/home.md @@ -33,6 +33,9 @@ Append a `/` at the end of a URL to view a listing of all the files in a package
`?module`
Expands all ["bare" `import` specifiers](https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier) in JavaScript modules to unpkg URLs. This feature is *very experimental*
+ +
`?bundle`
+
Returns the specified bundle, or the default one if it is not available
### Workflow diff --git a/server/middleware/findFile.js b/server/middleware/findFile.js index 5f6154de..00f89b51 100644 --- a/server/middleware/findFile.js +++ b/server/middleware/findFile.js @@ -90,6 +90,20 @@ function findFile(req, res, next) { // The "unpkg" field allows packages to explicitly declare the // file to serve at the bare URL. filename = req.packageConfig.unpkg; + } else if (typeof req.packageConfig.unpkg === "object") { + // The "unpkg" field can also be an object specifying a more + // advanced configuration. The "bundles" field is used to + // specify different entry points for the same package. + const bundles = req.packageConfig.unpkg.bundles; + if (typeof bundles === "object") { + // Use the `bundle` get parameter. Fall back to the default + // bundle if the parameter is not present. + const requestedBundle = req.query.bundle || "default"; + // Check if the requested bundle exists + // If it doesn't exist in the available bundles, fall back + // to the default one. + filename = bundles[requestedBundle] || bundles.default; + } } else if (typeof req.packageConfig.browser === "string") { // Fall back to the "browser" field if declared (only support strings). // Deprecated, see https://github.com/unpkg/unpkg/issues/63 @@ -98,7 +112,8 @@ function findFile(req, res, next) { // Count which packages + versions are actually using this fallback // so we can warn them when we deprecate this functionality. incrementCounter("package-json-browser-fallback", req.packageSpec, 1); - } else { + } + if (!filename) { // Fall back to "main" or / (same as npm). filename = req.packageConfig.main || "/"; } diff --git a/server/middleware/validateQuery.js b/server/middleware/validateQuery.js index f9b524e0..62e00831 100644 --- a/server/middleware/validateQuery.js +++ b/server/middleware/validateQuery.js @@ -3,7 +3,8 @@ const createSearch = require("../utils/createSearch"); const knownQueryParams = { main: true, // Deprecated, see #63 meta: true, - module: true + module: true, + bundle: true }; function isKnownQueryParam(param) {