|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
老的js语法没有引入模块这种概念,只能从网页上引入许多js文件,使得全局变量都被污染了,十分不利于做大的项目。es6除了模块的这个概念,为了实现他我们需要安装额外的插件‘webpack’。
首先用npm进行全局安装
然后在我们的工作目录下安装依赖项- 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语法,然后直接在目录里输入就会自动打包为一个文件了。
现在我完成一下这个步骤,首先在input目录下新建一个模块文件script.js,写入以下代码
然后在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文件,把它引入网页就可以显示‘你好’了。 |
评分
-
查看全部评分
|