From 03a1f4cf6984e5ae0edb57688c8a9dd1bf16c4aa Mon Sep 17 00:00:00 2001 From: Michael Dodge Date: Mon, 2 Apr 2018 18:08:44 -0600 Subject: [PATCH 1/8] First stab at SFC to npm documentation --- src/v2/cookbook/packaing-sfc-for-npm.md | 200 ++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 src/v2/cookbook/packaing-sfc-for-npm.md diff --git a/src/v2/cookbook/packaing-sfc-for-npm.md b/src/v2/cookbook/packaing-sfc-for-npm.md new file mode 100644 index 0000000000..154f8bf544 --- /dev/null +++ b/src/v2/cookbook/packaing-sfc-for-npm.md @@ -0,0 +1,200 @@ +--- +title: Packaging Vue Components for npm +type: cookbook +order: 10 +--- + +## Base Example + +Vue components by nature are meant to be re-used. This is easy when the component is only used within a single application. But how can you write a component once and use it in multiple sites/applications? Perhaps the easiest solution is via npm. + +By packaging your component to be shared via npm, it can be imported/required into a build process for use full-fledged web applications: + +```js +import MyComponent from 'my-component'; + +export default { + name: 'my-app', + components: { + MyComponent, + }, + data() { + ... + }, + ... +} +``` + +Or even used via ` + + ... + + ... +``` + +Not only does this help you avoid copy/pasting components around, but it also allows you to give back to the Vue community! + +## Why Shouldn't I Share .vue Files Directly? + +Vue already allows components to be written as a single file. Because a Single File Component (SFC) is already just one file, you might ask: + +> "Why can't people use my .vue file directly? Isn't that the simplest way to share components?" + +It's true, you could share .vue files directly, and as long as the [Vue build](https://vuejs.org/v2/guide/installation.html#Explanation-of-Different-Builds) being used contains the Vue compiler, that would be enough. But this excludes anyone who wishes to use the component directly in a browser via `