简体   繁体   English

main.js如何链接到Vue-cli webpack模板中的Index.html

[英]How does main.js link to Index.html in Vue-cli webpack template

I noticed vue-cli webpack project-name template loads these codes. 我注意到vue-cli webpack project-name模板加载了这些代码。

main.js main.js

...
new Vue({
    el: '#app', 
    render: h => h(App), 
});

index.html 的index.html

...
<head>
...
</head>
<body>
    <div id="app"></div>  <!-- if I change app to app1, error message states: "Cannot find element app" -->
    <script src="./dist/build.js"></script>
</body>
....

The two are linked together. 这两者是联系在一起的。 However, how are they linked? 但是,它们是如何联系起来的? It appears to be a result of build.js but I'm unable to understand the code as it has been compiled and minified, uglified etc... 它似乎是build.js的结果,但我无法理解代码,因为它已被编译和缩小,uglified等...

My webpack.config.js settings is default template. 我的webpack.config.js设置是默认模板。

you project used Webpack as a module bundler - it injects app.js into index.html 你使用Webpack作为模块捆绑器 - 它将app.js注入index.html

to get a non-uglified bundle, edit the Webpack settings like this: 要获取非uglified包,请编辑Webpack设置,如下所示:

comment call plugin uglifyjs-webpack-plugin in build/webpack.prod.conf.js build / webpack.prod.conf.js中注释调用插件uglifyjs-webpack-plugin

before 之前

...
plugins: [
// http://vuejs.github.io/vue-loader/en/workflow/production.html
new webpack.DefinePlugin({
  'process.env': env
}),
new UglifyJsPlugin({
  uglifyOptions: {
    compress: {
      warnings: false
    }
  },
  sourceMap: config.build.productionSourceMap,
  parallel: true
}),
// extract css into its own file
new ExtractTextPlugin({
  filename: utils.assetsPath('css/[name].[contenthash].css'),
  // set the following option to `true` if you want to extract CSS from
  // codesplit chunks into this main css file as well.
  // This will result in *all* of your app's CSS being loaded upfront.
  allChunks: false,
})
...

after

...
plugins: [
// http://vuejs.github.io/vue-loader/en/workflow/production.html
new webpack.DefinePlugin({
  'process.env': env
}),
// new UglifyJsPlugin({
//   uglifyOptions: {
//     compress: {
//       warnings: false
//     }
//   },
//   sourceMap: config.build.productionSourceMap,
//   parallel: true
// }),
// extract css into its own file
new ExtractTextPlugin({
  filename: utils.assetsPath('css/[name].[contenthash].css'),
  // set the following option to `true` if you want to extract CSS from
  // codesplit chunks into this main css file as well.
  // This will result in *all* of your app's CSS being loaded upfront.
  allChunks: false,
}),
...

also if you want to change the name of index.html to foo.html of the input file you can do it here: 如果你想将index.html的名称更改为输入文件的foo.html,你可以在这里执行:

line 68 in build/webpack.prod.conf.js change to build / webpack.prod.conf.js中的第68行更改为

template: 'foo.html',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM