Skip to content

Commit

Permalink
doc: 为每个demo写中文README
Browse files Browse the repository at this point in the history
  • Loading branch information
halwu(吴浩麟) committed Jun 8, 2017
1 parent 3641c65 commit dccd149
Show file tree
Hide file tree
Showing 19 changed files with 358 additions and 160 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Created by .ignore support plugin (hsz.mobi)
.idea/
node_modules
package-lock.json
24 changes: 24 additions & 0 deletions demo/auto-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 管理多个单页应用
`AutoWebPlugin` 可以自动管理多个单页应用。简单使用如下
```js
new AutoWebPlugin('./src/', {
ignorePages: ['ignore'],
template: './src/template.html',
commonsChunk: {
name: 'common',// name prop is require
minChunks: 2,
},
outputPagemap: true,
})
```
以上配置会让`AutoWebPlugin`找出`./src/`目录下的所有目录,为每个目录生产一个HTML作为单页应用的入口。
- `ignorePages` 属性用了忽略名叫`ignore`的目录,不为这个目录生产HTML入口
- `template` 属性指出所有单页应用生产HTML入口时公用的HTML模版,模版语法和`WebPlugin`里一致。
- `commonsChunk` 属性用来为`AutoWebPlugin`找出的所有单页应用抽离公共代码,用webpack自带的`CommonsChunkPlugin`实现。`commonsChunk`的配置属性和`CommonsChunkPlugin`一样。
- `outputPagemap` 属性用来输出一个叫`pagemap.json`的文件,里面存放了`AutoWebPlugin`找出的所有单页应用的名称和URL的映射关系。

`AutoWebPlugin`还支持很多配置选项,你可以[在这里看到](https://github.com/gwuhaolin/web-webpack-plugin/blob/master/lib/AutoWebPlugin.js#L33)

更多关于`AutoWebPlugin`的资料可以参考以下文章:
- [webpack原理与实战](https://github.com/gwuhaolin/blog/issues/4)
- [webpack2 终极优化](https://github.com/gwuhaolin/blog/issues/2)
35 changes: 35 additions & 0 deletions demo/config-resource/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 在webpack里直接通过JS配置资源注入到HTML
通过配置webpack的entry,webpack为我们生产了一系列chunk。但是我们需要把这些chunk注入到HTML里才可以在浏览器里运行起来。
`WebPlugin`可以方便的帮你把chunk注入到HTML里。
```js
new WebPlugin({
filename: 'index.html',
requires: {
'ie-polyfill': {
_ie: true
},
'inline': {
_inline: true,
_dist: true
},
'dev': {
_dev: true
},
//load a local google analyze file direct without local var webpack
'./google-analytics.js': {
_dist: true
}
}
})
```
通过`requires`属性可以配置chunk注入到HTML里的方式。`requires`的值可以是一个数组或是一个object。
- 为数组时,数组里的每一项都是chunk的名称。
- 为object时,可以配置每个chunk的注入方式

支持以下注入方式
- `_dist` 只有在生产环境下才引入该资源
- `_dev` 只有在开发环境下才引入该资源
- `_inline` 把该资源的内容潜入到html里
- `_ie` 只有IE浏览器才需要引入的资源,通过 `[if IE]>resource<![endif]` 注释实现

这种方式没有使用HTML模版,`WebPlugin`会采用默认的模版来生成一个HTML。如果你想要更细粒度的控制chunk的注入位置,你最好[使用模版](https://github.com/gwuhaolin/web-webpack-plugin/tree/master/demo/use-template)
51 changes: 51 additions & 0 deletions demo/extract-css/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 从JS中提取出CSS
有些时候webpack生产的chunk里包含了一些CSS代码,这些代码会通过JS注入到HTML HEAD里来加载。如果你想把JS里的CSS提取出来你可以使用`extract-text-webpack-plugin`插件提取。
提取后这个chunk会输出2个文件,一个是纯JS,一个是纯CSS。
这种情况下在使用`WebPlugin`时,你需要把提取出的CSS注入进去,如下:
webpack配置:
```js
module: {
loaders: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader'
})
}
]
},
entry: {
1: './1',
2: './2',
3: './3',
4: './4',
},
plugins: [
new ExtractTextPlugin('[name].css'),
new WebPlugin({
filename: 'index.html',
template: './template.html',
requires: ['1', '2', '3', '4']
}),
]
```
这样webpack会生成4个名叫 1, 2, 3, 4 的chunk,并且他们里面的CSS都被提取出来了。所以在HTML模版中你需要这样注入提取的CSS
```html
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="1">
<link rel="stylesheet" href="2?_inline">
<link rel="stylesheet" href="3?_ie">
<script src="1"></script>
<!--STYLE-->
</head>
<body>
<script src="2"></script>
<!--SCRIPT-->
<footer>footer</footer>
</body>
</html>
```
32 changes: 16 additions & 16 deletions demo/extract-css/dist/1.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ 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)) {
Expand All @@ -46,7 +46,7 @@
/******/ });
/******/ }
/******/ };

/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
Expand All @@ -55,13 +55,13 @@
/******/ __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 = 4);
/******/ })
Expand Down
32 changes: 16 additions & 16 deletions demo/extract-css/dist/2.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ 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)) {
Expand All @@ -46,7 +46,7 @@
/******/ });
/******/ }
/******/ };

/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
Expand All @@ -55,13 +55,13 @@
/******/ __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 = 5);
/******/ })
Expand Down
32 changes: 16 additions & 16 deletions demo/extract-css/dist/3.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ 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)) {
Expand All @@ -46,7 +46,7 @@
/******/ });
/******/ }
/******/ };

/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
Expand All @@ -55,13 +55,13 @@
/******/ __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 = 6);
/******/ })
Expand Down
Loading

0 comments on commit dccd149

Please sign in to comment.