UMD 模块 vs CJS 模块

使用方式

UMD, window 全局注册后,直接使用


  1. <!DOCTYPE html>
  2. <html lang="zh-Hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <meta name="author" content="xgqfrms">
  8. <meta name="generator" content="VS code">
  9. <title>xyz demo 2</title>
  10. <!-- 引入样式 -->
  11. <link rel="stylesheet" href="https://unpkg.com/element-ui@2.14.1/lib/theme-chalk/index.css">
  12. <link rel="stylesheet" href="./xyz.css">
  13. </head>
  14. <body>
  15. <main>
  16. <div id="vue_app">
  17. <section>
  18. <xyz-hello :msg="author"></xyz-hello>
  19. <xyz-message :msg="msg"></xyz-message>
  20. <!-- <br />
  21. <Hello :msg="author"></Hello>
  22. <Message :msg="msg"></Message> -->
  23. <!-- element-ui -->
  24. <el-row>
  25. <el-button>默认按钮</el-button>
  26. <el-button type="primary">主要按钮</el-button>
  27. <el-button type="success">成功按钮</el-button>
  28. <el-button type="info">信息按钮</el-button>
  29. <el-button type="warning">警告按钮</el-button>
  30. <el-button type="danger">危险按钮</el-button>
  31. </el-row>
  32. <!-- xyz-ui -->
  33. <el-row>
  34. <xyz-hello :msg="author"></xyz-hello>
  35. <xyz-message :msg="msg"></xyz-message>
  36. </el-row>
  37. </section>
  38. </div>
  39. <main>
  40. <!-- 引入组件库 -->
  41. <script src="https://unpkg.com/vue@2.6.11/dist/vue.min.js"></script>
  42. <script src="https://unpkg.com/element-ui@2.14.1/lib/index.js"></script>
  43. <!-- <script src="./xyz.umd.js"></script> -->
  44. <script src="./xyz.umd.min.js"></script>
  45. <script>
  46. console.log(`xyz =`, xyz);
  47. console.log(`xyz.default[0] =`, xyz, xyz.default[0]);
  48. console.log(`xyz.default[1] =`, xyz, xyz.default[1]);
  49. var vm = new Vue({
  50. el: '#vue_app',
  51. // vm === data
  52. data: {
  53. msg: "vue ui lib ",
  54. author: "xgqfrms",
  55. },
  56. component: {
  57. // xyz 模块,引用方式
  58. // Hello: xyz.default[0],
  59. // Message: xyz.default[1],
  60. },
  61. });
  62. setTimeout(() => {
  63. let div = document.querySelector("#vue_app");
  64. div.style.display = "block";
  65. }, 0);
  66. </script>
  67. </body>
  68. </html>

CJS


  1. import Vue from "vue";
  2. import App from "./App.vue";
  3. import router from "./router";
  4. import store from "./store";
  5. import ElementUI from "element-ui";
  6. import "element-ui/lib/theme-chalk/index.css";
  7. // 全局导入
  8. import XYZ from "../lib/xyz.common";
  9. import '../lib/xyz.css';
  10. Vue.use(ElementUI);
  11. Vue.use(XYZ);
  12. Vue.config.productionTip = false;
  13. new Vue({
  14. router,
  15. store,
  16. render: h => h(App)
  17. }).$mount("#app");

npm package


  1. import Vue from "vue";
  2. import App from "./App.vue";
  3. import router from "./router";
  4. import store from "./store";
  5. import ElementUI from "element-ui";
  6. import "element-ui/lib/theme-chalk/index.css";
  7. // 全局导入, ??? src/index 全局注册入口
  8. import XYZ from "@xgqfrms/xyz/lib/xyz.common.js";
  9. import '@xgqfrms/xyz/lib/lib/xyz.css';
  10. Vue.use(ElementUI);
  11. Vue.use(XYZ);
  12. Vue.config.productionTip = false;
  13. new Vue({
  14. router,
  15. store,
  16. render: h => h(App)
  17. }).$mount("#app");

