Skip to content

Commit 02f2e3c

Browse files
committed
Remove dependency Drupal's core libraries.
1 parent 55d3af0 commit 02f2e3c

10 files changed

Lines changed: 58 additions & 54 deletions

File tree

dist/init.js

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,36 @@
1-
(function (drupalSettings) {
1+
if (window.wire === undefined) {
2+
window.wire = new Wire();
3+
window.Wire = window.wire;
4+
}
25

3-
var isDebug = drupalSettings.wireIsDebug || false;
4-
5-
if (window.wire === undefined) {
6-
window.wire = new Wire();
7-
window.wire.devTools(isDebug);
8-
window.Wire = window.wire;
9-
window.wire_app_url = drupalSettings.wireAppUrl || '/';
10-
}
11-
12-
if (isDebug && window.Alpine) {
13-
document.addEventListener("DOMContentLoaded", function () {
14-
setTimeout(function () {
15-
console.warn("Wire: It looks like AlpineJS has already been loaded. Make sure Wire\'s scripts are loaded before Alpine.\\n\\n Reference docs for more info: https://wire-drupal.com/docs/alpine-js")
16-
})
17-
});
18-
}
19-
20-
window.deferLoadingAlpine = function (callback) {
21-
window.addEventListener('wire:load', function () {
22-
callback();
23-
});
24-
};
6+
if (window.Alpine) {
7+
document.addEventListener("DOMContentLoaded", function () {
8+
setTimeout(function () {
9+
console.warn("Wire: It looks like AlpineJS has already been loaded. Make sure Wire\'s scripts are loaded before Alpine.\\n\\n Reference docs for more info: https://wire-drupal.com/docs/alpine-js")
10+
})
11+
});
12+
}
2513

26-
var started = false;
14+
window.deferLoadingAlpine = function (callback) {
15+
window.addEventListener('wire:load', function () {
16+
callback();
17+
});
18+
};
2719

28-
window.addEventListener('alpine:initializing', function () {
29-
if (!started) {
30-
window.wire.start();
20+
var started = false;
3121

32-
started = true;
33-
}
34-
});
22+
window.addEventListener('alpine:initializing', function () {
23+
if (!started) {
24+
window.wire.start();
3525

36-
document.addEventListener("DOMContentLoaded", function () {
37-
if (!started) {
38-
window.wire.start();
26+
started = true;
27+
}
28+
});
3929

40-
started = true;
41-
}
42-
});
30+
document.addEventListener("DOMContentLoaded", function () {
31+
if (!started) {
32+
window.wire.start();
4333

44-
})(drupalSettings);
34+
started = true;
35+
}
36+
});

dist/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"/wire.js":"/wire.js?id=0cca5d7f474f99be4663"}
1+
{"/wire.js":"/wire.js?id=60ae6f8eea0ee779d6d3"}

dist/wire.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/wire.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/component/UploadManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class UploadManager {
7676

7777
let csrfToken = getCsrfToken()
7878

79-
if (csrfToken) headers['X-CSRF-TOKEN'] = csrfToken
79+
if (csrfToken) headers['W-CSRF-TOKEN'] = csrfToken
8080

8181
this.makeRequest(name, formData, 'post', url, headers, response => {
8282
return response.paths

js/connection/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import store from '@/Store'
22
import componentStore from '../Store'
3-
import { getCsrfToken } from '@/util'
3+
import { getCsrfToken, getAppUrl } from '@/util'
44

55
export default class Connection {
66
constructor() {
@@ -30,7 +30,7 @@ export default class Connection {
3030
sendMessage(message) {
3131
let payload = message.payload()
3232
let csrfToken = getCsrfToken()
33-
let appUrl = window.wire_app_url
33+
let appUrl = getAppUrl()
3434

3535
if (this.shouldUseLocalePrefix(payload)) {
3636
appUrl = `${appUrl}/${payload.fingerprint.locale}`

js/util/getAppUrl.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function getAppUrl() {
2+
const appUrlTag = document.head.querySelector('meta[name="w-app-url"]')
3+
4+
if (appUrlTag) {
5+
return appUrlTag.content
6+
}
7+
8+
return window.wire_app_url ?? window.location.origin ?? ''
9+
}

js/util/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './wire-directives'
44
export * from './walk'
55
export * from './dispatch'
66
export * from './getCsrfToken'
7+
export * from './getAppUrl'
78

89
export function kebabCase(subject) {
910
return subject.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/[_\s]/, '-').toLowerCase()

wire.libraries.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
init:
2-
version: 0.0.2
2+
version: 0.0.3
33
js:
44
dist/init.js: {
55
minified: false
66
}
77
dependencies:
8-
- core/drupalSettings
98
- wire/js-base
109
- wire/styles
1110

@@ -18,7 +17,7 @@ styles:
1817
}
1918

2019
js-base:
21-
version: 0cca5d7f474f99be4663
20+
version: 60ae6f8eea0ee779d6d3
2221
js:
2322
dist/wire.js: {
2423
minified: true,

wire.module

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
function wire_page_attachments(array &$page): void {
1212

1313
$request = Drupal::request();
14-
$container = \Drupal::getContainer();
1514

16-
// Add needed values.
17-
$page['#attached']['drupalSettings'] = [
18-
'wireAppUrl' => $request->getSchemeAndHttpHost(),
19-
'wireIsDebug' => $container->hasParameter('twig.config')
20-
&& \is_array($container->getParameter('twig.config'))
21-
&& $container->getParameter('twig.config')['debug'],
15+
// Add App url to the page.
16+
$page['#attached']['html_head'][] = [
17+
[
18+
'#tag' => 'meta',
19+
'#attributes' => [
20+
'name' => 'w-app-url',
21+
'content' => Drupal::request()->getSchemeAndHttpHost(),
22+
],
23+
],
24+
'wire-app-url',
2225
];
2326

2427
// Add CSRF token to the page for authenticated users.

0 commit comments

Comments
 (0)