聊城网站建设哪个好些,做盗版网站的,景区网站建设材料,石家庄seorollup
rollup是一个javascript的模块化打包工具 可以帮助我们编译小的代码到一个大的负载的代码中 比如一个库或者一个应用
rollup与webpack的区别 rollup主要针对ES Module进行打包 另外webpack通常可以通过各种loader处理各种各样的文件 以及处理他们的依赖关系 rollup更多…rollup
rollup是一个javascript的模块化打包工具 可以帮助我们编译小的代码到一个大的负载的代码中 比如一个库或者一个应用
rollup与webpack的区别 rollup主要针对ES Module进行打包 另外webpack通常可以通过各种loader处理各种各样的文件 以及处理他们的依赖关系 rollup更多时候专注于处理javascript代码的当然也可以处理css font vue等文件 rollup的配置和理念相对于webpack来收 更加的简洁和容易理解 在早期webpack不支持tree shaking时 rollup具备更强的优势
通常在实际项目开发过程中 都会使用webpack
rollup的基本使用
npm install rollup
创建main.js 打包到bundle.js 打包浏览器的库 npx rollup ./src/main.js -f iife -o dist/bundle.js 打包AMD的库 npx rollup ./src/main.js -f amd -o dist/bundle.js 打包CommonJS的库 npx rollup ./src/main.js -f cjs --o dist/bundle.js 打包通用的库必须跟上name npx rollup ./src/main.js -f umd --name mathUtil -o dist/bundle.js
rollup的配置
module.exports {//入口input: ./lib/index.js,//出口output: [{format: umd,name: whyUtils,file: ./build/bundle.umd.js,},{format: cjs,file: ./build/bundle.cjs.js,},{format: amd,file: ./build/bundle.amd.js,},{format: iife,file: ./build/bundle.browser.js,},],
};解决rollup和第三方库的问题
安装解决commonjs的库 npm install rollup/plugin-commonjs
安装解决node_modules的库 npm install rollup/plugin-node-resolve
const commonjs require(rollup/plugin-commonjs)
const resolve require(rollup/plugin-node-resolve)module.exports {//入口input: ./lib/index.js,//出口output: {format: umd,name: utils,file: ./build/bundle.umd.js,},plugins: [resolve(),commonjs()],
};Babel转换代码
安装rollup对应的babel插件 npm install rollup/plugin-babel 修改配置文件
const {babel} require(rollup/plugin-babel)module.exports {//入口input: ./lib/index.js,//出口output: {format: umd,name: utils,file: ./build/bundle.umd.js,},plugins: [babel({babelHelpers: bundled,exclude: /node_modules/})],
};babel.config.js文件 npm install babel/preset-env
module.exports {presets: [babel/preset-env]
}Terser代码压缩
如果我们希望对代码进行压缩 可以使用rollup/plugin-terser npm install rollup/plugin-terser
const {babel} require(rollup/plugin-babel)
const terser require(rollup/plugin-terser)
module.exports {//入口input: ./lib/index.js,//出口output: {format: umd,name: utils,file: ./build/bundle.umd.js,},plugins: [babel({babelHelpers: bundled,exclude: /node_modules/}),terser()],
};处理css文件
npm install rollup-plugin-postcss postcss 配置postcss的插件
const postcss require(rollup-plugin-postcss postcss);module.exports {//入口input: ./lib/index.js,//出口output: {format: umd,name: utils,file: ./build/bundle.umd.js,},plugins: [postcss()],
};处理vue文件
处理vue文件 我们需要使用rollup-plugin-vue插件 但是注意默认 情况下我们安装的是vue3.x的版本 所以我们这里指定了以下rollup-plugn-vue的版本 npm install rollup-plugin-vue vue/compiler-sfc
使用vue插件
const vue require(rollup-plugin-vue);
module.exports {//入口input: ./lib/index.js,//出口output: {format: umd,name: utils,file: ./build/bundle.umd.js,},plugins: [vue()],
};在我们打包vue项目后 汇报process is not edefined的错误 这时因为我们在打包的vue代码中 用到process.env.NODE_ENV 所以我们可以使用一个插件rollup-plugin-replace设置它对应的值 npm install rollup-plugin-replace
const vue require(rollup-plugin-vue);
const replace require(rollup-plugin-replace);module.exports {//入口input: ./lib/index.js,//出口output: {format: umd,name: utils,file: ./build/bundle.umd.js,},plugins: [vue(),replace({process.envNODE_ENV: JSON.stringify(production),}),],
};搭建本地服务器
第一步 使用rollup-plugin-serve搭建服务 npm install rollup-plugin-serve
const serve require(rollup-plugin-serve);module.exports {//入口input: ./lib/index.js,//出口output: {format: umd,name: utils,file: ./build/bundle.umd.js,},plugins: [serve({port: 8000,open: true,contentBase: .,}),],
};第二步 当文件发生变化 自动刷新浏览器 npm install rollup-plugin-livereload
const serve require(rollup-plugin-serve);
const livereload require(rollup-plugin-livereload)module.exports {//入口input: ./lib/index.js,//出口output: {format: umd,name: utils,file: ./build/bundle.umd.js,},plugins: [serve({port: 8000,open: true,contentBase: .,}),libereload()],
};第三步 启动时 开启文件监听 npx rollup -c -w
区分开发环境
我们可以在package.json中创建一个开发和构建的脚本 scripts: {serve:rollup -c --environment NODE_ENVdevelopment -w,build:rollup -c --environment NODE_ENVproduction },const serve require(rollup-plugin-serve);
const livereload require(rollup-plugin-livereload);
const postcss require(rollup-plugin-postcss postcss);
const terser require(rollup/plugin-terser);
const vue require(rollup-plugin-vue);
const replace require(rollup-plugin-replace);const isProduction process.env.NODE_ENV production;const plugins [postcss(),vue(),replace({process.envNODE_ENV: JSON.stringify(production),}),
];if (isProduction) {plugins.push(terser());
} else {const extraPlugins [serve({port: 8000,open: true,contentBase: .,}),livereload(),];plugins.push(...extraPlugins);
}module.exports {//入口input: ./lib/index.js,//出口output: {format: umd,name: utils,file: ./build/bundle.umd.js,},plugins: plugins,
}; 文章转载自: http://www.morning.rqqn.cn.gov.cn.rqqn.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.ctrkh.cn.gov.cn.ctrkh.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.kdldx.cn.gov.cn.kdldx.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.bmssj.cn.gov.cn.bmssj.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.tnjff.cn.gov.cn.tnjff.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.cndxl.cn.gov.cn.cndxl.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.kxypt.cn.gov.cn.kxypt.cn http://www.morning.fssjw.cn.gov.cn.fssjw.cn http://www.morning.ndhxn.cn.gov.cn.ndhxn.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.sxtdh.com.gov.cn.sxtdh.com http://www.morning.jtcq.cn.gov.cn.jtcq.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.jrpmf.cn.gov.cn.jrpmf.cn http://www.morning.yxbdl.cn.gov.cn.yxbdl.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.njpny.cn.gov.cn.njpny.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.qrlkt.cn.gov.cn.qrlkt.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.pmjhm.cn.gov.cn.pmjhm.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.wmpw.cn.gov.cn.wmpw.cn http://www.morning.nzlsm.cn.gov.cn.nzlsm.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn http://www.morning.bhdtx.cn.gov.cn.bhdtx.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.kxgn.cn.gov.cn.kxgn.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.qnjcx.cn.gov.cn.qnjcx.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.ggrzk.cn.gov.cn.ggrzk.cn http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn http://www.morning.mhsmj.cn.gov.cn.mhsmj.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn http://www.morning.nxdqz.cn.gov.cn.nxdqz.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.lxyyp.cn.gov.cn.lxyyp.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.nmlpp.cn.gov.cn.nmlpp.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.rfwgg.cn.gov.cn.rfwgg.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.bpmft.cn.gov.cn.bpmft.cn http://www.morning.nflpk.cn.gov.cn.nflpk.cn http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.qcztm.cn.gov.cn.qcztm.cn