src/index 全局注册入口

  1. /* Automatically generated by './build/bin/build-entry.js' */
  2. // 全局引入 ElementUI
  3. import Vue from 'vue';
  4. import ElementUI from 'element-ui';
  5. import 'element-ui/lib/theme-chalk/index.css';
  6. Vue.use(ElementUI);
  7. // 导入组件
  8. import Hello from './hello';
  9. import Message from './message';
  10. import * as PACKAGE from '../package.json';
  11. // 存储组件列表
  12. const components = [
  13. Hello,
  14. Message,
  15. ];
  16. // 定义 install 方法,接收 Vue 作为参数。
  17. // 如果使用 use 注册插件,则所有的组件都将被注册
  18. const install = function (Vue) {
  19. // 判断是否安装
  20. if (install.installed) return;
  21. // 遍历注册全局组件
  22. components.forEach(component => {
  23. Vue.component(component.name, component);
  24. });
  25. // Vue.prototype 全局属性
  26. // Vue.prototype.$message = DuiMessage;
  27. }
  28. // 判断是否是直接引入文件
  29. if (typeof window !== 'undefined' && window.Vue) {
  30. install(window.Vue);
  31. }
  32. export default {
  33. version: PACKAGE.version,
  34. // 导出的对象必须具有 install,才能被 Vue.use() 方法安装
  35. install,
  36. // 以下是具体的组件列表
  37. ...components,
  38. };

脚本自动注册

build/bin/build-entry.js & components.json

  1. // 全局导入, ??? src/index 全局注册入口
  2. -import XYZ from "@xgqfrms/xyz/lib/xyz.common.js";
  3. +import XYZ from "@xgqfrms/xyz";
  4. import '@xgqfrms/xyz/lib/lib/xyz.css';

提高易用性, 缩短导入路径名称


  1. var Components = require('../../components.json');
  2. var fs = require('fs');
  3. var render = require('json-templater/string');
  4. var uppercamelcase = require('uppercamelcase');
  5. var path = require('path');
  6. var endOfLine = require('os').EOL;
  7. var OUTPUT_PATH = path.join(__dirname, '../../src/index.js');
  8. var IMPORT_TEMPLATE = 'import {{name}} from \'../packages/{{package}}/index.js\';';
  9. var INSTALL_COMPONENT_TEMPLATE = ' {{name}}';
  10. var MAIN_TEMPLATE = `
  11. /* Automatically generated by './build/bin/build-entry.js' */
  12. {{include}}
  13. import locale from 'element-ui/src/locale';
  14. import CollapseTransition from 'element-ui/src/transitions/collapse-transition';
  15. const components = [
  16. {{install}},
  17. CollapseTransition
  18. ];
  19. const install = function(Vue, opts = {}) {
  20. locale.use(opts.locale);
  21. locale.i18n(opts.i18n);
  22. components.forEach(component => {
  23. Vue.component(component.name, component);
  24. });
  25. Vue.use(InfiniteScroll);
  26. Vue.use(Loading.directive);
  27. Vue.prototype.$ELEMENT = {
  28. size: opts.size || '',
  29. zIndex: opts.zIndex || 2000
  30. };
  31. Vue.prototype.$loading = Loading.service;
  32. Vue.prototype.$msgbox = MessageBox;
  33. Vue.prototype.$alert = MessageBox.alert;
  34. Vue.prototype.$confirm = MessageBox.confirm;
  35. Vue.prototype.$prompt = MessageBox.prompt;
  36. Vue.prototype.$notify = Notification;
  37. Vue.prototype.$message = Message;
  38. };
  39. /* istanbul ignore if */
  40. if (typeof window !== 'undefined' && window.Vue) {
  41. install(window.Vue);
  42. }
  43. export default {
  44. version: '{{version}}',
  45. locale: locale.use,
  46. i18n: locale.i18n,
  47. install,
  48. CollapseTransition,
  49. Loading,
  50. {{list}}
  51. };
  52. `;
  53. delete Components.font;
  54. var ComponentNames = Object.keys(Components);
  55. var includeComponentTemplate = [];
  56. var installTemplate = [];
  57. var listTemplate = [];
  58. ComponentNames.forEach(name => {
  59. var componentName = uppercamelcase(name);
  60. includeComponentTemplate.push(render(IMPORT_TEMPLATE, {
  61. name: componentName,
  62. package: name
  63. }));
  64. if (['Loading', 'MessageBox', 'Notification', 'Message', 'InfiniteScroll'].indexOf(componentName) === -1) {
  65. installTemplate.push(render(INSTALL_COMPONENT_TEMPLATE, {
  66. name: componentName,
  67. component: name
  68. }));
  69. }
  70. if (componentName !== 'Loading') listTemplate.push(` ${componentName}`);
  71. });
  72. var template = render(MAIN_TEMPLATE, {
  73. include: includeComponentTemplate.join(endOfLine),
  74. install: installTemplate.join(',' + endOfLine),
  75. version: process.env.VERSION || require('../../package.json').version,
  76. list: listTemplate.join(',' + endOfLine)
  77. });
  78. fs.writeFileSync(OUTPUT_PATH, template);
  79. console.log('[build entry] DONE:', OUTPUT_PATH);

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


