@tmsfe/tmskit 0.0.5 → 0.0.6

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.
Files changed (42) hide show
  1. package/README.md +18 -2
  2. package/dist/index.cjs.js +1852 -1050
  3. package/package.json +24 -6
  4. package/src/config/constant.js +5 -2
  5. package/src/config/defaultTmsConfig.js +9 -10
  6. package/src/entry.js +48 -8
  7. package/src/gulp/build.js +5 -0
  8. package/src/gulp/compile.js +87 -0
  9. package/src/gulp/dev.js +102 -0
  10. package/src/gulp/plugins/less.js +116 -0
  11. package/src/gulp/plugins/mpCommonDep.js +131 -0
  12. package/src/gulp/plugins/mpJsonDep.js +108 -0
  13. package/src/gulp/plugins/mpWxmlDep.js +194 -0
  14. package/src/gulp/plugins/postcss-font-base64.js +72 -0
  15. package/src/gulp/plugins/replaceEnv.js +29 -0
  16. package/src/gulp/plugins/utils/pluginError.js +25 -0
  17. package/src/index.js +24 -9
  18. package/src/init.js +0 -5
  19. package/src/scripts/run/build/index.js +5 -4
  20. package/src/scripts/run/dev/index.js +30 -13
  21. package/src/scripts/run/index.js +30 -25
  22. package/src/scripts/run/init/index.js +31 -41
  23. package/src/scripts/run/install/index.js +19 -29
  24. package/src/utils/buildAppJson.js +100 -23
  25. package/src/utils/checkDependencies.js +6 -6
  26. package/src/utils/cloneModules.js +39 -13
  27. package/src/utils/findCssImport.js +30 -0
  28. package/src/utils/handleError.js +16 -0
  29. package/src/utils/io.js +85 -0
  30. package/src/utils/mpCiUtils.js +0 -1
  31. package/src/utils/npmUtils.js +77 -37
  32. package/src/utils/tkitUtils.js +90 -16
  33. package/src/utils/widgets.js +11 -14
  34. package/CHANGELOG.md +0 -0
  35. package/rollup.config.js +0 -179
  36. package/src/webpack/base.js +0 -65
  37. package/src/webpack/build.js +0 -21
  38. package/src/webpack/buildServer.js +0 -34
  39. package/src/webpack/dev.js +0 -31
  40. package/src/webpack/devServer.js +0 -37
  41. package/src/webpack/plugins/entryExtractPlugin/index.js +0 -28
  42. package/src/webpack/utils.js +0 -244
