8000 First stab at SFC to npm documentation by mgdodge · Pull Request #1558 · vuejs/v2.vuejs.org · GitHub
[go: up one dir, main page]

Skip to content

First stab at SFC to npm documentation #1558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 25, 2018
Prev Previous commit
Next Next commit
Fix order, other edits suggested by @sdras
  • Loading branch information
Michael Dodge committed May 21, 2018
commit d90dd1683fd550332d5f64639c2cdf81903cc4f4
12 changes: 7 additions & 5 deletions src/v2/cookbook/packaing-sfc-for-npm.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Packaging Vue Components for npm
type: cookbook
order: 10
order: 12
---

## Base Example
Expand Down Expand Up @@ -79,19 +79,19 @@ When webpack 2+, Rollup, or other modern build tools are used, they will pick up

### SSR Usage

You might have noticed something interesting - browsers aren't going to be using the `browser` version! That's because this field is actually intended to allow authors to provide [hints to bundlers](https://github.com/defunctzombie/package-browser-field-spec#spec) which in turn create their own packages for client side use. With a little creativity, this field allows us to map an alias to the `.vue` file itself. For example:
You might have noticed something interesting - browsers aren't going to be using the `browser` version. That's because this field is actually intended to allow authors to provide [hints to bundlers](https://github.com/defunctzombie/package-browser-field-spec#spec) which in turn create their own packages for client side use. With a little creativity, this field allows us to map an alias to the `.vue` file itself. For example:

```js
import MyComponent from 'my-component/sfc'; // Note the '/sfc'
```

Compatible bundlers see the `browser` definition in package.json and translate requests for `my-component/sfc` into `my-component/src/my-component.vue`, resulting in the original `.vue` file being used instead. Now the SSR process can use the string concatenation optimizations it needs to for a boost in performance.

> Note: When using `.vue` components directly, pay attention to any type of pre-processing required by `script` and `style` tags. These dependencies will be passed on to users. Consider providing 'plain' SFCs to keep things as light as possible.
<p class="tip">Note: When using `.vue` components directly, pay attention to any type of pre-processing required by `script` and `style` tags. These dependencies will be passed on to users. Consider providing 'plain' SFCs to keep things as light as possible.</p>

### How do I make multiple versions of my component?

There is no need to write your module multiple times. It is possible to prepare all 3 versions of your module in one step, in a matter of seconds. The example here uses [Rollup](https://rollupjs.org) due to its minimal configuration, but similar configuration is possible with other build tools. The package.json `scripts` section can be updated with a single entry for each build target, and a more generic `build` script that runs them all in one pass. The sample package.json file now looks like this:
There is no need to write your module multiple times. It is possible to prepare all 3 versions of your module in one step, in a matter of seconds. The example here uses [Rollup](https://rollupjs.org) due to its minimal configuration, but similar configuration is possible with other build tools - more details on this decision can be found [here](https://medium.com/webpack/webpack-and-rollup-the-same-but-different-a41ad427058c). The package.json `scripts` section can be updated with a single entry for each build target, and a more generic `build` script that runs them all in one pass. The sample package.json file now looks like this:

```json
{
Expand Down Expand Up @@ -121,7 +121,9 @@ There is no need to write your module multiple times. It is possible to prepare
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I find lacking here reading through is how you created this base package.json. Did you use a generator? Are you going off of another file? How are you deciding what versions should be used for each? Are you manually looking at the current version of LTS in terminal? These are some things that might deserve a quick couple of sentences to clarify to be most helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding another paragraph explaining that this package.json illustrates a starting point, and that versions can/should be kept up to date as necessary.


That is all that is required in package.json to get up and running. Now, all that is needed is a small wrapper to export/auto-install the actual SFC, plus a mimimal Rollup configuration, and we're set!
<p class="tip">If you have an existing package.json file, it will likely contain a lot more than this one does. The file used in this recipe merely illustrates a starting point. Also, the <i>packages</i> listed in devDependencies (not their versions) are the minimum requirements for rollup to create the three separate builds (umd, es, and unpkg) mentioned. As newer versions become available, they should be updated as necessary.</p>

Our changes to package.json are complete. Next, we need a small wrapper to export/auto-install the actual SFC, plus a mimimal Rollup configuration, and we're set!

### What does my packaged component look like?

Expand Down
0