第五章webpack工程化-5节原理课程-day_第1页
第五章webpack工程化-5节原理课程-day_第2页
第五章webpack工程化-5节原理课程-day_第3页
第五章webpack工程化-5节原理课程-day_第4页
第五章webpack工程化-5节原理课程-day_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Wck- wck在执行npxwck进行打包后,都干了什么事情((function(modules)varinstalledModules=functionck_require{if(installedModules[moduleId])return}varmodule=(installedModules[moduleId]{i:moduleId,l:false,exports:{}wwmodule.l=true;} ck_require((ck_require.s"./index.js":function(module,{'//importafrom"./a";\n\nconsole.log(" word");\n\n\n//#sourceURL=w "./a.js":function(module,{'//importafrom"./a";\n\nconsole.log(" word");\n\n\n//#sourceURL=w "./b.js":function(module,{'//importafrom"./a";\n\nconsole.log(" word");\n\n\n//#sourceURL=w }大概的意思就是,我们实现了一个wck_require来实现自己的模块wck配置文出创建一个wck哪些内容:wck启动函自己实现一个模块分析:文件,分析代constfs=constconstfenximokuai=filename=>constcontent=fs.readFileSync(filename,"utf-8"); npminstallnpminstall@babel/parser--constfs=constparser=constfenximokuai=filename=>constcontent=fs.readFileSync(filename,"utf-constAst={sourceType:npminstall@babel/traverse--constconstfs=require("fs");constpath=require("path");constparser=consttraverse=constfenximokuai=filename=>constconstcontent=fs.readFileSync(filename,"utf-constAst={sourceType:constdependencies=traverse(Ast,{ImportDeclaration({node{console.log(node);}文件引入的模块引入路在项目中里的可以在浏览器里执行的处理现在的路径问constparser=//修改dependenciesconstdependencies=constnewfilename="./"+path.join(path.dirname(filename),dependencies[node.source.valuenewfilename;把代码处理成浏览器可运行的代码,需要借助@babel/core@babel/preset-env,把ast语法树转换成合适的constbabel=const{code}=babel.transformFromAst(Ast,{presets:["@babel/preset-导出所有分析出的returnreturn完成代码constfs=require('fs');constpath=require('path');constparser=consttraverse=require('@babel/traverse').default;constbabel=require('@babel/core');const yser=(filename)=>constcontent=fs.readFileSync(filename,'utf-8');constast=parser.parse(content,{sourceType:constdependencies={};traverse(ast,{ImportDeclaration({node})constdirname=path.dirname(filename);constnewFile='./'+path.join(dirname,dependencies[node.source.value]=}const{code}=babel.transformFromAst(ast,{presets:["@babel/preset-{filename,}constconstmoduleInfo=}分析依块的分析:constfs=require('fs');constpath=require('path');constparser=consttraverse=require('@babel/traverse').default;constbabel=require('@babel/core');const yser=(filename)=>constcontent=fs.readFileSync(filename,'utf-8');constast=parser.parse(content,{sourceType:constdependencies={};traverse(ast,{ImportDeclaration({node})constdirname=path.dirname(filename);constnewFile='./'+path.join(dirname,dependencies[node.source.value]=}const{code}=babel.transformFromAst(ast,{presets:["@babel/preset-{filename,}}constmakeDependenciesGraph=(entry){constentryModule=module constgraphArray=[entryModule];for(leti=0;i<graphArray.length;{constitem=graphArray[i];const{dependencies}=item;if(dependencies){for(letjin{ }}}constgraph={};graphArray.forEach(item=>{graph[item.filename]{dependencies:item.dependencies,code:item.code}return}constgraghInfo=makeDependenciesGraph('./src/index.js');生成代constfs=require('fs');constpath=require('path');constparser=consttraverse=require('@babel/traverse').default;constbabel=require('@babel/core');const yser=(filename)=>constcontent=fs.readFileSync(filename,'utf-8');constast=parser.parse(content,{sourceType:constdependencies={};traverse(ast,{ImportDeclaration({node})constdirname=path.dirname(filename);constnewFile='./'+path.join(dirname,dependencies[node.source.value]=}const{code}=babel.transformFromAst(ast,{presets:["@babel/preset-{filename,}}constmakeDependenciesGraph=(entry){constentryModule=module constgraphArray=[entryModule];for(leti=0;i<graphArray.length;{constitem=graphArray[i];const{dependencies}=item;if(dependencies){for(letjin{ }}}constgraph={};graphArray.forEach(item=>{graph[item.filename]{dependencies:item.dependencies,code:item.code}return}constgenerateCode=(entry)=>constgraph=JSON.stringify(makeDependenciesGraph(entry));return`functionrequire(module)function{return}varexports={};(function(require,exports,code){})(localRequire,exports,graph[module].code);returnexports;}constconstcode=generateCode('./src/index.js');"debug":"node--inspect--inspect- 如何自己编写一个接口文档 创建一个替换源码中字符串的omodule.exports=console.log(source,this,this.query);returnsource.replace('kkb','开课吧') 在配置文件中使用constpath=require('path')module:{rules:{test:use:path.resolve(dirname,}] module:rules:{test:/\.js$/,use:[{loader:path.resolve(dirname,name:开课}}]}]//constloaderUtilsrequire("loader- module.exports=function(source)//returnsource.replace("kkb",);constoptions=loaderUtils.getOptions(this);constresult=source.replace("kkb",);returnsource.replace("kkb",);}this.callback:如何返回多个信息,不止是处理好的源码呢,可以使用constconstloaderUtilsrequire("loader- module.exports=function(source)constoptions=loaderUtils.getOptions(this);constresult=source.replace("kkb",);this.callback(null,result);:Error|content:string|Buffer,sourceMap?:SourceMap,meta?:anyconstconstloaderUtils=require("loader-module.exports=function(source)constoptions=loaderUtils.getOptions(this);setTimeout(()=>{constresult=source.replace("kkb",return},我们使 constconstloaderUtils=require("loader-//callback就是this.callbackconstcallback=this.async();setTimeout(()=>{constresult=source.replace("kkb",);callback(null,result);},module.exports=function(source)constoptions=module.exports=returnsource.replace("开课吧",constloaderUtils=require("loader-utils");module.exports=function(source){constoptions= constcallback=this.async();setTimeout(()=>{constresult=source.replace("kkb",);callback(null,result);}, module:{rules:{use:[path.resolve(dirname,{loader:path.resolve(dirname,name:开课}}]//use:[path.resolve(dirname,}]顺序,自下而上,自右resolveLoader:modules:["node_modules",{rules:{test:/\.js$/,use:[{loader:"

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论