@tmsfe/tmskit 0.0.5-beta.0 → 0.0.5-beta.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tmskit",
3
- "version": "0.0.5-beta.0",
3
+ "version": "0.0.5-beta.1",
4
4
  "description": "tmskit",
5
5
  "main": "main.js",
6
6
  "bin": {
@@ -37,6 +37,7 @@
37
37
  "ejs": "^3.1.5",
38
38
  "glob-ignore": "^1.0.2",
39
39
  "gulp": "^4.0.2",
40
+ "gulp-image": "^5.1.0",
40
41
  "gulp-less": "^5.0.0",
41
42
  "gulp-px-to-rpx": "^1.0.7",
42
43
  "gulp-rename": "^2.0.0",
@@ -53,7 +54,7 @@
53
54
  "through2": "^4.0.2"
54
55
  },
55
56
  "engines": {
56
- "node": "^12.17.0 || >= 14.0.0"
57
+ "node": "^12.17.0 || >= 14.13.1"
57
58
  },
58
59
  "jest": {
59
60
  "testEnvironment": "jest-environment-node"
@@ -6,6 +6,8 @@ module.exports = {
6
6
  modules: [],
7
7
  cloudDir: 'cloud',
8
8
  gulp: {
9
+ // 第三方依赖代码需要拷贝到本项目的
10
+ dependencies: {},
9
11
  /** 编译输出文件夹位置 */
10
12
  outputDir: 'dist',
11
13
  /** 源码监听路径 */
package/src/gulp/build.js CHANGED
@@ -1,78 +1,6 @@
1
- const fs = require('fs');
2
- const { src, dest } = require('gulp');
3
- const px2rpx = require('gulp-px-to-rpx');
4
- const less = require('gulp-less');
5
- const rename = require('gulp-rename');
6
- const { resolve } = require('../utils/widgets');
7
- const { buildOutputAppJson } = require('../utils/buildAppJson');
8
- const { DEFAULT_COPY_CONFIG } = require('../config/constant');
9
- const replaceEnv = require('./replaceEnv');
10
1
 
11
- module.exports = async (tmsConfig, newModules) => {
12
- // app.json
13
- buildOutputAppJson(tmsConfig, newModules);
14
-
15
- // 监听根目录的文件
16
- DEFAULT_COPY_CONFIG.forEach((item) => {
17
- if (fs.existsSync(resolve(item))) {
18
- src(resolve(item))
19
- .pipe(dest(resolve(tmsConfig.gulp.outputDir)));
20
- }
21
- });
22
- src([resolve('app.less'), resolve('app.wxss')], { allowEmpty: true })
23
- .pipe(less())
24
- .pipe(px2rpx({
25
- designWidth: 375, // 设计稿宽度,默认为750
26
- precision: 2, // 小数最大精度,默认为6
27
- }))
28
- .pipe(rename({ extname: '.wxss' }))
29
- .pipe(dest(resolve(tmsConfig.gulp.outputDir)));
30
-
31
- src([resolve('app.js'), resolve('app.ts')], { allowEmpty: true })
32
- .pipe(replaceEnv(/process\.env(\.(\w*))?/g, tmsConfig.envData))
33
- .pipe(dest(resolve(tmsConfig.gulp.outputDir)));
34
-
35
- // 监听模块的文件
36
- for (const module of newModules) {
37
- // 监听模块js文件
38
- src([
39
- `${resolve(module.path)}/**/*.js`,
40
- `${resolve(module.path)}/**/*.ts`,
41
- `${resolve(module.path)}/**/*.wxs`,
42
- ])
43
- .pipe(replaceEnv(/process\.env(\.(\w*))?/g, tmsConfig.envData))
44
- .pipe(dest(resolve(tmsConfig.gulp.outputDir, module.root)));
2
+ const dev = require('./dev');
45
3
 
46
- // 监听json文件
47
- src([
48
- `${resolve(module.path)}/**/*.json`,
49
- ])
50
- .pipe(dest(resolve(tmsConfig.gulp.outputDir, module.root)));
51
-
52
- // 监听css文件
53
- src([
54
- `${resolve(module.path)}/**/*.less`,
55
- `${resolve(module.path)}/**/*.wxss`,
56
- ])
57
- .pipe(less())
58
- .pipe(px2rpx({
59
- designWidth: 375, // 设计稿宽度,默认为750
60
- precision: 2, // 小数最大精度,默认为6
61
- }))
62
- .pipe(rename({ extname: '.wxss' }))
63
- .pipe(dest(resolve(tmsConfig.gulp.outputDir, module.root)));
64
-
65
- // 监听wxml文件
66
- src([
67
- `${resolve(module.path)}/**/*.wxml`,
68
- ])
69
- .pipe(dest(resolve(tmsConfig.gulp.outputDir, module.root)));
70
- }
71
- // 监听copy模块
72
- if (tmsConfig?.gulp?.copyDir.length > 0) {
73
- for (const item of tmsConfig.gulp?.copyDir) {
74
- src([item.from])
75
- .pipe(dest(item.to));
76
- }
77
- }
4
+ module.exports = async (tmsConfig, newModules) => {
5
+ dev(tmsConfig, newModules, false);
78
6
  };
@@ -0,0 +1,67 @@
1
+ const { src, dest } = require('gulp');
2
+ const px2rpx = require('gulp-px-to-rpx');
3
+ const less = require('gulp-less');
4
+ const rename = require('gulp-rename');
5
+ const watch = require('gulp-watch');
6
+ const image = require('gulp-image');
7
+ const replaceEnv = require('./replaceEnv');
8
+ const includeDep = require('./includeDep');
9
+
10
+ module.exports = function (
11
+ tmsConfig,
12
+ {
13
+ glob,
14
+ destPath,
15
+ srcOption,
16
+ module,
17
+ watchOption = { events: ['change', 'add', 'unlink'] },
18
+ isWatch,
19
+ },
20
+ ) {
21
+ Object.keys(glob).forEach((globKey) => {
22
+ const globValue = glob[globKey];
23
+
24
+ let srcPipe = src(globValue, srcOption);
25
+ if (isWatch) {
26
+ srcPipe = srcPipe.pipe(watch(globValue, watchOption));
27
+ }
28
+
29
+ switch (globKey) {
30
+ case 'js':
31
+ srcPipe
32
+ .pipe(replaceEnv(/process\.env(\.(\w*))?/g, tmsConfig.envData))
33
+ .pipe(includeDep(tmsConfig, module))
34
+ .pipe(dest(destPath));
35
+ break;
36
+ case 'css':
37
+ srcPipe
38
+ .pipe(less())
39
+ .pipe(px2rpx({
40
+ designWidth: 375, // 设计稿宽度,默认为750
41
+ precision: 2, // 小数最大精度,默认为6
42
+ }))
43
+ .pipe(rename({ extname: '.wxss' }))
44
+ .pipe(dest(destPath));
45
+ break;
46
+ case 'json':
47
+ srcPipe
48
+ .pipe(dest(destPath));
49
+ break;
50
+ case 'html':
51
+ srcPipe
52
+ .pipe(dest(destPath));
53
+ break;
54
+ case 'image':
55
+ srcPipe
56
+ .pipe(image())
57
+ .pipe(dest(destPath));
58
+ break;
59
+ case 'other':
60
+ srcPipe
61
+ .pipe(dest(destPath));
62
+ break;
63
+ default:
64
+ break;
65
+ }
66
+ });
67
+ };
package/src/gulp/dev.js CHANGED
@@ -1,94 +1,83 @@
1
- const fs = require('fs');
2
- const { src, dest } = require('gulp');
3
- const px2rpx = require('gulp-px-to-rpx');
4
- const less = require('gulp-less');
5
- const rename = require('gulp-rename');
6
1
  const watch = require('gulp-watch');
7
2
  const { resolve } = require('../utils/widgets');
8
3
  const { buildOutputAppJson } = require('../utils/buildAppJson');
9
4
  const { DEFAULT_COPY_CONFIG } = require('../config/constant');
10
- const replaceEnv = require('./replaceEnv');
5
+ const compile = require('./compile');
11
6
 
12
- module.exports = async (tmsConfig, newModules) => {
7
+ module.exports = async (tmsConfig, newModules, isWatch = true) => {
13
8
  // 监听app.json
14
- watch(resolve('app.json'), { ignoreInitial: false, events: ['add', 'change'] }, () => {
9
+ if (isWatch) {
10
+ watch(resolve('app.json'), { ignoreInitial: false, events: ['add', 'change'] }, () => {
11
+ buildOutputAppJson(tmsConfig, newModules);
12
+ });
13
+ } else {
15
14
  buildOutputAppJson(tmsConfig, newModules);
16
- });
15
+ }
17
16
 
18
17
  // 监听根目录的文件
19
- DEFAULT_COPY_CONFIG.forEach((item) => {
20
- if (fs.existsSync(resolve(item))) {
21
- src(resolve(item))
22
- .pipe(watch(resolve(item), { events: ['change'] }))
23
- .pipe(dest(resolve(tmsConfig.gulp.outputDir)));
24
- }
18
+ compile(tmsConfig, {
19
+ glob: {
20
+ json: DEFAULT_COPY_CONFIG.map(item => resolve(item)),
21
+ css: ['app.less', 'app.wxss'].map(item => resolve(item)),
22
+ js: ['app.js', 'app.ts'].map(item => resolve(item)),
23
+ },
24
+ module: { from: '', to: '' },
25
+ destPath: resolve(tmsConfig.gulp.outputDir),
26
+ srcOption: { allowEmpty: true },
27
+ isWatch,
25
28
  });
26
- src([resolve('app.less'), resolve('app.wxss')], { allowEmpty: true })
27
- .pipe(watch(`${resolve()}/(app.less|app.wxss)`, { events: ['change'] }))
28
- .pipe(less())
29
- .pipe(px2rpx({
30
- designWidth: 375, // 设计稿宽度,默认为750
31
- precision: 2, // 小数最大精度,默认为6
32
- }))
33
- .pipe(rename({ extname: '.wxss' }))
34
- .pipe(dest(resolve(tmsConfig.gulp.outputDir)));
35
-
36
- src([resolve('app.js'), resolve('app.ts')], { allowEmpty: true })
37
- .pipe(watch(`${resolve()}/(app.js|app.ts)`, { events: ['change'] }))
38
- .pipe(replaceEnv(/process\.env(\.(\w*))?/g, tmsConfig.envData))
39
- .pipe(dest(resolve(tmsConfig.gulp.outputDir)));
40
29
 
41
30
  // 监听模块的文件
42
31
  for (const module of newModules) {
43
- // 监听模块js文件
44
- src([
45
- `${resolve(module.path)}/**/*.js`,
46
- `${resolve(module.path)}/**/*.ts`,
47
- `${resolve(module.path)}/**/*.wxs`,
48
- ])
49
- .pipe(watch(`${resolve(module.path)}/**/(*.js|*.ts|*.wxs)`, { events: ['add', 'change'] }))
50
- .pipe(replaceEnv(/process\.env(\.(\w*))?/g, tmsConfig.envData))
51
- .pipe(dest(resolve(tmsConfig.gulp.outputDir, module.root)));
52
-
53
- // 监听json文件
54
- src([
55
- `${resolve(module.path)}/**/*.json`,
56
- ])
57
- .pipe(watch(`${resolve(module.path)}/**/*.json`, { events: ['add', 'change'] }))
58
- .pipe(dest(resolve(tmsConfig.gulp.outputDir, module.root)));
59
-
60
- // 监听css文件
61
- src([
62
- `${resolve(module.path)}/**/*.less`,
63
- `${resolve(module.path)}/**/*.wxss`,
64
- ])
65
- .pipe(watch(`${resolve(module.path)}/**/(*.less|*.wxss)`, { events: ['add', 'change'] }))
66
- .pipe(less())
67
- .pipe(px2rpx({
68
- designWidth: 375, // 设计稿宽度,默认为750
69
- precision: 2, // 小数最大精度,默认为6
70
- }))
71
- .pipe(rename({ extname: '.wxss' }))
72
- .pipe(dest(resolve(tmsConfig.gulp.outputDir, module.root)));
73
-
74
- // 监听wxml文件
75
- src([
76
- `${resolve(module.path)}/**/*.wxml`,
77
- ])
78
- .pipe(watch(`${resolve(module.path)}/**/*.wxml`, { events: ['add', 'change'] }))
79
- .pipe(dest(resolve(tmsConfig.gulp.outputDir, module.root)));
80
-
81
- // 监听模块配置文件
82
- watch(`${resolve(module.path)}/**/module.config.json`, { events: ['change'] }, () => {
32
+ if (isWatch) {
33
+ // 监听模块配置文件
34
+ watch(`${resolve(module.path)}/**/module.config.json`, { events: ['change'] }, () => {
35
+ buildOutputAppJson(tmsConfig, newModules);
36
+ });
37
+ } else {
83
38
  buildOutputAppJson(tmsConfig, newModules);
39
+ }
40
+
41
+ const glob = {
42
+ js: `${resolve(module.path)}/**/*.{js,ts,wxs}`,
43
+ json: `${resolve(module.path)}/**/*.json`,
44
+ css: `${resolve(module.path)}/**/*.{less,wxss}`,
45
+ html: `${resolve(module.path)}/**/*.wxml`,
46
+ image: `${resolve(module.path)}/**/*.{png,jpg,jpeg,gif,svg}`,
47
+ };
48
+ compile(tmsConfig, {
49
+ glob: {
50
+ ...glob,
51
+ other: [`${resolve(module.path)}/**/*`, ...Object.keys(glob).map(key => `!${glob[key]}`)],
52
+ },
53
+ destPath: resolve(tmsConfig.gulp.outputDir, module.root),
54
+ module: { from: module.path, to: module.root },
55
+ srcOption: { allowEmpty: true },
56
+ isWatch,
84
57
  });
85
58
  }
59
+
86
60
  // 监听copy模块
87
61
  if (tmsConfig?.gulp?.copyDir.length > 0) {
88
62
  for (const item of tmsConfig.gulp?.copyDir) {
89
- src([item.from])
90
- .pipe(watch(item.from, { events: ['add', 'change'] }))
91
- .pipe(dest(item.to));
63
+ const glob = {
64
+ js: `${item.from}/**/*.{js,ts,wxs}`,
65
+ json: `${item.from}/**/*.json`,
66
+ css: `${item.from}/**/*.{less,wxss}`,
67
+ html: `${item.from}/**/*.wxml`,
68
+ image: `${item.from}/**/*.{png,jpg,jpeg,gif,svg}`,
69
+ };
70
+
71
+ compile(tmsConfig, {
72
+ glob: {
73
+ ...glob,
74
+ other: [`${item.from}/**/*`, ...Object.keys(glob).map(key => `!${glob[key]}`)],
75
+ },
76
+ destPath: item.to,
77
+ module: item,
78
+ srcOption: { allowEmpty: true },
79
+ isWatch,
80
+ });
92
81
  }
93
82
  }
94
83
  };
@@ -0,0 +1,130 @@
1
+ const through = require('through2');
2
+ const precinct = require('precinct');
3
+ const path = require('path');
4
+ const { copyFile, ext, fileInDir } = require('../utils/io');
5
+ const { resolve } = require('../utils/widgets');
6
+ const fs = require('fs');
7
+ const { fail } = require('../utils/log');
8
+
9
+ const dfsDep = function (includePath, anaFileOriginFile, anaFileDestFile, extensions) {
10
+ const contents = fs.readFileSync(anaFileOriginFile, 'utf8');
11
+ const deps = precinct(contents);
12
+ deps.forEach((depItem) => {
13
+ if (depItem.startsWith('.')) {
14
+ // 被依赖文件的存在的绝对路径
15
+ const depOriginPath = path.join(path.dirname(anaFileOriginFile), depItem);
16
+ // 被依赖文件加上后缀
17
+ const depOriginPathExt = ext(depOriginPath, extensions);
18
+
19
+ if (!depOriginPathExt) {
20
+ fail(`${anaFileOriginFile}的${depItem}引用路径存找不到文件,请检查引用路径`);
21
+ }
22
+
23
+ // 被依赖文件是否存在 include的path中 (只处理include的文件)
24
+ if (depOriginPathExt && fileInDir(includePath, depOriginPathExt.file)) {
25
+ const depOriginFile = depOriginPathExt.file;
26
+ const depDestPath = resolve(path.dirname(anaFileDestFile), depItem);
27
+ const depDestFile = depDestPath.endsWith(depOriginPathExt.ext)
28
+ ? depDestPath
29
+ : depDestPath + depOriginPathExt.ext;
30
+
31
+ if (fs.existsSync(depDestFile)) {
32
+ const depDestContent = fs.readFileSync(depDestFile, 'utf8');
33
+ const depOriginContent = fs.readFileSync(depOriginFile, 'utf8');
34
+ if (depDestContent !== depOriginContent) {
35
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
36
+ copyFile(depOriginFile, depDestFile);
37
+ }
38
+ } else {
39
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
40
+ copyFile(depOriginFile, depDestFile);
41
+ }
42
+ dfsDep(includePath, depOriginFile, depDestFile, extensions);
43
+ }
44
+ }
45
+ });
46
+ };
47
+
48
+ function includeThirdParty(tmsConfig, module, extensions = ['.js', '.ts', '.wxs']) {
49
+ const stream = through.obj(function (file, enc, cb) {
50
+ // 依赖分析的文件
51
+ const anaFileOriginFile = file.history[0];
52
+ const anaFileRelativeModule = path.relative(resolve(module.from), anaFileOriginFile);
53
+ const anaFileDestFile = resolve(tmsConfig.gulp.outputDir, module.to, anaFileRelativeModule);
54
+
55
+ if (file.isBuffer()) {
56
+ let contents = String(file.contents);
57
+ const deps = precinct(contents);
58
+ const copyModules = [];
59
+
60
+ Object.keys(tmsConfig.gulp.dependencies).forEach((includeName) => {
61
+ const includePath = tmsConfig.gulp.dependencies[includeName];
62
+ deps.forEach((depItem) => {
63
+ if (depItem.indexOf(includeName) > -1) {
64
+ // 被依赖文件的存在的绝对路径 (eg: /User/thirdparty/loadsh)
65
+ const depOriginPath = path.join(path.dirname(anaFileOriginFile), depItem);
66
+ // 被依赖文件加上后缀
67
+ const depOriginPathExt = ext(depOriginPath, extensions);
68
+
69
+ if (!depOriginPathExt) {
70
+ fail(`${anaFileOriginFile}的${depItem}引用路径存找不到文件,请检查引用路径`);
71
+ }
72
+
73
+ // 被依赖文件是否存在 include的path中 (只处理include的文件)
74
+ if (depOriginPathExt && fileInDir(includePath, depOriginPathExt.file)) {
75
+ // eslint-disable-next-line
76
+ const reg = new RegExp(`^(\.\.\/)+.*\/${includeName}\/(.*)`);
77
+ const regRes = depItem.match(reg) || [];
78
+ if (regRes[2]) {
79
+ const depDestPath = resolve(tmsConfig.gulp.outputDir, module.to, includeName, regRes[2]);
80
+ const depDestFile = depDestPath.endsWith(depOriginPathExt.ext)
81
+ ? depDestPath
82
+ : depDestPath + depOriginPathExt.ext;
83
+ // {
84
+ // depOriginFile: '/Users/odile/workspace/tms-frontend1/miniprogram/thirdparty/libs/timer.js',
85
+ // depDestFile: `/Users/odile/workspace/tms-frontend1/tools/demo1/dist/modules/
86
+ // aggredriving/thirdparty/libs/timer.js`,
87
+ // beforeDepPath: '../../../../thirdparty/libs/timer',
88
+ // afterDepPath: '../../thirdparty/libs/timer'
89
+ // }
90
+ copyModules.push({
91
+ depOriginFile: depOriginPathExt.file,
92
+ depDestFile,
93
+ beforeDepPath: depItem,
94
+ afterDepPath: path.relative(path.dirname(anaFileDestFile), depDestPath),
95
+ includePath,
96
+ });
97
+ }
98
+ }
99
+ }
100
+ });
101
+ });
102
+
103
+ copyModules.forEach(({ includePath, depOriginFile, depDestFile, beforeDepPath, afterDepPath }) => {
104
+ if (fs.existsSync(depDestFile)) {
105
+ const depDestContent = fs.readFileSync(depDestFile, 'utf8');
106
+ const depOriginContent = fs.readFileSync(depOriginFile, 'utf8');
107
+ if (depDestContent !== depOriginContent) {
108
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
109
+ copyFile(depOriginFile, depDestFile);
110
+ }
111
+ } else {
112
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
113
+ copyFile(depOriginFile, depDestFile);
114
+ }
115
+ contents = contents.replace(beforeDepPath, afterDepPath);
116
+ dfsDep(includePath, depOriginFile, depDestFile, extensions);
117
+ });
118
+
119
+ // eslint-disable-next-line
120
+ file.contents = new Buffer(contents);
121
+ }
122
+
123
+ this.push(file);
124
+ cb();
125
+ });
126
+
127
+ return stream;
128
+ }
129
+
130
+ module.exports = includeThirdParty;
@@ -14,6 +14,7 @@ function replaceEnv(reg = /process\.env(\.(\w*))?/g, envData) {
14
14
  contents = contents.replace(reg1, JSON.stringify(envData));
15
15
  }
16
16
  }
17
+
17
18
  // eslint-disable-next-line
18
19
  file.contents = new Buffer(contents);
19
20
  }
package/src/utils/io.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const fs = require('fs');
2
+ const path = require('path');
2
3
  /**
3
4
  * 判断目录是否为空
4
5
  * @param {string} dirname 目录名
@@ -15,7 +16,51 @@ const ensureDirExist = (dirname) => {
15
16
  }
16
17
  };
17
18
 
19
+ // 复制文件
20
+ const copyFile = function (src, dest) {
21
+ if (fs.existsSync(dest)) {
22
+ fs.unlinkSync(dest);
23
+ }
24
+ const dir = dest.substr(0, dest.lastIndexOf('/'));
25
+ ensureDirExist(dir);
26
+ fs.copyFileSync(src, dest);
27
+ };
28
+
29
+ // 添加后缀
30
+ function ext(filePath, extensions) {
31
+ let newFilePath = filePath;
32
+ try {
33
+ const stat = fs.lstatSync(newFilePath);
34
+ if (stat.isDirectory()) {
35
+ newFilePath += (newFilePath[newFilePath.length - 1] === '/') ? 'index' : '/index';
36
+ }
37
+ } catch (e) {}
38
+
39
+ for (const ext of extensions) {
40
+ const file = newFilePath.endsWith(ext) ? newFilePath : newFilePath + ext;
41
+ if (fs.existsSync(file)) {
42
+ return {
43
+ file,
44
+ ext,
45
+ };
46
+ }
47
+ }
48
+ return null;
49
+ }
50
+
51
+ // 判断文件是否在某个目录
52
+ const fileInDir = (dir, file) => {
53
+ const relativePath = path.relative(dir, file);
54
+ if (relativePath.startsWith('..')) {
55
+ return false;
56
+ }
57
+ return true;
58
+ };
59
+
18
60
  module.exports = {
19
61
  isDirEmpty,
62
+ copyFile,
20
63
  ensureDirExist,
64
+ ext,
65
+ fileInDir,
21
66
  };
@@ -111,7 +111,10 @@ const findAllPackageJson = (subRoots = [], contextDir) => {
111
111
 
112
112
 
113
113
  subRoots.forEach((subRoot) => {
114
- console.log('>>>>>>>>>>>>', subRoot);
114
+ if (!subRoot.root) {
115
+ LOG.fail(`请检查${subRoot.name}的module.config.json是否有root字段`);
116
+ process.exit(1);
117
+ }
115
118
  const toppath = path.join(cwd, subRoot.root); // 从该目录开始查找package.json文件
116
119
  const list = findFilesByFilter(toppath, packageJsonName);
117
120