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

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,11 +1,12 @@
1
1
  {
2
2
  "name": "@tmsfe/tmskit",
3
- "version": "0.0.5-beta.0",
3
+ "version": "0.0.5-beta.4",
4
4
  "description": "tmskit",
5
5
  "main": "main.js",
6
6
  "bin": {
7
7
  "tmskit": "main.js"
8
8
  },
9
+ "files": ["src", "dist", "main.js", "package.json"],
9
10
  "scripts": {
10
11
  "dev": "rollup -wc --environment TARGET:tmskit",
11
12
  "build": "rollup -c --environment TARGET:tmskit"
@@ -37,23 +38,30 @@
37
38
  "ejs": "^3.1.5",
38
39
  "glob-ignore": "^1.0.2",
39
40
  "gulp": "^4.0.2",
40
- "gulp-less": "^5.0.0",
41
+ "gulp-cache": "^1.1.3",
42
+ "gulp-image": "^5.1.0",
43
+ "gulp-postcss": "^9.0.1",
41
44
  "gulp-px-to-rpx": "^1.0.7",
42
- "gulp-rename": "^2.0.0",
43
45
  "gulp-watch": "^5.0.1",
46
+ "htmlparser2": "^7.2.0",
44
47
  "inquirer": "^7.3.3",
45
48
  "leven": "3.1.0",
46
49
  "lodash": "^4.17.21",
47
50
  "metalsmith": "^2.3.0",
48
51
  "miniprogram-ci": "1.4.13",
52
+ "object-assign": "^4.0.1",
49
53
  "ora": "^5.1.0",
54
+ "plugin-error": "^1.0.0",
55
+ "postcss": "^8.4.6",
50
56
  "precinct": "^8.3.1",
51
57
  "replace-ext": "^2.0.0",
52
58
  "shelljs": "^0.8.4",
53
- "through2": "^4.0.2"
59
+ "strip-comments": "^2.0.1",
60
+ "through2": "^4.0.2",
61
+ "vinyl-sourcemaps-apply": "^0.2.0"
54
62
  },
55
63
  "engines": {
56
- "node": "^12.17.0 || >= 14.0.0"
64
+ "node": "^12.17.0 || >= 14.13.1"
57
65
  },
58
66
  "jest": {
59
67
  "testEnvironment": "jest-environment-node"
@@ -49,6 +49,7 @@ const ENV = {
49
49
  };
50
50
 
51
51
  const TEMPLATE_TKIT_DIR = '_tmskit';
52
+ const MODULE_CONFIG_INVALID_KEY = ['entranceDeclare', 'entryPagePath'];
52
53
 
53
54
  export {
54
55
  HOME_DIR,
@@ -65,4 +66,5 @@ export {
65
66
  MODE,
66
67
  ENV,
67
68
  TEMPLATE_TKIT_DIR,
69
+ MODULE_CONFIG_INVALID_KEY,
68
70
  };
@@ -6,11 +6,13 @@ module.exports = {
6
6
  modules: [],
7
7
  cloudDir: 'cloud',
8
8
  gulp: {
9
+ // 第三方依赖代码需要拷贝到本项目的
10
+ dependencies: {},
9
11
  /** 编译输出文件夹位置 */
10
12
  outputDir: 'dist',
11
13
  /** 源码监听路径 */
12
14
  sourceDir: './',
13
- /** 源码Map */
14
- copyDir: [],
15
+ /** 静态资源目录 */
16
+ static: [],
15
17
  },
16
18
  };
package/src/gulp/build.js CHANGED
@@ -1,78 +1,8 @@
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');
1
+ const shelljs = require('shelljs');
2
+ const dev = require('./dev');
10
3
 
11
4
  module.exports = async (tmsConfig, newModules) => {
12
- // app.json
13
- buildOutputAppJson(tmsConfig, newModules);
5
+ shelljs.rm('-rf', tmsConfig.gulp.outputDir);
14
6
 
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)));
45
-
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
- }
7
+ dev(tmsConfig, newModules, false);
78
8
  };
