node环境打包js,webpack和rollup两个打包工具打包,能支持vue

引言

项目中经常用到共用的js,这里就需要用到共用js打包,这篇文章讲解两种打包方式,webpack打包和rollup打包两种方式

1、webpack打包js

1.1 在根目录创建 webpack.config.js,配置如下

bash 复制代码
const path = require('path');
module.exports = {
 entry: './index.js', 
 output:{
  filename:'index.js',
  path:path.resolve(__dirname,'dist'),
  library: 'myModule',
  libraryTarget:'umd'
 },
 // 以下代码为新添加代码
 module:{
  rules:[
   {
    test: /\.js$/, // 匹配所有 js 文件
    loader: 'babel-loader' // 使用 babel-loader 处理 js 文件
   },
  ]
 },
};

1.2 安装依赖包 package.json

npm install @babel/core @babel/preset-env babel-loader @babel/plugin-transform-runtime 这里用来将es6转成es5

npm install webpack webpack-cli

bash 复制代码
{
  "main": "index.js",
  "scripts": {
    "buildwebpack": "webpack"
  },
  "devDependencies": {
    "@babel/core": "^7.23.9",
    "@babel/preset-env": "^7.23.9",
    "babel-loader": "^9.1.3",
    "webpack": "^5.90.0",
    "webpack-cli": "^5.1.4"
  },
  "dependencies": {
    "@babel/plugin-transform-runtime": "^7.23.9"
  }
}

1.3 配置 babel.config.js

bash 复制代码
const presets = [
    [
        "@babel/env",
        {
            targets: {
                ie: "6",
                edge: "17",
                firefox: "60",
                chrome: "67",
                safari: "11.1"
            },
            //useBuiltIns: "usage",
            //corejs: "3", // <---  此处加个这个,就没有报错警告了

        },
    ],
];

module.exports = { presets,
    "plugins": ["@babel/transform-runtime"]
};

1.4 webpack 支持vue

"vue-loader": "^15.11.1",

"vue-template-compiler": "^2.6.12" //需要和vue版本一致

npm install vue-loader@15 vue-template-compiler@2 vue@2 css-loader

bash 复制代码
const path = require('path');
const {VueLoaderPlugin} = require('vue-loader')
module.exports = {
 entry: './index.js', 
 output:{
  filename:'index.js',
  path:path.resolve(__dirname,'dist'),
  library: 'myModule',
  libraryTarget:'umd'
 },
 // 以下代码为新添加代码
 module:{
  rules:[
   {
    test: /\.js$/, // 匹配所有 js 文件
    loader: 'babel-loader' // 使用 babel-loader 处理 js 文件
   },
   {
    test: /\.vue$/, 
    loader: 'vue-loader'
   },
//    { test: /\.css$/, 
//      use: ['style-loader','css-loader' ]
//    },
  ]
 },
 //需要注意的点:vue-loader 15版本需加入插件
 plugins: [
    new VueLoaderPlugin()
  ]
};

2、rollup 打包js

2.1 在根目录创建 rollup.config.js,配置如下

bash 复制代码
import babel from 'rollup-plugin-babel';
export default {
    input: 'index.js',
    output: {
        file: 'lib/index.js',
        format: 'cjs',
    },
    plugins: [ babel({
        runtimeHelpers: true, 
        presets: ["@babel/preset-env"],
    }) ],
};

2.2 安装依赖包 package.json

npm install @babel/core @babel/preset-env 这里用来将es6转成es5

npm install rollup rollup-plugin-babel

bash 复制代码
{
  "main": "index.js",
  "scripts": {
    "buildrollup": "rollup -c",
  },
  "devDependencies": {
    "@babel/core": "^7.23.9",
    "@babel/preset-env": "^7.23.9",
    "babel-loader": "^9.1.3"
  },
  "dependencies": {
    "@babel/plugin-transform-runtime": "^7.23.9",
    "rollup": "^2.79.1",
    "rollup-plugin-babel": "^4.4.0"
  }
}

2.4 rollup 打包vue

rollup-plugin-vue^5.1.9 + vue-template-compiler

rollup-plugin-postcss 识别css和预处理文件

npm install rollup-plugin-vue@5 vue-template-compiler rollup-plugin-postcss

bash 复制代码
export default {
    input: 'index.js',
    output: {
        file: 'lib/index.js',
        format: 'cjs',
    },
    plugins: [ babel({
        runtimeHelpers: true, 
        presets: ["@babel/preset-env"],
    }),
    vue2({
        css:true,
        compilerTemplate: true,
        preprocessStyles: true
    }) ],
};
相关推荐
神明不懂浪漫1 小时前
【第七章】Java中的常用类
java·开发语言·前端·经验分享·笔记
程序喵大人9 小时前
【C++进阶】STL容器与迭代器 - 01 STL 容器先解决元素放在哪里
开发语言·c++·stl
cui_ruicheng10 小时前
Python从入门到实战(十六):多进程编程
开发语言·python
shawxlee10 小时前
vue3在public下封装config.js自定义配置动态数据,可在打包后直接修改,方便后端部署及后续维护
前端·javascript·经验分享·vue·团队开发·js·项目优化
kyriewen11 小时前
我给前端项目的接口请求套了6层保护——才发现以前一直在裸奔
前端·javascript·面试
wdfk_prog11 小时前
嵌入式面试真题第 15 题:不可恢复异常后的通用崩溃快照、调用栈保存与离线分析架构
linux·开发语言·面试·架构
晴空了无痕11 小时前
从 Go 基础到 K8s:一条可落地的 Go 服务端成长路线
开发语言·后端·golang·kubernetes
神明不懂浪漫11 小时前
【第五章】Java中的继承与多态
java·开发语言
神州世通12 小时前
解密企业通信安全防线:Avaya Aura 三层安全架构深度解析
开发语言·php
郝亚军12 小时前
如何安装webstorm、Node.js和vue CLI
前端·javascript·vue.js