项目作者: pimlie

项目描述 :
Publish your Nuxt.js SPA as a vue-custom-element
高级语言: JavaScript
项目地址: git://github.com/pimlie/nuxt-custom-element.git
创建时间: 2019-10-10T17:45:57Z
项目社区:https://github.com/pimlie/nuxt-custom-element

开源协议:MIT License

下载


Nuxt Custom Element

npm version
npm downloads
Circle CI
Codecov

Publish your Nuxt SPA as a vue-custom-element

nuxt-custom-element is a Nuxt.js module that uses vue-custom-element to publish your Nuxt SPA as a custom element / web-component.

Example

See the example folder for an example project.

To run the example locally, clone this repo, run yarn install or npm i and then yarn example or npm run example

Installation

Install the module package

  1. $ yarn add nuxt-custom-element
  2. $ npm i nuxt-custom-element

Next add the nuxt-custom-element module to your config:

  1. // nuxt.config.js
  2. mode: 'spa', // this module only works in SPA mode!
  3. modules: [
  4. ['nuxt-custom-element', { name: 'my-nuxt-spa' }]
  5. ]

Options

name required

string (default: customElement) normally in dev mode, dont

The global name of your custom element. This should be a valid javascript identifier as its used as the globalName of your custom element

Make sure to choose a unique name, if two Nuxt app’s have the same globalName that will result in errors

elementName

string (default: same as name)

Optional, the name of your custom element. If you want your custom element to have a different name then the Nuxt globalName property

  1. // module options
  2. { name: 'nce', elementName: 'nuxt-custom-element' }
  3. // results in
  4. window.$nce
  5. <nuxt-custom-element></nuxt-custom-element>

props

array (default: [])

Optional, the props that your custom element supports.

  1. // module options
  2. { props: ['path'] }
  3. <custom-element path="/about"></custom-element>
  4. // pages/index.vue
  5. mounted() {
  6. console.log('Path prop has value', this.$root.path)
  7. }

How to develop

Just run nuxt dev, this module adds a static index.html which already has your custom component listed

How to build

Run nuxt build in your project and check the ./dist folder. The dist folder should contain 3 files, a js file, a css file and an index.html

Bundle size

Currently the minimum bundle size seems to be ~180KB. This includes the basic Nuxt.js app and all polyfills to run your custom element in IE9+

The minimum bundle size was reached by disabling most Nuxt.js features

Caveats

  • See vue-custom-element caveats for general remarks
  • The Nuxt app is not the root component. Instead, it is the first child of the root component. Eg in a default SPA project the Nuxt app has _uid: 0, but when using nuxt-custom-element it has _uid: 1
  • It’s advised to be very careful with using head. Using head will probably effect the parent page as well, not just your custom element

TODO

  • add support to run Nuxt.js as a normal SPA in dev mode (and not always has a custom element)
  • randomize globalName?