目錄
專案準備
1.建立檔案
mkdir webpack-vue && cd webpack-vue
複製程式碼
2.初始化檔案
1.主要建立package.json
npm init
複製程式碼
3.建立業務目錄
4.建立配置檔案
1.webpack.config.js
touch webpack.config.js
複製程式碼
5.檔案配置
- entry:入口起點
- module:配置loader
- plugins:提供外掛
- output:輸出檔案
- resolve:配置模組如何解析
- devtool:以及如何生成 source map
- devServer:開啟服務
6.安裝依賴包
npm install --save-dev webpack-dev-server
npm i html-loader vue-loader node-sass style-loader css-loader sass-loader -D
npm i vue vue-router
npm i webpack webpack-cli -D
npm i html-webpack-plugin clean-webpack-plugin
npm i vue-template-compiler -D
npm i vue-style-loader -D
複製程式碼
專案總結:
1.webpack webpack-cli必須同時安裝
2.配置sass環境時,必須安裝node-sass
3.取消loaders配置
改成:
rules:[{
test:/\.html$/,
loader:'html-loader'
},...]
複製程式碼
4.vue-loader載入不上
解決方式:
const VueLoaderPlugin = require('vue-loader/lib/plugin');
複製程式碼
5.報這樣的錯誤資訊
vue.runtime.esm.js:620 [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 )
解決方法配置
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js' // 用 webpack 1 時需用 'vue/dist/vue.common.js'
}
},
複製程式碼
webpack.config.js配置檔案
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
module.exports = {
entry: {
app: './app/js/main.js',
},
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000
},
module: {
rules:[
{
test:/\.html$/,
loader:'html-loader'
},
{
test:/\.vue$/,
loader:'vue-loader'
},
{ test: /\.css$/,
use: [
"vue-style-loader",
"css-loader"
]
},
{
test: /\.scss$/,
use: [{
loader: "style-loader" // 將 JS 字串生成為 style 節點
}, {
loader: "css-loader" // 將 CSS 轉化成 CommonJS 模組
}, {
loader: "sass-loader" // 將 Sass 編譯成 CSS
}]
}
]
},
plugins:[
new CleanWebpackPlugin(),
new VueLoaderPlugin(),
new HtmlWebpackPlugin({
template: './app/views/index.html'
})
],
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js' // 用 webpack 1 時需用 'vue/dist/vue.common.js'
}
},
output:{
filename: '[name].min.js',
path: path.resolve(__dirname, 'dist')
}
}
複製程式碼