Skip to content

Commit f17f85a

Browse files
committed
增加指定依赖使用示例
1 parent 04d7f27 commit f17f85a

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

project/build-global.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# 指定依赖
2+
3+
package.json 中 `spm.build.global` 指定依赖,他们不会被解析,但会被替换为 window 下的全局变量。
4+
5+
比如:
6+
7+
```js
8+
var $ = require('jquery');
9+
```
10+
11+
配置 `"global":{"jquery":"jQuery"}` 后被构建成:
12+
13+
```js
14+
var $ = window['jQuery'];
15+
```
16+
17+
你可以利用 global 避免打包一些基础模块,比如 `jQuery` `underscore.js`
18+
19+
## 开发指定依赖 jQuery 项目
20+
21+
22+
### 创建目录和文件
23+
```
24+
$ mkdir build-global && cd build-global
25+
$ touch index.html index.js package.json
26+
```
27+
> Windows 用户请创建 build-global 目录,进入 build-global目录创建 `index.html``index.js` `package.json` 文件
28+
29+
### 准备HTML
30+
在 index.html 写上基本的页面结构和样式:
31+
32+
```html
33+
<!DOCTYPE html>
34+
<html lang="en">
35+
<head>
36+
<meta charset="UTF-8">
37+
<title>使用 spm 开发指定依赖 jQuery 的项目</title>
38+
<style>
39+
#box {
40+
width: 100px;
41+
height: 100px;
42+
background-color: #ABCDEF;
43+
}
44+
</style>
45+
</head>
46+
<body>
47+
<div id="box"></div>
48+
<script src="lib/jquery.min.js"></script>
49+
<script src="index.js"></script>
50+
</body>
51+
</html>
52+
```
53+
54+
### 声明依赖
55+
56+
`package.json` 中写入:
57+
58+
```js
59+
{
60+
"spm": {
61+
"build": {
62+
"global": {
63+
"jquery": "jQuery"
64+
}
65+
}
66+
}
67+
}
68+
```
69+
70+
### 下载 jquery.min.js 至 lib 目录
71+
[https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js](https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js)
72+
73+
此时目录结构为:
74+
```
75+
└── lib/
76+
│ └── jquery.min.js
77+
├── index.html
78+
├── index.js
79+
└── package.json
80+
```
81+
### 编写 index.js
82+
83+
```
84+
var $ = require('jquery');
85+
$(function (){
86+
$('#box').hide(333).show(333);
87+
})
88+
```
89+
90+
### 调试代码
91+
92+
调试开发模式下的 CommonJS 代码:
93+
```
94+
$ spm server
95+
```
96+
打开 [http://127.0.0.1:8000/](http://127.0.0.1:8000/) 预览效果
97+
98+
### 构建项目
99+
开发完毕,一行命令构建:
100+
101+
```
102+
$ spm build index.js index.html
103+
```
104+
105+
构建后的目录结构为:
106+
```
107+
└── dist/
108+
├── index.html
109+
└── index.js
110+
└── lib/
111+
│ └── jquery.min.js
112+
├── index.html
113+
├── index.js
114+
└── package.json
115+
```
116+
117+
[dist/index.js](https://github.com/spmjs/examples/blob/master/build-global/dist/index.js) 文件内容为:
118+
119+
```js
120+
!function(r){function o(e){if(t[e])return t[e].exports;var n=t[e]={exports:{},id:e,loaded:!1};return r[e].call(n.exports,n,n.exports,o),n.loaded=!0,n.exports}var t={};return o.m=r,o.c=t,o.p="",o(0)}([function(r,o,t){var e=t(1);e(function(){e("#box").hide(333).show(333)})},function(r,o,t){r.exports=jQuery}]);
121+
```
122+
123+
[查看更多配置项](https://github.com/nimojs/docs/blob/master/project/configuration.md)

project/configuration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ var $ = require('jquery');
9393
var $ = window['jQuery'];
9494
```
9595

96+
[Demo](https://github.com/spmjs/examples/tree/spm-webpack/build-global)
97+
9698
### node
9799

98100
配置是否引入 node 相关的补丁。

0 commit comments

Comments
 (0)