001.最簡單的vue腳手架配置

1.新建空白項目目錄
mkdir simple-template
cd simple-template
2.初始化項目
npm init -y
3.安裝webpack
npm i webpack webpack-cli -D
image.png
4.新建src文件夾,并在其下新建main.js文件,如下
image.png
5.在根目錄下新建index.html文件,內(nèi)容如下
image.png
6.新建webpack.config.js配置文件
image.png
7.終端輸入npx webpack命令進行打包
image.png
8.打包成功,在瀏覽器打開index.html文件,如下
image.png
9.在src目錄下新建style.css樣式文件,如下
image.png
10.在main.js文件中引入style.css樣式文件,如下
image.png
11.再次打包,終端執(zhí)行npx webpack,報錯如下
  • 解析./src/stye.css錯誤,需要一個loader來解析
    image.png
12.安裝style-loader && css-loader來處理css文件,并配置webpack.config.js文件
npm i -D style-loader css-loader
image.png
13.再次打包,成功
image.png
14.瀏覽器打開index.html文件,如下,樣式加上了
image.png
15.在package.json自定義打包命令npm run build,替代npx webpack,如下
image.png
16.修改main.js,如下,并在src目錄下新建App.vue文件
image.png
// main.js內(nèi)容如下
import Vue from 'vue'
import App from './App.vue'

new Vue({
    el: '#app',
    render: h=>h(App),
})
// App.vue內(nèi)容如下
<template>
    <div>我是App.vue</div>
</template>

<script>
export default {

}
</script>

<style>

</style>
17.使用npm run build進行打包,報錯如下,需要一個loader來處理vue類型的文件
image.png
18.安裝vue-loadervue-template-compiler,并配置webpack.config.js文件

文檔地址:https://vue-loader.vuejs.org/zh/guide/#%E6%89%8B%E5%8A%A8%E8%AE%BE%E7%BD%AE

image.png

npm i vue-loader vue-template-compiler -D
// webpack.config.js內(nèi)容修改如下
const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = {
    entry: './src/main.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'build.js',
    },

    module: {
        rules: [{
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            }
        ]
    },
    plugins: [
        // 請確保引入這個插件!
        new VueLoaderPlugin()
    ]
}
19.執(zhí)行npm run build,報錯如下,是因為還沒有安裝vue
npm i vue -S
image.png
20.再進行打包,可成功并可在瀏覽器查看index.html內(nèi)容,
image.png
21.繼續(xù)
21.繼續(xù)
21.繼續(xù)
21.繼續(xù)
21.繼續(xù)
21.繼續(xù)
最終,有下面這2個文件就行了
// package.json
{
  "name": "simple-template",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server --open",
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "css-loader": "^5.0.1",
    "html-webpack-plugin": "^4.5.0",
    "node-sass": "^5.0.0",
    "sass-loader": "^10.1.0",
    "style-loader": "^2.0.0",
    "vue-loader": "^15.9.5",
    "vue-template-compiler": "^2.6.12",
    "webpack": "^5.6.0",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  },
  "dependencies": {
    "vue": "^2.6.12"
  }
}

// webpack.config.js
const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/main.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'build.js',
    },
    devServer: {
        contentBase: './dist'
    },
    module: {
        rules: [{
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
            },
            {
                test: /\.scss$/,
                use: ['style-loader', 'css-loader', 'sass-loader'],
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            }
        ]
    },
    plugins: [
        // 請確保引入這個插件!
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            title: '自定義標題',
            template: 'public/index.html'
        })
    ]

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容