Skip to content

Commit 5b2f812

Browse files
committed
build: 优化对 module.exports = 的支持
1 parent b0340e2 commit 5b2f812

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

CODE_OF_CONDUCT.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,18 @@
3535

3636
- `module.exports =`
3737

38-
因打包脚本缺陷问题,不允许使用 `module.exports = { name }` 的形式,此形式不能很好的压缩代码
38+
因打包脚本缺陷问题,不允许使用 `module.exports = { name: 'Jackie' }` 的形式,此形式会发生异常
3939

4040
```javascript
4141
//
42+
module.exports = {
43+
name: 'Jackie',
44+
age: 18
45+
};
46+
```
47+
48+
```javascript
49+
// ✅ 可以使用缩写形式
4250
const name = 'Jackie'
4351
const age = 18
4452

@@ -47,8 +55,8 @@
4755

4856
```javascript
4957
//
50-
module.exports.name = 'Jackie'
51-
module.exports.age = 18
58+
exports.name = 'Jackie'
59+
exports.age = 18
5260
```
5361

5462
- 工具模块统一使用 `.module.js` 为后缀,打包工具会忽略 `.module.js` 后缀的文件

rollup.config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ for (const filename of files) {
6464
* @param {string} code
6565
*/
6666
transform (code) {
67-
return code.replace(/module.exports\s* =/g, 'export default ')
68-
.replace(/module\.exports\.(\w+)\s*=/g, (str, name) => {
67+
return code.replace(/module\.exports\s* =\s*{/g, 'export {')
68+
.replace(/module\.exports\s* =/g, 'export default ')
69+
.replace(/(?:module\.)?exports\.(\w+)\s*=/g, (str, name) => {
6970
return `export const ${name} =`
7071
})
7172
}

0 commit comments

Comments
 (0)