webpack最基本的用法

webpack最基本的用法

大家好,又见面了,我是全栈君。

webpack

安装

webpack是所以Node.js开发的工具,可通过npm安装,首先要保证node已经安装完毕,可以去node官网下载, 然后通过npm下载webpack

npm install webpack -g

我们只需要webpack构建项目, 项目上线后是不需要依赖webpack的 所以我们在项目文件夹下安装时候可以安装在dev-dependencies中, 即:

npm install webpack --save-dev

在这里我们采用第一种安装方法

编写代码

接下来我们使用webpack构建一个简单的hello word应用, 包括两个js模块

1 生成文本”Hello word”的hello模块(hello.js)

module.exports = "Hello word";

2 打印文本的index模块(index.js)

var text = require('./hello');
console.log(text);

3 页面内容(index.html)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript" src="bundle.js"></script> </body> </html>

引入的bundle.js并不存在,他就是我们使用webpack构建出来的js文件

现在我们的目录结构应该是这样的:

index.js

hello.js

index.html

构建

此时在项目文件夹下使用命令行工具(没有安装git可以按住shift右键,可以直接打开控制台)输入命令

webpack ./index bundle.js

这个命令会告诉webpack 将index.js作为项目入口文件进行构建, 并将结果输出为bundle.js, 然后在项目文件夹下就可以看到bundle.js文件了, 现在在浏览器中打开index.html文件就会在控制台看到输入Hello word了

bundle.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; /******/ /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ /******/ // 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 = 1); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) { module.exports = "hello word!"; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { var text = __webpack_require__(0); console.log(text); /***/ }) /******/ ]);

转载于:https://www.cnblogs.com/mr-yuan/p/6608142.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108603.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SpringBoot——ResponseBody

    SpringBoot——ResponseBody很多情况下我们希望后端的调用结果能直接写入Http的响应体中以供前端获取来更新页面显示,此时需要我们在以@controller为注解的类中对相应方法加上@ResponseBody注解:@ControllerclassHelloWorldController{@RequestMapping(“/hello”)@ResponseBodypublicStringhello(){return”HelloWorld!”;}}我们也可

    2022年5月28日
    36
  • 数据库 分区表详解

    数据库 分区表详解如果你的数据库中某一个表中的数据满足以下几个条件,那么你就要考虑创建分区表了。1、数据库中某个表中的数据很多。很多是什么概念?一万条?两万条?还是十万条、一百万条?这个,我觉得是仁者见仁、智者见智的问题。当然数据表中的数据多到查询时明显感觉到数据很慢了,那么,你就可以考虑使用分区表了。如果非要我说一个数值的话,我认为是100万条。2、但是,数据多了并不是创建分区表的惟一条件,哪怕你有一千万条记录,但

    2022年6月6日
    42
  • 贝叶斯公式的理解【转】

    贝叶斯公式的理解【转】本文转载自:https://blog.csdn.net/qq_37953276/article/details/79297316作者:知乎用户 链接:https://www.zhihu.com/question/21134457/answer/169523403来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。最近我自己在学习一些关于机器学习的东西,…

    2022年5月5日
    56
  • MySQL数据库的性能优化总结

    MySQL数据库的性能优化总结

    2021年4月9日
    121
  • APP抓包工具Fidder

    APP抓包工具FidderAPP抓包工具Fidder0.安装下载Classic版本https://www.telerik.com/download/fiddler1.使用a.设置Fiddler打开Fiddler,Tools->FiddlerOptions(配置完后记得要重启Fiddler)选中”DecrptHTTPStraffic”,Fiddler就可以截获HTTPS请求选中”Allowremotecomputerstoconnect”.是允许别的机器把HTTP/HTTPS请求

    2022年5月22日
    57
  • pycharm2021.5.2激活码(最新序列号破解)

    pycharm2021.5.2激活码(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    48

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号