webpack创建Vue3

webpack创建

创建相关文件目录

 npm init -y    
 tsc --init      

在这里插入图片描述

下载webpack和webpack-cli

创建webpack.config.js必须为common js写法

npm add webpack  
npm add webpack-cli 
//添加webpack启动器
npm add webpack-dev-server    

添加html-webpack-plugin

 npm add html-webpack-plugin  

安装所需的依赖

{
  "name": "webpackcreatevue3",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "dev": "webpack-dev-server"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@vue/compiler-sfc": "^3.2.47",
    "css-loader": "^6.7.3",
    "friendly-errors-webpack-plugin": "^1.7.0",
    "html-webpack-plugin": "^5.5.0",
    "pnpm": "^7.29.3",
    "style-loader": "^3.3.2",
    "ts": "^0.2.2",
    "typescript": "^5.0.2",
    "vue": "^3.2.47",
    "vue-loader": "^17.0.1",
    "webpack": "^5.76.2",
    "webpack-cli": "^5.0.1",
    "webpack-dev-server": "^4.13.1"
  }
}

配置vue声明文件,否则无法识别vue

创建

declare module "*.vue" {
    import { DefineComponent } from "vue"
    const component: DefineComponent<{}, {}, any>
    export default component
  }

webpack.config.js

const {Configuration} = require ('webpack')
const  path = require("path")
const htmlwebpackPlugin = require('html-webpack-plugin')
const FriendlyErrorWebpackPlugin = require('friendly-errors-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader/dist/index');
//注解方式加提示
/**
 * @type {Configuration}
 */
const config = {
    mode:"development",
    entry: "./src/main.ts",
    module: {
        //文件规则
        rules: [
            {
            //使用vue-loader解析.vue文件
            test:/\.vue$/,
            use: "vue-loader"
            },
            {
                test: /\.css$/, //解析css
                use: ["style-loader", "css-loader"],
            },
            {
                test: /\.ts$/,  //解析ts
                loader: "ts-loader",
                options: {
                    configFile: path.resolve(process.cwd(), 'tsconfig.json'),
                    appendTsSuffixTo: [/\.vue$/]
                },
            }]
    },
    //打包后文文件规则
    output: {
        filename: "[hash].js",
        path: path.resolve(__dirname,'dist'),
        clean:true
    },
    //配置补全文件,别名
    resolve: {
        alias: {
            '@':path.resolve(__dirname,'src')
        },
        extensions: ['.vue','.ts','.js']
    },
    //插件分装
    plugins: [
        new htmlwebpackPlugin({
            template: "./public/index.html"
        }),
        new VueLoaderPlugin(),
        new FriendlyErrorWebpackPlugin({
            compilationSuccessInfo:{ //美化样式
                messages:['You application is running here http://localhost:9001']
            }
        })
    ],
    devServer: {
        port:9001
    },
    //性能优化,打包后大小更小
    externals: {
        vue:"vue"
    }
}

module.exports = config
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值