uTools-Manuals/docs/vue/guide/deployment.html
2019-04-21 11:50:48 +08:00

110 lines
9.4 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="content guide with-sidebar deployment-guide">
<h1>生产环境部署</h1>
<blockquote>
<p>以下大多数内容在你使用 <a href="https://cli.vuejs.org/zh/" rel="noopener" target="_blank">Vue CLI</a> 时都是默认开启的。该章节仅跟你自定义的构建设置有关。</p>
</blockquote>
<h2 id="开启生产环境模式"><a class="headerlink" href="#开启生产环境模式" title="开启生产环境模式"></a>开启生产环境模式</h2><p>开发环境下Vue 会提供很多警告来帮你对付常见的错误与陷阱。而在生产环境下,这些警告语句却没有用,反而会增加应用的体积。此外,有些警告检查还有一些小的运行时开销,这在生产环境模式下是可以避免的。</p>
<h3 id="不使用构建工具"><a class="headerlink" href="#不使用构建工具" title="不使用构建工具"></a>不使用构建工具</h3><p>如果用 Vue 完整独立版本,即直接用 <code>&lt;script&gt;</code> 元素引入 Vue 而不提前进行构建,请记得在生产环境下使用压缩后的版本 (<code>vue.min.js</code>)。两种版本都可以在<a href="installation.html#直接用-lt-script-gt-引入">安装指导</a>中找到。</p>
<h3 id="使用构建工具"><a class="headerlink" href="#使用构建工具" title="使用构建工具"></a>使用构建工具</h3><p>当使用 webpack 或 Browserify 类似的构建工具时Vue 源码会根据 <code>process.env.NODE_ENV</code> 决定是否启用生产环境模式,默认情况为开发环境模式。在 webpack 与 Browserify 中都有方法来覆盖此变量,以启用 Vue 的生产环境模式,同时在构建过程中警告语句也会被压缩工具去除。所有这些在 <code>vue-cli</code> 模板中都预先配置好了,但了解一下怎样配置会更好。</p>
<h4 id="webpack"><a class="headerlink" href="#webpack" title="webpack"></a>webpack</h4><p>在 webpack 4+ 中,你可以使用 <code>mode</code> 选项:</p>
<pre><code class="hljs js"><span class="hljs-built_in">module</span>.exports = {
<span class="hljs-attr">mode</span>: <span class="hljs-string">'production'</span>
}</code></pre>
<p>但是在 webpack 3 及其更低版本中,你需要使用 <a href="https://webpack.js.org/plugins/define-plugin/" rel="noopener" target="_blank">DefinePlugin</a></p>
<pre><code class="hljs js"><span class="hljs-keyword">var</span> webpack = <span class="hljs-built_in">require</span>(<span class="hljs-string">'webpack'</span>)
<span class="hljs-built_in">module</span>.exports = {
<span class="hljs-comment">// ...</span>
plugins: [
<span class="hljs-comment">// ...</span>
<span class="hljs-keyword">new</span> webpack.DefinePlugin({
<span class="hljs-string">'process.env.NODE_ENV'</span>: <span class="hljs-built_in">JSON</span>.stringify(<span class="hljs-string">'production'</span>)
})
]
}</code></pre>
<h4 id="Browserify"><a class="headerlink" href="#Browserify" title="Browserify"></a>Browserify</h4><ul>
<li>在运行打包命令时将 <code>NODE_ENV</code> 设置为 <code>"production"</code>。这等于告诉 <code>vueify</code> 避免引入热重载和开发相关的代码。</li>
<li>对打包后的文件进行一次全局的 <a href="https://github.com/hughsk/envify" rel="noopener" target="_blank">envify</a> 转换。这使得压缩工具能清除掉 Vue 源码中所有用环境变量条件包裹起来的警告语句。例如:</li>
</ul>
<pre><code class="hljs bash">NODE_ENV=production browserify -g envify -e main.js | uglifyjs -c -m &gt; build.js</code></pre>
<ul>
<li><p>或者在 Gulp 中使用 <a href="https://github.com/hughsk/envify" rel="noopener" target="_blank">envify</a></p>
<pre><code class="hljs js"><span class="hljs-comment">// 使用 envify 的自定义模块来定制环境变量</span>
<span class="hljs-keyword">var</span> envify = <span class="hljs-built_in">require</span>(<span class="hljs-string">'envify/custom'</span>)
browserify(browserifyOptions)
.transform(vueify)
.transform(
<span class="hljs-comment">// 必填项,以处理 node_modules 里的文件</span>
{ <span class="hljs-attr">global</span>: <span class="hljs-literal">true</span> },
envify({ <span class="hljs-attr">NODE_ENV</span>: <span class="hljs-string">'production'</span> })
)
.bundle()</code></pre>
</li>
<li><p>或者配合 Grunt 和 <a href="https://github.com/jmreidy/grunt-browserify" rel="noopener" target="_blank">grunt-browserify</a> 使用 <a href="https://github.com/hughsk/envify" rel="noopener" target="_blank">envify</a></p>
<pre><code class="hljs js"><span class="hljs-comment">// 使用 envify 自定义模块指定环境变量</span>
<span class="hljs-keyword">var</span> envify = <span class="hljs-built_in">require</span>(<span class="hljs-string">'envify/custom'</span>)
browserify: {
<span class="hljs-attr">dist</span>: {
<span class="hljs-attr">options</span>: {
       <span class="hljs-comment">// 该函数用来调整 grunt-browserify 的默认指令</span>
       configure: <span class="hljs-function"><span class="hljs-params">b</span> =&gt;</span> b
.transform(<span class="hljs-string">'vueify'</span>)
.transform(
           <span class="hljs-comment">// 用来处理 `node_modules` 文件</span>
{ <span class="hljs-attr">global</span>: <span class="hljs-literal">true</span> },
envify({ <span class="hljs-attr">NODE_ENV</span>: <span class="hljs-string">'production'</span> })
)
.bundle()
}
}
}</code></pre>
</li>
</ul>
<h4 id="Rollup"><a class="headerlink" href="#Rollup" title="Rollup"></a>Rollup</h4><p>使用 <a href="https://github.com/rollup/rollup-plugin-replace" rel="noopener" target="_blank">rollup-plugin-replace</a></p>
<pre><code class="hljs js"><span class="hljs-keyword">const</span> replace = <span class="hljs-built_in">require</span>(<span class="hljs-string">'rollup-plugin-replace'</span>)
rollup({
<span class="hljs-comment">// ...</span>
plugins: [
replace({
<span class="hljs-string">'process.env.NODE_ENV'</span>: <span class="hljs-built_in">JSON</span>.stringify( <span class="hljs-string">'production'</span> )
})
]
}).then(...)</code></pre>
<h2 id="模板预编译"><a class="headerlink" href="#模板预编译" title="模板预编译"></a>模板预编译</h2><p>当使用 DOM 内模板或 JavaScript 内的字符串模板时,模板会在运行时被编译为渲染函数。通常情况下这个过程已经足够快了,但对性能敏感的应用还是最好避免这种用法。</p>
<p>预编译模板最简单的方式就是使用<a href="./single-file-components.html">单文件组件</a>——相关的构建设置会自动把预编译处理好,所以构建好的代码已经包含了编译出来的渲染函数而不是原始的模板字符串。</p>
<p>如果你使用 webpack并且喜欢分离 JavaScript 和模板文件,你可以使用 <a href="https://github.com/ktsn/vue-template-loader" rel="noopener" target="_blank">vue-template-loader</a>,它也可以在构建过程中把模板文件转换成为 JavaScript 渲染函数。</p>
<h2 id="提取组件的-CSS"><a class="headerlink" href="#提取组件的-CSS" title="提取组件的 CSS"></a>提取组件的 CSS</h2><p>当使用单文件组件时,组件内的 CSS 会以 <code>&lt;style&gt;</code> 标签的方式通过 JavaScript 动态注入。这有一些小小的运行时开销,如果你使用服务端渲染,这会导致一段“无样式内容闪烁 (fouc)”。将所有组件的 CSS 提取到同一个文件可以避免这个问题,也会让 CSS 更好地进行压缩和缓存。</p>
<p>查阅这个构建工具各自的文档来了解更多:</p>
<ul>
<li><a href="https://vue-loader.vuejs.org/zh-cn/configurations/extract-css.html" rel="noopener" target="_blank">webpack + vue-loader</a> (<code>vue-cli</code> 的 webpack 模板已经预先配置好)</li>
<li><a href="https://github.com/vuejs/vueify#css-extraction" rel="noopener" target="_blank">Browserify + vueify</a></li>
<li><a href="https://vuejs.github.io/rollup-plugin-vue/#/en/2.3/?id=custom-handler" rel="noopener" target="_blank">Rollup + rollup-plugin-vue</a></li>
</ul>
<h2 id="跟踪运行时错误"><a class="headerlink" href="#跟踪运行时错误" title="跟踪运行时错误"></a>跟踪运行时错误</h2><p>如果在组件渲染时出现运行错误,错误将会被传递至全局 <code>Vue.config.errorHandler</code> 配置函数 (如果已设置)。利用这个钩子函数来配合错误跟踪服务是个不错的主意。比如 <a href="https://sentry.io" rel="noopener" target="_blank">Sentry</a>,它为 Vue 提供了<a href="https://sentry.io/for/vue/" rel="noopener" target="_blank">官方集成</a></p>
<div class="guide-links">
<span><a href="typescript.html">TypeScript 支持</a></span>
<span style="float: right;"><a href="routing.html">路由</a></span>
</div>
<div class="footer">
<script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
<div class="bsa-cpc"></div>
<script>
(function(){
if(typeof _bsa !== 'undefined' && _bsa) {
_bsa.init('default', 'CKYD62QM', 'placement:vuejsorg', {
target: '.bsa-cpc',
align: 'horizontal',
disable_css: 'true'
});
}
})();
</script>
发现错误?想参与编辑?
<a href="https://github.com/vuejs/cn.vuejs.org/blob/master/srcdeployment.md" target="_blank">
在 GitHub 上编辑此页!
</a>
</div>
</div>