-
Notifications
You must be signed in to change notification settings - Fork 199
Get document URL in esm-amd-loader #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
0c68b63
9adc331
b1d7def
4f2d5b8
790de98
e8f136f
8c1e6fd
14b8fae
7e59cca
95c92ad
a0f2f83
4a2f82e
74c41c4
f23ac6f
ae21038
5a7679a
c7be81f
ff64dbc
9a7aea5
1c955f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ interface Window { | |
| define: ((deps: string[], moduleBody: OnExecutedCallback) => void)&{ | ||
| _reset?: () => void; | ||
| }; | ||
| HTMLImports: { importForElement: (element: Element) => HTMLLinkElement | undefined } | ||
| } | ||
|
|
||
| type OnExecutedCallback = (...args: Array<{}>) => void; | ||
|
|
@@ -421,14 +422,15 @@ window.define = function(deps: string[], moduleBody?: OnExecutedCallback) { | |
| return [deps, moduleBody]; | ||
| }; | ||
|
|
||
| // Case #2: We are a top-level script in the HTML document. Our URL is the | ||
| // document's base URL. We can discover this case by waiting a tick, and if | ||
| // we haven't already been defined by the "onload" handler from case #1, | ||
| // then this must be case #2. | ||
| // Case #2: We are a top-level script in the HTML document or a HTML import. | ||
| // Resolve the URL relative to the document url. We can discover this case | ||
| // by waiting a tick, and if we haven't already been defined by the "onload" | ||
| // handler from case #1, then this must be case #2. | ||
| const documentUrl = getDocumentUrl(); | ||
| setTimeout(() => { | ||
| if (defined === false) { | ||
| pendingDefine = undefined; | ||
| const url = baseUrl + '#' + topLevelScriptIdx++ as NormalizedUrl; | ||
| const url = documentUrl + '#' + topLevelScriptIdx++ as NormalizedUrl; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you'll also need to update line 267 (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I've made the change. |
||
| // It's actually Initialized, but we're skipping over the Loading | ||
| // state, because this is a top level document and it's already loaded. | ||
| const mod = getModule(url) as ModuleG<StateEnum.Loading>; | ||
|
|
@@ -545,4 +547,37 @@ function getBaseUrl(): NormalizedUrl { | |
| (document.querySelector('base') || window.location).href) as | ||
| NormalizedUrl; | ||
| } | ||
|
|
||
| /** | ||
| * Get the url of the current document. If the document is the main document, the base | ||
| * url is returned. Otherwise if the module was imported by a HTML import we need to | ||
| * resolve the URL relative to the HTML import. | ||
| * | ||
| * document.currentScript does not work in IE11, but the HTML import polyfill mocks it | ||
| * when executing an import so for this case that's ok | ||
| */ | ||
| function getDocumentUrl() { | ||
| const { currentScript } = document; | ||
| // On IE11 document.currentScript is not defined when not in a HTML import | ||
| if (!currentScript) { | ||
| return baseUrl; | ||
| } | ||
|
|
||
| if (window.HTMLImports) { | ||
| // When the HTMLImports polyfill is active, we can take the path from the link element | ||
| const htmlImport = window.HTMLImports.importForElement(currentScript); | ||
| if (!htmlImport) { | ||
| // If there is no import for the current script, we are in the index.html. Take the base url. | ||
| return baseUrl; | ||
| } | ||
| // Take the import href and strip off the filename | ||
| return htmlImport.href.substring(0, htmlImport.href.lastIndexOf('/') + 1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to be a bit more careful here, e.g. this will return
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if the href will ever be http://example.com for a html import, won't it always need to be a specific file? But it turns out we can just return the href here, this is in line with what we're returning from the native implementation as well. |
||
| } else { | ||
| // On chrome's native implementation it's not possible to get a direct reference to the link element, | ||
| // create a new script and let the browser resolve the url. | ||
| const script = currentScript.ownerDocument.createElement('script'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use an |
||
| script.src = './'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs to be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't think '' would resolve to anything, but it did. It's better because it resolves to the filename. |
||
| return script.src; | ||
| } | ||
| } | ||
| })(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTMLImports?: ...since it might not be defined