@tmsfe/tmskit 0.0.5-beta.3 → 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/README.md +16 -2
- package/dist/index.cjs.js +802 -339
- package/package.json +11 -4
- package/src/config/constant.js +2 -0
- package/src/config/defaultTmsConfig.js +2 -2
- package/src/gulp/compile.js +33 -14
- package/src/gulp/dev.js +11 -10
- package/src/gulp/plugins/less.js +116 -0
- package/src/gulp/plugins/mpCommonDep.js +131 -0
- package/src/gulp/plugins/mpJsonDep.js +108 -0
- package/src/gulp/plugins/mpWxmlDep.js +194 -0
- package/src/gulp/plugins/postcss-font-base64.js +72 -0
- package/src/gulp/plugins/utils/pluginError.js +25 -0
- package/src/utils/buildAppJson.js +58 -2
- package/src/utils/findCssImport.js +30 -0
- package/src/utils/io.js +43 -3
- package/src/webpack/base.js +65 -0
- package/src/webpack/build.js +21 -0
- package/src/webpack/buildServer.js +34 -0
- package/src/webpack/dev.js +31 -0
- package/src/webpack/devServer.js +37 -0
- package/src/webpack/plugins/entryExtractPlugin/index.js +28 -0
- package/src/webpack/utils.js +244 -0
- package/CHANGELOG.md +0 -0
- package/rollup.config.js +0 -179
- package/src/gulp/jsDep.js +0 -150
- package/src/gulp/mpJsonDep.js +0 -122
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tmsfe/tmskit",
|
|
3
|
-
"version": "0.0.5-beta.
|
|
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,21 +38,27 @@
|
|
|
37
38
|
"ejs": "^3.1.5",
|
|
38
39
|
"glob-ignore": "^1.0.2",
|
|
39
40
|
"gulp": "^4.0.2",
|
|
41
|
+
"gulp-cache": "^1.1.3",
|
|
40
42
|
"gulp-image": "^5.1.0",
|
|
41
|
-
"gulp-
|
|
43
|
+
"gulp-postcss": "^9.0.1",
|
|
42
44
|
"gulp-px-to-rpx": "^1.0.7",
|
|
43
|
-
"gulp-rename": "^2.0.0",
|
|
44
45
|
"gulp-watch": "^5.0.1",
|
|
46
|
+
"htmlparser2": "^7.2.0",
|
|
45
47
|
"inquirer": "^7.3.3",
|
|
46
48
|
"leven": "3.1.0",
|
|
47
49
|
"lodash": "^4.17.21",
|
|
48
50
|
"metalsmith": "^2.3.0",
|
|
49
51
|
"miniprogram-ci": "1.4.13",
|
|
52
|
+
"object-assign": "^4.0.1",
|
|
50
53
|
"ora": "^5.1.0",
|
|
54
|
+
"plugin-error": "^1.0.0",
|
|
55
|
+
"postcss": "^8.4.6",
|
|
51
56
|
"precinct": "^8.3.1",
|
|
52
57
|
"replace-ext": "^2.0.0",
|
|
53
58
|
"shelljs": "^0.8.4",
|
|
54
|
-
"
|
|
59
|
+
"strip-comments": "^2.0.1",
|
|
60
|
+
"through2": "^4.0.2",
|
|
61
|
+
"vinyl-sourcemaps-apply": "^0.2.0"
|
|
55
62
|
},
|
|
56
63
|
"engines": {
|
|
57
64
|
"node": "^12.17.0 || >= 14.13.1"
|
package/src/config/constant.js
CHANGED
|
@@ -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
|
};
|
package/src/gulp/compile.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
const { src, dest } = require('gulp');
|
|
1
|
+
const { src, dest, lastRun } = require('gulp');
|
|
2
2
|
const px2rpx = require('gulp-px-to-rpx');
|
|
3
|
-
const
|
|
4
|
-
const rename = require('gulp-rename');
|
|
3
|
+
const postcss = require('gulp-postcss');
|
|
5
4
|
const watch = require('gulp-watch');
|
|
5
|
+
const cache = require('gulp-cache');
|
|
6
6
|
const image = require('gulp-image');
|
|
7
7
|
const replaceEnv = require('./replaceEnv');
|
|
8
|
-
const {
|
|
9
|
-
const { mpJsonDep } = require('./mpJsonDep');
|
|
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');
|
|
10
13
|
|
|
14
|
+
const since = task => file => (lastRun(task) > file.stat.ctime ? lastRun(task) : 0);
|
|
11
15
|
module.exports = function (
|
|
12
16
|
tmsConfig,
|
|
13
17
|
{
|
|
@@ -21,7 +25,9 @@ module.exports = function (
|
|
|
21
25
|
) {
|
|
22
26
|
Object.keys(glob).forEach((globKey) => {
|
|
23
27
|
const globValue = glob[globKey];
|
|
24
|
-
|
|
28
|
+
const task = () => src(globValue, { ...srcOption, since: since(task) });
|
|
29
|
+
let srcPipe = task();
|
|
30
|
+
|
|
25
31
|
if (isWatch) {
|
|
26
32
|
srcPipe = srcPipe.pipe(watch(globValue, watchOption));
|
|
27
33
|
}
|
|
@@ -30,31 +36,44 @@ module.exports = function (
|
|
|
30
36
|
case 'js':
|
|
31
37
|
srcPipe
|
|
32
38
|
.pipe(replaceEnv(/process\.env(\.(\w*))?/g, tmsConfig.envData))
|
|
33
|
-
.pipe(
|
|
34
|
-
.pipe(dest(destPath))
|
|
39
|
+
.pipe(mpCommonDep(tmsConfig, module, ['.js', '.ts', '.wxs', '.json'], isWatch))
|
|
40
|
+
.pipe(dest(destPath))
|
|
41
|
+
.on('error', (err) => {
|
|
42
|
+
fail(`js编译报错${err}`);
|
|
43
|
+
});
|
|
35
44
|
break;
|
|
36
|
-
case '
|
|
45
|
+
case 'wxss':
|
|
37
46
|
srcPipe
|
|
38
|
-
.pipe(less
|
|
47
|
+
.pipe(mpCommonDep(tmsConfig, module, ['.wxss', '.less'], isWatch))
|
|
48
|
+
.pipe(postcss([base64()]))
|
|
49
|
+
.on('error', (err) => {
|
|
50
|
+
fail(`postcss编译报错${err}`);
|
|
51
|
+
})
|
|
39
52
|
.pipe(px2rpx({
|
|
40
53
|
designWidth: 375, // 设计稿宽度,默认为750
|
|
41
54
|
precision: 2, // 小数最大精度,默认为6
|
|
42
55
|
}))
|
|
43
|
-
.pipe(rename({ extname: '.wxss' }))
|
|
44
56
|
.pipe(dest(destPath));
|
|
45
57
|
break;
|
|
46
58
|
case 'json':
|
|
47
59
|
srcPipe
|
|
48
|
-
.pipe(mpJsonDep(tmsConfig, module))
|
|
60
|
+
.pipe(mpJsonDep(tmsConfig, module, ['.json'], ['.wxml', '.json', '.js', '.ts', '.wxss', '.less'], isWatch))
|
|
61
|
+
.on('error', (err) => {
|
|
62
|
+
fail(`json编译报错${err}`);
|
|
63
|
+
})
|
|
49
64
|
.pipe(dest(destPath));
|
|
50
65
|
break;
|
|
51
|
-
case '
|
|
66
|
+
case 'wxml':
|
|
52
67
|
srcPipe
|
|
68
|
+
.pipe(mpWxmlDep(tmsConfig, module, isWatch))
|
|
53
69
|
.pipe(dest(destPath));
|
|
54
70
|
break;
|
|
55
71
|
case 'image':
|
|
56
72
|
srcPipe
|
|
57
|
-
.pipe(image())
|
|
73
|
+
.pipe(cache(image()))
|
|
74
|
+
.on('error', (err) => {
|
|
75
|
+
fail(`image编译报错${err}`);
|
|
76
|
+
})
|
|
58
77
|
.pipe(dest(destPath));
|
|
59
78
|
break;
|
|
60
79
|
case 'other':
|
package/src/gulp/dev.js
CHANGED
|
@@ -4,8 +4,9 @@ const { buildOutputAppJson } = require('../utils/buildAppJson');
|
|
|
4
4
|
const { DEFAULT_COPY_CONFIG } = require('../config/constant');
|
|
5
5
|
const compile = require('./compile');
|
|
6
6
|
|
|
7
|
-
function
|
|
7
|
+
function excludeGlob(glob) {
|
|
8
8
|
const otherArr = new Set();
|
|
9
|
+
otherArr.add('!**/*.{ttf,otf,woff,eot}');
|
|
9
10
|
Object.keys(glob).forEach((globKey) => {
|
|
10
11
|
if (typeof glob[globKey] === 'string') {
|
|
11
12
|
const data = glob[globKey].startsWith('!') ? glob[globKey] : `!${glob[globKey]}`;
|
|
@@ -36,7 +37,7 @@ module.exports = async (tmsConfig, newModules, isWatch = true) => {
|
|
|
36
37
|
compile(tmsConfig, {
|
|
37
38
|
glob: {
|
|
38
39
|
json: DEFAULT_COPY_CONFIG.map(item => resolve(item)),
|
|
39
|
-
|
|
40
|
+
wxss: ['app.less', 'app.wxss'].map(item => resolve(item)),
|
|
40
41
|
js: ['app.js', 'app.ts'].map(item => resolve(item)),
|
|
41
42
|
},
|
|
42
43
|
module: { from: '', to: '' },
|
|
@@ -64,15 +65,15 @@ module.exports = async (tmsConfig, newModules, isWatch = true) => {
|
|
|
64
65
|
const glob = {
|
|
65
66
|
js: [`${resolve(module.path)}/**/*.{js,ts,wxs}`, ...excludes],
|
|
66
67
|
json: [`${resolve(module.path)}/**/*.json`, ...excludes],
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
wxss: [`${resolve(module.path)}/**/*.{less,wxss}`, ...excludes],
|
|
69
|
+
wxml: [`${resolve(module.path)}/**/*.wxml`, ...excludes],
|
|
69
70
|
image: [`${resolve(module.path)}/**/*.{png,jpg,jpeg,gif,svg}`, ...excludes],
|
|
70
71
|
};
|
|
71
72
|
|
|
72
73
|
compile(tmsConfig, {
|
|
73
74
|
glob: {
|
|
74
75
|
...glob,
|
|
75
|
-
other: [`${resolve(module.path)}/**/*`, ...
|
|
76
|
+
other: [`${resolve(module.path)}/**/*`, ...excludeGlob(glob)],
|
|
76
77
|
},
|
|
77
78
|
destPath: resolve(tmsConfig.gulp.outputDir, module.root),
|
|
78
79
|
module: { from: module.path, to: module.root },
|
|
@@ -82,20 +83,20 @@ module.exports = async (tmsConfig, newModules, isWatch = true) => {
|
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
// 监听copy模块
|
|
85
|
-
if (tmsConfig?.gulp?.
|
|
86
|
-
for (const item of tmsConfig.gulp?.
|
|
86
|
+
if (tmsConfig?.gulp?.static && tmsConfig?.gulp?.static.length > 0) {
|
|
87
|
+
for (const item of tmsConfig.gulp?.static) {
|
|
87
88
|
const glob = {
|
|
88
89
|
js: `${item.from}/**/*.{js,ts,wxs}`,
|
|
89
90
|
json: `${item.from}/**/*.json`,
|
|
90
|
-
|
|
91
|
-
|
|
91
|
+
wxss: `${item.from}/**/*.{less,wxss}`,
|
|
92
|
+
wxml: `${item.from}/**/*.wxml`,
|
|
92
93
|
image: `${item.from}/**/*.{png,jpg,jpeg,gif,svg}`,
|
|
93
94
|
};
|
|
94
95
|
|
|
95
96
|
compile(tmsConfig, {
|
|
96
97
|
glob: {
|
|
97
98
|
...glob,
|
|
98
|
-
other: [`${item.from}/**/*`, ...
|
|
99
|
+
other: [`${item.from}/**/*`, ...excludeGlob(glob)],
|
|
99
100
|
},
|
|
100
101
|
destPath: item.to,
|
|
101
102
|
module: item,
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
const through = require('through2');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { ext, fileInDir, diffContentCopyFile } = require('../../utils/io');
|
|
5
|
+
const { resolve } = require('../../utils/widgets');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const { pluginError } = require('./utils/pluginError');
|
|
8
|
+
const { dfsFindCommonDep } = require('./mpCommonDep');
|
|
9
|
+
|
|
10
|
+
function mpJsonDep(
|
|
11
|
+
tmsConfig,
|
|
12
|
+
module,
|
|
13
|
+
extensions = ['.json'],
|
|
14
|
+
filesExt = ['.wxml', '.json', '.js', '.ts', '.wxss', '.less'],
|
|
15
|
+
isWatch,
|
|
16
|
+
) {
|
|
17
|
+
const stream = through.obj(function (file, enc, cb) {
|
|
18
|
+
// 当前分析的文件的路径
|
|
19
|
+
const anaFileOriginFile = file.history[0];
|
|
20
|
+
const anaFileRelativeModule = path.relative(resolve(module.from), anaFileOriginFile);
|
|
21
|
+
const anaFileDestFile = resolve(tmsConfig.gulp.outputDir, module.to, anaFileRelativeModule);
|
|
22
|
+
|
|
23
|
+
if (file.isBuffer()) {
|
|
24
|
+
const copyModules = new Map();
|
|
25
|
+
let contents = String(file.contents);
|
|
26
|
+
try {
|
|
27
|
+
contents = JSON.parse(contents);
|
|
28
|
+
if (contents.usingComponents) {
|
|
29
|
+
Object.keys(tmsConfig.gulp.dependencies).forEach((includeName) => {
|
|
30
|
+
const includePath = tmsConfig.gulp.dependencies[includeName];
|
|
31
|
+
Object.keys(contents.usingComponents).forEach((componentKey) => {
|
|
32
|
+
const componentPath = contents.usingComponents[componentKey];
|
|
33
|
+
if (componentPath.indexOf(includeName) > -1) {
|
|
34
|
+
const depOriginPath = path.join(path.dirname(anaFileOriginFile), componentPath);
|
|
35
|
+
// 被依赖文件加上后缀
|
|
36
|
+
const { ext: extAlias, file: depOriginFile, extPath } = ext(depOriginPath, extensions);
|
|
37
|
+
|
|
38
|
+
const isFileInDir = fileInDir(includePath, depOriginFile);
|
|
39
|
+
if (!isFileInDir) {
|
|
40
|
+
pluginError(
|
|
41
|
+
new Error(`${anaFileOriginFile}引用的路径${depOriginFile}不在${includePath}不在文件夹内, 请检查应用路径`),
|
|
42
|
+
isWatch,
|
|
43
|
+
);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// eslint-disable-next-line
|
|
48
|
+
const reg = new RegExp(`^(\.\.\/)+.*\/${includeName}\/(.*)`);
|
|
49
|
+
const regRes = componentPath.match(reg) || [];
|
|
50
|
+
if (regRes[2]) {
|
|
51
|
+
const depDestPath = resolve(tmsConfig.gulp.outputDir, module.to, includeName, regRes[2]);
|
|
52
|
+
const depDestFile = depDestPath.endsWith(extAlias) ? depDestPath : depDestPath + extPath;
|
|
53
|
+
|
|
54
|
+
if (!copyModules.has(depDestFile)) {
|
|
55
|
+
copyModules.set(depDestFile, {
|
|
56
|
+
depOriginFile,
|
|
57
|
+
depOriginExt: extAlias,
|
|
58
|
+
depDestFile,
|
|
59
|
+
beforeDepPath: componentPath,
|
|
60
|
+
afterDepPath: path.relative(path.dirname(anaFileDestFile), depDestPath),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
} catch (e) {
|
|
69
|
+
pluginError(e, isWatch);
|
|
70
|
+
}
|
|
71
|
+
// console.log('json copyModules', copyModules);
|
|
72
|
+
copyModules.forEach(({ depOriginFile, depOriginExt, depDestFile, beforeDepPath, afterDepPath }) => {
|
|
73
|
+
// 拷贝当前依赖组件几个部分 wxml、wxss、wxs、json
|
|
74
|
+
filesExt.forEach((extKey) => {
|
|
75
|
+
const originFile = depOriginFile.replace(depOriginExt, extKey);
|
|
76
|
+
const destFile = depDestFile.replace(depOriginExt, extKey);
|
|
77
|
+
|
|
78
|
+
if (fs.existsSync(originFile)) {
|
|
79
|
+
diffContentCopyFile(originFile, destFile);
|
|
80
|
+
|
|
81
|
+
const extensionsFilter = ['.js', '.ts', '.wxss', '.less'];
|
|
82
|
+
if (extensionsFilter.indexOf(extKey) > -1) {
|
|
83
|
+
const defs = dfsFindCommonDep(originFile, destFile, extensionsFilter);
|
|
84
|
+
// console.log('json defs', defs);
|
|
85
|
+
defs.forEach((item) => {
|
|
86
|
+
diffContentCopyFile(item.depOriginFile, item.depDestFile);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
contents = typeof contents === 'object' ? JSON.stringify(contents, null, 2) : contents;
|
|
92
|
+
const reg = new RegExp(`['"]${beforeDepPath}["']`, 'g');
|
|
93
|
+
contents = contents.replace(reg, `"${afterDepPath}"`);
|
|
94
|
+
});
|
|
95
|
+
contents = typeof contents === 'object' ? JSON.stringify(contents, null, 2) : contents;
|
|
96
|
+
file.contents = new Buffer(contents);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this.push(file);
|
|
100
|
+
cb();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return stream;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
module.exports = {
|
|
107
|
+
mpJsonDep,
|
|
108
|
+
};
|