We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在webpack特性里面,它可以支持将非javaScript文件打包,但前面写到webpack的模块化打包只能应用于含有特定规范的JavaScript文件。本次介绍的loader则是用来解决这类问题的。本文章loader的实现基于code-splitting
举个例子:
webpack.config.js中的配置loader
module: { rules: [ { test: /\.js$/, loader: "test-loader!test-loader2" } ] }
业务代码中的内联loader
require('d!c');
分析:
我们需要将这些loader解析成可运行的函数,并在parse模块解析语法树之前运行掉这些loader函数
所以我们需要:
实现思路:
最终require内的字符串如下
/Users/zhujian/Documents/workspace/webpack/simple-webpack/example/node_modules/d.js! /Users/zhujian/Documents/workspace/webpack/simple-webpack/example/node_modules/test-loader/index.js! /Users/zhujian/Documents/workspace/webpack/simple-webpack/example/node_modules/test-loader2/index.js! /Users/zhujian/Documents/workspace/webpack/simple-webpack/example/node_modules/c.js
loader递归逻辑如下:
nextLoader.apply(null, content); function nextLoader() { const args = Array.prototype.slice.apply(arguments); if (loaderFunctions.length > 0) { const loaderFunction = loaderFunctions.pop(); let async = false; const context = { fileName, options, debug: options.debug, async: function () { async = true; return nextLoader; }, callback: function () { async = true; nextLoader.apply(null, arguments) } }; const resVal = loaderFunction.apply(context, args); if (!async) { nextLoader(resVal); } } else { callback(null, args[0]) } }
将以上3个loader,test-loader,test-loader2,异步loader d.js,打包c.js
test-loader
module.exports = function(content) { return content+"\nexports.answer = 42;\n" }
test-loader2
module.exports = function(content) { return content+"\nexports.test2 = test2;\n" }
异步loader d.js
module.exports = function (content) { const d = 'd'; this.async(); const b = content + "\nexports.d = 2000;\n"; setTimeout(this.callback.bind(this, b), 2000); }
c.js
const c = 'c'; module.exports = c;
最终打包出来的c.js的代码如下
.... /* 1 */ /***/(function(module, exports,__webpack_require__) { const c = 'c'; module.exports = c; exports.test2 = test2; exports.answer = 42; exports.d = 2000; /***/} ....
本人的简易版webpack实现simple-webpack
(完)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
前言
在webpack特性里面,它可以支持将非javaScript文件打包,但前面写到webpack的模块化打包只能应用于含有特定规范的JavaScript文件。本次介绍的loader则是用来解决这类问题的。本文章loader的实现基于code-splitting
功能分析
举个例子:
webpack.config.js中的配置loader
业务代码中的内联loader
分析:
我们需要将这些loader解析成可运行的函数,并在parse模块解析语法树之前运行掉这些loader函数
所以我们需要:
实现
resolve 模块
实现思路:
最终require内的字符串如下
buildDeps模块
实现思路:
loader递归逻辑如下:
测试
将以上3个loader,test-loader,test-loader2,异步loader d.js,打包c.js
test-loader
test-loader2
异步loader d.js
c.js
最终打包出来的c.js的代码如下
代码实现
本人的简易版webpack实现simple-webpack
(完)
The text was updated successfully, but these errors were encountered: