yarn init -y
加-y
可以跳过步骤,后期可以直接在package.json
里面修改
yarn add webpack webpack-dev-server webpack-merge webpack-cli
其中webpack-dev-server
和webpack-cli
启动本地服务,webpack-merge
用于合并配置文件,下面有用到
yarn add vue@next vue-loader@next @vue/compiler-sfc
加@next
安装最新版本,vue-loader
负责将.vue
文件转换为.html .js .css
等,而vue-loader
在版本16后就不再支持vue-template-compiler
,转而使用@vue/compiler-sfc
来解析template->render
yarn add css-loader style-loader less less-loader
css-loader
处理js
中通过import/require() @import / url
引入的css
文件,而style-loader
处理js
中的style标签的
样式
yarn add typescript ts-loader
ts
必须有个显式的配置文件,在根目录下创建tsconfig.json
// tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
// 这样就可以对 `this` 上的数据属性进行更严格的推断
"strict": true,
"jsx": "preserve",
"moduleResolution": "node"
},
"include": [
"src/**/*.ts",
"src/**/*.vue",
"src/**/*.js",
],
"exclude": [
"node_modules"
]
}
根目录下创建webpack.config.js
//webpack.config.js
const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const {
VueLoaderPlugin
} = require("vue-loader");
module.exports = {
entry: "./src/main.ts",
output: {
path: path.resolve(__dirname, "dist"),
filename: "js/[name].[hash:8].js"
},
// 启动服务
devServer: {
hot: true, // 热模块替换
port: "3000",
// open: true, // 启动后打开浏览器
},
module: {
rules: [{
test: /\.vue$/,
use: "vue-loader"
},
{
test: /\.(css|less)$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader"
}, {
loader: "less-loader",
options: {
lessOptions: {
javascriptEnabled: true // 允许链式调用
}
}
}]
},
{
test: /\.(t|j)s$/,
exclude: /node_modules/,
use: {
loader: "ts-loader",
options: {
configFile: path.resolve(__dirname, "tsconfig.json"), // 指定ts的配置文件
// 对应文件添加个.ts或.tsx后缀
appendTsSuffixTo: [/\.vue$/],
transpileOnly: true, // ? 关闭类型检查,即只进行转译
}
}
},
{
test: /\.(png|jpg|jepg|gif)$/,
use: {
loader: "url-loader",
options: {
limit: 5000,
name: "img/[name].[hash:8].[ext]"
}
}
}
],
},
resolve: {
// 别名
alias: {
"@": path.resolve(__dirname, "src"),
"@hook": path.resolve(__dirname, "src/hooks"),
},
extensions: [".js", ".ts", ".vue"]
},
plugins: [
// 将打包后的js加在index.html上
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "./public/index.html")
}),
// 加载vue相关的配置规则
new VueLoaderPlugin(),
// new webpack.NamedModulesPlugin(),
// new webpack.HotModuleReplacementPlugin(),
]
}
public
文件夹,再创建index.html
src
文件夹,创建app.vue
和main.ts
//app.vue
<template>
<h3>Hello world</h3>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
setup() {},
});
</script>
<style lang="less">
</style>
// main.ts
import {
createApp
} from "vue"
import App from "./app.vue"
const app = createApp(App);
app.mount("#app");
在main.ts
中会报错,不识别app.vue
,此时在根目录下创建文件shims-vue.d.ts
即可解决
// shims-vue.d.ts
declare module '*.vue' {
import { defineComponent } from 'vue'
const component: ReturnType<typeof defineComponent>
export default component
}
// package.json
{
//....
"scripts": {
"dev": "webpack-dev-server --config webpack.dev.js"
},
// ...
}
此处指定了配置文件从webpack.dev.js
读取,为了实现开发和生产环境的区别对待,新增webpack.dev.js
const {
merge
} = require("webpack-merge");
const webpack = require("webpack");
const webpackConfig = require("./webpack.config");
module.exports = merge(webpackConfig, {
mode: "development",
plugins: [
// 注入环境变量`PROCESS_ENV`,可在项目文件中直接使用
new webpack.DefinePlugin({
PROCESS_ENV: JSON.stringify("development")
})
]
})
yarn run dev
运行成功。
全部的依赖安装没有使用--save-dev
,这个可以在生产阶段对webpack
的优化中进行处理