Open zhouzhongyuan opened 7 years ago
假语村言
一开始Javascript本来就是按文件分开加载的。后来觉得文件大了,于是用minify压缩单个文件。再后来觉得发多个HTTP取数个文件太浪费,就用webpack打包成一个文件。现在又觉得打包在一起单个文件太大了,又要把包拆分下载。。。你说前段TMD怎么那么多事呢?! alienbat
webpack --profile --json > stats.json
./node_modules/webpack-bundle-analyzer/lib/bin/analyzer.js stats.json dist -m static
并没有选用webpack作为Yigo1.6的打包工具,因为:
zijiang
这类项目中的代码(一行都不能改)。如果使用webpack
就不得不更改zijiang
中的部分代码,只得选用r.js
。
[,'css!lib/yigo/controls/YIGO.ui.LabelText.css']
->['css!lib/yigo/controls/YIGO.ui.LabelText.css']
r.js
使用./node_modules/.bin/r.js -o build.js
build.js
(r.js的配置文件),示例:example.build.js
1.6的代码中,使用了require-css。恰好,require-css
中的css-builder.js提供了对打包的支持。
cssAPI.load
: 加载每个css文件
layerBuffer
: 一个数组,包含所有的css内容
cssAPI.onLayerEnd
: 保存成bundle.css
。
此处,我对这个文件进行了稍微的修改。
把
var css = layerBuffer.reverse().join('');
改为
var css = layerBuffer().join('');
因为通过r.js加载的css顺序是相反的,例如,r.js
会首先加载页面css,然后控件css,然后base css。
把
cssBuffer[name] = normalize(loadFile(fileUrl), fileSiteUrl, siteRoot);
改为:
const platform = 'android';
if(name === 'lib/yigo/css/ios' && platform === 'android' ){
cssBuffer[name] = '/*ios Don\'t import.*/';
}else{
cssBuffer[name] = normalize(loadFile(fileUrl), fileSiteUrl, siteRoot);
}
From Require.js to Webpack - Part 2 (the how)
r.js
应该就是一个文件分析器,分析所有用到require的地方publicPath: 路由,类似express中的static
loader与plugin的区别
Loaders do the pre-processing transformation of virtually any file format when you use sth like require("my-loader!./my-awesome-module") in your code. Compared to plugins, they are quite simple as they
Plugins on the other hand can deeply integrate into webpack because they can register hooks within webpacks build system and access (and modify) the compiler, and how it works, as well as the compilation. Therefore, they are more powerful, but also harder to maintain.