package/package.json CHANGED
@@ -1,11 +1,17 @@
1
1
  {
2
2
  "name": "@tmsfe/tmskit",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "tmskit",
5
5
  "main": "main.js",
6
6
  "bin": {
7
7
  "tmskit": "main.js"
8
8
  },
9
+ "files": [
10
+ "src",
11
+ "dist",
12
+ "main.js",
13
+ "package.json"
14
+ ],
9
15
  "scripts": {
10
16
  "dev": "rollup -wc --environment TARGET:tmskit",
11
17
  "build": "rollup -c --environment TARGET:tmskit"
@@ -31,25 +37,37 @@
31
37
  "dependencies": {
32
38
  "async": "^3.2.2",
33
39
  "chalk": "^4.1.0",
34
- "commander": "^6.2.1",
40
+ "commander": "^8.3.0",
35
41
  "copy-webpack-plugin": "^9.1.0",
36
42
  "download-git-repo": "^3.0.2",
37
43
  "ejs": "^3.1.5",
44
+ "fs-extra": "^10.0.1",
38
45
  "glob-ignore": "^1.0.2",
46
+ "gulp": "^4.0.2",
47
+ "gulp-cache": "^1.1.3",
48
+ "gulp-image": "^5.1.0",
49
+ "gulp-postcss": "^9.0.1",
50
+ "gulp-px-to-rpx": "^1.0.7",
51
+ "gulp-watch": "^5.0.1",
52
+ "htmlparser2": "^7.2.0",
39
53
  "inquirer": "^7.3.3",
40
54
  "leven": "3.1.0",
41
55
  "lodash": "^4.17.21",
42
56
  "metalsmith": "^2.3.0",
43
57
  "miniprogram-ci": "1.4.13",
58
+ "object-assign": "^4.0.1",
44
59
  "ora": "^5.1.0",
60
+ "plugin-error": "^1.0.0",
61
+ "postcss": "^8.4.6",
62
+ "precinct": "^8.3.1",
45
63
  "replace-ext": "^2.0.0",
46
64
  "shelljs": "^0.8.4",
47
- "ts-loader": "^9.2.6",
48
- "webpack": "^5.64.0",
49
- "webpack-chain": "^6.5.1"
65
+ "strip-comments": "^2.0.1",
66
+ "through2": "^4.0.2",
67
+ "vinyl-sourcemaps-apply": "^0.2.0"
50
68
  },
51
69
  "engines": {
52
- "node": "^12.17.0 || >= 14.0.0"
70
+ "node": "^12.17.0 || >= 14.13.1"
53
71
  },
54
72
  "jest": {
55
73
  "testEnvironment": "jest-environment-node"
@@ -21,6 +21,7 @@ const TMS_NAME = 'tmskit';
21
21
  // 脚手架的配置名称
22
22
  const TMS_CONFIG_FILENAME = 'tms.config.js';
23
23
 
24
+ const TMS_PRIVATE_FILENAME = 'tms.private.config.js';
24
25
 
25
26
  // 模块代码的默认在modules子目录
26
27
  const DEFAULT_MODULE_DIR = 'modules';
@@ -34,11 +35,10 @@ const DEFAULT_WEBPACK_ENTRY = {
34
35
  };
35
36
 
36
37
  // 默认从源码拷贝到编译后的配置
37
- const DEFAULT_COPY_CONFIG = ['package.json', 'app.js', 'app.ts', 'app.less', 'app.wxss', 'sitemap.json'];
38
+ const DEFAULT_COPY_CONFIG = ['package.json', 'sitemap.json'];
38
39
 
39
40
  // 开发模式
40
41
  const MODE = {
41
- main: 'tkitmain',
42
42
  all: 'all',
43
43
  multi: 'multi',
44
44
  };
@@ -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,
@@ -57,6 +58,7 @@ export {
57
58
  TEMPLATE_PATH,
58
59
  TMS_NAME,
59
60
  TMS_CONFIG_FILENAME,
61
+ TMS_PRIVATE_FILENAME,
60
62
  DEFAULT_MODULE_DIR,
61
63
  MODULE_CONFIG_FILENAME,
62
64
  DEFAULT_WEBPACK_ENTRY,
@@ -65,4 +67,5 @@ export {
65
67
  MODE,
66
68
  ENV,
67
69
  TEMPLATE_TKIT_DIR,
70
+ MODULE_CONFIG_INVALID_KEY,
68
71
  };
@@ -4,14 +4,13 @@ module.exports = {
4
4
  envData: {},
5
5
  // 模块配置信息
6
6
  modules: [],
7
- webpack: {
8
- /** 编译输出文件夹位置 */
9
- outputDir: 'dist',
10
- /** 源码监听路径 */
11
- sourceDir: './',
12
- /** 源码Map */
13
- sourceMap: 'none',
14
- /** 别名 */
15
- alias: {},
16
- },
7
+ cloudDir: 'cloud',
8
+ // 第三方依赖代码需要拷贝到本项目的
9
+ dependencies: {},
10
+ /** 编译输出文件夹位置 */
11
+ outputDir: 'dist',
12
+ /** 源码监听路径 */
13
+ sourceDir: './',
14
+ /** 静态资源目录 */
15
+ static: [],
17
16
  };
package/src/entry.js CHANGED
@@ -7,14 +7,54 @@ module.exports = [
7
7
  },
8
8
  },
9
9
  {
10
- command: 'run <command-name>',
11
- options: [
12
- ['-m, --module [moduleName]', '模块名称'],
13
- ['-e, --env [env]', '环境变量'],
10
+ name: 'run',
11
+ type: 'child',
12
+ description: '项目开发使用的命令',
13
+ commands: [
14
+ {
15
+ command: 'install',
16
+ description: '安装依赖',
17
+ options: [
18
+ ['-m, --module [moduleName]', '模块名称'],
19
+ ['-e, --env [env]', '环境变量'],
20
+ ],
21
+ action: (cmd) => {
22
+ require('./scripts/run/index')('install', cmd);
23
+ },
24
+ },
25
+ {
26
+ command: 'dev',
27
+ description: 'dev 打包编译',
28
+ options: [
29
+ ['-m, --module [moduleName]', '模块名称'],
30
+ ['-e, --env [env]', '环境变量'],
31
+ ],
32
+ action: (cmd) => {
33
+ require('./scripts/run/index')('dev', cmd);
34
+ },
35
+ },
36
+ {
37
+ command: 'build',
38
+ description: 'prod 打包编译',
39
+ options: [
40
+ ['-m, --module [moduleName]', '模块名称'],
41
+ ['-e, --env [env]', '环境变量'],
42
+ ],
43
+ action: (cmd) => {
44
+ require('./scripts/run/index')('build', cmd);
45
+ },
46
+ },
47
+ {
48
+ command: 'init',
49
+ description: '根据模块配置初始化项目(eg: 动态拷贝模块、下载依赖、生成app.json等)',
50
+ options: [
51
+ ['-m, --module [moduleName]', '模块名称'],
52
+ ['-e, --env [env]', '环境变量'],
53
+ ],
54
+ action: (cmd) => {
55
+ require('./scripts/run/index')('init', cmd);
56
+ },
57
+ },
14
58
  ],
15
- description: '运行模块',
16
- action: (commandName, cmd) => {
17
- require('./scripts/run/index')(commandName, cmd);
18
- },
19
59
  },
20
60
  ];
@@ -0,0 +1,5 @@
1
+ const dev = require('./dev');
2
+
3
+ module.exports = async (tmsConfig, newModules) => {
4
+ dev(tmsConfig, newModules, false);
5
+ };
@@ -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('./plugins/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
+ };
@@ -0,0 +1,102 @@
1
+ const watch = require('gulp-watch');
2
+ const { resolve } = require('../utils/widgets');
3
+ const { buildOutputAppJson } = require('../utils/buildAppJson');
4
+ const { DEFAULT_COPY_CONFIG } = require('../config/constant');
5
+ const compile = require('./compile');
6
+
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) => {
27
+ // 监听app.json
28
+ if (isWatch) {
29
+ watch(resolve('app.json'), { ignoreInitial: false, events: ['add', 'change'] }, () => {
30
+ buildOutputAppJson(tmsConfig, newModules, isWatch);
31
+ });
32
+ } else {
33
+ buildOutputAppJson(tmsConfig, newModules, isWatch);
34
+ }
35
+
36
+ // 监听根目录的文件
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.outputDir),
45
+ srcOption: { allowEmpty: true },
46
+ isWatch,
47
+ });
48
+
49
+ // 监听模块的文件
50
+ for (let module of newModules) {
51
+ // 处理默认参数
52
+ module = {
53
+ ...{ exclude: [] },
54
+ ...module,
55
+ };
56
+
57
+ if (isWatch) {
58
+ // 监听模块配置文件
59
+ watch(`${resolve(module.path)}/**/module.config.json`, { events: ['change'] }, () => {
60
+ buildOutputAppJson(tmsConfig, newModules, isWatch);
61
+ });
62
+ }
63
+
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
+ };
72
+
73
+ compile(tmsConfig, {
74
+ glob: {
75
+ ...glob,
76
+ other: [`${resolve(module.path)}/**/*`, ...excludeGlob(glob)],
77
+ },
78
+ destPath: resolve(tmsConfig.outputDir, module.root),
79
+ module: { from: module.path, to: module.root },
80
+ srcOption: { allowEmpty: true },
81
+ isWatch,
82
+ });
83
+ }
84
+
85
+ // 静态资源目录-拷贝
86
+ if (tmsConfig?.static && tmsConfig?.static.length > 0) {
87
+ for (const item of tmsConfig.static) {
88
+ const from = item?.from.startsWith('/') ? item.from : resolve(item.from);
89
+ const to = item?.to.startsWith('/') ? item.to : resolve(item.to);
90
+
91
+ compile(tmsConfig, {
92
+ glob: {
93
+ other: [from],
94
+ },
95
+ destPath: to,
96
+ module: item,
97
+ srcOption: { allowEmpty: true },
98
+ isWatch,
99
+ });
100
+ }
101
+ }
102
+ };
@@ -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.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.dependencies).forEach((includeName) => {
71
+ const includePath = tmsConfig.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.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).replace(/\\/g, '/'),
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 = Buffer.from(contents);
120
+ }
121
+ this.push(file);
122
+ cb();
123
+ });
124
+
125
+ return stream;
126
+ }
127
+
128
+ module.exports = {
129
+ mpCommonDep,
130
+ dfsFindCommonDep,
131
+ };