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.

Support for external vue

See original GitHub issue

Is your feature request related to a problem? Please describe. I’m using vue.global.js in rails application through unpkg link and having vue inside ViteJs application. I would like to tell Vite to do not import vue from modules but make a reference to window.Vue. (use the external one)

Describe the solution you’d like It would be awesome to have code what allows us to modify modules resolution. eg.: vite.config.ts

alias: {
  "vue": "window.Vue"
}

or any better alternative what allows us to use one global Vue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
sionzeecommented, Jul 15, 2020

@yyx990803 Passing an external to rollupInputOptions won’t solve development mode. It still complains about presence of two Vue applications. Is there a way how to tell Vite to search Vue from Vue.global.js? Your code solves only bundling process.

1reaction
elwynelwyncommented, Apr 15, 2021

I had the same issue for React where the existing page had a global copy of React loaded, which I needed to use in my new ViteJS bundled component. Rollup externals worked perfectly for the production build, but in development Vite was sending a second instance of React, which results in errors from React:

Error: Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.

This plugin looked like it should work around this issue: https://github.com/lceric/vite-plugin-resolve-externals

However unfortunately I had issues with the plugin (perhaps due to running on Windows? or just PEBKAC?) so ended up using a modified version of the plugin:

function resolveExternalsPlugin(
    externals: Record<string, string> = {}
) {
    const extractId = (id: string): string => {
        // match blah/node_modules/.vite/foo.js?1234
        const match = id.match(/^.*\/node_modules\/\.vite\/(.+)\.js.*$/i);
        if (match && match.length === 2) {
            return match[1];
        }

        return id;
    };

    return {
        name: "vite-plugin-resolve-externals",
        config(config: UserConfig) {
            const { resolve } = config;
            Object.assign(externals, resolve.externals);
            return config;
        },
        resolveId(path: string) {
            const id = extractId(path);
            if (externals[id]) {
                return id;
            }
        },
        load(path: string) {
            const id = extractId(path);
            if (externals[id]) {
                return `const externals = window.${externals[id]};
    export default externals`;
            }
        },
    };
}

And in my vite.config.ts:

plugins: [
    resolveExternalsPlugin({
        react: 'React',
        'react-dom': 'ReactDOM'
    })
]

Hopefully that helps if others run into this situation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Building external modules in VueJS - Pusher Blog
To build our external Vue module, we need to make sure it is one that can be reused across multiple projects and not...
Read more >
How to Make Secure HTTP Requests with Vue and Express
TL;DR: In this article, you're going to build an Express API and then make API calls to it from an existing Vue.js application....
Read more >
Add External Scripts - Gridsome
It is really easy to use any external or third-party JavaScript with Gridsome. Since Gridsome is built on Vue, any method of importing...
Read more >
Composables - Vue.js
In the context of Vue applications, a "composable" is a function that leverages Vue's Composition API to encapsulate and reuse stateful logic. When...
Read more >
VueJs vue-router linking an external website - Stack Overflow
For external links, just use <a href="..."> to point directly to whatever you're linking to. vue-router is only needed for internal links. – ......
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