@@ -0,0 +1,87 @@
1
+ const { src, dest, lastRun } = require('gulp');
2
+ const px2rpx = require('gulp-px-to-rpx');
3
+ const postcss = require('gulp-postcss');
4
+ const watch = require('gulp-watch');
5
+ const cache = require('gulp-cache');
6
+ const image = require('gulp-image');
7
+ const replaceEnv = require('./replaceEnv');
8
+ const { mpCommonDep } = require('./plugins/mpCommonDep');
9
+ const { mpJsonDep } = require('./plugins/mpJsonDep');
10
+ const { mpWxmlDep } = require('./plugins/mpWxmlDep');
11
+ const base64 = require('./plugins/postcss-font-base64');
12
+ const { fail } = require('../utils/log');
13
+
14
+ const since = task => file => (lastRun(task) > file.stat.ctime ? lastRun(task) : 0);
15
+ module.exports = function (
16
+ tmsConfig,
17
+ {
18
+ glob,
19
+ destPath,
20
+ srcOption,
21
+ module,
22
+ watchOption = { events: ['change', 'add', 'unlink'] },
23
+ isWatch,
24
+ },
25
+ ) {
26
+ Object.keys(glob).forEach((globKey) => {
27
+ const globValue = glob[globKey];
28
+ const task = () => src(globValue, { ...srcOption, since: since(task) });
29
+ let srcPipe = task();
30
+
31
+ if (isWatch) {
32
+ srcPipe = srcPipe.pipe(watch(globValue, watchOption));
33
+ }
34
+
35
+ switch (globKey) {
36
+ case 'js':
37
+ srcPipe
38
+ .pipe(replaceEnv(/process\.env(\.(\w*))?/g, tmsConfig.envData))
39
+ .pipe(mpCommonDep(tmsConfig, module, ['.js', '.ts', '.wxs', '.json'], isWatch))
40
+ .pipe(dest(destPath))
41
+ .on('error', (err) => {
42
+ fail(`js编译报错${err}`);
43
+ });
44
+ break;
45
+ case 'wxss':
46
+ srcPipe
47
+ .pipe(mpCommonDep(tmsConfig, module, ['.wxss', '.less'], isWatch))
48
+ .pipe(postcss([base64()]))
49
+ .on('error', (err) => {
50
+ fail(`postcss编译报错${err}`);
51
+ })
52
+ .pipe(px2rpx({
53
+ designWidth: 375, // 设计稿宽度,默认为750
54
+ precision: 2, // 小数最大精度,默认为6
55
+ }))
56
+ .pipe(dest(destPath));
57
+ break;
58
+ case 'json':
59
+ srcPipe
60
+ .pipe(mpJsonDep(tmsConfig, module, ['.json'], ['.wxml', '.json', '.js', '.ts', '.wxss', '.less'], isWatch))
61
+ .on('error', (err) => {
62
+ fail(`json编译报错${err}`);
63
+ })
64
+ .pipe(dest(destPath));
65
+ break;
66
+ case 'wxml':
67
+ srcPipe
68
+ .pipe(mpWxmlDep(tmsConfig, module, isWatch))
69
+ .pipe(dest(destPath));
70
+ break;
71
+ case 'image':
72
+ srcPipe
73
+ .pipe(cache(image()))
74
+ .on('error', (err) => {
75
+ fail(`image编译报错${err}`);
76
+ })
77
+ .pipe(dest(destPath));
78
+ break;
79
+ case 'other':
80
+ srcPipe
81
+ .pipe(dest(destPath));
82
+ break;
83
+ default:
84
+ break;
85
+ }
86
+ });
87
+ };
package/src/gulp/dev.js CHANGED
@@ -1,94 +1,108 @@
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
+ function excludeGlob(glob) {
8
+ const otherArr = new Set();
9
+ otherArr.add('!**/*.{ttf,otf,woff,eot}');
10
+ Object.keys(glob).forEach((globKey) => {
11
+ if (typeof glob[globKey] === 'string') {
12
+ const data = glob[globKey].startsWith('!') ? glob[globKey] : `!${glob[globKey]}`;
13
+ otherArr.add(data);
14
+ } if (Array.isArray(glob[globKey])) {
15
+ glob[globKey].forEach((value) => {
16
+ if (typeof value === 'string') {
17
+ const data = value.startsWith('!') ? value : `!${value}`;
18
+ otherArr.add(data);
19
+ }
20
+ });
21
+ }
22
+ });
23
+ return Array.from(otherArr);
24
+ }
25
+
26
+ module.exports = async (tmsConfig, newModules, isWatch = true) => {
13
27
  // 监听app.json
14
- watch(resolve('app.json'), { ignoreInitial: false, events: ['add', 'change'] }, () => {
28
+ if (isWatch) {
29
+ watch(resolve('app.json'), { ignoreInitial: false, events: ['add', 'change'] }, () => {
30
+ buildOutputAppJson(tmsConfig, newModules);
31
+ });
32
+ } else {
15
33
  buildOutputAppJson(tmsConfig, newModules);
16
- });
34
+ }
17
35
 
18
36
  // 监听根目录的文件
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
- }
37
+ compile(tmsConfig, {
38
+ glob: {
39
+ json: DEFAULT_COPY_CONFIG.map(item => resolve(item)),
40
+ wxss: ['app.less', 'app.wxss'].map(item => resolve(item)),
41
+ js: ['app.js', 'app.ts'].map(item => resolve(item)),
42
+ },
43
+ module: { from: '', to: '' },
44
+ destPath: resolve(tmsConfig.gulp.outputDir),
45
+ srcOption: { allowEmpty: true },
46
+ isWatch,
25
47
  });
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
48
 
