You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// main.ejs(function(modules){// webpackBootstrap// The module cachevarinstalledModules={};// The require functionfunction__webpack_require__(moduleId){// Check if module is in cacheif(installedModules[moduleId]){returninstalledModules[moduleId].exports;}// Create a new module (and put it into the cache)varmodule=installedModules[moduleId]={i: moduleId,l: false,exports: {}};// Execute the module functionmodules[moduleId].call(module.exports,module,module.exports,__webpack_require__);// Flag the module as loadedmodule.l=true;// Return the exports of the modulereturnmodule.exports;}return__webpack_require__(__webpack_require__.s="<%-entryId%>");})({<%for(letkeyinmodules){%>"<%-key%>":
(function(module,exports,__webpack_require__){eval(`<%-modules[key]%>`);}),<%}%>});
支持loader
支持plugin
The text was updated successfully, but these errors were encountered:
实现一个mini-webpack
本来记录自己实现一个
mini-webpack
时候遇到的坑和总结webpack
的大致流程初始化
我们需要写一个
webpack
的包,首先需要初始化一下项目然后我们配置一下
package.json
的bin
选项然后我们需要新建一个
bin
文件夹和新起一个cy-pack.js
文件,我们看下文件的内容#! /usr/bin/env node
表示当前运行环境是node环境,下面就是对应的代码,这里不做解释了最后通过命令
npm link
将这个包映射到全局npm上去,在另外一个文件夹使用我们的包的时候只需要npm link cy-pack
就好啦手写一个最简单的打包工具
我们来总结一下基本的流程
通过在使用我们包的目录使用
npx cy-pack
运行我们的打包工具,然后我们可以拿到当前执行这个命令的路径根据这个路径我们可以拿到
webpack.config.js
,并将对应的数据进行存储然后调用
buildModule
创建我们的模块关系,主要思路是通过配置中的入口路径读取到入口文件的内容,然后调用bodylon.parse
将读到的内容转化为AST, 然后通过traverse
找到代码中的require
,将他替换成我们自己实现的__webpack_require__
,然后替换对应require
中的路径,最后生成依赖表单就好了(依赖表单是处理过的路径)然后通过遍历依赖表单调用
buildModule
方法递归处理完全部依赖的文件最后可以通过ejs模板等方式将数据写入模板中,最后输出到配置的
output
对应的路径和文件就好了我们来看下最基本的模板
支持loader
支持plugin
The text was updated successfully, but these errors were encountered: