为什么要手写webpack 不用cli (脑子有病)并不是 其实是为了加深我们对webpack 的了解方便以后灵活运用webpack 的技术
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack-dev-server",
"build": "webpack"
},
npm add webpack
npm add webpack -li // 注意 安装 3以上的版本
npm add webpack-dev-server // 启动dev 的环境
npm add html-webpack-plugin // webpack html模板插件
const {
Configuration } = require('webpack') // 本来 没有智能提示的 不过可以通过注解的形式,就可以有智能提示了
const path = require('path') // 引入nodejs 的path
const htmlWebpackPlugin = require('html-webpack-plugin') // 引入 html 模板插件 配置模板
const {
CleanWebpackPlugin } = require('clean-webpack-plugin') // 清空dist文件使用的插件
const {
VueLoaderPlugin } = require('vue-loader/dist/index'); // 这个插件是 为了 webpack 能够去识别 template 组件的
const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin"); // 美化打包样式的
/**
* @type {Configuration}
*/
const config = {
// 本来这里是没有智能提示的
mode: "development", // 指定 打包模式 开发环境打包出来的代码是不会压缩的 如果需要压缩的 需要用生产模式 production
entry: "./src/main.ts", // 入口文件 注意 如果是TS写的 就是 main.ts
module: {
rules: [ // 放在 rules 中 都是处理文件的
{
test: /\.vue$/, //解析vue 模板
use: "vue-loader"
}
]
},
output: {
// 出口
filename: '[hash].js', // 打完包后的文件名
path: path.resolve(__dirname, 'dist') // 拼接一下 dirname 输出位置
},
resolve: {
alias: {
"@": path.resolve(__dirname, './src') // 别名
},
extensions: ['.js', '.json', '.vue', '.ts', '.tsx'] //识别后缀
},
stats:"errors-only", //取消提示
devServer: {
// 一些端口 跨域之类的配置
proxy: {
},
port: 9001,
hot: true,
open: true,
},
externals: {
// 性能优化的
vue: "Vue" //CDN 引入
},
plugins: [ // 放在 plugins 中 都是插件
new htmlWebpackPlugin({
template: "./public/index.html" // 指定一下模板的位置
}),
new CleanWebpackPlugin(), //打包清空dist
new VueLoaderPlugin(), //解析vue
new FriendlyErrorsWebpackPlugin({
compilationSuccessInfo:{
//美化样式
messages:['You application is running here http://localhost:8080']
}
})
]
}
module.exports = config
import {
createApp } from 'vue';
import App from './App.vue' // 如果 ts 无法识别 vue文件 需要去声明一下 在 src 下面 创建一个 env.d.ts 的文件
createApp(App).mount('#app') // 挂载点 由于是手写的 所以需要去 pubilc文件下的html文件的body中创建一个 div 给个 id 叫app 作为挂载点 到时候会渲染到这个地方
// src 下 env.d.ts 声明文件
declare module "*.vue" {
import {
DefineComponent } from "vue"
const component: DefineComponent<{
}, {
}, any>
export default component
}
npm add vue-loader@next
npm add @vue/compiler-sfc
resolve: {
alias: {
"@": path.resolve(__dirname, './src') // 别名
},
extensions: ['.js', '.json', '.vue', '.ts', '.tsx'] //识别后缀
},
npm add css-loader
npm add style-loader
{
test