webpack初始化
  YQBUvtbFE7rq 2023年12月08日 38 0

1.项⽬目初始化: 详⻅见⼿手动初始化案例例

2.导⼊入相关插件:

(1). 引⼊入vue:

$ npm install vue --save ```
package.json⽂文件中增加依赖: ```
"dependencies": {
"vue": "^2.6.6" }

修改src/main.js⽂文件: ``` import Vue from 'vue' var vm = new Vue({ el:'#app', data: { msg:'hello vue' } })

在index.html⽂文件中的body元素增加如下: ```
<div id="app">{{ msg }}</div>

(2). 引⼊入babel: 使⽤用了了es6的语法,但是现在很多浏览器器对es6的⽀支持不不是很好,所以在编译时需要将这些语法转换es5的语法,可以使⽤用babel来进⾏行行编译.

$ npm i babel-core babel-loader --save ```
在webpack.config.js的rules属性下添加: ```
{
test: /\.js$/, # ⽤用正则匹配⽂文件,⽤用require或者import引⼊入的都会匹配到
} ```
loader: "babel-loader", exclude: /node_modules/
# 加载器器名
# 排除node_modules⽬目录(不不加载node模块中的js)
webpack编译打包:

$ ./node_modules/.bin/webpack --mode="development" ``` 报错:

Cannot find module '@babel/core'
babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'. ```
解决⽅方案:
1. 版本问题: package.json修改babel-loader版本为7.1.5: ```
{
"devDependencies": { "babel-core": "^6.26.3", "babel-loader": "^7.1.5"
} }

原有版本为^8.0.5

  1. 安装@babel/core依赖:
$ npm install --save-dev @babel/core ```
webpack编译完成后⽤用浏览器器打开index.html⽂文件报错: ```
[Vue warn]: Cannot find element: #app

 [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
(found in <Root>) ```
原因是使⽤用了了vue的运⾏行行时的版本,⽽而此版本在编译时不不可使⽤用的,需要切换成(运⾏行行时+编译的)版本.
package.json:

⽂文件解析resolve resolve: {

alias: { 'vue$': 'vue/dist/vue.esm.js' } }

vue$的作⽤用其实就是为了了解决⽂文件路路径过⻓长,⽅方便便引⽤用.

(3). 引⼊入css加载器器: 引⼊入css-loader(加载器器)和style-loader(转换器器).
style-loader⽬目的是把css转成js,再在html中以style的⽅方式嵌⼊入css.

npm install --D css-loader style-loader webpack.config.js的rules中增加如下配置: { test: /\.css$/, loader: 'style-loader!css-loader'

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月08日 0

暂无评论

推荐阅读
  JZjRRktyDDvK   19天前   37   0   0 Vue
  onf2Mh1AWJAW   3天前   12   0   0 Vue
YQBUvtbFE7rq