Nuxt.js: import my own library

Created on 6 Aug 2017  路  10Comments  路  Source: nuxt/nuxt.js

Hi,
I have some js files with functions. I would like to import using build.vendor. How would I do it?
Cheers?

This question is available on Nuxt.js community (#c1153)
help-wanted

Most helpful comment

A simplified example:

With build:{ vendor:[ 'axios' ] }

dist/
    nuxt.bundle.hash.js # your app code
    vendor.bundle.hash.js # "axios" bundled in here
    ...other files

Without

dist/
    nuxt.bundle.hash.js # "axios" bundled with app code
    ...other files

The build.vendor option is meant for splitting vendor code to another bundle in order to reduce the size of the app bundle.

All 10 comments

in nuxt.config.js, you can add config in build like:
build:{ vendor:[ '~/plugins/a.js' ] }

That what I did, but the variable is not available, like
build:{ vendor:[ 'axios' ] }

Inside of my page

mounted() {
            console.log(axios);
        },

Got error
ReferenceError: axios is not defined

Do I still need to import axios in the component?

Do I still need to import axios in the component?

Sure you need:

import axios from '~/plugins/axios'

So what is the benefit to insert in build.vendor? Didn't get it!

to reduce the size of the app bundle
https://nuxtjs.org/api/configuration-build#vendor

Well, it would be the same if I just import axioes normally as usual? Sorry, got confused with this.
Do you know any article that explains it more?

A simplified example:

With build:{ vendor:[ 'axios' ] }

dist/
    nuxt.bundle.hash.js # your app code
    vendor.bundle.hash.js # "axios" bundled in here
    ...other files

Without

dist/
    nuxt.bundle.hash.js # "axios" bundled with app code
    ...other files

The build.vendor option is meant for splitting vendor code to another bundle in order to reduce the size of the app bundle.

Also, it's useful when you deploy new version of your app, vendor.bundle will be the same (if you don't add new libs) and user don't need to download vendor.bundle again. So only small nuxt.bundle will be downloaded.

Now makes sense! Thank you very much everyone!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vadimsg picture vadimsg  路  3Comments

pehbehbeh picture pehbehbeh  路  3Comments

surmon-china picture surmon-china  路  3Comments

vadimsg picture vadimsg  路  3Comments

danieloprado picture danieloprado  路  3Comments