Repository: googlemaps/js-api-loader
Pull request #57 (2020-09-21 06:05:55)

build(deps-dev): bump prettier from 2.1.1 to 2.1.2
Bumps [prettier](https://github.com/prettier/prettier) from 2.1.1 to 2.1.2.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/prettier/prettier/releases">prettier's releases</a>.</em></p>
<blockquote>
<h2>2.1.2</h2>
<p><a href="https://github.com/prettier/prettier/blob/master/CHANGELOG.md#212">🔗Changelog</a></p>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/prettier/prettier/blob/master/CHANGELOG.md">prettier's changelog</a>.</em></p>
<blockquote>
<h1>2.1.2</h1>
<p><a href="https://github.com/prettier/prettier/compare/2.1.1...2.1.2">diff</a></p>
<h4>Fix formatting for directives in fields (<a href="https://github-redirect.dependabot.com/prettier/prettier/pull/9116">#9116</a> by <a href="https://github.com/sosukesuzuki">@sosukesuzuki</a>)</h4>
<!-- raw HTML omitted -->
<pre lang="graphql"><code># Input
type Query {
  someQuery(id: ID!, someOtherData: String!): String! @deprecated @isAuthenticated
  versions: Versions!
}
<h1>Prettier stable</h1>
<p>type Query {
someQuery(id: ID!, someOtherData: String!): String!
<a href="https://github.com/deprecated">@deprecated</a>
<a href="https://github.com/isAuthenticated">@isAuthenticated</a>
versions: Versions!
}</p>
<h1>Prettier master</h1>
<p>type Query {
someQuery(id: ID!, someOtherData: String!): String!
<a href="https://github.com/deprecated">@deprecated</a>
<a href="https://github.com/isAuthenticated">@isAuthenticated</a>
versions: Versions!
}</p>
<p></code></pre></p>
<h4>Fix line breaks for CSS in JS (<a href="https://github-redirect.dependabot.com/prettier/prettier/pull/9136">#9136</a> by <a href="https://github.com/sosukesuzuki">@sosukesuzuki</a>)</h4>
<!-- raw HTML omitted -->
<pre lang="js"><code>// Input
styled.div`
  // prettier-ignore
  @media (aaaaaaaaaaaaa) {
	z-index: ${(props) =&gt; (props.isComplete ? '1' : '0')};
  }
`;
styled.div`
  ${props =&gt; getSize(props.$size.xs)}
  ${props =&gt; getSize(props.$size.sm, 'sm')}
  ${props =&gt; getSize(props.$size.md, 'md')}
`;
<p>&lt;/tr&gt;&lt;/table&gt; ... (truncated)
</code></pre></p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/prettier/prettier/commit/8eb8f9ed2dc5eb1c3e6f787e5a5e6fe68af2b8d1"><code>8eb8f9e</code></a> Release 2.1.2</li>
<li><a href="https://github.com/prettier/prettier/commit/f75844a453beb6cb16791c14e5ff738c02d0b4df"><code>f75844a</code></a> Fix changelog</li>
<li><a href="https://github.com/prettier/prettier/commit/9153bf20ac2dfcd525b5dd3f3b37d9b6005d20bc"><code>9153bf2</code></a> Build(deps): Bump yaml-unist-parser from 1.3.0 to 1.3.1 (<a href="https://github-redirect.dependabot.com/prettier/prettier/issues/9169">#9169</a>)</li>
<li><a href="https://github.com/prettier/prettier/commit/ec1b4193079a625633dd0dc86f9a9217a3ad9d7e"><code>ec1b419</code></a> YAML: Fix printing doubles a blank line before a comment (<a href="https://github-redirect.dependabot.com/prettier/prettier/issues/9143">#9143</a>)</li>
<li><a href="https://github.com/prettier/prettier/commit/ab9474eacc9c883e588ef2500c6230057328ade5"><code>ab9474e</code></a> Fix line breaks for CSS in JS (<a href="https://github-redirect.dependabot.com/prettier/prettier/issues/9136">#9136</a>)</li>
<li><a href="https://github.com/prettier/prettier/commit/d5da779cb195443c935fd7171f5e81141e2f62e5"><code>d5da779</code></a> GraphQL: Fix formatting for directives in fields (<a href="https://github-redirect.dependabot.com/prettier/prettier/issues/9116">#9116</a>)</li>
<li><a href="https://github.com/prettier/prettier/commit/a8363197118e530d948978da6e5c414a765ba9c0"><code>a836319</code></a> Bump Prettier dependency to 2.1.1</li>
<li>See full diff in <a href="https://github.com/prettier/prettier/compare/2.1.1...2.1.2">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=prettier&package-manager=npm_and_yarn&previous-version=2.1.1&new-version=2.1.2)](https://docs.github.com/en/github/managing-security-vulnerabilities/configuring-github-dependabot-security-updates)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>

codecov[bot]: # [Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/57?src=pr&el=h1) Report
> Merging [#57](https://codecov.io/gh/googlemaps/js-api-loader/pull/57?src=pr&el=desc) into [master](https://codecov.io/gh/googlemaps/js-api-loader/commit/caffe67d7b989af3a1c7f4a1a6c79bd9fb2b4eb0?el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/googlemaps/js-api-loader/pull/57/graphs/tree.svg?width=650&height=150&src=pr&token=aAmgcWFXMs)](https://codecov.io/gh/googlemaps/js-api-loader/pull/57?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master       #57   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           74        74           
  Branches        10        10           
=========================================
  Hits            74        74           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/57?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/57?src=pr&el=footer). Last update [caffe67...e28956a](https://codecov.io/gh/googlemaps/js-api-loader/pull/57?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index f9c31c3..678efda 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -38,3 +38,15 @@ jobs:
         env:
           GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
           NPM_TOKEN: ${{ secrets.NPM_WOMBAT_TOKEN }}
+      - run: |        
+          npm i
+          npm run docs
+          cp -r dist docs/dist
+          cp -r examples docs/examples
+      - uses: peaceiris/actions-gh-pages@v3
+        with:
+          github_token: ${{ secrets.GITHUB_TOKEN }}
+          publish_dir: ./docs
+          user_name: 'googlemaps-bot'
+          user_email: 'googlemaps-bot@users.noreply.github.com'
+          commit_message: ${{ github.event.head_commit.message }}
diff --git a/.releaserc b/.releaserc
new file mode 100644
index 0000000..8343600
--- /dev/null
+++ b/.releaserc
@@ -0,0 +1,8 @@
+plugins:
+  - "@semantic-release/commit-analyzer"
+  - "@semantic-release/release-notes-generator"
+  - "@semantic-release/github"
+  - "@semantic-release/npm"
+  - "@semantic-release/git"
+options:
+  debug: true
diff --git a/package-lock.json b/package-lock.json
index 4734901..10993fa 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "@googlemaps/js-api-loader",
-  "version": "1.0.0",
+  "version": "1.4.1",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
@@ -2760,9 +2760,9 @@
       }
     },
     "@types/jest": {
-      "version": "26.0.10",
-      "resolved": "https://registry.npmjs.org/@types/jest/-/jest-26.0.10.tgz",
-      "integrity": "sha512-i2m0oyh8w/Lum7wWK/YOZJakYF8Mx08UaKA1CtbmFeDquVhAEdA7znacsVSf2hJ1OQ/OfVMGN90pw/AtzF8s/Q==",
+      "version": "26.0.14",
+      "resolved": "https://registry.npmjs.org/@types/jest/-/jest-26.0.14.tgz",
+      "integrity": "sha512-Hz5q8Vu0D288x3iWXePSn53W7hAjP0H7EQ6QvDO9c7t46mR0lNOLlfuwQ+JkVxuhygHzlzPX+0jKdA3ZgSh+Vg==",
       "dev": true,
       "requires": {
         "jest-diff": "^25.2.1",
@@ -7426,9 +7426,9 @@
       "dev": true
     },
     "prettier": {
-      "version": "2.1.1",
-      "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.1.1.tgz",
-      "integrity": "sha512-9bY+5ZWCfqj3ghYBLxApy2zf6m+NJo5GzmLTpr9FsApsfjriNnS2dahWReHMi7qNPhhHl9SYHJs2cHZLgexNIw==",
+      "version": "2.1.2",
+      "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.1.2.tgz",
+      "integrity": "sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==",
       "dev": true
     },
     "pretty-format": {
diff --git a/package.json b/package.json
index 31eeac6..1210dcf 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@googlemaps/js-api-loader",
-  "version": "1.0.0",
+  "version": "1.4.1",
   "description": "Wrapper for the loading of Google Maps JavaScript API script in the browser",
   "keywords": [
     "google",
diff --git a/src/index.ts b/src/index.ts
index 733be02..35ed926 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -23,7 +23,13 @@ declare global {
   }
 }
 
-type Libraries = ("drawing" | "geometry" | "localContext" | "places" | "visualization")[];
+type Libraries = (
+  | "drawing"
+  | "geometry"
+  | "localContext"
+  | "places"
+  | "visualization"
+)[];
 
 /**
  * The Google Maps JavaScript API
@@ -65,6 +71,10 @@ export interface LoaderOptions {
    * receive your default channel.
    */
   version?: string;
+  /**
+   * The id of the script tag. Before adding a new script, the Loader will check for an existing one.
+   */
+  id?: string;
   /**
    * When loading the Maps JavaScript API via the URL you may optionally load
    * additional libraries through use of the libraries URL parameter. Libraries
@@ -130,6 +140,10 @@ export interface LoaderOptions {
    * your bootstrap request.
    */
   mapIds?: string[];
+  /**
+   * Use a custom url and path to load the Google Maps API script.
+   */
+  url?: string;
 }
 
 /**
@@ -160,6 +174,10 @@ export class Loader {
    * See [[LoaderOptions.apiKey]]
    */
   apiKey: string;
+  /**
+   * See [[LoaderOptions.id]]
+   */
+  id: string;
   /**
    * See [[LoaderOptions.libraries]]
    */
@@ -179,8 +197,12 @@ export class Loader {
    */
   mapIds: string[];
 
+  /**
+   * See [[LoaderOptions.url]]
+   */
+  url: string;
+
   private CALLBACK = "__googleMapsCallback";
-  private URL = "https://maps.googleapis.com/maps/api/js";
   private callbacks: ((e: Event) => void)[] = [];
   private done = false;
   private loading = false;
@@ -197,18 +219,22 @@ export class Loader {
    */
   constructor({
     apiKey,
+    id = "__googleMapsScriptId",
     libraries = [],
     language,
     region,
     version,
     mapIds,
+    url = "https://maps.googleapis.com/maps/api/js",
   }: LoaderOptions) {
     this.version = version;
     this.apiKey = apiKey;
+    this.id = id;
     this.libraries = libraries;
     this.language = language;
     this.region = region;
     this.mapIds = mapIds;
+    this.url = url;
   }
   /**
    * CreateUrl returns the Google Maps JavaScript API script url given the [[LoaderOptions]].
@@ -216,7 +242,7 @@ export class Loader {
    * @ignore
    */
   createUrl(): string {
-    let url = this.URL;
+    let url = this.url;
 
     url += `?callback=${this.CALLBACK}`;
 
@@ -283,9 +309,14 @@ export class Loader {
    * Set the script on document.
    */
   private setScript(): void {
+    if (this.id && document.getElementById(this.id)) {
+      this.callback();
+      return;      
+    }
+    
     const url = this.createUrl();
     const script = document.createElement("script");
-
+    script.id = this.id;
     script.type = "text/javascript";
     script.src = url;
     script.onerror = this.loadErrorCallback.bind(this);


Test patch:
diff --git a/src/index.test.ts b/src/index.test.ts
index de5f0d7..727cff2 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -16,6 +16,10 @@
 
 import { Loader, LoaderOptions } from ".";
 
+afterEach(() => {
+  document.getElementsByTagName('html')[0].innerHTML = ''; 
+});
+
 test.each([
   [{}, "https://maps.googleapis.com/maps/api/js?callback=__googleMapsCallback"],
   [
@@ -36,6 +40,10 @@ test.each([
     { mapIds: ["foo", "bar"] },
     "https://maps.googleapis.com/maps/api/js?callback=__googleMapsCallback&map_ids=foo,bar",
   ],
+  [
+    { url: "https://example.com/js" },
+    "https://example.com/js?callback=__googleMapsCallback",
+  ],
 ])("createUrl is correct", (options: LoaderOptions, expected: string) => {
   const loader = new Loader(options);
   expect(loader.createUrl()).toEqual(expected);
@@ -48,6 +56,7 @@ test("setScript adds a script to head with correct attributes", () => {
 
   const script = document.head.childNodes[0] as HTMLScriptElement;
 
+  expect(script.id).toEqual(loader.id);
   expect(script.src).toEqual(loader.createUrl());
   expect(script.defer).toBeTruthy();
   expect(script.async).toBeTruthy();
@@ -55,6 +64,21 @@ test("setScript adds a script to head with correct attributes", () => {
   expect(script.type).toEqual("text/javascript");
 });
 
+test("setScript does not add second script with same id", () => {
+  new Loader({ apiKey: "foo", id: "bar" })['setScript']();
+  new Loader({ apiKey: "foo", id: "bar" })['setScript']();
+
+  expect(document.head.childNodes.length).toBe(1);
+});
+
+test("setScript adds a script with id", () => {
+  const loader = new Loader({ apiKey: "foo", id: "bar" });
+  loader["setScript"]();
+
+  const script = document.head.childNodes[0] as HTMLScriptElement;
+  expect(script.id).toEqual(loader.id);
+});
+
 test("load should return a promise that resolves even if called twice", () => {
   const loader = new Loader({ apiKey: "foo" });