npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

inertia-page-loader

v0.8.0

Published

The plugin page loader for Inertia.js

Downloads

11

Readme

Inertia Page Loader

NPM version Software License GitHub Tests Action Status Total Downloads

The plugin page loader for Inertia.js, that allows the server-side to use Inertia::render('my-package::Page');.

Features

  • Powered by unplugin
  • Supports static build with Vite and Laravel Mix
  • Supports load pages on runtime
  • Define the namespace mapping for plugins pages directory
  • Or read namespace from the npm / composer package

Install

First, install the Inertia Plugin to your main Inertia app:

npm i inertia-page-loader -D
// vite.config.js
import InertiaPageLoader from 'inertia-page-loader/vite'

export default defineConfig({
  plugins: [
    InertiaPageLoader({ /* options */ }),
  ],
})

// webpack.config.js
const InertiaPageLoaderPlugin = require('inertia-page-loader/webpack')

module.exports = {
  /* ... */
  plugins: [
    InertiaPageLoaderPlugin({ /* options */ }),
  ],
}

// webpack.mix.js
const InertiaPageLoaderPlugin = require('inertia-page-loader/webpack')

mix
  .webpackConfig({
    plugins: [
      InertiaPageLoaderPlugin({ /* options */ }),
    ],
  })

Type

Add to env.d.ts:

/// <reference types="inertia-page-loader/client" />

Usage

This package supports the Static and Runtime to load the pages (can be mixed to use), so you can select the way to build and use your Inertia pages:

Build for Static

Then select the source from which you want to load the page:

If you created or have a package, you can select the build tool to use the package:

Load Pages from NPM Package

You must create an npm package that contains the pages folder:

src/pages/
  ├── Some.vue
  └── Dir/
     └── Other.vue

And added the inertia field to define the namespace mapping, for example in node_modules/my-plugin/package.json:

{
  "name": "my-plugin",
  "inertia": {
    "my-package": "src/pages"
  }
}

Publish this package and back to the main Inertia app to install this package:

npm i my-plugin

Next step you can select the build tool to use:

Load Pages from Composer Package

You must create a composer package that contains the pages folder:

resources/js/pages/
  ├── Some.vue
  └── Dir/
     └── Other.vue

And added the extra.inertia field to define the namespace mapping, for example in vendor/ycs77/my-php-package/composer.json:

{
    "name": "ycs77/my-php-package",
    "extra": {
        "inertia": {
            "my-php-package": "resources/js/pages"
        }
    }
}

Publish this package and back to the main Inertia app to install this package:

composer require ycs77/my-php-package

Next step you can select the build tool to use:

Usage with Vite

Add inertia-page-loader to vite.config.js, and you can use the function npm() or composer() to load the namespace:

import InertiaPageLoader from 'inertia-page-loader/vite'

export default defineConfig({
  plugins: [
    InertiaPageLoader({
      namespaces: ({ npm, composer }) => [
        // load namespace from npm package:
        npm('my-plugin'),

        // load namespace from composer package:
        composer('ycs77/my-php-package'),
      ],
    }),
  ],
})