41
49
  // 监听模块的文件
42
- 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)));
50
+ for (let module of newModules) {
51
+ // 处理默认参数
52
+ module = {
53
+ ...{ exclude: [] },
54
+ ...module,
55
+ };
52
56
 
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)));
57
+ if (isWatch) {
58
+ // 监听模块配置文件
59
+ watch(`${resolve(module.path)}/**/module.config.json`, { events: ['change'] }, () => {
60
+ buildOutputAppJson(tmsConfig, newModules);
61
+ });
62
+ }
73
63
 
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)));
64
+ const excludes = module.exclude.map(key => `!${resolve(key)}`);
65
+ const glob = {
66
+ js: [`${resolve(module.path)}/**/*.{js,ts,wxs}`, ...excludes],
67
+ json: [`${resolve(module.path)}/**/*.json`, ...excludes],
68
+ wxss: [`${resolve(module.path)}/**/*.{less,wxss}`, ...excludes],
69
+ wxml: [`${resolve(module.path)}/**/*.wxml`, ...excludes],
70
+ image: [`${resolve(module.path)}/**/*.{png,jpg,jpeg,gif,svg}`, ...excludes],
71
+ };
80
72
 
81
- // 监听模块配置文件
82
- watch(`${resolve(module.path)}/**/module.config.json`, { events: ['change'] }, () => {
83
- buildOutputAppJson(tmsConfig, newModules);
73
+ compile(tmsConfig, {
74
+ glob: {
75
+ ...glob,
76
+ other: [`${resolve(module.path)}/**/*`, ...excludeGlob(glob)],
77
+ },
78
+ destPath: resolve(tmsConfig.gulp.outputDir, module.root),
79
+ module: { from: module.path, to: module.root },
80
+ srcOption: { allowEmpty: true },
81
+ isWatch,
84
82
  });
85
83
  }
84
+
86
85
  // 监听copy模块
87
- if (tmsConfig?.gulp?.copyDir.length > 0) {
88
- for (const item of tmsConfig.gulp?.copyDir) {
89
- src([item.from])
90
- .pipe(watch(item.from, { events: ['add', 'change'] }))
91
- .pipe(dest(item.to));
86
+ if (tmsConfig?.gulp?.static && tmsConfig?.gulp?.static.length > 0) {
87
+ for (const item of tmsConfig.gulp?.static) {
88
+ const glob = {
89
+ js: `${item.from}/**/*.{js,ts,wxs}`,
90
+ json: `${item.from}/**/*.json`,
91
+ wxss: `${item.from}/**/*.{less,wxss}`,
92
+ wxml: `${item.from}/**/*.wxml`,
93
+ image: `${item.from}/**/*.{png,jpg,jpeg,gif,svg}`,
94
+ };
95
+
96
+ compile(tmsConfig, {
97
+ glob: {
98
+ ...glob,
99
+ other: [`${item.from}/**/*`, ...excludeGlob(glob)],
100
+ },
101
+ destPath: item.to,
102
+ module: item,
103
+ srcOption: { allowEmpty: true },
104
+ isWatch,
105
+ });
92
106
  }
93
107
  }
94
108
  };
@@ -0,0 +1,116 @@
1
+ /* eslint-disable no-param-reassign */
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const less = require('less');
5
+ const through2 = require('through2');
6
+ const replaceExt = require('replace-ext');
7
+ const assign = require('object-assign');
8
+ const applySourceMap = require('vinyl-sourcemaps-apply');
9
+ const PluginError = require('plugin-error');
10
+ const { fail } = require('../../utils/log');
11
+
12
+ function inlineSources(map) {
13
+ if (map.sourcesContent) {
14
+ return Promise.resolve(map);
15
+ }
16
+
17
+ return Promise.all(map.sources.map(source => new Promise((resolve, reject) => {
18
+ fs.readFile(source, 'utf8', (err, data) => {
19
+ if (err) {
20
+ reject(err);
21
+ } else {
22
+ resolve(data);
23
+ }
24
+ });
25
+ }))).then(
26
+ (contents) => {
27
+ map.sourcesContent = contents;
28
+ return map;
29
+ },
30
+ () => map,
31
+ );
32
+ }
33
+
34
+ function renderLess(str, opts) {
35
+ return new Promise((resolve, reject) => {
36
+ less.render(str, opts, (err, res) => {
37
+ if (err) {
38
+ reject(err);
39
+ } else {
40
+ const obj = {
41
+ result: res.css,
42
+ imports: res.imports,
43
+ };
44
+ if (opts.sourceMap && res.map) {
45
+ obj.sourcemap = JSON.parse(res.map);
46
+ inlineSources(obj.sourcemap).then((map) => {
47
+ obj.sourcemap = map;
48
+ resolve(obj);
49
+ });
50
+ } else {
51
+ resolve(obj);
52
+ }
53
+ }
54
+ });
55
+ });
56
+ }
57
+
58
+ module.exports = function (options) {
59
+ const opts = assign({}, {
60
+ compress: false,
61
+ paths: [],
62
+ isWatch: false,
63
+ }, options);
64
+
65
+ return through2.obj((file, enc, cb) => {
66
+ if (file.isNull()) {
67
+ return cb(null, file);
68
+ }
69
+
70
+ if (file.isStream()) {
71
+ return cb(new PluginError('gulp-less', 'Streaming not supported'));
72
+ }
73
+
74
+ const str = file.contents.toString();
75
+
76
+ // Injects the path of the current file
77
+ opts.filename = file.path;
78
+
79
+ // Bootstrap source maps
80
+ if (file.sourceMap || opts.sourcemap) {
81
+ opts.sourceMap = true;
82
+ }
83
+
84
+ renderLess(str, opts).then((res) => {
85
+ file.contents = Buffer.from(res.result);
86
+ file.path = replaceExt(file.path, '.wxss');
87
+ if (res.sourcemap) {
88
+ res.sourcemap.file = file.relative;
89
+ res.sourcemap.sources = res.sourcemap.sources.map(source => path.relative(file.base, source));
90
+
91
+ applySourceMap(file, res.sourcemap);
92
+ }
93
+ return file;
94
+ })
95
+ .then((file) => {
96
+ console.log(`less编译完成${file.path}`);
97
+ cb(null, file);
98
+ })
99
+ .catch((err) => {
100
+ // Convert the keys so PluginError can read them
101
+ err.lineNumber = err.line;
102
+ err.fileName = err.filename;
103
+
104
+ // Add a better error message
105
+ const message = `${err.message} in file ${err.fileName} line no. ${err.lineNumber}`;
106
+ err.message = message;
107
+ if (opts.isWatch) {
108
+ // watch的时候,报错提示,不退出进程
109
+ fail(err);
110
+ return cb(null, file);
111
+ }
112
+ // 不watch的时候,报错退出进程
113
+ return cb(new PluginError('gulp-less', err));
114
+ });
115
+ });
116
+ };
@@ -0,0 +1,131 @@
1
+ /* eslint-disable no-param-reassign */
2
+ const through = require('through2');
3
+ const precinct = require('precinct');
4
+ const path = require('path');
5
+ const { findCssImports } = require('../../utils/findCssImport');
6
+ const { ext, fileInDir, diffContentCopyFile } = require('../../utils/io');
7
+ const { resolve } = require('../../utils/widgets');
8
+ const fs = require('fs');
9
+ const { pluginError } = require('./utils/pluginError');
10
+
11
+ const cssFilter = ['.less', '.wxss'];
12
+ const dfsFindCommonDep = function (anaFileOriginFile, anaFileDestFile, extensions, isWatch = true) {
13
+ const resDep = new Map();
14
+ function dfs(anaFileOriginFile, anaFileDestFile, extensions) {
15
+ let contents = '';
16
+ try {
17
+ contents = fs.readFileSync(anaFileOriginFile, 'utf8');
18
+ } catch (e) {
19
+ pluginError(e, isWatch);
20
+ }
21
+ const deps = cssFilter.indexOf(path.extname(anaFileOriginFile)) > -1
22
+ ? findCssImports(contents)
23
+ : precinct(contents);
24
+ deps.forEach((depItem) => {
25
+ if (depItem.startsWith('.')) {
26
+ // 被依赖文件的存在的绝对路径
27
+ const depOriginPath = path.join(path.dirname(anaFileOriginFile), depItem);
28
+ // 被依赖文件加上后缀
29
+ const { ext: extAlias, file: depOriginFile, extPath } = ext(depOriginPath, extensions);
30
+
31
+ if (!fs.existsSync(depOriginFile)) {
32
+ pluginError(
33
+ new Error(`${anaFileOriginFile}引用路径${depOriginFile}文件不存在, 请检查应用路径`),
34
+ isWatch,
35
+ );
36
+ return;
37
+ }
38
+
39
+ const depDestPath = resolve(path.dirname(anaFileDestFile), depItem);
40
+ const depDestFile = depDestPath.endsWith(extAlias) ? depDestPath : depDestPath + extPath;
41
+ if (!resDep.has(depDestFile)) {
42
+ resDep.set(depDestFile, {
43
+ anaFileOriginFile,
44
+ anaFileDestFile,
45
+ depDestFile,
46
+ depOriginFile,
47
+ });
48
+ dfs(depOriginFile, depDestFile, extensions);
49
+ }
50
+ }
51
+ });
52
+ }
53
+ dfs(anaFileOriginFile, anaFileDestFile, extensions);
54
+
55
+ return resDep;
56
+ };
57
+
58
+ function mpCommonDep(tmsConfig, module, extensions = [], isWatch = true) {
59
+ const stream = through.obj(function (file, enc, cb) {
60
+ // 依赖分析的文件
61
+ const anaFileOriginFile = file.history[0];
62
+ const anaFileRelativeModule = path.relative(resolve(module.from), anaFileOriginFile);
63
+ const anaFileDestFile = resolve(tmsConfig.gulp.outputDir, module.to, anaFileRelativeModule);
64
+
65
+ if (file.isBuffer()) {
66
+ let contents = String(file.contents);
67
+ const deps = cssFilter.indexOf(path.extname(file.path)) > -1 ? findCssImports(contents) : precinct(contents);
68
+
69
+ const copyModules = new Map();
70
+ Object.keys(tmsConfig.gulp.dependencies).forEach((includeName) => {
71
+ const includePath = tmsConfig.gulp.dependencies[includeName];
72
+ deps.forEach((depItem) => {
73
+ if (depItem.indexOf(includeName) > -1) {
74
+ // 被依赖文件的存在的绝对路径 (eg: /User/thirdparty/loadsh)
75
+ const depOriginPath = path.join(path.dirname(anaFileOriginFile), depItem);
76
+ // 被依赖文件加上后缀
77
+ const { ext: extAlias, file: depOriginFile, extPath } = ext(depOriginPath, extensions);
78
+
79
+ if (!fileInDir(includePath, depOriginFile)) {
80
+ pluginError(
81
+ new Error(`${anaFileOriginFile}引用路径${depOriginFile}不在${includePath}不在文件夹内, 请检查应用路径`),
82
+ isWatch,
83
+ );
84
+ return;
85
+ }
86
+
87
+ // eslint-disable-next-line
88
+ const reg = new RegExp(`^(\.\.\/)+.*\/${includeName}\/(.*)`);
89
+ const regRes = depItem.match(reg) || [];
90
+ if (regRes[2]) {
91
+ const depDestPath = resolve(tmsConfig.gulp.outputDir, module.to, includeName, regRes[2]);
92
+ const depDestFile = depDestPath.endsWith(extAlias) ? depDestPath : depDestPath + extPath;
93
+ if (!copyModules.has(depDestFile)) {
94
+ copyModules.set(depDestFile, {
95
+ depOriginFile,
96
+ depDestFile,
97
+ beforeDepPath: depItem,
98
+ afterDepPath: path.relative(path.dirname(anaFileDestFile), depDestPath),
99
+ });
100
+ }
101
+ }
102
+ }
103
+ });
104
+ });
105
+
106
+ // console.log('mpCommonDep copyModules', copyModules);
107
+ copyModules.forEach(({ depOriginFile, depDestFile, beforeDepPath, afterDepPath }) => {
108
+ diffContentCopyFile(depOriginFile, depDestFile);
109
+ const reg = new RegExp(`['"]${beforeDepPath}["']`, 'g');
110
+ contents = contents.replace(reg, `"${afterDepPath}"`);
111
+
112
+ const defs = dfsFindCommonDep(depOriginFile, depDestFile, extensions, isWatch);
113
+ defs.forEach((item) => {
114
+ diffContentCopyFile(item.depOriginFile, item.depDestFile);
115
+ });
116
+ // console.log('mpCommonDep defs', defs);
117
+ });
118
+
119
+ file.contents = new Buffer(contents);
120
+ }
121
+ this.push(file);
122
+ cb();
123
+ });
124
+
125
+ return stream;
126
+ }
127
+
128
+ module.exports = {
129
+ mpCommonDep,
130
+ dfsFindCommonDep,
131
+ };