| 1 | /**
|
|---|
| 2 | * Copyright 2018 Google Inc. All Rights Reserved.
|
|---|
| 3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 4 | * you may not use this file except in compliance with the License.
|
|---|
| 5 | * You may obtain a copy of the License at
|
|---|
| 6 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 7 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 8 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 10 | * See the License for the specific language governing permissions and
|
|---|
| 11 | * limitations under the License.
|
|---|
| 12 | */
|
|---|
| 13 |
|
|---|
| 14 | // If the loader is already loaded, just stop.
|
|---|
| 15 | if (!self.<%- amdFunctionName %>) {
|
|---|
| 16 | let registry = {};
|
|---|
| 17 |
|
|---|
| 18 | // Used for `eval` and `importScripts` where we can't get script URL by other means.
|
|---|
| 19 | // In both cases, it's safe to use a global var because those functions are synchronous.
|
|---|
| 20 | let nextDefineUri;
|
|---|
| 21 |
|
|---|
| 22 | const singleRequire = (uri, parentUri) => {
|
|---|
| 23 | uri = new URL(uri + ".js", parentUri).href;
|
|---|
| 24 | return registry[uri] || (
|
|---|
| 25 | <% if (useEval) { %>
|
|---|
| 26 | fetch(uri)
|
|---|
| 27 | .then(resp => resp.text())
|
|---|
| 28 | .then(code => {
|
|---|
| 29 | nextDefineUri = uri;
|
|---|
| 30 | eval(code);
|
|---|
| 31 | })
|
|---|
| 32 | <% } else { %>
|
|---|
| 33 | new Promise(resolve => {
|
|---|
| 34 | if ("document" in self) {
|
|---|
| 35 | const script = document.createElement("script");
|
|---|
| 36 | script.src = uri;
|
|---|
| 37 | script.onload = resolve;
|
|---|
| 38 | document.head.appendChild(script);
|
|---|
| 39 | } else {
|
|---|
| 40 | nextDefineUri = uri;
|
|---|
| 41 | importScripts(uri);
|
|---|
| 42 | resolve();
|
|---|
| 43 | }
|
|---|
| 44 | })
|
|---|
| 45 | <% } %>
|
|---|
| 46 | .then(() => {
|
|---|
| 47 | let promise = registry[uri];
|
|---|
| 48 | if (!promise) {
|
|---|
| 49 | throw new Error(`Module ${uri} didn’t register its module`);
|
|---|
| 50 | }
|
|---|
| 51 | return promise;
|
|---|
| 52 | })
|
|---|
| 53 | );
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | self.<%- amdFunctionName %> = (depsNames, factory) => {
|
|---|
| 57 | const uri = nextDefineUri || ("document" in self ? document.currentScript.src : "") || location.href;
|
|---|
| 58 | if (registry[uri]) {
|
|---|
| 59 | // Module is already loading or loaded.
|
|---|
| 60 | return;
|
|---|
| 61 | }
|
|---|
| 62 | let exports = {};
|
|---|
| 63 | const require = depUri => singleRequire(depUri, uri);
|
|---|
| 64 | const specialDeps = {
|
|---|
| 65 | module: { uri },
|
|---|
| 66 | exports,
|
|---|
| 67 | require
|
|---|
| 68 | };
|
|---|
| 69 | registry[uri] = Promise.all(depsNames.map(
|
|---|
| 70 | depName => specialDeps[depName] || require(depName)
|
|---|
| 71 | )).then(deps => {
|
|---|
| 72 | factory(...deps);
|
|---|
| 73 | return exports;
|
|---|
| 74 | });
|
|---|
| 75 | };
|
|---|
| 76 | }
|
|---|