And use resolvePage() in resources/js/app.js to resolve the app pages and npm / composer pages (don't use one line function):

import { resolvePage } from '~inertia'

createInertiaApp({
  resolve: resolvePage(() => {
    return import.meta.glob('./pages/**/*.vue', { eager: true })
  }),
})

Or you can add the persistent layout:

import Layout from './Layout'

createInertiaApp({
  resolve: resolvePage(name => {
    return import.meta.glob('./pages/**/*.vue', { eager: true })
  }, page => {
    page.layout = Layout
    return page
  }),
})

Now you can use the page in your controller:

Inertia::render('my-package::Some'); // in npm package
Inertia::render('my-php-package::Some'); // in composer package

Usage with Laravel Mix

Add inertia-page-loader to webpack.mix.js, and you can use the function npm() or composer() to load the namespace:

mix
  .webpackConfig({
    plugins: [
      InertiaPageLoaderPlugin({
        namespaces: ({ npm, composer }) => [
          // load namespace from npm package:
          npm('my-plugin'),

          // load namespace from composer package:
          composer('ycs77/my-php-package'),
        ],
      }),
    ],
  })

And use resolvePage() in resources/js/app.js to resolve the app pages and npm / composer pages:

import { resolvePage } from '~inertia'

createInertiaApp({
  resolve: resolvePage(name => require(`./pages/${name}`)),
})

Or you can add the persistent layout:

import Layout from './Layout'

createInertiaApp({
  resolve: resolvePage(name => require(`./pages/${name}`), page => {
    page.layout = Layout
    return page
  }),
})

Now you can use the page in your controller:

Inertia::render('my-package::Some'); // in npm package
Inertia::render('my-php-package::Some'); // in composer package

Load pages from Modules (in the main app)

If you use the modules package to manage your Laravel application, such as Laravel Modules, you can also define namespace mapping:

Note: Of course, can also be load pages from other locations in the main application.

export default defineConfig({
  plugins: [
    InertiaPageLoader({
      namespaces: [
        // define namespace mapping:
        { 'my-module': 'Modules/MyModule/Resources/js/pages' },

        // define more namespace mapping:
        {
          'my-module-2': 'Modules/MyModule2/Resources/js/pages',
          'special-modal': 'resources/js/SpecialModals',
        },
      ],
    }),
  ],
})

Now you can use the page in your controller:

Inertia::render('my-module::Some');
Inertia::render('my-module-2::Some');
Inertia::render('special-modal::VeryCoolModal');

Build for Runtime

[!WARNING] The runtime is not working with Vue Composition, it's recommended to use Build for Static.

Sometimes you may want users to use the pages without compiling them after installing the composer package, at this time you can load them at runtime. This is the package directory structure:

resources/js/
  ├── my-runtime-plugin.js
  └── pages/
     ├── Some.vue
     └── Other.vue

Use the InertiaPages runtime API in resources/js/my-runtime-plugin.js to load pages:

window.InertiaPages.addNamespace('my-runtime', name => require(`./pages/${name}`))

And setting webpack.mix.js to build assets:

const mix = require('laravel-mix')

mix
  .setPublicPath('public')
  .js('resources/js/my-runtime-plugin.js', 'public/js')
  .vue({ runtimeOnly: true })
  .version()
  .disableNotifications()

Now you can publish this package and install it in the Inertia app, publish assets (my-runtime-plugin.js) to public/vendor/inertia-plugins, and open app.blade.php to include scripts to load pages:

<head>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/runtime.iife.js" defer></script>
  <script src="/vendor/inertia-plugins/my-runtime-plugin.js" defer></script>
  <!-- app.js must be last one -->
  <script src="{{ mix('/js/app.js') }}" defer></script>
</head>

But the app.js must build with inertia-page-loader, you can follow Install chapter to install it (does not need to include any option), like this:

// vite.config.js
import InertiaPageLoader from 'inertia-page-loader/vite'

export default defineConfig({
  plugins: [
    InertiaPageLoader(),
  ],
})

Or using in Laravel Mix:

// webpack.mix.js
const InertiaPageLoaderPlugin = require('inertia-page-loader/webpack')

mix
  .webpackConfig({
    plugins: [
      InertiaPageLoaderPlugin(),
    ],
  })

Now you can use the page in your controller:

Inertia::render('my-runtime::Some');

Migrate from inertia-plugin

Update package in package.json:

 {
   "devDependencies": {
-    "inertia-plugin": "*"
+    "inertia-page-loader": "^0.7.0"
   }
 }

Rename vite plugin:

 // vite.config.js
-import Inertia from 'inertia-plugin/vite'
+import InertiaPageLoader from 'inertia-page-loader/vite'

 export default defineConfig({
   plugins: [
-    Inertia({ /* options */ }),
+    InertiaPageLoader({ /* options */ }),
   ],
 })

Rename webpack plugin:

 // webpack.config.js
-const InertiaPlugin = require('inertia-plugin/webpack')
+const InertiaPageLoaderPlugin = require('inertia-page-loader/webpack')

 module.exports = {
   /* ... */
   plugins: [
-    InertiaPlugin({ /* options */ }),
+    InertiaPageLoaderPlugin({ /* options */ }),
   ],
 }

Update CDN link if you used:

-<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/runtime.iife.js"></script>
+<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/runtime.iife.js" defer></script>

Configuration

InertiaPageLoader({
  // Current work directory.
  cwd: process.cwd(),

  // Define namespace mapping.
  namespaces: [],

  // Namespace separator.
  separator: '::',

  // Module extensions.
  extensions: '',
  // extensions: '',            // webpack default
  // extensions: 'vue',         // webpack example
  // extensions: 'vue',         // vite default
  // extensions: ['vue', 'js'], // vite example

  // Use `import()` to load pages for webpack, default is using `require()`.
  // Only for webpack.
  import: false,

  // Enable SSR mode.
  ssr: false,
})

Sponsor

If you think this package has helped you, please consider Becoming a sponsor to support my work~ and your avatar will be visible on my major projects.

Credits

License

MIT LICENSE