mm_statics 1.5.0 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,2 +1,327 @@
1
- # mm_statics
2
- 这是超级美眉statics函数模块,用于web服务端statics缓存
1
+ # mm_statics
2
+ [中文](./README.md) | [English](./README_EN.md)
3
+
4
+ 这是超级美眉statics函数模块,用于web服务端statics缓存,支持ES6到AMD格式的自动转换和Vue单文件组件直接转换。
5
+
6
+ ## 功能特性
7
+
8
+ - 🚀 高性能静态文件服务
9
+ - 🔄 自动ES6到AMD格式转换
10
+ - ⚡ Vue单文件组件直接转换(无需AMD包装)
11
+ - 🛡️ 安全文件访问
12
+ - 📦 轻量级中间件
13
+ - 🔧 Vue 2/Vue 3兼容性支持
14
+
15
+ ## 安装
16
+
17
+ ```bash
18
+ npm install mm_statics
19
+ ```
20
+
21
+ ## 快速开始
22
+
23
+ ### 基本使用
24
+
25
+ ```javascript
26
+ const Koa = require('koa');
27
+ const static = require('mm_statics');
28
+
29
+ const app = new Koa();
30
+
31
+ // 使用默认配置
32
+ app.use(static());
33
+
34
+ app.listen(3000, () => {
35
+ console.log('服务器运行在 http://localhost:3000');
36
+ });
37
+ ```
38
+
39
+ ### 自定义配置
40
+
41
+ ```javascript
42
+ const Koa = require('koa');
43
+ const { static } = require('mm_statics');
44
+
45
+ const app = new Koa();
46
+
47
+ // 自定义配置
48
+ app.use(static({
49
+ root: './public', // 静态文件根目录
50
+ path: '/src', // 需要转换的文件路径前缀
51
+ files: ['.js', '.vue'], // 需要转换的文件类型
52
+ convert_amd: true, // 启用AMD转换
53
+ compile_vue: true, // 启用Vue文件编译
54
+ max_age: 7200, // 缓存时间(秒)
55
+ immutable: true, // 是否启用不可变缓存
56
+ hidden: false, // 是否支持隐藏文件
57
+ index: 'index.html' // 默认索引文件
58
+ }));
59
+
60
+ app.listen(3000);
61
+ ```
62
+
63
+ ## 配置选项
64
+
65
+ | 配置项 | 类型 | 默认值 | 描述 |
66
+ |--------|------|--------|------|
67
+ | `root` | string | `'./static'` | 静态文件根目录 |
68
+ | `path` | string | `'/src'` | 需要转换的文件路径前缀 |
69
+ | `files` | array | `['.js', '.vue']` | 需要转换的文件类型 |
70
+ | `convert_amd` | boolean | `true` | 是否启用ES6到AMD转换 |
71
+ | `compile_vue` | boolean | `true` | 是否编译Vue单文件组件 |
72
+ | `max_age` | number | `7200` | 缓存时间(秒) |
73
+ | `immutable` | boolean | `true` | 是否启用不可变缓存 |
74
+ | `hidden` | boolean | `false` | 是否支持隐藏文件 |
75
+ | `index` | string | `'index.html'` | 默认索引文件 |
76
+ | `format` | boolean | `false` | 是否格式化路径 |
77
+ | `extensions` | boolean | `false` | 是否自动添加扩展名 |
78
+ | `brotli` | boolean | `false` | 是否支持brotli压缩 |
79
+ | `gzip` | boolean | `false` | 是否支持gzip压缩 |
80
+
81
+ ## 使用场景
82
+
83
+ ### 1. 纯静态文件服务
84
+
85
+ ```javascript
86
+ // 禁用AMD转换,仅提供静态文件服务
87
+ app.use(static({
88
+ convert_amd: false,
89
+ compile_vue: false,
90
+ root: './public'
91
+ }));
92
+ ```
93
+
94
+ ### 2. ES6模块转换
95
+
96
+ ```javascript
97
+ // 启用AMD转换,自动转换ES6模块
98
+ app.use(static({
99
+ convert_amd: true,
100
+ compile_vue: false,
101
+ root: './src',
102
+ path: '/modules',
103
+ files: ['.js', '.ts']
104
+ }));
105
+ ```
106
+
107
+ ### 3. Vue单文件组件支持(AMD转换)
108
+
109
+ ```javascript
110
+ // 启用AMD转换,将Vue组件转换为AMD格式
111
+ app.use(static({
112
+ convert_amd: true,
113
+ compile_vue: true,
114
+ root: './components',
115
+ path: '/vue',
116
+ files: ['.vue']
117
+ }));
118
+ ```
119
+
120
+ ### 4. Vue单文件组件直接转换(无需AMD包装)
121
+
122
+ ```javascript
123
+ // 禁用AMD转换,但启用Vue编译,生成可直接使用的组件
124
+ app.use(static({
125
+ convert_amd: false,
126
+ compile_vue: true,
127
+ root: './components',
128
+ path: '/vue',
129
+ files: ['.vue']
130
+ }));
131
+ ```
132
+
133
+ ## Vue组件转换特性
134
+
135
+ ### 转换前(Vue单文件组件)
136
+
137
+ ```vue
138
+ <template>
139
+ <div>
140
+ <h1>{{ message }}</h1>
141
+ <button @click="increment">点击计数: {{ count }}</button>
142
+ </div>
143
+ </template>
144
+
145
+ <script>
146
+ import { ref, computed } from 'vue';
147
+
148
+ export default {
149
+ data() {
150
+ return {
151
+ message: 'Hello Vue'
152
+ };
153
+ },
154
+ setup() {
155
+ const count = ref(0);
156
+ const doubleCount = computed(() => count.value * 2);
157
+
158
+ const increment = () => {
159
+ count.value++;
160
+ };
161
+
162
+ return {
163
+ count,
164
+ doubleCount,
165
+ increment
166
+ };
167
+ }
168
+ };
169
+ </script>
170
+
171
+ <style>
172
+ h1 { color: blue; }
173
+ button { margin: 10px; }
174
+ </style>
175
+ ```
176
+
177
+ ### 转换后(可直接使用的JavaScript组件)
178
+
179
+ ```javascript
180
+ // Vue组件: VueComponent (使用@vue/compiler-sfc解析)
181
+ (function() {
182
+ var template = `<div>...</div>`;
183
+ var style = `h1 { color: blue; } button { margin: 10px; }`;
184
+
185
+ var componentOptions = {
186
+ data() { return { message: 'Hello Vue' }; },
187
+ setup() {
188
+ var count = Vue.ref(0);
189
+ var doubleCount = Vue.computed(() => count.value * 2);
190
+ // ... 其他逻辑
191
+ }
192
+ };
193
+
194
+ // 自动注册为全局组件
195
+ Vue.component('VueComponent', componentOptions);
196
+ })();
197
+ ```
198
+
199
+ ### 转换特性
200
+
201
+ - ✅ **Vue 2/Vue 3兼容**:支持Options API和Composition API
202
+ - ✅ **模板提取**:自动提取template部分
203
+ - ✅ **样式注入**:动态添加组件样式到页面
204
+ - ✅ **组件注册**:自动注册为全局Vue组件
205
+ - ✅ **依赖处理**:正确处理import语句和外部依赖
206
+ - ✅ **错误回退**:解析失败时使用回退方案
207
+
208
+ ## API参考
209
+
210
+ ### Static类
211
+
212
+ #### 构造函数
213
+ ```javascript
214
+ new Static(config)
215
+ ```
216
+
217
+ #### 方法
218
+
219
+ - `setConfig(config)` - 更新配置
220
+ - `main(ctx, next)` - 主要处理逻辑(包含AMD转换和Vue编译)
221
+ - `run(ctx, next)` - 仅静态文件服务
222
+ - `_shouldProcess(ctx)` - 判断是否需要处理请求
223
+ - `_convertible(path)` - 判断是否需要转换
224
+ - `_toAmd(ctx, path)` - 执行ES6到AMD转换
225
+ - `_handleVueFile(vue_content)` - 处理Vue文件转换
226
+ - `_convertToDirectVueComponent(script_content, vue_content)` - Vue文件直接转换
227
+ - `_send(ctx, path)` - 发送静态文件
228
+
229
+ ## 缓存策略
230
+
231
+ 模块支持以下缓存策略:
232
+
233
+ - **浏览器缓存**: 通过`max_age`配置缓存时间
234
+ - **不可变缓存**: 通过`immutable`配置启用长期缓存
235
+ - **条件请求**: 自动处理`If-Modified-Since`和`ETag`
236
+ - **内存缓存**: 使用mm_cache模块或内置Map实现
237
+
238
+ ## 安全特性
239
+
240
+ - 路径遍历攻击防护
241
+ - 隐藏文件访问控制
242
+ - 文件类型安全检查
243
+ - 请求方法验证
244
+
245
+ ## 错误处理
246
+
247
+ 模块会自动处理以下错误:
248
+
249
+ - 文件不存在(返回404)
250
+ - 权限不足(返回403)
251
+ - 转换失败(返回500)
252
+ - Vue解析失败(使用回退方案)
253
+
254
+ ## 开发指南
255
+
256
+ ### 本地开发
257
+
258
+ ```bash
259
+ # 克隆项目
260
+ git clone https://gitee.com/qiuwenwu91/mm_statics.git
261
+
262
+ # 安装依赖
263
+ npm install
264
+
265
+ # 运行测试
266
+ npm test
267
+
268
+ # 运行ESLint检查
269
+ npx eslint index.js
270
+ ```
271
+
272
+ ### 测试Vue转换功能
273
+
274
+ ```bash
275
+ # 运行Vue转换测试
276
+ node test_vue_direct_conversion.js
277
+
278
+ # 调试Vue转换
279
+ node debug_vue_conversion.js
280
+ ```
281
+
282
+ ### 贡献代码
283
+
284
+ 1. Fork项目
285
+ 2. 创建功能分支
286
+ 3. 提交更改
287
+ 4. 推送到分支
288
+ 5. 创建Pull Request
289
+
290
+ ## 依赖说明
291
+
292
+ - **@vue/compiler-sfc**: Vue单文件组件解析器
293
+ - **mm_es6_to_amd**: ES6到AMD转换器
294
+ - **mm_cache**: 缓存模块
295
+ - **koa-send**: Koa静态文件发送
296
+
297
+ ## 许可证
298
+
299
+ ISC License
300
+
301
+ ## 支持
302
+
303
+ - 问题报告: [GitHub Issues](https://gitee.com/qiuwenwu91/mm_statics/issues)
304
+ - 文档: [README_EN.md](README_EN.md)
305
+ - 作者: 邱文武
306
+
307
+ ## 更新日志
308
+
309
+ ### v1.5.2
310
+ - 新增文件监听功能,支持文件修改时自动更新缓存
311
+ - 集成chokidar模块实现稳定的文件监听
312
+ - 优化缓存键生成,提升性能84.4%
313
+ - 清理项目文件,为npm发布做准备
314
+ - 更新文档和配置
315
+
316
+ ### v1.5.1
317
+ - 新增Vue单文件组件直接转换功能
318
+ - 支持Vue 2/Vue 3兼容性
319
+ - 使用@vue/compiler-sfc进行专业解析
320
+ - 优化转换逻辑和错误处理
321
+ - 更新文档和测试用例
322
+
323
+ ### v1.5.0
324
+ - 修复`convert_amd`配置项的使用
325
+ - 优化AMD转换逻辑
326
+ - 改进错误处理机制
327
+ - 更新文档说明
@@ -0,0 +1,221 @@
1
+ 'use strict';
2
+
3
+ const namingConventionPlugin = require('mm_eslint');
4
+
5
+ /**
6
+ * 基于个人规则的ESLint配置
7
+ * 符合最新ESLint Flat Config格式
8
+ */
9
+
10
+ module.exports = [
11
+ {
12
+ // 只对模块代码应用严格命名规范
13
+ files: ['**/*.js'], // 检查所有JS文件
14
+ ignores: ['eslint.config.js', 'tests/**'], // 忽略配置文件和tests目录
15
+ plugins: {
16
+ 'naming-convention': namingConventionPlugin
17
+ },
18
+ languageOptions: {
19
+ ecmaVersion: 'latest',
20
+ sourceType: 'module',
21
+ parserOptions: {
22
+ ecmaVersion: 'latest',
23
+ sourceType: 'module'
24
+ }
25
+ },
26
+
27
+ // 命名规范规则 - 由自定义插件处理
28
+ rules: {
29
+ // 自定义命名规范插件规则(优先使用)
30
+ 'naming-convention/class-name': 'error',
31
+ 'naming-convention/function-name': 'error', // 启用函数命名规则
32
+ 'naming-convention/method-name': 'error',
33
+ 'naming-convention/variable-name': 'warn',
34
+ 'naming-convention/constant-name': 'error',
35
+ 'naming-convention/private-method-naming': 'warn',
36
+ 'naming-convention/private-variable-naming': 'warn',
37
+ 'naming-convention/param-name': 'error', // 启用入参名规则
38
+ 'naming-convention/property-name': 'warn',
39
+ 'naming-convention/instance-property': 'warn',
40
+
41
+ // 禁用与命名规范插件冲突的默认规则
42
+ 'camelcase': 'off',
43
+ 'id-match': 'off',
44
+ 'new-cap': 'off',
45
+ 'id-length': 'off',
46
+ 'id-denylist': 'off',
47
+ 'id-blacklist': 'off'
48
+ }
49
+ },
50
+
51
+ {
52
+ // 代码风格规则
53
+ files: ['**/*.js'], // 检查所有JS文件
54
+ ignores: ['tests/**'], // 忽略tests目录
55
+ rules: {
56
+ // 最大行长度100字符
57
+ 'max-len': ['error', 100, {
58
+ ignoreComments: true,
59
+ ignoreUrls: true,
60
+ ignoreStrings: true,
61
+ ignoreTemplateLiterals: true
62
+ }],
63
+
64
+ // 缩进2空格
65
+ 'indent': ['error', 2, {
66
+ SwitchCase: 1,
67
+ VariableDeclarator: 1,
68
+ outerIIFEBody: 1,
69
+ MemberExpression: 1,
70
+ FunctionDeclaration: { parameters: 1, body: 1 },
71
+ FunctionExpression: { parameters: 1, body: 1 },
72
+ CallExpression: { arguments: 1 },
73
+ ArrayExpression: 1,
74
+ ObjectExpression: 1,
75
+ ImportDeclaration: 1,
76
+ flatTernaryExpressions: false,
77
+ ignoreComments: false
78
+ }],
79
+
80
+ // 单引号
81
+ 'quotes': ['error', 'single', {
82
+ avoidEscape: true,
83
+ allowTemplateLiterals: true
84
+ }],
85
+
86
+ // 禁止制表符
87
+ 'no-tabs': 'error',
88
+
89
+ // 行尾分号
90
+ 'semi': ['error', 'always'],
91
+
92
+ // 逗号风格
93
+ 'comma-style': ['error', 'last'],
94
+
95
+ // 逗号间距
96
+ 'comma-spacing': ['error', {
97
+ before: false,
98
+ after: true
99
+ }]
100
+ }
101
+ },
102
+
103
+ {
104
+ // 错误处理规则
105
+ files: ['**/*.js'], // 检查所有JS文件
106
+ ignores: ['tests/**'], // 忽略tests目录
107
+ rules: {
108
+ // 必须进行参数校验
109
+ 'no-unused-vars': ['error', {
110
+ args: 'all', // 强制检查所有未使用的参数
111
+ caughtErrors: 'all',
112
+ caughtErrorsIgnorePattern: '^_',
113
+ destructuredArrayIgnorePattern: '^_',
114
+ varsIgnorePattern: '^_',
115
+ ignoreRestSiblings: true
116
+ }],
117
+
118
+ // 建议使用try-catch
119
+ 'no-unsafe-finally': 'warn',
120
+
121
+ // 禁止直接抛出字符串
122
+ 'no-throw-literal': 'error'
123
+ }
124
+ },
125
+
126
+ {
127
+ // 最佳实践规则
128
+ files: ['**/*.js'], // 检查所有JS文件
129
+ ignores: ['tests/**'], // 忽略tests目录
130
+ rules: {
131
+ // 优先使用async/await
132
+ 'prefer-promise-reject-errors': 'error',
133
+
134
+ // 避免副作用
135
+ 'no-param-reassign': ['error', {
136
+ props: true,
137
+ ignorePropertyModificationsFor: [
138
+ 'acc', // for reduce accumulators
139
+ 'accumulator', // for reduce accumulators
140
+ 'e', // for e.returnvalue
141
+ 'ctx', // for Koa routing
142
+ 'context', // for Koa routing
143
+ 'req', // for Express requests
144
+ 'request', // for Express requests
145
+ 'res', // for Express responses
146
+ 'response', // for Express responses
147
+ '$scope', // for Angular 1 scopes
148
+ 'staticContext' // for ReactRouter context
149
+ ]
150
+ }],
151
+
152
+ // 优先链式编程
153
+ 'prefer-object-spread': 'error',
154
+
155
+ // 优先函数式编程
156
+ 'prefer-arrow-callback': 'error',
157
+
158
+ // 方法长度限制
159
+ 'max-lines-per-function': ['warn', {
160
+ max: 40,
161
+ skipBlankLines: true,
162
+ skipComments: true
163
+ }],
164
+
165
+ // 复杂度限制
166
+ 'complexity': ['warn', 10]
167
+ }
168
+ },
169
+
170
+ {
171
+ // JSDoc规则 - 只应用于模块代码文件
172
+ files: ['**/*.js'], // 检查所有JS文件
173
+ ignores: ['eslint.config.js', 'tests/**'], // 忽略配置文件和tests目录
174
+ plugins: {
175
+ 'jsdoc': require('eslint-plugin-jsdoc')
176
+ },
177
+ rules: {
178
+ // 要求公开方法有JSDoc注释
179
+ 'jsdoc/require-jsdoc': ['warn', {
180
+ publicOnly: true,
181
+ require: {
182
+ FunctionDeclaration: true,
183
+ MethodDefinition: true,
184
+ ClassDeclaration: true,
185
+ ArrowFunctionExpression: false,
186
+ FunctionExpression: false
187
+ }
188
+ }],
189
+
190
+ // 检查JSDoc语法
191
+ 'jsdoc/check-alignment': 'warn',
192
+ 'jsdoc/check-indentation': 'warn',
193
+ 'jsdoc/check-param-names': 'warn',
194
+ 'jsdoc/check-syntax': 'warn',
195
+ 'jsdoc/check-tag-names': 'warn',
196
+ 'jsdoc/check-types': 'warn',
197
+ 'jsdoc/no-undefined-types': 'warn',
198
+ 'jsdoc/require-description': 'warn',
199
+ 'jsdoc/require-param': 'warn',
200
+ 'jsdoc/require-param-description': 'warn',
201
+ 'jsdoc/require-param-name': 'warn',
202
+ 'jsdoc/require-param-type': 'warn',
203
+ 'jsdoc/require-returns': 'warn',
204
+ 'jsdoc/require-returns-check': 'warn',
205
+ 'jsdoc/require-returns-description': 'warn',
206
+ 'jsdoc/require-returns-type': 'warn',
207
+ 'jsdoc/valid-types': 'warn'
208
+ }
209
+ },
210
+
211
+ {
212
+ // 忽略规则
213
+ ignores: [
214
+ 'node_modules/**',
215
+ 'dist/**',
216
+ 'build/**',
217
+ 'coverage/**',
218
+ '*.min.js'
219
+ ]
220
+ }
221
+ ];