importmap-rails 0.4.3 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +94 -16
- data/app/assets/javascripts/es-module-shims.js +1 -1
- data/app/assets/javascripts/es-module-shims@0.14.0.csp.js +1635 -0
- data/app/helpers/importmap/importmap_tags_helper.rb +18 -7
- data/lib/importmap/commands.rb +45 -3
- data/lib/importmap/map.rb +1 -1
- data/lib/importmap/packager.rb +68 -0
- data/lib/importmap/version.rb +1 -1
- data/lib/install/config/importmap.rb +0 -4
- metadata +4 -18
- data/app/assets/javascripts/es-module-shims@0.12.8.js +0 -698
- data/lib/importmap/pinner.rb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5c7d66e249679156d7602787f9edd58abbabee35a8e8b75ee88b87cd729ac3b
|
4
|
+
data.tar.gz: a1af718e58dc8d15865c7e6ebd1c0f0508b237a6fbd83db710c5fb38c869cbb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db464f8246d2b23941b7841224e857f134c23fe7da5a5750005f17a32bf345e0d0355ef8a849ca3570c38ab213cc3ed31e9a42f9eabd0ce1bb585d0636b340ec
|
7
|
+
data.tar.gz: 5992e6f9db39cf53ba3ae0078db871fab936de376655717d7acdd74419ae230ab360a9765890e9adf15f21d560016c617989d777fe1f6be4f537241ebd17b6a9
|
data/README.md
CHANGED
@@ -1,49 +1,48 @@
|
|
1
1
|
# Importmap for Rails
|
2
2
|
|
3
|
-
[Import maps](https://github.com/WICG/import-maps) let you import JavaScript modules using logical names that map to versioned/digested files – directly from the browser. So you can build modern JavaScript applications using JavaScript libraries made for ESM without the need for transpiling or bundling.
|
3
|
+
[Import maps](https://github.com/WICG/import-maps) let you import JavaScript modules using logical names that map to versioned/digested files – directly from the browser. So you can [build modern JavaScript applications using JavaScript libraries made for ESM without the need for transpiling or bundling](https://world.hey.com/dhh/modern-web-apps-without-javascript-bundling-or-transpiling-a20f2755).This frees you from needing Webpack, Yarn, npm, or any other part of the JavaScript toolchain. All you need is the asset pipeline that's already included in Rails.
|
4
4
|
|
5
|
-
With this approach you'll ship many small JavaScript files instead of one big JavaScript file. Thanks to HTTP2 that no longer carries a material performance penalty during the initial transport, and in fact offers substantial benefits over the long run due to better caching dynamics. Whereas before
|
5
|
+
With this approach you'll ship many small JavaScript files instead of one big JavaScript file. Thanks to HTTP2 that no longer carries a material performance penalty during the initial transport, and in fact offers substantial benefits over the long run due to better caching dynamics. Whereas before any change to any JavaScript file included in your big bundle would invalidate the cache for the the whole bundle, now only the cache for that single file is invalidated.
|
6
6
|
|
7
7
|
There's [native support for import maps in Chrome/Edge 89+](https://caniuse.com/?search=importmap), and [a shim available](https://github.com/guybedford/es-module-shims) for any browser with basic ESM support. So your app will be able to work with all the evergreen browsers.
|
8
8
|
|
9
9
|
|
10
10
|
## Installation
|
11
11
|
|
12
|
-
|
12
|
+
Importmap for Rails is automatically included in Rails 7+ for new applications, but you can also install it manually in existing applications:
|
13
|
+
|
14
|
+
1. Add `importmap-rails` to your Gemfile with `gem 'importmap-rails'`
|
13
15
|
2. Run `./bin/bundle install`
|
14
16
|
3. Run `./bin/rails importmap:install`
|
15
17
|
|
16
|
-
By default, all the files in `app/assets/javascripts` and the three major Rails JavaScript libraries are already mapped. You can add more mappings in `config/importmap.rb`.
|
17
|
-
|
18
18
|
Note: In order to use JavaScript from Rails frameworks like Action Cable, Action Text, and Active Storage, you must be running Rails 7.0+. This was the first version that shipped with ESM compatible builds of these libraries.
|
19
19
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
The import map is
|
23
|
+
The import map is setup through `Rails.application.config.importmap` via the configuration in `config/importmap.rb`. This file is automatically reloaded in development upon changes, but note that you must restart the server if you remove pins and need them gone from the rendered importmap or list of preloads.
|
24
24
|
|
25
|
-
This
|
25
|
+
This import map is inlined in the `<head>` of your application layout using `<%= javascript_importmap_tags %>`, which will setup the JSON configuration inside a `<script type="importmap">` tag. After that, the [es-module-shim](https://github.com/guybedford/es-module-shims) is loaded, and then finally the application entrypoint is imported via `<script type="module">import "application"</script>`. That logical entrypoint, `application`, is mapped in the importmap script tag to the file `app/javascript/application.js`.
|
26
26
|
|
27
27
|
It's in `app/javascript/application.js` you setup your application by importing any of the modules that have been defined in the import map. You can use the full ESM functionality of importing any particular export of the modules or everything.
|
28
28
|
|
29
|
-
It makes sense to use logical names that match the package names used by
|
29
|
+
It makes sense to use logical names that match the package names used by npm, such that if you later want to start transpiling or bundling your code, you won't have to change any module imports.
|
30
30
|
|
31
31
|
|
32
32
|
## Using node modules via JavaScript CDNs
|
33
33
|
|
34
34
|
Importmap for Rails is designed to be used with JavaScript CDNs for your node package dependencies. The CDNs provide pre-compiled distribution versions ready to use, and offer a fast, efficient way of serving them.
|
35
35
|
|
36
|
-
You can use the `./bin/importmap` command that's added as part of the install to pin
|
36
|
+
You can use the `./bin/importmap` command that's added as part of the install to pin, unpin, or update node packages in your import map. This command uses an API from [JSPM.org](https://jspm.org) to resolve your package dependencies efficiently, and then add the pins to your `config/importmap.rb` file. It can resolve these dependencies from JSPM itself, but also from other CDNs, like [unpkg.com](https://unpkg.com) and [jsdelivr.com](https://www.jsdelivr.com).
|
37
37
|
|
38
38
|
It works like so:
|
39
39
|
|
40
40
|
```bash
|
41
41
|
./bin/importmap pin react react-dom
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
Pinned 'scheduler' to https://ga.jspm.io/npm:scheduler@0.20.2/index.js
|
42
|
+
Pinning "react" to https://ga.jspm.io/npm:react@17.0.2/index.js
|
43
|
+
Pinning "react-dom" to https://ga.jspm.io/npm:react-dom@17.0.2/index.js
|
44
|
+
Pinning "object-assign" to https://ga.jspm.io/npm:object-assign@4.1.1/index.js
|
45
|
+
Pinning "scheduler" to https://ga.jspm.io/npm:scheduler@0.20.2/index.js
|
47
46
|
|
48
47
|
./bin/importmap json
|
49
48
|
|
@@ -67,12 +66,65 @@ import React from "react"
|
|
67
66
|
import ReactDOM from "react-dom"
|
68
67
|
```
|
69
68
|
|
70
|
-
|
69
|
+
You can also designate a specific version to pin:
|
70
|
+
|
71
|
+
```bash
|
72
|
+
./bin/importmap pin react@17.0.1
|
73
|
+
Pinning "react" to https://ga.jspm.io/npm:react@17.0.1/index.js
|
74
|
+
Pinning "object-assign" to https://ga.jspm.io/npm:object-assign@4.1.1/index.js
|
75
|
+
```
|
76
|
+
|
77
|
+
Or even remove pins:
|
78
|
+
|
79
|
+
```bash
|
80
|
+
./bin/importmap unpin react
|
81
|
+
Unpinning "react"
|
82
|
+
Unpinning "object-assign"
|
83
|
+
```
|
84
|
+
|
85
|
+
If you pin a package that has already been pinned, it'll be updated inline, along with its dependencies.
|
86
|
+
|
87
|
+
You can control the environment of the package for packages with separate "production" (the default) and "development" builds:
|
88
|
+
|
89
|
+
```bash
|
90
|
+
./bin/importmap pin react --env development
|
91
|
+
Pinning "react" to https://ga.jspm.io/npm:react@17.0.2/dev.index.js
|
92
|
+
Pinning "object-assign" to https://ga.jspm.io/npm:object-assign@4.1.1/index.js
|
93
|
+
```
|
94
|
+
|
95
|
+
You can also pick an alternative, supported CDN provider when pinning, like `unpkg` or `jsdelivr` (`jspm` is the default):
|
96
|
+
|
97
|
+
```bash
|
98
|
+
./bin/importmap pin react --from jsdelivr
|
99
|
+
Pinning "react" to https://cdn.jsdelivr.net/npm/react@17.0.2/index.js
|
100
|
+
```
|
101
|
+
|
102
|
+
Remember, though, that if you switch a pin from one provider to another, you may have to clean up dependencies added by the first provider that isn't used by the second provider.
|
103
|
+
|
104
|
+
Run `./bin/importmap` to see all options.
|
105
|
+
|
106
|
+
Note that this command is merely a convenience wrapper to resolving logical package names to CDN URLs. You can also just lookup the CDN URLs yourself, and then pin those. For example, if you wanted to use Skypack for React, you could just add the following to `config/importmap.rb`:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
pin "react", to: "https://cdn.skypack.dev/react"
|
110
|
+
```
|
111
|
+
|
112
|
+
|
113
|
+
## What if I don't like to use a JavaScript CDN?
|
114
|
+
|
115
|
+
You always have the option to simply download the compiled JavaScript packages from the CDNs, and saving them locally in your application. You can put such files in app/javascript/vendor, and then reference them with local pins, like:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
# config/importmap.rb
|
119
|
+
pin "react", to: "vendor/react@17.0.2.js"
|
120
|
+
```
|
121
|
+
|
122
|
+
But using a JavaScript CDN is fast, secure, and easier to deal with. Start there.
|
71
123
|
|
72
124
|
|
73
125
|
## Preloading pinned modules
|
74
126
|
|
75
|
-
To
|
127
|
+
To avoid the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, we use [modulepreload links](https://developers.google.com/web/updates/2017/12/modulepreload). Pinned modules are preloaded by default, but you can turn this off with `preload: false`.
|
76
128
|
|
77
129
|
Example:
|
78
130
|
|
@@ -89,6 +141,32 @@ pin "md5", to: "https://cdn.jsdelivr.net/npm/md5@2.3.0/md5.js", preload: false
|
|
89
141
|
...
|
90
142
|
```
|
91
143
|
|
144
|
+
## Composing import maps
|
145
|
+
|
146
|
+
By default, Rails loads import map definition from the application's `config/importmap.rb` to the `Importmap::Map` object available at `Rails.application.config.importmap`.
|
147
|
+
|
148
|
+
You can combine multiple import maps by drawing their definitions onto the `Rails.application.config.importmap`. For example, appending import maps defined in Rails engines:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
# my_engine/lib/my_engine/engine.rb
|
152
|
+
|
153
|
+
module MyEngine
|
154
|
+
class Engine < ::Rails::Engine
|
155
|
+
# ...
|
156
|
+
initializer "my-engine.importmap" do |app|
|
157
|
+
app.config.importmap.draw(Engine.root.join("config/importmap.rb"))
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
```
|
162
|
+
|
163
|
+
And pinning JavaScript modules from the engine:
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
# my_engine/config/importmap.rb
|
167
|
+
|
168
|
+
pin_all_from File.expand_path("../app/assets/javascripts", __dir__)
|
169
|
+
```
|
92
170
|
|
93
171
|
## Caching the import map and preload modules
|
94
172
|
|
@@ -1 +1 @@
|
|
1
|
-
//= require ./es-module-shims@0.
|
1
|
+
//= require ./es-module-shims@0.14.0.csp.js
|
@@ -0,0 +1,1635 @@
|
|
1
|
+
/* ES Module Shims CSP 0.14.0 */
|
2
|
+
(function () {
|
3
|
+
|
4
|
+
const resolvedPromise = Promise.resolve();
|
5
|
+
|
6
|
+
const edge = navigator.userAgent.match(/Edge\/\d\d\.\d+$/);
|
7
|
+
|
8
|
+
let baseUrl;
|
9
|
+
|
10
|
+
function createBlob (source, type = 'text/javascript') {
|
11
|
+
return URL.createObjectURL(new Blob([source], { type }));
|
12
|
+
}
|
13
|
+
|
14
|
+
const noop = () => {};
|
15
|
+
|
16
|
+
const baseEl = document.querySelector('base[href]');
|
17
|
+
if (baseEl)
|
18
|
+
baseUrl = baseEl.href;
|
19
|
+
|
20
|
+
if (!baseUrl && typeof location !== 'undefined') {
|
21
|
+
baseUrl = location.href.split('#')[0].split('?')[0];
|
22
|
+
const lastSepIndex = baseUrl.lastIndexOf('/');
|
23
|
+
if (lastSepIndex !== -1)
|
24
|
+
baseUrl = baseUrl.slice(0, lastSepIndex + 1);
|
25
|
+
}
|
26
|
+
|
27
|
+
function isURL (url) {
|
28
|
+
try {
|
29
|
+
new URL(url);
|
30
|
+
return true;
|
31
|
+
}
|
32
|
+
catch {
|
33
|
+
return false;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
const backslashRegEx = /\\/g;
|
38
|
+
function resolveIfNotPlainOrUrl (relUrl, parentUrl) {
|
39
|
+
// strip off any trailing query params or hashes
|
40
|
+
parentUrl = parentUrl && parentUrl.split('#')[0].split('?')[0];
|
41
|
+
if (relUrl.indexOf('\\') !== -1)
|
42
|
+
relUrl = relUrl.replace(backslashRegEx, '/');
|
43
|
+
// protocol-relative
|
44
|
+
if (relUrl[0] === '/' && relUrl[1] === '/') {
|
45
|
+
return parentUrl.slice(0, parentUrl.indexOf(':') + 1) + relUrl;
|
46
|
+
}
|
47
|
+
// relative-url
|
48
|
+
else if (relUrl[0] === '.' && (relUrl[1] === '/' || relUrl[1] === '.' && (relUrl[2] === '/' || relUrl.length === 2 && (relUrl += '/')) ||
|
49
|
+
relUrl.length === 1 && (relUrl += '/')) ||
|
50
|
+
relUrl[0] === '/') {
|
51
|
+
const parentProtocol = parentUrl.slice(0, parentUrl.indexOf(':') + 1);
|
52
|
+
// Disabled, but these cases will give inconsistent results for deep backtracking
|
53
|
+
//if (parentUrl[parentProtocol.length] !== '/')
|
54
|
+
// throw new Error('Cannot resolve');
|
55
|
+
// read pathname from parent URL
|
56
|
+
// pathname taken to be part after leading "/"
|
57
|
+
let pathname;
|
58
|
+
if (parentUrl[parentProtocol.length + 1] === '/') {
|
59
|
+
// resolving to a :// so we need to read out the auth and host
|
60
|
+
if (parentProtocol !== 'file:') {
|
61
|
+
pathname = parentUrl.slice(parentProtocol.length + 2);
|
62
|
+
pathname = pathname.slice(pathname.indexOf('/') + 1);
|
63
|
+
}
|
64
|
+
else {
|
65
|
+
pathname = parentUrl.slice(8);
|
66
|
+
}
|
67
|
+
}
|
68
|
+
else {
|
69
|
+
// resolving to :/ so pathname is the /... part
|
70
|
+
pathname = parentUrl.slice(parentProtocol.length + (parentUrl[parentProtocol.length] === '/'));
|
71
|
+
}
|
72
|
+
|
73
|
+
if (relUrl[0] === '/')
|
74
|
+
return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
|
75
|
+
|
76
|
+
// join together and split for removal of .. and . segments
|
77
|
+
// looping the string instead of anything fancy for perf reasons
|
78
|
+
// '../../../../../z' resolved to 'x/y' is just 'z'
|
79
|
+
const segmented = pathname.slice(0, pathname.lastIndexOf('/') + 1) + relUrl;
|
80
|
+
|
81
|
+
const output = [];
|
82
|
+
let segmentIndex = -1;
|
83
|
+
for (let i = 0; i < segmented.length; i++) {
|
84
|
+
// busy reading a segment - only terminate on '/'
|
85
|
+
if (segmentIndex !== -1) {
|
86
|
+
if (segmented[i] === '/') {
|
87
|
+
output.push(segmented.slice(segmentIndex, i + 1));
|
88
|
+
segmentIndex = -1;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
// new segment - check if it is relative
|
93
|
+
else if (segmented[i] === '.') {
|
94
|
+
// ../ segment
|
95
|
+
if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {
|
96
|
+
output.pop();
|
97
|
+
i += 2;
|
98
|
+
}
|
99
|
+
// ./ segment
|
100
|
+
else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
|
101
|
+
i += 1;
|
102
|
+
}
|
103
|
+
else {
|
104
|
+
// the start of a new segment as below
|
105
|
+
segmentIndex = i;
|
106
|
+
}
|
107
|
+
}
|
108
|
+
// it is the start of a new segment
|
109
|
+
else {
|
110
|
+
segmentIndex = i;
|
111
|
+
}
|
112
|
+
}
|
113
|
+
// finish reading out the last segment
|
114
|
+
if (segmentIndex !== -1)
|
115
|
+
output.push(segmented.slice(segmentIndex));
|
116
|
+
return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join('');
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
/*
|
121
|
+
* Import maps implementation
|
122
|
+
*
|
123
|
+
* To make lookups fast we pre-resolve the entire import map
|
124
|
+
* and then match based on backtracked hash lookups
|
125
|
+
*
|
126
|
+
*/
|
127
|
+
function resolveUrl (relUrl, parentUrl) {
|
128
|
+
return resolveIfNotPlainOrUrl(relUrl, parentUrl) || (relUrl.indexOf(':') !== -1 ? relUrl : resolveIfNotPlainOrUrl('./' + relUrl, parentUrl));
|
129
|
+
}
|
130
|
+
|
131
|
+
function resolveAndComposePackages (packages, outPackages, baseUrl, parentMap) {
|
132
|
+
for (let p in packages) {
|
133
|
+
const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
|
134
|
+
let target = packages[p];
|
135
|
+
if (typeof target !== 'string')
|
136
|
+
continue;
|
137
|
+
const mapped = resolveImportMap(parentMap, resolveIfNotPlainOrUrl(target, baseUrl) || target, baseUrl);
|
138
|
+
if (mapped) {
|
139
|
+
outPackages[resolvedLhs] = mapped;
|
140
|
+
continue;
|
141
|
+
}
|
142
|
+
targetWarning(p, packages[p], 'bare specifier did not resolve');
|
143
|
+
}
|
144
|
+
}
|
145
|
+
|
146
|
+
function resolveAndComposeImportMap (json, baseUrl, parentMap) {
|
147
|
+
const outMap = { imports: Object.assign({}, parentMap.imports), scopes: Object.assign({}, parentMap.scopes) };
|
148
|
+
|
149
|
+
if (json.imports)
|
150
|
+
resolveAndComposePackages(json.imports, outMap.imports, baseUrl, parentMap);
|
151
|
+
|
152
|
+
if (json.scopes)
|
153
|
+
for (let s in json.scopes) {
|
154
|
+
const resolvedScope = resolveUrl(s, baseUrl);
|
155
|
+
resolveAndComposePackages(json.scopes[s], outMap.scopes[resolvedScope] || (outMap.scopes[resolvedScope] = {}), baseUrl, parentMap);
|
156
|
+
}
|
157
|
+
|
158
|
+
return outMap;
|
159
|
+
}
|
160
|
+
|
161
|
+
function getMatch (path, matchObj) {
|
162
|
+
if (matchObj[path])
|
163
|
+
return path;
|
164
|
+
let sepIndex = path.length;
|
165
|
+
do {
|
166
|
+
const segment = path.slice(0, sepIndex + 1);
|
167
|
+
if (segment in matchObj)
|
168
|
+
return segment;
|
169
|
+
} while ((sepIndex = path.lastIndexOf('/', sepIndex - 1)) !== -1)
|
170
|
+
}
|
171
|
+
|
172
|
+
function applyPackages (id, packages) {
|
173
|
+
const pkgName = getMatch(id, packages);
|
174
|
+
if (pkgName) {
|
175
|
+
const pkg = packages[pkgName];
|
176
|
+
if (pkg === null) return;
|
177
|
+
if (id.length > pkgName.length && pkg[pkg.length - 1] !== '/')
|
178
|
+
targetWarning(pkgName, pkg, "should have a trailing '/'");
|
179
|
+
else
|
180
|
+
return pkg + id.slice(pkgName.length);
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
function targetWarning (match, target, msg) {
|
185
|
+
console.warn("Package target " + msg + ", resolving target '" + target + "' for " + match);
|
186
|
+
}
|
187
|
+
|
188
|
+
function resolveImportMap (importMap, resolvedOrPlain, parentUrl) {
|
189
|
+
let scopeUrl = parentUrl && getMatch(parentUrl, importMap.scopes);
|
190
|
+
while (scopeUrl) {
|
191
|
+
const packageResolution = applyPackages(resolvedOrPlain, importMap.scopes[scopeUrl]);
|
192
|
+
if (packageResolution)
|
193
|
+
return packageResolution;
|
194
|
+
scopeUrl = getMatch(scopeUrl.slice(0, scopeUrl.lastIndexOf('/')), importMap.scopes);
|
195
|
+
}
|
196
|
+
return applyPackages(resolvedOrPlain, importMap.imports) || resolvedOrPlain.indexOf(':') !== -1 && resolvedOrPlain;
|
197
|
+
}
|
198
|
+
|
199
|
+
const optionsScript = document.querySelector('script[type=esms-options]');
|
200
|
+
|
201
|
+
const esmsInitOptions$1 = optionsScript ? JSON.parse(optionsScript.innerHTML) : self.esmsInitOptions ? self.esmsInitOptions : {};
|
202
|
+
|
203
|
+
let shimMode = !!esmsInitOptions$1.shimMode;
|
204
|
+
const resolveHook = shimMode && esmsInitOptions$1.resolve;
|
205
|
+
|
206
|
+
const skip = esmsInitOptions$1.skip ? new RegExp(esmsInitOptions$1.skip) : /^https:\/\/(cdn\.skypack\.dev|jspm\.dev)\//;
|
207
|
+
|
208
|
+
let nonce = esmsInitOptions$1.nonce;
|
209
|
+
|
210
|
+
if (!nonce) {
|
211
|
+
const nonceElement = document.querySelector('script[nonce]');
|
212
|
+
if (nonceElement)
|
213
|
+
nonce = nonceElement.nonce;
|
214
|
+
}
|
215
|
+
|
216
|
+
const {
|
217
|
+
fetchHook = fetch,
|
218
|
+
onerror = noop,
|
219
|
+
revokeBlobURLs,
|
220
|
+
noLoadEventRetriggers,
|
221
|
+
} = esmsInitOptions$1;
|
222
|
+
|
223
|
+
const enable = Array.isArray(esmsInitOptions$1.polyfillEnable) ? esmsInitOptions$1.polyfillEnable : [];
|
224
|
+
const cssModulesEnabled = enable.includes('css-modules');
|
225
|
+
const jsonModulesEnabled = enable.includes('json-modules');
|
226
|
+
|
227
|
+
function setShimMode () {
|
228
|
+
shimMode = true;
|
229
|
+
}
|
230
|
+
|
231
|
+
let err;
|
232
|
+
self.addEventListener('error', e => err = e.error);
|
233
|
+
|
234
|
+
function dynamicImport (specifier) {
|
235
|
+
const s = Object.assign(document.createElement('script'), {
|
236
|
+
type: 'module',
|
237
|
+
src: createBlob(
|
238
|
+
`import*as m from'${specifier}';self._esmsi=m`
|
239
|
+
),
|
240
|
+
nonce
|
241
|
+
});
|
242
|
+
s.setAttribute('noshim', '');
|
243
|
+
const p = new Promise((resolve, reject) => {
|
244
|
+
// Safari is unique in supporting module script error events
|
245
|
+
s.addEventListener('error', cb);
|
246
|
+
s.addEventListener('load', cb);
|
247
|
+
|
248
|
+
function cb () {
|
249
|
+
document.head.removeChild(s);
|
250
|
+
if (self._esmsi) {
|
251
|
+
resolve(self._esmsi, baseUrl);
|
252
|
+
self._esmsi = null;
|
253
|
+
}
|
254
|
+
else {
|
255
|
+
reject(err);
|
256
|
+
}
|
257
|
+
}
|
258
|
+
});
|
259
|
+
document.head.appendChild(s);
|
260
|
+
return p;
|
261
|
+
}
|
262
|
+
|
263
|
+
const supportsDynamicImportCheck = dynamicImport(createBlob('if(0)import("")'));
|
264
|
+
|
265
|
+
// support browsers without dynamic import support (eg Firefox 6x)
|
266
|
+
let supportsJsonAssertions = false;
|
267
|
+
let supportsCssAssertions = false;
|
268
|
+
|
269
|
+
let supportsImportMeta = false;
|
270
|
+
let supportsImportMaps = false;
|
271
|
+
let supportsDynamicImport = false;
|
272
|
+
|
273
|
+
const featureDetectionPromise = Promise.all([
|
274
|
+
dynamicImport(createBlob('import"data:text/css,{}"assert{type:"css"}')).then(() => supportsCssAssertions = true, noop),
|
275
|
+
dynamicImport(createBlob('import"data:text/json,{}"assert{type:"json"}')).then(() => supportsJsonAssertions = true, noop),
|
276
|
+
dynamicImport(createBlob('import.meta')).then(() => supportsImportMeta = true, noop),
|
277
|
+
Promise.resolve(supportsDynamicImportCheck).then(() => supportsDynamicImport = true, new Promise(resolve => {
|
278
|
+
self._$s = v => {
|
279
|
+
document.body.removeChild(iframe);
|
280
|
+
if (v) supportsImportMaps = true;
|
281
|
+
delete self._$s;
|
282
|
+
resolve();
|
283
|
+
};
|
284
|
+
const iframe = document.createElement('iframe');
|
285
|
+
iframe.style.display = 'none';
|
286
|
+
document.body.appendChild(iframe);
|
287
|
+
// we use document.write here because eg Weixin built-in browser doesn't support setting srcdoc
|
288
|
+
iframe.contentWindow.document.write(`<script type=importmap nonce="${nonce}">{"imports":{"x":"data:text/javascript,"}}<${''}/script><script nonce="${nonce}">import('x').then(()=>1,()=>0).then(v=>parent._$s(v))<${''}/script>`);
|
289
|
+
})).catch(noop)
|
290
|
+
]);
|
291
|
+
|
292
|
+
let source, pos, end,
|
293
|
+
openTokenDepth,
|
294
|
+
lastTokenPos,
|
295
|
+
openTokenPosStack,
|
296
|
+
openClassPosStack,
|
297
|
+
curDynamicImport,
|
298
|
+
templateStackDepth,
|
299
|
+
facade,
|
300
|
+
lastSlashWasDivision,
|
301
|
+
nextBraceIsClass,
|
302
|
+
templateDepth,
|
303
|
+
templateStack,
|
304
|
+
imports,
|
305
|
+
exports$1,
|
306
|
+
name;
|
307
|
+
|
308
|
+
function addImport (ss, s, e, d) {
|
309
|
+
const impt = { ss, se: d === -2 ? e : d === -1 ? e + 1 : 0, s, e, d, a: -1, n: undefined };
|
310
|
+
imports.push(impt);
|
311
|
+
return impt;
|
312
|
+
}
|
313
|
+
|
314
|
+
function readName (impt) {
|
315
|
+
let { d, s } = impt;
|
316
|
+
if (d !== -1)
|
317
|
+
s++;
|
318
|
+
impt.n = readString(s, source.charCodeAt(s - 1));
|
319
|
+
}
|
320
|
+
|
321
|
+
// Note: parsing is based on the _assumption_ that the source is already valid
|
322
|
+
function parse (_source, _name) {
|
323
|
+
openTokenDepth = 0;
|
324
|
+
curDynamicImport = null;
|
325
|
+
templateDepth = -1;
|
326
|
+
lastTokenPos = -1;
|
327
|
+
lastSlashWasDivision = false;
|
328
|
+
templateStack = Array(1024);
|
329
|
+
templateStackDepth = 0;
|
330
|
+
openTokenPosStack = Array(1024);
|
331
|
+
openClassPosStack = Array(1024);
|
332
|
+
nextBraceIsClass = false;
|
333
|
+
facade = true;
|
334
|
+
name = _name || '@';
|
335
|
+
|
336
|
+
imports = [];
|
337
|
+
exports$1 = new Set();
|
338
|
+
|
339
|
+
source = _source;
|
340
|
+
pos = -1;
|
341
|
+
end = source.length - 1;
|
342
|
+
let ch = 0;
|
343
|
+
|
344
|
+
// start with a pure "module-only" parser
|
345
|
+
m: while (pos++ < end) {
|
346
|
+
ch = source.charCodeAt(pos);
|
347
|
+
|
348
|
+
if (ch === 32 || ch < 14 && ch > 8)
|
349
|
+
continue;
|
350
|
+
|
351
|
+
switch (ch) {
|
352
|
+
case 101/*e*/:
|
353
|
+
if (openTokenDepth === 0 && keywordStart(pos) && source.startsWith('xport', pos + 1)) {
|
354
|
+
tryParseExportStatement();
|
355
|
+
// export might have been a non-pure declaration
|
356
|
+
if (!facade) {
|
357
|
+
lastTokenPos = pos;
|
358
|
+
break m;
|
359
|
+
}
|
360
|
+
}
|
361
|
+
break;
|
362
|
+
case 105/*i*/:
|
363
|
+
if (keywordStart(pos) && source.startsWith('mport', pos + 1))
|
364
|
+
tryParseImportStatement();
|
365
|
+
break;
|
366
|
+
case 59/*;*/:
|
367
|
+
break;
|
368
|
+
case 47/*/*/: {
|
369
|
+
const next_ch = source.charCodeAt(pos + 1);
|
370
|
+
if (next_ch === 47/*/*/) {
|
371
|
+
lineComment();
|
372
|
+
// dont update lastToken
|
373
|
+
continue;
|
374
|
+
}
|
375
|
+
else if (next_ch === 42/***/) {
|
376
|
+
blockComment(true);
|
377
|
+
// dont update lastToken
|
378
|
+
continue;
|
379
|
+
}
|
380
|
+
// fallthrough
|
381
|
+
}
|
382
|
+
default:
|
383
|
+
// as soon as we hit a non-module token, we go to main parser
|
384
|
+
facade = false;
|
385
|
+
pos--;
|
386
|
+
break m;
|
387
|
+
}
|
388
|
+
lastTokenPos = pos;
|
389
|
+
}
|
390
|
+
|
391
|
+
while (pos++ < end) {
|
392
|
+
ch = source.charCodeAt(pos);
|
393
|
+
|
394
|
+
if (ch === 32 || ch < 14 && ch > 8)
|
395
|
+
continue;
|
396
|
+
|
397
|
+
switch (ch) {
|
398
|
+
case 101/*e*/:
|
399
|
+
if (openTokenDepth === 0 && keywordStart(pos) && source.startsWith('xport', pos + 1))
|
400
|
+
tryParseExportStatement();
|
401
|
+
break;
|
402
|
+
case 105/*i*/:
|
403
|
+
if (keywordStart(pos) && source.startsWith('mport', pos + 1))
|
404
|
+
tryParseImportStatement();
|
405
|
+
break;
|
406
|
+
case 99/*c*/:
|
407
|
+
if (keywordStart(pos) && source.startsWith('lass', pos + 1) && isBrOrWs(source.charCodeAt(pos + 5)))
|
408
|
+
nextBraceIsClass = true;
|
409
|
+
break;
|
410
|
+
case 40/*(*/:
|
411
|
+
openTokenPosStack[openTokenDepth++] = lastTokenPos;
|
412
|
+
break;
|
413
|
+
case 41/*)*/:
|
414
|
+
if (openTokenDepth === 0)
|
415
|
+
syntaxError();
|
416
|
+
openTokenDepth--;
|
417
|
+
if (curDynamicImport && curDynamicImport.d === openTokenPosStack[openTokenDepth]) {
|
418
|
+
if (curDynamicImport.e === 0)
|
419
|
+
curDynamicImport.e = pos;
|
420
|
+
curDynamicImport.se = pos;
|
421
|
+
curDynamicImport = null;
|
422
|
+
}
|
423
|
+
break;
|
424
|
+
case 123/*{*/:
|
425
|
+
// dynamic import followed by { is not a dynamic import (so remove)
|
426
|
+
// this is a sneaky way to get around { import () {} } v { import () }
|
427
|
+
// block / object ambiguity without a parser (assuming source is valid)
|
428
|
+
if (source.charCodeAt(lastTokenPos) === 41/*)*/ && imports.length && imports[imports.length - 1].e === lastTokenPos) {
|
429
|
+
imports.pop();
|
430
|
+
}
|
431
|
+
openClassPosStack[openTokenDepth] = nextBraceIsClass;
|
432
|
+
nextBraceIsClass = false;
|
433
|
+
openTokenPosStack[openTokenDepth++] = lastTokenPos;
|
434
|
+
break;
|
435
|
+
case 125/*}*/:
|
436
|
+
if (openTokenDepth === 0)
|
437
|
+
syntaxError();
|
438
|
+
if (openTokenDepth-- === templateDepth) {
|
439
|
+
templateDepth = templateStack[--templateStackDepth];
|
440
|
+
templateString();
|
441
|
+
}
|
442
|
+
else {
|
443
|
+
if (templateDepth !== -1 && openTokenDepth < templateDepth)
|
444
|
+
syntaxError();
|
445
|
+
}
|
446
|
+
break;
|
447
|
+
case 39/*'*/:
|
448
|
+
case 34/*"*/:
|
449
|
+
stringLiteral(ch);
|
450
|
+
break;
|
451
|
+
case 47/*/*/: {
|
452
|
+
const next_ch = source.charCodeAt(pos + 1);
|
453
|
+
if (next_ch === 47/*/*/) {
|
454
|
+
lineComment();
|
455
|
+
// dont update lastToken
|
456
|
+
continue;
|
457
|
+
}
|
458
|
+
else if (next_ch === 42/***/) {
|
459
|
+
blockComment(true);
|
460
|
+
// dont update lastToken
|
461
|
+
continue;
|
462
|
+
}
|
463
|
+
else {
|
464
|
+
// Division / regex ambiguity handling based on checking backtrack analysis of:
|
465
|
+
// - what token came previously (lastToken)
|
466
|
+
// - if a closing brace or paren, what token came before the corresponding
|
467
|
+
// opening brace or paren (lastOpenTokenIndex)
|
468
|
+
const lastToken = source.charCodeAt(lastTokenPos);
|
469
|
+
if (isExpressionPunctuator(lastToken) &&
|
470
|
+
!(lastToken === 46/*.*/ && (source.charCodeAt(lastTokenPos - 1) >= 48/*0*/ && source.charCodeAt(lastTokenPos - 1) <= 57/*9*/)) &&
|
471
|
+
!(lastToken === 43/*+*/ && source.charCodeAt(lastTokenPos - 1) === 43/*+*/) && !(lastToken === 45/*-*/ && source.charCodeAt(lastTokenPos - 1) === 45/*-*/) ||
|
472
|
+
lastToken === 41/*)*/ && isParenKeyword(openTokenPosStack[openTokenDepth]) ||
|
473
|
+
lastToken === 125/*}*/ && (isExpressionTerminator(openTokenPosStack[openTokenDepth]) || openClassPosStack[openTokenDepth]) ||
|
474
|
+
lastToken === 47/*/*/ && lastSlashWasDivision ||
|
475
|
+
isExpressionKeyword(lastTokenPos) ||
|
476
|
+
!lastToken) {
|
477
|
+
regularExpression();
|
478
|
+
lastSlashWasDivision = false;
|
479
|
+
}
|
480
|
+
else {
|
481
|
+
lastSlashWasDivision = true;
|
482
|
+
}
|
483
|
+
}
|
484
|
+
break;
|
485
|
+
}
|
486
|
+
case 96/*`*/:
|
487
|
+
templateString();
|
488
|
+
break;
|
489
|
+
}
|
490
|
+
lastTokenPos = pos;
|
491
|
+
}
|
492
|
+
|
493
|
+
if (templateDepth !== -1 || openTokenDepth)
|
494
|
+
syntaxError();
|
495
|
+
|
496
|
+
return [imports, [...exports$1], facade];
|
497
|
+
}
|
498
|
+
|
499
|
+
function tryParseImportStatement () {
|
500
|
+
const startPos = pos;
|
501
|
+
|
502
|
+
pos += 6;
|
503
|
+
|
504
|
+
let ch = commentWhitespace(true);
|
505
|
+
|
506
|
+
switch (ch) {
|
507
|
+
// dynamic import
|
508
|
+
case 40/*(*/:
|
509
|
+
openTokenPosStack[openTokenDepth++] = startPos;
|
510
|
+
if (source.charCodeAt(lastTokenPos) === 46/*.*/)
|
511
|
+
return;
|
512
|
+
// dynamic import indicated by positive d
|
513
|
+
const impt = addImport(startPos, pos + 1, 0, startPos);
|
514
|
+
curDynamicImport = impt;
|
515
|
+
// try parse a string, to record a safe dynamic import string
|
516
|
+
pos++;
|
517
|
+
ch = commentWhitespace(true);
|
518
|
+
if (ch === 39/*'*/ || ch === 34/*"*/) {
|
519
|
+
stringLiteral(ch);
|
520
|
+
}
|
521
|
+
else {
|
522
|
+
pos--;
|
523
|
+
return;
|
524
|
+
}
|
525
|
+
pos++;
|
526
|
+
ch = commentWhitespace(true);
|
527
|
+
if (ch === 44/*,*/) {
|
528
|
+
impt.e = pos;
|
529
|
+
pos++;
|
530
|
+
ch = commentWhitespace(true);
|
531
|
+
impt.a = pos;
|
532
|
+
readName(impt);
|
533
|
+
pos--;
|
534
|
+
}
|
535
|
+
else if (ch === 41/*)*/) {
|
536
|
+
openTokenDepth--;
|
537
|
+
impt.e = pos;
|
538
|
+
impt.se = pos;
|
539
|
+
readName(impt);
|
540
|
+
}
|
541
|
+
else {
|
542
|
+
pos--;
|
543
|
+
}
|
544
|
+
return;
|
545
|
+
// import.meta
|
546
|
+
case 46/*.*/:
|
547
|
+
pos++;
|
548
|
+
ch = commentWhitespace(true);
|
549
|
+
// import.meta indicated by d === -2
|
550
|
+
if (ch === 109/*m*/ && source.startsWith('eta', pos + 1) && source.charCodeAt(lastTokenPos) !== 46/*.*/)
|
551
|
+
addImport(startPos, startPos, pos + 4, -2);
|
552
|
+
return;
|
553
|
+
|
554
|
+
default:
|
555
|
+
// no space after "import" -> not an import keyword
|
556
|
+
if (pos === startPos + 6)
|
557
|
+
break;
|
558
|
+
case 34/*"*/:
|
559
|
+
case 39/*'*/:
|
560
|
+
case 123/*{*/:
|
561
|
+
case 42/***/:
|
562
|
+
// import statement only permitted at base-level
|
563
|
+
if (openTokenDepth !== 0) {
|
564
|
+
pos--;
|
565
|
+
return;
|
566
|
+
}
|
567
|
+
while (pos < end) {
|
568
|
+
ch = source.charCodeAt(pos);
|
569
|
+
if (ch === 39/*'*/ || ch === 34/*"*/) {
|
570
|
+
readImportString(startPos, ch);
|
571
|
+
return;
|
572
|
+
}
|
573
|
+
pos++;
|
574
|
+
}
|
575
|
+
syntaxError();
|
576
|
+
}
|
577
|
+
}
|
578
|
+
|
579
|
+
function tryParseExportStatement () {
|
580
|
+
const sStartPos = pos;
|
581
|
+
|
582
|
+
pos += 6;
|
583
|
+
|
584
|
+
const curPos = pos;
|
585
|
+
|
586
|
+
let ch = commentWhitespace(true);
|
587
|
+
|
588
|
+
if (pos === curPos && !isPunctuator(ch))
|
589
|
+
return;
|
590
|
+
|
591
|
+
switch (ch) {
|
592
|
+
// export default ...
|
593
|
+
case 100/*d*/:
|
594
|
+
exports$1.add(source.slice(pos, pos + 7));
|
595
|
+
return;
|
596
|
+
|
597
|
+
// export async? function*? name () {
|
598
|
+
case 97/*a*/:
|
599
|
+
pos += 5;
|
600
|
+
commentWhitespace(true);
|
601
|
+
// fallthrough
|
602
|
+
case 102/*f*/:
|
603
|
+
pos += 8;
|
604
|
+
ch = commentWhitespace(true);
|
605
|
+
if (ch === 42/***/) {
|
606
|
+
pos++;
|
607
|
+
ch = commentWhitespace(true);
|
608
|
+
}
|
609
|
+
const startPos = pos;
|
610
|
+
ch = readToWsOrPunctuator(ch);
|
611
|
+
exports$1.add(source.slice(startPos, pos));
|
612
|
+
pos--;
|
613
|
+
return;
|
614
|
+
|
615
|
+
case 99/*c*/:
|
616
|
+
if (source.startsWith('lass', pos + 1) && isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos + 5))) {
|
617
|
+
pos += 5;
|
618
|
+
ch = commentWhitespace(true);
|
619
|
+
const startPos = pos;
|
620
|
+
ch = readToWsOrPunctuator(ch);
|
621
|
+
exports$1.add(source.slice(startPos, pos));
|
622
|
+
pos--;
|
623
|
+
return;
|
624
|
+
}
|
625
|
+
pos += 2;
|
626
|
+
// fallthrough
|
627
|
+
|
628
|
+
// export var/let/const name = ...(, name = ...)+
|
629
|
+
case 118/*v*/:
|
630
|
+
case 109/*l*/:
|
631
|
+
// destructured initializations not currently supported (skipped for { or [)
|
632
|
+
// also, lexing names after variable equals is skipped (export var p = function () { ... }, q = 5 skips "q")
|
633
|
+
pos += 2;
|
634
|
+
facade = false;
|
635
|
+
do {
|
636
|
+
pos++;
|
637
|
+
ch = commentWhitespace(true);
|
638
|
+
const startPos = pos;
|
639
|
+
ch = readToWsOrPunctuator(ch);
|
640
|
+
// dont yet handle [ { destructurings
|
641
|
+
if (ch === 123/*{*/ || ch === 91/*[*/) {
|
642
|
+
pos--;
|
643
|
+
return;
|
644
|
+
}
|
645
|
+
if (pos === startPos)
|
646
|
+
return;
|
647
|
+
exports$1.add(source.slice(startPos, pos));
|
648
|
+
ch = commentWhitespace(true);
|
649
|
+
if (ch === 61/*=*/) {
|
650
|
+
pos--;
|
651
|
+
return;
|
652
|
+
}
|
653
|
+
} while (ch === 44/*,*/);
|
654
|
+
pos--;
|
655
|
+
return;
|
656
|
+
|
657
|
+
|
658
|
+
// export {...}
|
659
|
+
case 123/*{*/:
|
660
|
+
pos++;
|
661
|
+
ch = commentWhitespace(true);
|
662
|
+
while (true) {
|
663
|
+
const startPos = pos;
|
664
|
+
readToWsOrPunctuator(ch);
|
665
|
+
const endPos = pos;
|
666
|
+
commentWhitespace(true);
|
667
|
+
ch = readExportAs(startPos, endPos);
|
668
|
+
// ,
|
669
|
+
if (ch === 44/*,*/) {
|
670
|
+
pos++;
|
671
|
+
ch = commentWhitespace(true);
|
672
|
+
}
|
673
|
+
if (ch === 125/*}*/)
|
674
|
+
break;
|
675
|
+
if (pos === startPos)
|
676
|
+
return syntaxError();
|
677
|
+
if (pos > end)
|
678
|
+
return syntaxError();
|
679
|
+
}
|
680
|
+
pos++;
|
681
|
+
ch = commentWhitespace(true);
|
682
|
+
break;
|
683
|
+
|
684
|
+
// export *
|
685
|
+
// export * as X
|
686
|
+
case 42/***/:
|
687
|
+
pos++;
|
688
|
+
commentWhitespace(true);
|
689
|
+
ch = readExportAs(pos, pos);
|
690
|
+
ch = commentWhitespace(true);
|
691
|
+
break;
|
692
|
+
}
|
693
|
+
|
694
|
+
// from ...
|
695
|
+
if (ch === 102/*f*/ && source.startsWith('rom', pos + 1)) {
|
696
|
+
pos += 4;
|
697
|
+
readImportString(sStartPos, commentWhitespace(true));
|
698
|
+
}
|
699
|
+
else {
|
700
|
+
pos--;
|
701
|
+
}
|
702
|
+
}
|
703
|
+
|
704
|
+
/*
|
705
|
+
* Ported from Acorn
|
706
|
+
*
|
707
|
+
* MIT License
|
708
|
+
|
709
|
+
* Copyright (C) 2012-2020 by various contributors (see AUTHORS)
|
710
|
+
|
711
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
712
|
+
* of this software and associated documentation files (the "Software"), to deal
|
713
|
+
* in the Software without restriction, including without limitation the rights
|
714
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
715
|
+
* copies of the Software, and to permit persons to whom the Software is
|
716
|
+
* furnished to do so, subject to the following conditions:
|
717
|
+
|
718
|
+
* The above copyright notice and this permission notice shall be included in
|
719
|
+
* all copies or substantial portions of the Software.
|
720
|
+
|
721
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
722
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
723
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
724
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
725
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
726
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
727
|
+
* THE SOFTWARE.
|
728
|
+
*/
|
729
|
+
let acornPos;
|
730
|
+
function readString (start, quote) {
|
731
|
+
acornPos = start;
|
732
|
+
let out = '', chunkStart = acornPos;
|
733
|
+
for (;;) {
|
734
|
+
if (acornPos >= source.length) syntaxError();
|
735
|
+
const ch = source.charCodeAt(acornPos);
|
736
|
+
if (ch === quote) break;
|
737
|
+
if (ch === 92) { // '\'
|
738
|
+
out += source.slice(chunkStart, acornPos);
|
739
|
+
out += readEscapedChar();
|
740
|
+
chunkStart = acornPos;
|
741
|
+
}
|
742
|
+
else if (ch === 0x2028 || ch === 0x2029) {
|
743
|
+
++acornPos;
|
744
|
+
}
|
745
|
+
else {
|
746
|
+
if (isBr(ch)) syntaxError();
|
747
|
+
++acornPos;
|
748
|
+
}
|
749
|
+
}
|
750
|
+
out += source.slice(chunkStart, acornPos++);
|
751
|
+
return out;
|
752
|
+
}
|
753
|
+
|
754
|
+
// Used to read escaped characters
|
755
|
+
|
756
|
+
function readEscapedChar () {
|
757
|
+
let ch = source.charCodeAt(++acornPos);
|
758
|
+
++acornPos;
|
759
|
+
switch (ch) {
|
760
|
+
case 110: return '\n'; // 'n' -> '\n'
|
761
|
+
case 114: return '\r'; // 'r' -> '\r'
|
762
|
+
case 120: return String.fromCharCode(readHexChar(2)); // 'x'
|
763
|
+
case 117: return readCodePointToString(); // 'u'
|
764
|
+
case 116: return '\t'; // 't' -> '\t'
|
765
|
+
case 98: return '\b'; // 'b' -> '\b'
|
766
|
+
case 118: return '\u000b'; // 'v' -> '\u000b'
|
767
|
+
case 102: return '\f'; // 'f' -> '\f'
|
768
|
+
case 13: if (source.charCodeAt(acornPos) === 10) ++acornPos; // '\r\n'
|
769
|
+
case 10: // ' \n'
|
770
|
+
return '';
|
771
|
+
case 56:
|
772
|
+
case 57:
|
773
|
+
syntaxError();
|
774
|
+
default:
|
775
|
+
if (ch >= 48 && ch <= 55) {
|
776
|
+
let octalStr = source.substr(acornPos - 1, 3).match(/^[0-7]+/)[0];
|
777
|
+
let octal = parseInt(octalStr, 8);
|
778
|
+
if (octal > 255) {
|
779
|
+
octalStr = octalStr.slice(0, -1);
|
780
|
+
octal = parseInt(octalStr, 8);
|
781
|
+
}
|
782
|
+
acornPos += octalStr.length - 1;
|
783
|
+
ch = source.charCodeAt(acornPos);
|
784
|
+
if (octalStr !== '0' || ch === 56 || ch === 57)
|
785
|
+
syntaxError();
|
786
|
+
return String.fromCharCode(octal);
|
787
|
+
}
|
788
|
+
if (isBr(ch)) {
|
789
|
+
// Unicode new line characters after \ get removed from output in both
|
790
|
+
// template literals and strings
|
791
|
+
return '';
|
792
|
+
}
|
793
|
+
return String.fromCharCode(ch);
|
794
|
+
}
|
795
|
+
}
|
796
|
+
|
797
|
+
// Used to read character escape sequences ('\x', '\u', '\U').
|
798
|
+
|
799
|
+
function readHexChar (len) {
|
800
|
+
const start = acornPos;
|
801
|
+
let total = 0, lastCode = 0;
|
802
|
+
for (let i = 0; i < len; ++i, ++acornPos) {
|
803
|
+
let code = source.charCodeAt(acornPos), val;
|
804
|
+
|
805
|
+
if (code === 95) {
|
806
|
+
if (lastCode === 95 || i === 0) syntaxError();
|
807
|
+
lastCode = code;
|
808
|
+
continue;
|
809
|
+
}
|
810
|
+
|
811
|
+
if (code >= 97) val = code - 97 + 10; // a
|
812
|
+
else if (code >= 65) val = code - 65 + 10; // A
|
813
|
+
else if (code >= 48 && code <= 57) val = code - 48; // 0-9
|
814
|
+
else break;
|
815
|
+
if (val >= 16) break;
|
816
|
+
lastCode = code;
|
817
|
+
total = total * 16 + val;
|
818
|
+
}
|
819
|
+
|
820
|
+
if (lastCode === 95 || acornPos - start !== len) syntaxError();
|
821
|
+
|
822
|
+
return total;
|
823
|
+
}
|
824
|
+
|
825
|
+
// Read a string value, interpreting backslash-escapes.
|
826
|
+
|
827
|
+
function readCodePointToString () {
|
828
|
+
const ch = source.charCodeAt(acornPos);
|
829
|
+
let code;
|
830
|
+
if (ch === 123) { // '{'
|
831
|
+
++acornPos;
|
832
|
+
code = readHexChar(source.indexOf('}', acornPos) - acornPos);
|
833
|
+
++acornPos;
|
834
|
+
if (code > 0x10FFFF) syntaxError();
|
835
|
+
} else {
|
836
|
+
code = readHexChar(4);
|
837
|
+
}
|
838
|
+
// UTF-16 Decoding
|
839
|
+
if (code <= 0xFFFF) return String.fromCharCode(code);
|
840
|
+
code -= 0x10000;
|
841
|
+
return String.fromCharCode((code >> 10) + 0xD800, (code & 1023) + 0xDC00);
|
842
|
+
}
|
843
|
+
|
844
|
+
/*
|
845
|
+
* </ Acorn Port>
|
846
|
+
*/
|
847
|
+
|
848
|
+
function readExportAs (startPos, endPos) {
|
849
|
+
let ch = source.charCodeAt(pos);
|
850
|
+
if (ch === 97 /*a*/) {
|
851
|
+
pos += 2;
|
852
|
+
ch = commentWhitespace(true);
|
853
|
+
startPos = pos;
|
854
|
+
readToWsOrPunctuator(ch);
|
855
|
+
endPos = pos;
|
856
|
+
ch = commentWhitespace(true);
|
857
|
+
}
|
858
|
+
if (pos !== startPos)
|
859
|
+
exports$1.add(source.slice(startPos, endPos));
|
860
|
+
return ch;
|
861
|
+
}
|
862
|
+
|
863
|
+
function readImportString (ss, ch) {
|
864
|
+
const startPos = pos + 1;
|
865
|
+
if (ch === 39/*'*/ || ch === 34/*"*/) {
|
866
|
+
stringLiteral(ch);
|
867
|
+
}
|
868
|
+
else {
|
869
|
+
syntaxError();
|
870
|
+
return;
|
871
|
+
}
|
872
|
+
const impt = addImport(ss, startPos, pos, -1);
|
873
|
+
readName(impt);
|
874
|
+
pos++;
|
875
|
+
ch = commentWhitespace(false);
|
876
|
+
if (ch !== 97/*a*/ || !source.startsWith('ssert', pos + 1)) {
|
877
|
+
pos--;
|
878
|
+
return;
|
879
|
+
}
|
880
|
+
const assertIndex = pos;
|
881
|
+
|
882
|
+
pos += 6;
|
883
|
+
ch = commentWhitespace(true);
|
884
|
+
if (ch !== 123/*{*/) {
|
885
|
+
pos = assertIndex;
|
886
|
+
return;
|
887
|
+
}
|
888
|
+
const assertStart = pos;
|
889
|
+
do {
|
890
|
+
pos++;
|
891
|
+
ch = commentWhitespace(true);
|
892
|
+
if (ch === 39/*'*/ || ch === 34/*"*/) {
|
893
|
+
stringLiteral(ch);
|
894
|
+
pos++;
|
895
|
+
ch = commentWhitespace(true);
|
896
|
+
}
|
897
|
+
else {
|
898
|
+
ch = readToWsOrPunctuator(ch);
|
899
|
+
}
|
900
|
+
if (ch !== 58/*:*/) {
|
901
|
+
pos = assertIndex;
|
902
|
+
return;
|
903
|
+
}
|
904
|
+
pos++;
|
905
|
+
ch = commentWhitespace(true);
|
906
|
+
if (ch === 39/*'*/ || ch === 34/*"*/) {
|
907
|
+
stringLiteral(ch);
|
908
|
+
}
|
909
|
+
else {
|
910
|
+
pos = assertIndex;
|
911
|
+
return;
|
912
|
+
}
|
913
|
+
pos++;
|
914
|
+
ch = commentWhitespace(true);
|
915
|
+
if (ch === 44/*,*/) {
|
916
|
+
pos++;
|
917
|
+
ch = commentWhitespace(true);
|
918
|
+
if (ch === 125/*}*/)
|
919
|
+
break;
|
920
|
+
continue;
|
921
|
+
}
|
922
|
+
if (ch === 125/*}*/)
|
923
|
+
break;
|
924
|
+
pos = assertIndex;
|
925
|
+
return;
|
926
|
+
} while (true);
|
927
|
+
impt.a = assertStart;
|
928
|
+
impt.se = pos + 1;
|
929
|
+
}
|
930
|
+
|
931
|
+
function commentWhitespace (br) {
|
932
|
+
let ch;
|
933
|
+
do {
|
934
|
+
ch = source.charCodeAt(pos);
|
935
|
+
if (ch === 47/*/*/) {
|
936
|
+
const next_ch = source.charCodeAt(pos + 1);
|
937
|
+
if (next_ch === 47/*/*/)
|
938
|
+
lineComment();
|
939
|
+
else if (next_ch === 42/***/)
|
940
|
+
blockComment(br);
|
941
|
+
else
|
942
|
+
return ch;
|
943
|
+
}
|
944
|
+
else if (br ? !isBrOrWs(ch): !isWsNotBr(ch)) {
|
945
|
+
return ch;
|
946
|
+
}
|
947
|
+
} while (pos++ < end);
|
948
|
+
return ch;
|
949
|
+
}
|
950
|
+
|
951
|
+
function templateString () {
|
952
|
+
while (pos++ < end) {
|
953
|
+
const ch = source.charCodeAt(pos);
|
954
|
+
if (ch === 36/*$*/ && source.charCodeAt(pos + 1) === 123/*{*/) {
|
955
|
+
pos++;
|
956
|
+
templateStack[templateStackDepth++] = templateDepth;
|
957
|
+
templateDepth = ++openTokenDepth;
|
958
|
+
return;
|
959
|
+
}
|
960
|
+
if (ch === 96/*`*/)
|
961
|
+
return;
|
962
|
+
if (ch === 92/*\*/)
|
963
|
+
pos++;
|
964
|
+
}
|
965
|
+
syntaxError();
|
966
|
+
}
|
967
|
+
|
968
|
+
function blockComment (br) {
|
969
|
+
pos++;
|
970
|
+
while (pos++ < end) {
|
971
|
+
const ch = source.charCodeAt(pos);
|
972
|
+
if (!br && isBr(ch))
|
973
|
+
return;
|
974
|
+
if (ch === 42/***/ && source.charCodeAt(pos + 1) === 47/*/*/) {
|
975
|
+
pos++;
|
976
|
+
return;
|
977
|
+
}
|
978
|
+
}
|
979
|
+
}
|
980
|
+
|
981
|
+
function lineComment () {
|
982
|
+
while (pos++ < end) {
|
983
|
+
const ch = source.charCodeAt(pos);
|
984
|
+
if (ch === 10/*\n*/ || ch === 13/*\r*/)
|
985
|
+
return;
|
986
|
+
}
|
987
|
+
}
|
988
|
+
|
989
|
+
function stringLiteral (quote) {
|
990
|
+
while (pos++ < end) {
|
991
|
+
let ch = source.charCodeAt(pos);
|
992
|
+
if (ch === quote)
|
993
|
+
return;
|
994
|
+
if (ch === 92/*\*/) {
|
995
|
+
ch = source.charCodeAt(++pos);
|
996
|
+
if (ch === 13/*\r*/ && source.charCodeAt(pos + 1) === 10/*\n*/)
|
997
|
+
pos++;
|
998
|
+
}
|
999
|
+
else if (isBr(ch))
|
1000
|
+
break;
|
1001
|
+
}
|
1002
|
+
syntaxError();
|
1003
|
+
}
|
1004
|
+
|
1005
|
+
function regexCharacterClass () {
|
1006
|
+
while (pos++ < end) {
|
1007
|
+
let ch = source.charCodeAt(pos);
|
1008
|
+
if (ch === 93/*]*/)
|
1009
|
+
return ch;
|
1010
|
+
if (ch === 92/*\*/)
|
1011
|
+
pos++;
|
1012
|
+
else if (ch === 10/*\n*/ || ch === 13/*\r*/)
|
1013
|
+
break;
|
1014
|
+
}
|
1015
|
+
syntaxError();
|
1016
|
+
}
|
1017
|
+
|
1018
|
+
function regularExpression () {
|
1019
|
+
while (pos++ < end) {
|
1020
|
+
let ch = source.charCodeAt(pos);
|
1021
|
+
if (ch === 47/*/*/)
|
1022
|
+
return;
|
1023
|
+
if (ch === 91/*[*/)
|
1024
|
+
ch = regexCharacterClass();
|
1025
|
+
else if (ch === 92/*\*/)
|
1026
|
+
pos++;
|
1027
|
+
else if (ch === 10/*\n*/ || ch === 13/*\r*/)
|
1028
|
+
break;
|
1029
|
+
}
|
1030
|
+
syntaxError();
|
1031
|
+
}
|
1032
|
+
|
1033
|
+
function readToWsOrPunctuator (ch) {
|
1034
|
+
do {
|
1035
|
+
if (isBrOrWs(ch) || isPunctuator(ch))
|
1036
|
+
return ch;
|
1037
|
+
} while (ch = source.charCodeAt(++pos));
|
1038
|
+
return ch;
|
1039
|
+
}
|
1040
|
+
|
1041
|
+
// Note: non-asii BR and whitespace checks omitted for perf / footprint
|
1042
|
+
// if there is a significant user need this can be reconsidered
|
1043
|
+
function isBr (c) {
|
1044
|
+
return c === 13/*\r*/ || c === 10/*\n*/;
|
1045
|
+
}
|
1046
|
+
|
1047
|
+
function isWsNotBr (c) {
|
1048
|
+
return c === 9 || c === 11 || c === 12 || c === 32 || c === 160;
|
1049
|
+
}
|
1050
|
+
|
1051
|
+
function isBrOrWs (c) {
|
1052
|
+
return c > 8 && c < 14 || c === 32 || c === 160;
|
1053
|
+
}
|
1054
|
+
|
1055
|
+
function isBrOrWsOrPunctuatorNotDot (c) {
|
1056
|
+
return c > 8 && c < 14 || c === 32 || c === 160 || isPunctuator(c) && c !== 46/*.*/;
|
1057
|
+
}
|
1058
|
+
|
1059
|
+
function keywordStart (pos) {
|
1060
|
+
return pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - 1));
|
1061
|
+
}
|
1062
|
+
|
1063
|
+
function readPrecedingKeyword (pos, match) {
|
1064
|
+
if (pos < match.length - 1)
|
1065
|
+
return false;
|
1066
|
+
return source.startsWith(match, pos - match.length + 1) && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - match.length)));
|
1067
|
+
}
|
1068
|
+
|
1069
|
+
function readPrecedingKeyword1 (pos, ch) {
|
1070
|
+
return source.charCodeAt(pos) === ch && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - 1)));
|
1071
|
+
}
|
1072
|
+
|
1073
|
+
// Detects one of case, debugger, delete, do, else, in, instanceof, new,
|
1074
|
+
// return, throw, typeof, void, yield, await
|
1075
|
+
function isExpressionKeyword (pos) {
|
1076
|
+
switch (source.charCodeAt(pos)) {
|
1077
|
+
case 100/*d*/:
|
1078
|
+
switch (source.charCodeAt(pos - 1)) {
|
1079
|
+
case 105/*i*/:
|
1080
|
+
// void
|
1081
|
+
return readPrecedingKeyword(pos - 2, 'vo');
|
1082
|
+
case 108/*l*/:
|
1083
|
+
// yield
|
1084
|
+
return readPrecedingKeyword(pos - 2, 'yie');
|
1085
|
+
default:
|
1086
|
+
return false;
|
1087
|
+
}
|
1088
|
+
case 101/*e*/:
|
1089
|
+
switch (source.charCodeAt(pos - 1)) {
|
1090
|
+
case 115/*s*/:
|
1091
|
+
switch (source.charCodeAt(pos - 2)) {
|
1092
|
+
case 108/*l*/:
|
1093
|
+
// else
|
1094
|
+
return readPrecedingKeyword1(pos - 3, 101/*e*/);
|
1095
|
+
case 97/*a*/:
|
1096
|
+
// case
|
1097
|
+
return readPrecedingKeyword1(pos - 3, 99/*c*/);
|
1098
|
+
default:
|
1099
|
+
return false;
|
1100
|
+
}
|
1101
|
+
case 116/*t*/:
|
1102
|
+
// delete
|
1103
|
+
return readPrecedingKeyword(pos - 2, 'dele');
|
1104
|
+
default:
|
1105
|
+
return false;
|
1106
|
+
}
|
1107
|
+
case 102/*f*/:
|
1108
|
+
if (source.charCodeAt(pos - 1) !== 111/*o*/ || source.charCodeAt(pos - 2) !== 101/*e*/)
|
1109
|
+
return false;
|
1110
|
+
switch (source.charCodeAt(pos - 3)) {
|
1111
|
+
case 99/*c*/:
|
1112
|
+
// instanceof
|
1113
|
+
return readPrecedingKeyword(pos - 4, 'instan');
|
1114
|
+
case 112/*p*/:
|
1115
|
+
// typeof
|
1116
|
+
return readPrecedingKeyword(pos - 4, 'ty');
|
1117
|
+
default:
|
1118
|
+
return false;
|
1119
|
+
}
|
1120
|
+
case 110/*n*/:
|
1121
|
+
// in, return
|
1122
|
+
return readPrecedingKeyword1(pos - 1, 105/*i*/) || readPrecedingKeyword(pos - 1, 'retur');
|
1123
|
+
case 111/*o*/:
|
1124
|
+
// do
|
1125
|
+
return readPrecedingKeyword1(pos - 1, 100/*d*/);
|
1126
|
+
case 114/*r*/:
|
1127
|
+
// debugger
|
1128
|
+
return readPrecedingKeyword(pos - 1, 'debugge');
|
1129
|
+
case 116/*t*/:
|
1130
|
+
// await
|
1131
|
+
return readPrecedingKeyword(pos - 1, 'awai');
|
1132
|
+
case 119/*w*/:
|
1133
|
+
switch (source.charCodeAt(pos - 1)) {
|
1134
|
+
case 101/*e*/:
|
1135
|
+
// new
|
1136
|
+
return readPrecedingKeyword1(pos - 2, 110/*n*/);
|
1137
|
+
case 111/*o*/:
|
1138
|
+
// throw
|
1139
|
+
return readPrecedingKeyword(pos - 2, 'thr');
|
1140
|
+
default:
|
1141
|
+
return false;
|
1142
|
+
}
|
1143
|
+
}
|
1144
|
+
return false;
|
1145
|
+
}
|
1146
|
+
|
1147
|
+
function isParenKeyword (curPos) {
|
1148
|
+
return source.charCodeAt(curPos) === 101/*e*/ && source.startsWith('whil', curPos - 4) ||
|
1149
|
+
source.charCodeAt(curPos) === 114/*r*/ && source.startsWith('fo', curPos - 2) ||
|
1150
|
+
source.charCodeAt(curPos - 1) === 105/*i*/ && source.charCodeAt(curPos) === 102/*f*/;
|
1151
|
+
}
|
1152
|
+
|
1153
|
+
function isPunctuator (ch) {
|
1154
|
+
// 23 possible punctuator endings: !%&()*+,-./:;<=>?[]^{}|~
|
1155
|
+
return ch === 33/*!*/ || ch === 37/*%*/ || ch === 38/*&*/ ||
|
1156
|
+
ch > 39 && ch < 48 || ch > 57 && ch < 64 ||
|
1157
|
+
ch === 91/*[*/ || ch === 93/*]*/ || ch === 94/*^*/ ||
|
1158
|
+
ch > 122 && ch < 127;
|
1159
|
+
}
|
1160
|
+
|
1161
|
+
function isExpressionPunctuator (ch) {
|
1162
|
+
// 20 possible expression endings: !%&(*+,-.:;<=>?[^{|~
|
1163
|
+
return ch === 33/*!*/ || ch === 37/*%*/ || ch === 38/*&*/ ||
|
1164
|
+
ch > 39 && ch < 47 && ch !== 41 || ch > 57 && ch < 64 ||
|
1165
|
+
ch === 91/*[*/ || ch === 94/*^*/ || ch > 122 && ch < 127 && ch !== 125/*}*/;
|
1166
|
+
}
|
1167
|
+
|
1168
|
+
function isExpressionTerminator (curPos) {
|
1169
|
+
// detects:
|
1170
|
+
// => ; ) finally catch else
|
1171
|
+
// as all of these followed by a { will indicate a statement brace
|
1172
|
+
switch (source.charCodeAt(curPos)) {
|
1173
|
+
case 62/*>*/:
|
1174
|
+
return source.charCodeAt(curPos - 1) === 61/*=*/;
|
1175
|
+
case 59/*;*/:
|
1176
|
+
case 41/*)*/:
|
1177
|
+
return true;
|
1178
|
+
case 104/*h*/:
|
1179
|
+
return source.startsWith('catc', curPos - 4);
|
1180
|
+
case 121/*y*/:
|
1181
|
+
return source.startsWith('finall', curPos - 6);
|
1182
|
+
case 101/*e*/:
|
1183
|
+
return source.startsWith('els', curPos - 3);
|
1184
|
+
}
|
1185
|
+
return false;
|
1186
|
+
}
|
1187
|
+
|
1188
|
+
function syntaxError () {
|
1189
|
+
throw Object.assign(new Error(`Parse error ${name}:${source.slice(0, pos).split('\n').length}:${pos - source.lastIndexOf('\n', pos - 1)}`), { idx: pos });
|
1190
|
+
}
|
1191
|
+
|
1192
|
+
async function defaultResolve (id, parentUrl) {
|
1193
|
+
return resolveImportMap(importMap, resolveIfNotPlainOrUrl(id, parentUrl) || id, parentUrl);
|
1194
|
+
}
|
1195
|
+
|
1196
|
+
async function _resolve (id, parentUrl) {
|
1197
|
+
const urlResolved = resolveIfNotPlainOrUrl(id, parentUrl);
|
1198
|
+
return {
|
1199
|
+
r: resolveImportMap(importMap, urlResolved || id, parentUrl),
|
1200
|
+
// b = bare specifier
|
1201
|
+
b: !urlResolved && !isURL(id)
|
1202
|
+
};
|
1203
|
+
}
|
1204
|
+
|
1205
|
+
const resolve = resolveHook ? async (id, parentUrl) => ({ r: await esmsInitOptions.resolve(id, parentUrl, defaultResolve), b: false }) : _resolve;
|
1206
|
+
|
1207
|
+
let id = 0;
|
1208
|
+
const registry = {};
|
1209
|
+
|
1210
|
+
async function loadAll (load, seen) {
|
1211
|
+
if (load.b || seen[load.u])
|
1212
|
+
return;
|
1213
|
+
seen[load.u] = 1;
|
1214
|
+
await load.L;
|
1215
|
+
await Promise.all(load.d.map(dep => loadAll(dep, seen)));
|
1216
|
+
if (!load.n)
|
1217
|
+
load.n = load.d.some(dep => dep.n);
|
1218
|
+
}
|
1219
|
+
|
1220
|
+
let importMap = { imports: {}, scopes: {} };
|
1221
|
+
let hasImportMap = false;
|
1222
|
+
let importMapSrcOrLazy = false;
|
1223
|
+
let importMapPromise = resolvedPromise;
|
1224
|
+
|
1225
|
+
let waitingForImportMapsInterval;
|
1226
|
+
let firstTopLevelProcess = true;
|
1227
|
+
async function topLevelLoad (url, fetchOpts, source, nativelyLoaded, lastStaticLoadPromise) {
|
1228
|
+
// no need to even fetch if we have feature support
|
1229
|
+
await featureDetectionPromise;
|
1230
|
+
if (waitingForImportMapsInterval > 0) {
|
1231
|
+
clearTimeout(waitingForImportMapsInterval);
|
1232
|
+
waitingForImportMapsInterval = 0;
|
1233
|
+
}
|
1234
|
+
if (firstTopLevelProcess) {
|
1235
|
+
firstTopLevelProcess = false;
|
1236
|
+
processScripts();
|
1237
|
+
}
|
1238
|
+
await importMapPromise;
|
1239
|
+
// early analysis opt-out
|
1240
|
+
if (nativelyLoaded && supportsDynamicImport && supportsImportMeta && supportsImportMaps && (!jsonModulesEnabled || supportsJsonAssertions) && (!cssModulesEnabled || supportsCssAssertions) && !importMapSrcOrLazy) {
|
1241
|
+
// dont reexec inline for polyfills -> just return null (since no module id for executed inline module scripts)
|
1242
|
+
return source && nativelyLoaded ? null : dynamicImport(source ? createBlob(source) : url);
|
1243
|
+
}
|
1244
|
+
await undefined;
|
1245
|
+
const load = getOrCreateLoad(url, fetchOpts, source);
|
1246
|
+
const seen = {};
|
1247
|
+
await loadAll(load, seen);
|
1248
|
+
lastLoad = undefined;
|
1249
|
+
resolveDeps(load, seen);
|
1250
|
+
await lastStaticLoadPromise;
|
1251
|
+
if (source && !shimMode && !load.n) {
|
1252
|
+
if (lastStaticLoadPromise) {
|
1253
|
+
didExecForReadyPromise = true;
|
1254
|
+
if (domContentLoaded)
|
1255
|
+
didExecForDomContentLoaded = true;
|
1256
|
+
}
|
1257
|
+
const module = await dynamicImport(createBlob(source));
|
1258
|
+
if (revokeBlobURLs) revokeObjectURLs(Object.keys(seen));
|
1259
|
+
return module;
|
1260
|
+
}
|
1261
|
+
const module = await dynamicImport(load.b);
|
1262
|
+
if (lastStaticLoadPromise && (!nativelyLoaded || load.b !== load.u)) {
|
1263
|
+
didExecForReadyPromise = true;
|
1264
|
+
if (domContentLoaded)
|
1265
|
+
didExecForDomContentLoaded = true;
|
1266
|
+
}
|
1267
|
+
// if the top-level load is a shell, run its update function
|
1268
|
+
if (load.s)
|
1269
|
+
(await dynamicImport(load.s)).u$_(module);
|
1270
|
+
if (revokeBlobURLs) revokeObjectURLs(Object.keys(seen));
|
1271
|
+
// when tla is supported, this should return the tla promise as an actual handle
|
1272
|
+
// so readystate can still correspond to the sync subgraph exec completions
|
1273
|
+
return module;
|
1274
|
+
}
|
1275
|
+
|
1276
|
+
function revokeObjectURLs(registryKeys) {
|
1277
|
+
let batch = 0;
|
1278
|
+
const keysLength = registryKeys.length;
|
1279
|
+
const schedule = self.requestIdleCallback ? self.requestIdleCallback : self.requestAnimationFrame;
|
1280
|
+
schedule(cleanup);
|
1281
|
+
function cleanup() {
|
1282
|
+
const batchStartIndex = batch * 100;
|
1283
|
+
if (batchStartIndex > keysLength) return
|
1284
|
+
for (const key of registryKeys.slice(batchStartIndex, batchStartIndex + 100)) {
|
1285
|
+
const load = registry[key];
|
1286
|
+
if (load) URL.revokeObjectURL(load.b);
|
1287
|
+
}
|
1288
|
+
batch++;
|
1289
|
+
schedule(cleanup);
|
1290
|
+
}
|
1291
|
+
}
|
1292
|
+
|
1293
|
+
async function importShim (id, parentUrl = baseUrl, _assertion) {
|
1294
|
+
await featureDetectionPromise;
|
1295
|
+
// Make sure all the "in-flight" import maps are loaded and applied.
|
1296
|
+
await importMapPromise;
|
1297
|
+
return topLevelLoad((await resolve(id, parentUrl)).r || throwUnresolved(id, parentUrl), { credentials: 'same-origin' });
|
1298
|
+
}
|
1299
|
+
|
1300
|
+
self.importShim = importShim;
|
1301
|
+
|
1302
|
+
const meta = {};
|
1303
|
+
|
1304
|
+
async function importMetaResolve (id, parentUrl = this.url) {
|
1305
|
+
await importMapPromise;
|
1306
|
+
return (await resolve(id, `${parentUrl}`)).r || throwUnresolved(id, parentUrl);
|
1307
|
+
}
|
1308
|
+
|
1309
|
+
self._esmsm = meta;
|
1310
|
+
|
1311
|
+
function urlJsString (url) {
|
1312
|
+
return `'${url.replace(/'/g, "\\'")}'`;
|
1313
|
+
}
|
1314
|
+
|
1315
|
+
let lastLoad;
|
1316
|
+
function resolveDeps (load, seen) {
|
1317
|
+
if (load.b || !seen[load.u])
|
1318
|
+
return;
|
1319
|
+
seen[load.u] = 0;
|
1320
|
+
|
1321
|
+
for (const dep of load.d)
|
1322
|
+
resolveDeps(dep, seen);
|
1323
|
+
|
1324
|
+
// use direct native execution when possible
|
1325
|
+
// load.n is therefore conservative
|
1326
|
+
if (!shimMode && !load.n) {
|
1327
|
+
load.b = lastLoad = load.u;
|
1328
|
+
load.S = undefined;
|
1329
|
+
return;
|
1330
|
+
}
|
1331
|
+
|
1332
|
+
const [imports] = load.a;
|
1333
|
+
|
1334
|
+
// "execution"
|
1335
|
+
const source = load.S;
|
1336
|
+
|
1337
|
+
// edge doesnt execute sibling in order, so we fix this up by ensuring all previous executions are explicit dependencies
|
1338
|
+
let resolvedSource = edge && lastLoad ? `import '${lastLoad}';` : '';
|
1339
|
+
|
1340
|
+
if (!imports.length) {
|
1341
|
+
resolvedSource += source;
|
1342
|
+
}
|
1343
|
+
else {
|
1344
|
+
// once all deps have loaded we can inline the dependency resolution blobs
|
1345
|
+
// and define this blob
|
1346
|
+
let lastIndex = 0, depIndex = 0;
|
1347
|
+
for (const { s: start, se: end, d: dynamicImportIndex } of imports) {
|
1348
|
+
// dependency source replacements
|
1349
|
+
if (dynamicImportIndex === -1) {
|
1350
|
+
const depLoad = load.d[depIndex++];
|
1351
|
+
let blobUrl = depLoad.b;
|
1352
|
+
if (!blobUrl) {
|
1353
|
+
// circular shell creation
|
1354
|
+
if (!(blobUrl = depLoad.s)) {
|
1355
|
+
blobUrl = depLoad.s = createBlob(`export function u$_(m){${
|
1356
|
+
depLoad.a[1].map(
|
1357
|
+
name => name === 'default' ? `$_default=m.default` : `${name}=m.${name}`
|
1358
|
+
).join(',')
|
1359
|
+
}}${
|
1360
|
+
depLoad.a[1].map(name =>
|
1361
|
+
name === 'default' ? `let $_default;export{$_default as default}` : `export let ${name}`
|
1362
|
+
).join(';')
|
1363
|
+
}\n//# sourceURL=${depLoad.r}?cycle`);
|
1364
|
+
}
|
1365
|
+
}
|
1366
|
+
// circular shell execution
|
1367
|
+
else if (depLoad.s) {
|
1368
|
+
resolvedSource += `${source.slice(lastIndex, start - 1)}/*${source.slice(start - 1, end)}*/${urlJsString(blobUrl)};import*as m$_${depIndex} from'${depLoad.b}';import{u$_ as u$_${depIndex}}from'${depLoad.s}';u$_${depIndex}(m$_${depIndex})`;
|
1369
|
+
lastIndex = end;
|
1370
|
+
depLoad.s = undefined;
|
1371
|
+
continue;
|
1372
|
+
}
|
1373
|
+
resolvedSource += `${source.slice(lastIndex, start - 1)}/*${source.slice(start - 1, end)}*/${urlJsString(blobUrl)}`;
|
1374
|
+
lastIndex = end;
|
1375
|
+
}
|
1376
|
+
// import.meta
|
1377
|
+
else if (dynamicImportIndex === -2) {
|
1378
|
+
meta[load.r] = { url: load.r, resolve: importMetaResolve };
|
1379
|
+
resolvedSource += `${source.slice(lastIndex, start)}self._esmsm[${urlJsString(load.r)}]`;
|
1380
|
+
lastIndex = end;
|
1381
|
+
}
|
1382
|
+
// dynamic import
|
1383
|
+
else {
|
1384
|
+
resolvedSource += `${source.slice(lastIndex, dynamicImportIndex + 6)}Shim(${source.slice(start, end)}, ${load.r && urlJsString(load.r)}`;
|
1385
|
+
lastIndex = end;
|
1386
|
+
}
|
1387
|
+
}
|
1388
|
+
|
1389
|
+
resolvedSource += source.slice(lastIndex);
|
1390
|
+
}
|
1391
|
+
|
1392
|
+
resolvedSource = resolvedSource.replace(/\/\/# sourceMappingURL=(.*)\s*$/, (match, url) => {
|
1393
|
+
return match.replace(url, new URL(url, load.r));
|
1394
|
+
});
|
1395
|
+
let hasSourceURL = false;
|
1396
|
+
resolvedSource = resolvedSource.replace(/\/\/# sourceURL=(.*)\s*$/, (match, url) => {
|
1397
|
+
hasSourceURL = true;
|
1398
|
+
return match.replace(url, new URL(url, load.r));
|
1399
|
+
});
|
1400
|
+
if (!hasSourceURL) {
|
1401
|
+
resolvedSource += '\n//# sourceURL=' + load.r;
|
1402
|
+
}
|
1403
|
+
|
1404
|
+
load.b = lastLoad = createBlob(resolvedSource);
|
1405
|
+
load.S = undefined;
|
1406
|
+
}
|
1407
|
+
|
1408
|
+
const jsContentType = /^(text|application)\/(x-)?javascript(;|$)/;
|
1409
|
+
const jsonContentType = /^(text|application)\/json(;|$)/;
|
1410
|
+
const cssContentType = /^(text|application)\/css(;|$)/;
|
1411
|
+
const wasmContentType = /^application\/wasm(;|$)/;
|
1412
|
+
|
1413
|
+
const cssUrlRegEx = /url\(\s*(?:(["'])((?:\\.|[^\n\\"'])+)\1|((?:\\.|[^\s,"'()\\])+))\s*\)/g;
|
1414
|
+
|
1415
|
+
async function doFetch (url, fetchOpts) {
|
1416
|
+
const res = await fetchHook(url, fetchOpts);
|
1417
|
+
if (!res.ok)
|
1418
|
+
throw new Error(`${res.status} ${res.statusText} ${res.url}`);
|
1419
|
+
const contentType = res.headers.get('content-type');
|
1420
|
+
if (jsContentType.test(contentType))
|
1421
|
+
return { r: res.url, s: await res.text(), t: 'js' };
|
1422
|
+
else if (jsonContentType.test(contentType))
|
1423
|
+
return { r: res.url, s: `export default ${await res.text()}`, t: 'json' };
|
1424
|
+
else if (cssContentType.test(contentType))
|
1425
|
+
return { r: res.url, s: `var s=new CSSStyleSheet();s.replaceSync(${
|
1426
|
+
JSON.stringify((await res.text()).replace(cssUrlRegEx, (_match, quotes, relUrl1, relUrl2) => `url(${quotes}${resolveUrl(relUrl1 || relUrl2, url)}${quotes})`))
|
1427
|
+
});export default s;`, t: 'css' };
|
1428
|
+
else if (wasmContentType.test(contentType))
|
1429
|
+
throw new Error('WASM modules not yet supported');
|
1430
|
+
else
|
1431
|
+
throw new Error(`Unknown Content-Type "${contentType}"`);
|
1432
|
+
}
|
1433
|
+
|
1434
|
+
function getOrCreateLoad (url, fetchOpts, source) {
|
1435
|
+
let load = registry[url];
|
1436
|
+
if (load)
|
1437
|
+
return load;
|
1438
|
+
|
1439
|
+
load = registry[url] = {
|
1440
|
+
// url
|
1441
|
+
u: url,
|
1442
|
+
// response url
|
1443
|
+
r: undefined,
|
1444
|
+
// fetchPromise
|
1445
|
+
f: undefined,
|
1446
|
+
// source
|
1447
|
+
S: undefined,
|
1448
|
+
// linkPromise
|
1449
|
+
L: undefined,
|
1450
|
+
// analysis
|
1451
|
+
a: undefined,
|
1452
|
+
// deps
|
1453
|
+
d: undefined,
|
1454
|
+
// blobUrl
|
1455
|
+
b: undefined,
|
1456
|
+
// shellUrl
|
1457
|
+
s: undefined,
|
1458
|
+
// needsShim
|
1459
|
+
n: false,
|
1460
|
+
// type
|
1461
|
+
t: null
|
1462
|
+
};
|
1463
|
+
|
1464
|
+
load.f = (async () => {
|
1465
|
+
if (!source) {
|
1466
|
+
// preload fetch options override fetch options (race)
|
1467
|
+
let t;
|
1468
|
+
({ r: load.r, s: source, t } = await (fetchCache[url] || doFetch(url, fetchOpts)));
|
1469
|
+
if (t && !shimMode) {
|
1470
|
+
if (t === 'css' && !cssModulesEnabled || t === 'json' && !jsonModulesEnabled)
|
1471
|
+
throw new Error(`${t}-modules must be enabled to polyfill via: window.esmsInitOptions = { polyfillEnable: ['${t}-modules'] }`);
|
1472
|
+
if (t === 'css' && !supportsCssAssertions || t === 'json' && !supportsJsonAssertions)
|
1473
|
+
load.n = true;
|
1474
|
+
}
|
1475
|
+
}
|
1476
|
+
try {
|
1477
|
+
load.a = parse(source, load.u);
|
1478
|
+
}
|
1479
|
+
catch (e) {
|
1480
|
+
console.warn(e);
|
1481
|
+
load.a = [[], []];
|
1482
|
+
}
|
1483
|
+
load.S = source;
|
1484
|
+
return load;
|
1485
|
+
})();
|
1486
|
+
|
1487
|
+
load.L = load.f.then(async () => {
|
1488
|
+
let childFetchOpts = fetchOpts;
|
1489
|
+
load.d = (await Promise.all(load.a[0].map(async ({ n, d }) => {
|
1490
|
+
if (d >= 0 && !supportsDynamicImport || d === 2 && !supportsImportMeta)
|
1491
|
+
load.n = true;
|
1492
|
+
if (!n) return;
|
1493
|
+
const { r, b } = await resolve(n, load.r || load.u);
|
1494
|
+
if (b && !supportsImportMaps)
|
1495
|
+
load.n = true;
|
1496
|
+
if (d !== -1) return;
|
1497
|
+
if (!r)
|
1498
|
+
throwUnresolved(n, load.r || load.u);
|
1499
|
+
if (skip.test(r)) return { b: r };
|
1500
|
+
if (childFetchOpts.integrity)
|
1501
|
+
childFetchOpts = Object.assign({}, childFetchOpts, { integrity: undefined });
|
1502
|
+
return getOrCreateLoad(r, childFetchOpts).f;
|
1503
|
+
}))).filter(l => l);
|
1504
|
+
});
|
1505
|
+
|
1506
|
+
return load;
|
1507
|
+
}
|
1508
|
+
|
1509
|
+
function processScripts () {
|
1510
|
+
if (waitingForImportMapsInterval > 0 && document.readyState !== 'loading') {
|
1511
|
+
clearTimeout(waitingForImportMapsInterval);
|
1512
|
+
waitingForImportMapsInterval = 0;
|
1513
|
+
}
|
1514
|
+
for (const link of document.querySelectorAll('link[rel="modulepreload"]'))
|
1515
|
+
processPreload(link);
|
1516
|
+
const scripts = document.querySelectorAll('script[type="module-shim"],script[type="importmap-shim"],script[type="module"],script[type="importmap"]');
|
1517
|
+
// early shim mode opt-in
|
1518
|
+
if (!shimMode) {
|
1519
|
+
for (const script of scripts) {
|
1520
|
+
if (script.type.endsWith('-shim'))
|
1521
|
+
setShimMode();
|
1522
|
+
}
|
1523
|
+
}
|
1524
|
+
for (const script of scripts)
|
1525
|
+
processScript(script);
|
1526
|
+
}
|
1527
|
+
|
1528
|
+
function getFetchOpts (script) {
|
1529
|
+
const fetchOpts = {};
|
1530
|
+
if (script.integrity)
|
1531
|
+
fetchOpts.integrity = script.integrity;
|
1532
|
+
if (script.referrerpolicy)
|
1533
|
+
fetchOpts.referrerPolicy = script.referrerpolicy;
|
1534
|
+
if (script.crossorigin === 'use-credentials')
|
1535
|
+
fetchOpts.credentials = 'include';
|
1536
|
+
else if (script.crossorigin === 'anonymous')
|
1537
|
+
fetchOpts.credentials = 'omit';
|
1538
|
+
else
|
1539
|
+
fetchOpts.credentials = 'same-origin';
|
1540
|
+
return fetchOpts;
|
1541
|
+
}
|
1542
|
+
|
1543
|
+
let staticLoadCnt = 0;
|
1544
|
+
let didExecForReadyPromise = false;
|
1545
|
+
let didExecForDomContentLoaded = false;
|
1546
|
+
let lastStaticLoadPromise = Promise.resolve();
|
1547
|
+
let domContentLoaded = false;
|
1548
|
+
document.addEventListener('DOMContentLoaded', () => domContentLoaded = true);
|
1549
|
+
function staticLoadCheck () {
|
1550
|
+
staticLoadCnt--;
|
1551
|
+
if (staticLoadCnt === 0 && !noLoadEventRetriggers) {
|
1552
|
+
if (didExecForDomContentLoaded)
|
1553
|
+
document.dispatchEvent(new Event('DOMContentLoaded'));
|
1554
|
+
if (didExecForReadyPromise && document.readyState === 'complete')
|
1555
|
+
document.dispatchEvent(new Event('readystatechange'));
|
1556
|
+
}
|
1557
|
+
}
|
1558
|
+
|
1559
|
+
function processScript (script, dynamic) {
|
1560
|
+
if (script.ep) // ep marker = script processed
|
1561
|
+
return;
|
1562
|
+
const shim = script.type.endsWith('-shim');
|
1563
|
+
if (shim && !shimMode) setShimMode();
|
1564
|
+
const type = shimMode ? script.type.slice(0, -5) : script.type;
|
1565
|
+
// dont process module scripts in shim mode or noshim module scripts in polyfill mode
|
1566
|
+
if (!shim && shimMode || script.getAttribute('noshim') !== null)
|
1567
|
+
return;
|
1568
|
+
// empty inline scripts sometimes show before domready
|
1569
|
+
if (!script.src && !script.innerHTML)
|
1570
|
+
return;
|
1571
|
+
script.ep = true;
|
1572
|
+
if (type === 'module') {
|
1573
|
+
const isReadyScript = document.readyState !== 'complete';
|
1574
|
+
if (isReadyScript) staticLoadCnt++;
|
1575
|
+
const loadPromise = topLevelLoad(script.src || `${baseUrl}?${id++}`, getFetchOpts(script), !script.src && script.innerHTML, !shimMode, isReadyScript && lastStaticLoadPromise).then(() => {
|
1576
|
+
script.dispatchEvent(new Event('load'));
|
1577
|
+
}).catch(e => {
|
1578
|
+
script.dispatchEvent(new Event('load'));
|
1579
|
+
setTimeout(() => { throw e; });
|
1580
|
+
onerror(e);
|
1581
|
+
});
|
1582
|
+
if (isReadyScript)
|
1583
|
+
lastStaticLoadPromise = loadPromise.then(staticLoadCheck);
|
1584
|
+
}
|
1585
|
+
else if (type === 'importmap') {
|
1586
|
+
// we dont currently support multiple, external or dynamic imports maps in polyfill mode to match native
|
1587
|
+
if (!shimMode && (hasImportMap || script.src || dynamic))
|
1588
|
+
return;
|
1589
|
+
hasImportMap = true;
|
1590
|
+
importMapPromise = importMapPromise.then(async () => {
|
1591
|
+
if (script.src || dynamic)
|
1592
|
+
importMapSrcOrLazy = true;
|
1593
|
+
importMap = resolveAndComposeImportMap(script.src ? await (await fetchHook(script.src)).json() : JSON.parse(script.innerHTML), script.src || baseUrl, importMap);
|
1594
|
+
});
|
1595
|
+
}
|
1596
|
+
}
|
1597
|
+
|
1598
|
+
const fetchCache = {};
|
1599
|
+
function processPreload (link) {
|
1600
|
+
if (link.ep) // ep marker = processed
|
1601
|
+
return;
|
1602
|
+
link.ep = true;
|
1603
|
+
if (fetchCache[link.href])
|
1604
|
+
return;
|
1605
|
+
fetchCache[link.href] = doFetch(link.href, getFetchOpts(link));
|
1606
|
+
}
|
1607
|
+
|
1608
|
+
new MutationObserver(mutations => {
|
1609
|
+
for (const mutation of mutations) {
|
1610
|
+
if (mutation.type !== 'childList') continue;
|
1611
|
+
for (const node of mutation.addedNodes) {
|
1612
|
+
if (node.tagName === 'SCRIPT' && node.type)
|
1613
|
+
processScript(node, !firstTopLevelProcess);
|
1614
|
+
else if (node.tagName === 'LINK' && node.rel === 'modulepreload')
|
1615
|
+
processPreload(node);
|
1616
|
+
else if (node.querySelectorAll) {
|
1617
|
+
for (const script of node.querySelectorAll('script[type="module-shim"],script[type="importmap-shim"],script[type="module"],script[type="importmap"]')) {
|
1618
|
+
processScript(script, !firstTopLevelProcess);
|
1619
|
+
}
|
1620
|
+
for (const link of node.querySelectorAll('link[rel=modulepreload]')) {
|
1621
|
+
processPreload(link);
|
1622
|
+
}
|
1623
|
+
}
|
1624
|
+
}
|
1625
|
+
}
|
1626
|
+
}).observe(document, { childList: true, subtree: true });
|
1627
|
+
|
1628
|
+
function throwUnresolved (id, parentUrl) {
|
1629
|
+
throw Error("Unable to resolve specifier '" + id + (parentUrl ? "' from " + parentUrl : "'"));
|
1630
|
+
}
|
1631
|
+
|
1632
|
+
processScripts();
|
1633
|
+
waitingForImportMapsInterval = setInterval(processScripts, 20);
|
1634
|
+
|
1635
|
+
}());
|