Skip to content

Commit

Permalink
update page order
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Jan 17, 2024
1 parent 280a5fe commit 8edf9f6
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 95 deletions.
91 changes: 0 additions & 91 deletions docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,97 +30,6 @@ If a module supports per method exports, like `lodash` does, it is recomended to
<x-import module="lodash" as="lodash" /> <!-- 78kb -->
```

## Path rewriting for local modules

If you want to import modules from any other directory than `node_modules`, you may add a `jsconfig.json` file to your project root with all your path aliases.

```json
{
"compilerOptions": {
"paths": {
"~/*": ["./resources/js/*"]
}
}
}
```

Consider the following example script `resources/js/alert.js`:

```javascript
export default function alertProxy(message) {
alert(message);
}
```

In order to use this script directly in your blade views, you simply need to import it using the `<x-import />` component.

```html
<x-import module="~/alert" as="alert" />

<script type="module">
const module = await _import("alert");
module("Hello World!");
</script>
```

## Self evaluating exports

**Beta**

You can use this mechanism to immediatly execute some code or to bootstrap & import other libraries.

Consider the following example the following file `resources/js/immediately-invoked.js`

```javascript
export default (() => {
alert('Hello World!)
})();
```
Then in your template you can use the `<x-import />` component to evaluate this function. Without the need of calling the `_import()` function. Note this only works with a [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE){:target="\_blank"}
```html
<!-- User will be alerted with 'Hello World' -->
<x-import module="~/immediately-invoked" />
```
This can be used in a variety of creative ways. For example for swapping out Laravel's default `bootstrap.js` for an approach where you only pull in a configured library when you need it.

```javascript
import axios from "axios";
export default (() => {
window.axios = axios;
window.axios.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
})();
```

When importing this module you can omit the `as` prop. Axios will be available on the window object since the function is evaluated on import.

```html
<x-import module="~/bootstrap/axios" />

<script type="module">
axios.get("/user/12345").then(function (response) {
console.log(response);
});
</script>
```
Note that your consuming script still needs to be of `type="module"` otherwise `window.axios` will be undefined.
{: .warning }
> Code splitting is [not supported](https://laravel-bundle.dev/caveats.html#code-splitting). Be careful when importing modules in your local scripts like this. When two script rely on the same dependency, it will be included in both bundles. This approach is meant to be used as a method to allow setup of more complex libraries. It is recommended to place business level code inside your templates instead.
Please note Bundle's primary goal is to get imports inside your Blade template. While the IIFE strategy can be very powerful, it is not the place to put a lot of business code since can be a lot harder to debug.
{: .note }
> Bundle is meant as a tool for Blade centric apps, like [Livewire](https://livewire.laravel.com), to enable code colocation with page specific JavaScript. Preferably the bulk of custom code should live inline in a script tag or in a [Alpine]((https://alpinejs.dev)) component.
## Sourcemaps

Sourcemaps are disabled by default. You may enable this by setting `BUNDLE_SOURCEMAPS_ENABLED` to true in your env file or by publishing and updating the bundle config.
Expand Down
2 changes: 1 addition & 1 deletion docs/caveats.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
nav_order: 6
nav_order: 7
title: Caveats
image: "/assets/social-square.png"
---
Expand Down
2 changes: 1 addition & 1 deletion docs/integrations/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
nav_order: 5
nav_order: 6
has_children: true
title: Integration examples
image: "/assets/social-square.png"
Expand Down
2 changes: 1 addition & 1 deletion docs/production-builds.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
nav_order: 4
nav_order: 5
title: Production builds
image: "/assets/social-square.png"
---
Expand Down
2 changes: 1 addition & 1 deletion docs/roadmap.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
nav_order: 7
nav_order: 8
title: Roadmap
image: "/assets/social-square.png"
---
Expand Down

0 comments on commit 8edf9f6

Please sign in to comment.