UMD 模块 vs CJS 模块的更多相关文章

  1. python常用模块(模块和包的解释,time模块,sys模块,random模块,os模块,json和pickle序列化模块)

    1.1模块 什么是模块: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文 ...

  2. Python模块之常用模块,反射以及正则表达式

    常用模块  1. OS模块 用于提供系统级别的操作,系统目录,文件,路径,环境变量等 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("di ...

  3. python基础知识8——模块1——自定义模块和第三方开源模块

    模块的认识 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...

  4. Python引用模块和查找模块路径

    模块间相互独立相互引用是任何一种编程语言的基础能力.对于"模块"这个词在各种编程语言中或许是不同的,但我们可以简单认为一个程序文件是一个模块,文件里包含了类或者方法的定义.对于编译 ...

  5. threading模块和queue模块实现程序并发功能和消息队列

    简介: 通过三个例子熟悉一下python threading模块和queue模块实现程序并发功能和消息队列. 说明:以下实验基于python2.6 基本概念 什么是进程? 拥有独立的地址空间,内存,数 ...

  6. VBA标准模块与类模块

    大家通过之前的介绍,已知道怎么将一个空模块插入VBA的工程中.从插入模块中可以看到,模块有有两种——标准模块与类模块.类模块是含有类定义的特殊模块,包括其属性和方法的定义.在后面会有介绍与说明. 随着 ...

  7. ansible定时任务模块和用户组模块使用

    接上篇,还是一些基础模块的使用,这里主要介绍的是系统模块的使用. 下面例子都进行过相关的实践,从而可以直接进行使用相关的命令. 3.用户模块的使用 用户模块主要用来管理用户账号和用户的属性(对远程主机 ...

  8. ansible服务模块和组模块使用

    本篇文章主要是介绍ansible服务模块和组模块的使用. 主要模块为ansible service module和ansible group moudle,下面的内容均是通过实践得到,可以直接运行相关 ...

  9. python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块

    正则表达式   语法:             mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...

随机推荐

  1. Python+Selenium+Unittest实现PO模式web自动化框架(1)

    1.什么是PO模式? PO是Page Object的缩写 PO模式是自动化测试项目开发实践的最佳设计模式之一,讲页面定位和业务操作分开,也就是把对象的定位和测试脚本分开,从而提供可维护性. 主要有以下 ...

  2. SELECT ... FOR UPDATE or SELECT ... FOR SHARE Locking Reads session

    小结: 1.注意使用限制 Locking reads are only possible when autocommit is disabled (either by beginning transa ...

  3. git branch --set-upstream-to=

    test@uat:/usr/server/app_server# git config --local -lcore.repositoryformatversion=0core.filemode=tr ...

  4. python基础(格式化输出、基本运算符、编码)

    1,格式化输出. 现有一练习需求,问用户的姓名.年龄.工作.爱好 ,然后打印成以下格式 ------------ info of Alex Li ----------- Name : Alex Li ...

  5. 《进击吧!Blazor!》第一章 3.页面制作

    作者介绍 陈超超Ant Design Blazor 项目贡献者拥有十多年从业经验,长期基于.Net技术栈进行架构与开发产品的工作,Ant Design Blazor 项目贡献者,现就职于正泰集团 写专 ...

  6. Node.js 安全指南

    当项目周期快结束时,开发人员会越来越关注应用的"安全性"问题.一个安全的应用程序并不是一种奢侈,而是必要的.你应该在开发的每个阶段都考虑应用程序的安全性,例如系统架构.设计.编码, ...

  7. eclipse 断点调试方法

    1 Debug视图 1.1 线程堆栈视图 线程堆栈视图表示当前线程的堆栈,从中可以看出在运行哪些代码,并且整个调用过程,以及代码行号.分别介绍一下这几个按钮的含义.从左至右分别为: 1.表示当前实现继 ...

  8. Spring boot 使用Redis 消息队列

    package com.loan.msg.config; import com.loan.msg.service.MessageReceiver; import org.springframework ...

  9. zabbix添加持续告警

  10. 代码审计学习01-in_array() 函数缺陷

    一.开始代码审计之旅 01 从今天起,学习代码审计了,这篇文章就叫代码审计01吧,题目来自 PHP SECURITY CALENDAR 2017 的第一题,结合 红日安全 写的文章,开始吧. 二.先看 ...