鱼C论坛

 找回密码
 立即注册
查看: 3076|回复: 0

[学习笔记] es6语法之模块化

[复制链接]
发表于 2017-7-12 06:10:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
老的js语法没有引入模块这种概念,只能从网页上引入许多js文件,使得全局变量都被污染了,十分不利于做大的项目。es6除了模块的这个概念,为了实现他我们需要安装额外的插件‘webpack’。
首先用npm进行全局安装
npm install webpack -g
然后在我们的工作目录下安装依赖项
npm install webpack --save-dev
接着要新建配置文件webpack.config.js,在文件里写入
module.exports={
        entry:__dirname+'/output/bianliang.js',
        output:{
                path:__dirname,
                filename:'webpack.js'
        }
}
entry是入口文件,output是生成的文件,我们把用es6语法写成的文件先用babel翻译成es5语法,然后直接在目录里输入
webpack
就会自动打包为一个文件了。
现在我完成一下这个步骤,首先在input目录下新建一个模块文件script.js,写入以下代码
module.exports='你好'
然后在input文件夹下bianliang.js文件里写入
const a=require('./script')
document.write(a)
接着在用原来的方法运行babel进行编译,然后输入webpack进行打包,自动生成webpack.js文件
/******/ (function(modules) { // webpackBootstrap
/******/         // The module cache
/******/         var installedModules = {};
/******/
/******/         // The require function
/******/         function __webpack_require__(moduleId) {
/******/
/******/                 // Check if module is in cache
/******/                 if(installedModules[moduleId]) {
/******/                         return installedModules[moduleId].exports;
/******/                 }
/******/                 // Create a new module (and put it into the cache)
/******/                 var module = installedModules[moduleId] = {
/******/                         i: moduleId,
/******/                         l: false,
/******/                         exports: {}
/******/                 };
/******/
/******/                 // Execute the module function
/******/                 modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/                 // Flag the module as loaded
/******/                 module.l = true;
/******/
/******/                 // Return the exports of the module
/******/                 return module.exports;
/******/         }
/******/
/******/
/******/         // expose the modules object (__webpack_modules__)
/******/         __webpack_require__.m = modules;
/******/
/******/         // expose the module cache
/******/         __webpack_require__.c = installedModules;
/******/
/******/         // define getter function for harmony exports
/******/         __webpack_require__.d = function(exports, name, getter) {
/******/                 if(!__webpack_require__.o(exports, name)) {
/******/                         Object.defineProperty(exports, name, {
/******/                                 configurable: false,
/******/                                 enumerable: true,
/******/                                 get: getter
/******/                         });
/******/                 }
/******/         };
/******/
/******/         // getDefaultExport function for compatibility with non-harmony modules
/******/         __webpack_require__.n = function(module) {
/******/                 var getter = module && module.__esModule ?
/******/                         function getDefault() { return module['default']; } :
/******/                         function getModuleExports() { return module; };
/******/                 __webpack_require__.d(getter, 'a', getter);
/******/                 return getter;
/******/         };
/******/
/******/         // Object.prototype.hasOwnProperty.call
/******/         __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/         // __webpack_public_path__
/******/         __webpack_require__.p = "";
/******/
/******/         // Load entry module and return exports
/******/         return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


var a = __webpack_require__(1);
document.write(a);

/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


module.exports = '你好';

/***/ })
/******/ ]);
这里面乱七八糟的代码不用管,直接新建个html文件,把它引入网页就可以显示‘你好’了。

评分

参与人数 1荣誉 +4 鱼币 +4 收起 理由
小甲鱼 + 4 + 4 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-7 08:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表