question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic import/require in resolveComponent/resolve function using modules?

See original GitHub issue

Versions:

  • @inertiajs/inertia version: 0.10.1
  • @inertiajs/inertia-react version: 0.7.1

Describe the problem:

Hello everyone. I use InertiaJS with Laravel. Also I use nWidart/laravel-modules. I want to render JS files from my modules dynamically. They are in Modules/Module-name/Resources/assets/js. This is my controller

    public function showLoginForm(): Response
    {
        return Inertia::render('AdminPanel::Login');
    }

My webpack.mix

mix.js('resources/js/app.js', 'public/js')
    .react()
    .sass('resources/sass/app.scss', 'public/css')
    .webpackConfig({
        output: {chunkFilename: 'js/[name].js?id=[chunkhash]'},
        resolve: {
            alias: {
                '@': path.resolve(__dirname, 'resources/js'),
                '~': path.resolve(__dirname, 'Modules'),
            }
        },
    })

mix.version()

This is my app.js

const app = document.getElementById('app');

render(
    <InertiaApp
        initialPage={JSON.parse(app.dataset.page)}
        resolveComponent={async name => {
            let parts = name.split('::')
            if(parts.length > 1) {
                return import(`~/${parts[0]}/Resources/assets/js/Pages/${parts[1]}`).then(module => module.default)
            }else {
                return import('./src/Pages/' + name).then(module => module.default)
            }
        }}
    />,
    app
);

In this case I go many webpack errors like:

ERROR in ./node_modules/enhanced-resolve/lib/MainFieldPlugin.js 8:13-28
Module not found: Error: Can't resolve 'path' in '/Volumes/Storage/Shared/pump-manager/node_modules/enhanced-resolve/lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
        - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "path": false }

I also tried with CreateInertiaApp and required functions - the same result

But if I use static path to module files like: ~/AdminPanel/Resources/assets/js/${parts[0]} then there are no errors and everything works fine. But that means I should describe all my modules and static paths to their js files in app.js. I would want to escape this.

I also checked this issue and this one but unsuccessfully… Someone has the same errors, someone has it works fine

Thanks

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
maxonfjviponcommented, Nov 28, 2021

Just in case, if somebody does not want to load a single chunk for every single page file. You may do: Module structure: image app.js in your module image Every page file should be exported as default function: image main app.js (in resources/js/) image

So, now every js chunk will contain all js files for your module

1reaction
maxonfjviponcommented, Nov 15, 2021

@rafalkrawiec @interviadmin I bow before you. It finally works. Thank you so much. Now it looks like this: image

I did not realize before that first argument in require.context() function and path in context are not the same! @rafalkrawiec - you were right about the map. So we’re done here. Thanks again. ❤️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Do require.resolve for ES modules - javascript - Stack Overflow
As long as import.meta.resolve stays experimental, you can use createRequire to get back the require.resolve functionality in ES modules, ...
Read more >
How to Dynamically Import ECMAScript Modules
You can import modules dynamically if you use import as a function — import(pathToModule) — a feature available starting ES2020.
Read more >
Dynamic imports - The Modern JavaScript Tutorial
The import(module) expression loads the module and returns a promise that resolves into a module object that contains all its exports. It can...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Provides a module-relative resolution function scoped to each module, returning the URL string. const dependencyAsset = await import.meta.resolve('component-lib ...
Read more >
Dynamic component loader - Angular
An application might need to load new components at runtime. ... This makes it impractical to use a template with a static component...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found