@tmsfe/tmskit 0.0.3 → 0.0.5-beta.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tmskit",
3
- "version": "0.0.3",
3
+ "version": "0.0.5-beta.2",
4
4
  "description": "tmskit",
5
5
  "main": "main.js",
6
6
  "bin": {
@@ -29,43 +29,32 @@
29
29
  "rollup-plugin-typescript2": "0.27.0"
30
30
  },
31
31
  "dependencies": {
32
- "@babel/core": "^7.15.0",
33
- "@babel/plugin-transform-modules-commonjs": "^7.16.0",
34
- "@babel/preset-env": "^7.16.0",
35
32
  "async": "^3.2.2",
36
- "axios": "^0.21.4",
37
- "babel-core": "^6.26.3",
38
- "babel-loader": "^8.2.3",
39
33
  "chalk": "^4.1.0",
40
- "commander": "^6.2.1",
34
+ "commander": "^8.3.0",
41
35
  "copy-webpack-plugin": "^9.1.0",
42
- "cross-env": "^7.0.3",
43
36
  "download-git-repo": "^3.0.2",
44
37
  "ejs": "^3.1.5",
45
- "file-loader": "^6.2.0",
46
- "fs-extra": "^9.0.1",
47
38
  "glob-ignore": "^1.0.2",
39
+ "gulp": "^4.0.2",
40
+ "gulp-image": "^5.1.0",
41
+ "gulp-less": "^5.0.0",
42
+ "gulp-px-to-rpx": "^1.0.7",
43
+ "gulp-rename": "^2.0.0",
44
+ "gulp-watch": "^5.0.1",
48
45
  "inquirer": "^7.3.3",
49
46
  "leven": "3.1.0",
50
47
  "lodash": "^4.17.21",
51
48
  "metalsmith": "^2.3.0",
52
- "mini-css-extract-plugin": "^2.4.5",
53
- "minimist": "^1.2.5",
54
49
  "miniprogram-ci": "1.4.13",
55
50
  "ora": "^5.1.0",
51
+ "precinct": "^8.3.1",
56
52
  "replace-ext": "^2.0.0",
57
53
  "shelljs": "^0.8.4",
58
- "ts-loader": "^9.2.6",
59
- "tslib": "^1.14.1",
60
- "typescript": "^3.8.3",
61
- "url-loader": "^4.1.1",
62
- "username": "5.1.0",
63
- "webpack": "^5.64.0",
64
- "webpack-chain": "^6.5.1",
65
- "webpack-cli": "^4.9.1"
54
+ "through2": "^4.0.2"
66
55
  },
67
56
  "engines": {
68
- "node": "^12.17.0 || >= 14.0.0"
57
+ "node": "^12.17.0 || >= 14.13.1"
69
58
  },
70
59
  "jest": {
71
60
  "testEnvironment": "jest-environment-node"
@@ -34,7 +34,7 @@ const DEFAULT_WEBPACK_ENTRY = {
34
34
  };
35
35
 
36
36
  // 默认从源码拷贝到编译后的配置
37
- const DEFAULT_COPY_CONFIG = ['package.json', 'app.js', 'app.ts', 'app.less', 'app.wxss', 'sitemap.json'];
37
+ const DEFAULT_COPY_CONFIG = ['package.json', 'sitemap.json'];
38
38
 
39
39
  // 开发模式
40
40
  const MODE = {
@@ -4,14 +4,15 @@ module.exports = {
4
4
  envData: {},
5
5
  // 模块配置信息
6
6
  modules: [],
7
- webpack: {
7
+ cloudDir: 'cloud',
8
+ gulp: {
9
+ // 第三方依赖代码需要拷贝到本项目的
10
+ dependencies: {},
8
11
  /** 编译输出文件夹位置 */
9
12
  outputDir: 'dist',
10
13
  /** 源码监听路径 */
11
14
  sourceDir: './',
12
15
  /** 源码Map */
13
- sourceMap: 'none',
14
- /** 别名 */
15
- alias: {},
16
+ copyDir: [],
16
17
  },
17
18
  };
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,6 @@
1
+
2
+ const dev = require('./dev');
3
+
4
+ module.exports = async (tmsConfig, newModules) => {
5
+ dev(tmsConfig, newModules, false);
6
+ };
@@ -0,0 +1,68 @@
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 { jsDep } = require('./jsDep');
9
+ const { mpJsonDep } = require('./mpJsonDep');
10
+
11
+ module.exports = function (
12
+ tmsConfig,
13
+ {
14
+ glob,
15
+ destPath,
16
+ srcOption,
17
+ module,
18
+ watchOption = { events: ['change', 'add', 'unlink'] },
19
+ isWatch,
20
+ },
21
+ ) {
22
+ Object.keys(glob).forEach((globKey) => {
23
+ const globValue = glob[globKey];
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(jsDep(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(mpJsonDep(tmsConfig, module))
49
+ .pipe(dest(destPath));
50
+ break;
51
+ case 'html':
52
+ srcPipe
53
+ .pipe(dest(destPath));
54
+ break;
55
+ case 'image':
56
+ srcPipe
57
+ .pipe(image())
58
+ .pipe(dest(destPath));
59
+ break;
60
+ case 'other':
61
+ srcPipe
62
+ .pipe(dest(destPath));
63
+ break;
64
+ default:
65
+ break;
66
+ }
67
+ });
68
+ };
@@ -0,0 +1,107 @@
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 handleOtherGlob(glob) {
8
+ const otherArr = [];
9
+ Object.keys(glob).forEach((globKey) => {
10
+ if (typeof glob[globKey] === 'string') {
11
+ const data = glob[globKey].startsWith('!') ? glob[globKey] : `!${glob[globKey]}`;
12
+ otherArr.push(data);
13
+ } if (Array.isArray(glob[globKey])) {
14
+ glob[globKey].forEach((value) => {
15
+ if (typeof value === 'string') {
16
+ const data = value.startsWith('!') ? value : `!${value}`;
17
+ otherArr.push(data);
18
+ }
19
+ });
20
+ }
21
+ });
22
+ return otherArr;
23
+ }
24
+
25
+ module.exports = async (tmsConfig, newModules, isWatch = true) => {
26
+ // 监听app.json
27
+ if (isWatch) {
28
+ watch(resolve('app.json'), { ignoreInitial: false, events: ['add', 'change'] }, () => {
29
+ buildOutputAppJson(tmsConfig, newModules);
30
+ });
31
+ } else {
32
+ buildOutputAppJson(tmsConfig, newModules);
33
+ }
34
+
35
+ // 监听根目录的文件
36
+ compile(tmsConfig, {
37
+ glob: {
38
+ json: DEFAULT_COPY_CONFIG.map(item => resolve(item)),
39
+ css: ['app.less', 'app.wxss'].map(item => resolve(item)),
40
+ js: ['app.js', 'app.ts'].map(item => resolve(item)),
41
+ },
42
+ module: { from: '', to: '' },
43
+ destPath: resolve(tmsConfig.gulp.outputDir),
44
+ srcOption: { allowEmpty: true },
45
+ isWatch,
46
+ });
47
+
48
+ // 监听模块的文件
49
+ for (let module of newModules) {
50
+ // 处理默认参数
51
+ module = {
52
+ ...{ excludes: [] },
53
+ ...module,
54
+ };
55
+
56
+ if (isWatch) {
57
+ // 监听模块配置文件
58
+ watch(`${resolve(module.path)}/**/module.config.json`, { events: ['change'] }, () => {
59
+ buildOutputAppJson(tmsConfig, newModules);
60
+ });
61
+ }
62
+
63
+ const excludes = module.exclude.map(key => `!${resolve(key)}`);
64
+ const glob = {
65
+ js: [`${resolve(module.path)}/**/*.{js,ts,wxs}`, ...excludes],
66
+ json: [`${resolve(module.path)}/**/*.json`, ...excludes],
67
+ css: [`${resolve(module.path)}/**/*.{less,wxss}`, ...excludes],
68
+ html: [`${resolve(module.path)}/**/*.wxml`, ...excludes],
69
+ image: [`${resolve(module.path)}/**/*.{png,jpg,jpeg,gif,svg}`, ...excludes],
70
+ };
71
+
72
+ compile(tmsConfig, {
73
+ glob: {
74
+ ...glob,
75
+ other: [`${resolve(module.path)}/**/*`, ...handleOtherGlob(glob)],
76
+ },
77
+ destPath: resolve(tmsConfig.gulp.outputDir, module.root),
78
+ module: { from: module.path, to: module.root },
79
+ srcOption: { allowEmpty: true },
80
+ isWatch,
81
+ });
82
+ }
83
+
84
+ // 监听copy模块
85
+ if (tmsConfig?.gulp?.copyDir.length > 0) {
86
+ for (const item of tmsConfig.gulp?.copyDir) {
87
+ const glob = {
88
+ js: `${item.from}/**/*.{js,ts,wxs}`,
89
+ json: `${item.from}/**/*.json`,
90
+ css: `${item.from}/**/*.{less,wxss}`,
91
+ html: `${item.from}/**/*.wxml`,
92
+ image: `${item.from}/**/*.{png,jpg,jpeg,gif,svg}`,
93
+ };
94
+
95
+ compile(tmsConfig, {
96
+ glob: {
97
+ ...glob,
98
+ other: [`${item.from}/**/*`, ...handleOtherGlob(glob)],
99
+ },
100
+ destPath: item.to,
101
+ module: item,
102
+ srcOption: { allowEmpty: true },
103
+ isWatch,
104
+ });
105
+ }
106
+ }
107
+ };
@@ -0,0 +1,150 @@
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 dfsFindJsDep = function (anaFileOriginFile, anaFileDestFile, extensions) {
10
+ const resDep = new Map();
11
+ function dfs(anaFileOriginFile, anaFileDestFile, extensions) {
12
+ const contents = fs.readFileSync(anaFileOriginFile, 'utf8');
13
+ const deps = precinct(contents);
14
+ deps.forEach((depItem) => {
15
+ if (depItem.startsWith('.')) {
16
+ // 被依赖文件的存在的绝对路径
17
+ const depOriginPath = path.join(path.dirname(anaFileOriginFile), depItem);
18
+ // 被依赖文件加上后缀
19
+ const depOriginPathExt = ext(depOriginPath, extensions);
20
+
21
+ if (!depOriginPathExt) {
22
+ fail(`${anaFileOriginFile}的${depItem}引用路径存找不到文件,请检查引用路径`);
23
+ }
24
+
25
+ // 被依赖文件是否存在 include的path中 (只处理include的文件)
26
+ if (depOriginPathExt) {
27
+ const depOriginFile = depOriginPathExt.file;
28
+ const depDestPath = resolve(path.dirname(anaFileDestFile), depItem);
29
+ const depDestFile = depDestPath.endsWith(depOriginPathExt.ext)
30
+ ? depDestPath
31
+ : depDestPath + depOriginPathExt.ext;
32
+
33
+ if (!resDep.has(depDestFile)) {
34
+ resDep.set(depDestFile, {
35
+ anaFileOriginFile,
36
+ anaFileDestFile,
37
+ depDestFile,
38
+ depOriginFile,
39
+ });
40
+ dfs(depOriginFile, depDestFile, extensions);
41
+ }
42
+ }
43
+ }
44
+ });
45
+ }
46
+ dfs(anaFileOriginFile, anaFileDestFile, extensions);
47
+
48
+ return resDep;
49
+ };
50
+
51
+ function handleCopyFile(depDestFile, depOriginFile) {
52
+ if (fs.existsSync(depDestFile)) {
53
+ const depDestContent = fs.readFileSync(depDestFile, 'utf8');
54
+ const depOriginContent = fs.readFileSync(depOriginFile, 'utf8');
55
+ if (depDestContent !== depOriginContent) {
56
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
57
+ copyFile(depOriginFile, depDestFile);
58
+ }
59
+ } else {
60
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
61
+ copyFile(depOriginFile, depDestFile);
62
+ }
63
+ }
64
+
65
+ function jsDep(tmsConfig, module, extensions = ['.js', '.ts', '.wxs']) {
66
+ const stream = through.obj(function (file, enc, cb) {
67
+ // 依赖分析的文件
68
+ const anaFileOriginFile = file.history[0];
69
+ const anaFileRelativeModule = path.relative(resolve(module.from), anaFileOriginFile);
70
+ const anaFileDestFile = resolve(tmsConfig.gulp.outputDir, module.to, anaFileRelativeModule);
71
+
72
+ if (file.isBuffer()) {
73
+ let contents = String(file.contents);
74
+ const deps = precinct(contents);
75
+ const copyModules = new Map();
76
+
77
+ Object.keys(tmsConfig.gulp.dependencies).forEach((includeName) => {
78
+ const includePath = tmsConfig.gulp.dependencies[includeName];
79
+ deps.forEach((depItem) => {
80
+ if (depItem.indexOf(includeName) > -1) {
81
+ // 被依赖文件的存在的绝对路径 (eg: /User/thirdparty/loadsh)
82
+ const depOriginPath = path.join(path.dirname(anaFileOriginFile), depItem);
83
+ // 被依赖文件加上后缀
84
+ const depOriginPathExt = ext(depOriginPath, extensions);
85
+
86
+ if (!depOriginPathExt) {
87
+ fail(`${anaFileOriginFile}的${depItem}引用路径存找不到文件,请检查引用路径`);
88
+ }
89
+
90
+ const isFileInDir = fileInDir(includePath, depOriginPathExt.file);
91
+ if (!isFileInDir) {
92
+ fail(`${depOriginPathExt.file}不在${includePath}不在文件夹内, 请检查应用路径`);
93
+ }
94
+
95
+ // 被依赖文件是否存在 include的path中 (只处理include的文件)
96
+ if (depOriginPathExt && isFileInDir) {
97
+ // eslint-disable-next-line
98
+ const reg = new RegExp(`^(\.\.\/)+.*\/${includeName}\/(.*)`);
99
+ const regRes = depItem.match(reg) || [];
100
+ if (regRes[2]) {
101
+ const depDestPath = resolve(tmsConfig.gulp.outputDir, module.to, includeName, regRes[2]);
102
+ const depDestFile = depDestPath.endsWith(depOriginPathExt.ext)
103
+ ? depDestPath
104
+ : depDestPath + depOriginPathExt.ext;
105
+ // {
106
+ // depOriginFile: '/Users/odile/workspace/tms-frontend1/miniprogram/thirdparty/libs/timer.js',
107
+ // depDestFile: `/Users/odile/workspace/tms-frontend1/tools/demo1/dist/modules/
108
+ // aggredriving/thirdparty/libs/timer.js`,
109
+ // beforeDepPath: '../../../../thirdparty/libs/timer',
110
+ // afterDepPath: '../../thirdparty/libs/timer'
111
+ // }
112
+ if (!copyModules.has(depDestFile)) {
113
+ copyModules.set(depDestFile, {
114
+ depOriginFile: depOriginPathExt.file,
115
+ depDestFile,
116
+ beforeDepPath: depItem,
117
+ afterDepPath: path.relative(path.dirname(anaFileDestFile), depDestPath),
118
+ });
119
+ }
120
+ }
121
+ }
122
+ }
123
+ });
124
+ });
125
+
126
+ copyModules.forEach(({ depOriginFile, depDestFile, beforeDepPath, afterDepPath }) => {
127
+ handleCopyFile(depDestFile, depOriginFile);
128
+ contents = contents.replace(beforeDepPath, afterDepPath);
129
+
130
+ const defs = dfsFindJsDep(depOriginFile, depDestFile, extensions);
131
+ defs.forEach((item) => {
132
+ handleCopyFile(item.depDestFile, item.depOriginFile);
133
+ });
134
+ });
135
+
136
+ // eslint-disable-next-line
137
+ file.contents = new Buffer(contents);
138
+ }
139
+
140
+ this.push(file);
141
+ cb();
142
+ });
143
+
144
+ return stream;
145
+ }
146
+
147
+ module.exports = {
148
+ jsDep,
149
+ dfsFindJsDep,
150
+ };
@@ -0,0 +1,122 @@
1
+ const through = require('through2');
2
+ const path = require('path');
3
+ const { copyFile, ext, fileInDir } = require('../utils/io');
4
+ const { resolve } = require('../utils/widgets');
5
+ const fs = require('fs');
6
+ const { fail } = require('../utils/log');
7
+ const { dfsFindJsDep } = require('./jsDep');
8
+
9
+ function handleCopyFile(depDestFile, depOriginFile) {
10
+ if (fs.existsSync(depDestFile)) {
11
+ const depDestContent = fs.readFileSync(depDestFile, 'utf8');
12
+ const depOriginContent = fs.readFileSync(depOriginFile, 'utf8');
13
+ if (depDestContent !== depOriginContent) {
14
+ console.log(`json拷贝${depOriginFile}内容到${depDestFile}`);
15
+ copyFile(depOriginFile, depDestFile);
16
+ }
17
+ } else {
18
+ console.log(`json拷贝${depOriginFile}内容到${depDestFile}`);
19
+ copyFile(depOriginFile, depDestFile);
20
+ }
21
+ }
22
+
23
+ function mpJsonDep(
24
+ tmsConfig,
25
+ module,
26
+ extensions = ['.wxml'],
27
+ filesExt = ['.wxml', '.json', '.js', '.ts', '.wxss', '.less'],
28
+ ) {
29
+ const stream = through.obj(function (file, enc, cb) {
30
+ // 当前分析的文件的路径
31
+ const anaFileOriginFile = file.history[0];
32
+ const anaFileRelativeModule = path.relative(resolve(module.from), anaFileOriginFile);
33
+ const anaFileDestFile = resolve(tmsConfig.gulp.outputDir, module.to, anaFileRelativeModule);
34
+
35
+ if (file.isBuffer()) {
36
+ const copyModules = new Map();
37
+ let contents = String(file.contents);
38
+ try {
39
+ contents = JSON.parse(contents);
40
+ if (contents.usingComponents) {
41
+ Object.keys(tmsConfig.gulp.dependencies).forEach((includeName) => {
42
+ const includePath = tmsConfig.gulp.dependencies[includeName];
43
+ Object.keys(contents.usingComponents).forEach((componentKey) => {
44
+ const componentPath = contents.usingComponents[componentKey];
45
+ if (componentPath.indexOf(includeName) > -1) {
46
+ const depOriginPath = path.join(path.dirname(anaFileOriginFile), componentPath);
47
+ // 被依赖文件加上后缀
48
+ const depOriginPathExt = ext(depOriginPath, extensions);
49
+
50
+ if (!depOriginPathExt) {
51
+ fail(`${anaFileOriginFile}的${componentPath}引用路径存找不到文件,请检查引用路径`);
52
+ }
53
+
54
+ const isFileInDir = fileInDir(includePath, depOriginPathExt.file);
55
+
56
+ if (!isFileInDir) {
57
+ fail(`${depOriginPathExt.file}不在${includePath}不在文件夹内, 请检查应用路径`);
58
+ }
59
+
60
+ if (depOriginPathExt && isFileInDir) {
61
+ // eslint-disable-next-line
62
+ const reg = new RegExp(`^(\.\.\/)+.*\/${includeName}\/(.*)`);
63
+ const regRes = componentPath.match(reg) || [];
64
+ if (regRes[2]) {
65
+ const depDestPath = resolve(tmsConfig.gulp.outputDir, module.to, includeName, regRes[2]);
66
+ const depDestFile = depDestPath.endsWith(depOriginPathExt.ext)
67
+ ? depDestPath
68
+ : depDestPath + depOriginPathExt.ext;
69
+
70
+ if (!copyModules.has(depDestFile)) {
71
+ copyModules.set(depDestFile, {
72
+ depOriginFile: depOriginPathExt.file,
73
+ depOriginExt: depOriginPathExt.ext,
74
+ depDestFile,
75
+ beforeDepPath: componentPath,
76
+ afterDepPath: path.relative(path.dirname(anaFileDestFile), depDestPath),
77
+ });
78
+ }
79
+ }
80
+ }
81
+ }
82
+ });
83
+ });
84
+ }
85
+ } catch (e) {
86
+ fail(`解析jsonDep出现错误:${e}`);
87
+ }
88
+ copyModules.forEach(({ depOriginFile, depOriginExt, depDestFile, beforeDepPath, afterDepPath }) => {
89
+ filesExt.forEach((extKey) => {
90
+ const originFile = depOriginFile.replace(depOriginExt, extKey);
91
+ const destFile = depDestFile.replace(depOriginExt, extKey);
92
+
93
+ if (fs.existsSync(originFile)) {
94
+ handleCopyFile(destFile, originFile);
95
+
96
+ const jsExtensions = ['.js', '.ts'];
97
+ if (jsExtensions.indexOf(extKey) > -1) {
98
+ const defs = dfsFindJsDep(originFile, destFile, jsExtensions);
99
+ defs.forEach((item) => {
100
+ handleCopyFile(item.depDestFile, item.depOriginFile);
101
+ });
102
+ }
103
+ }
104
+ });
105
+ contents = typeof contents === 'object' ? JSON.stringify(contents, null, 2) : contents;
106
+ contents = contents.replace(beforeDepPath, afterDepPath);
107
+ });
108
+ contents = typeof contents === 'object' ? JSON.stringify(contents, null, 2) : contents;
109
+ // eslint-disable-next-line
110
+ file.contents = new Buffer(contents);
111
+ }
112
+
113
+ this.push(file);
114
+ cb();
115
+ });
116
+
117
+ return stream;
118
+ }
119
+
120
+ module.exports = {
121
+ mpJsonDep,
122
+ };
@@ -0,0 +1,29 @@
1
+ const through = require('through2');
2
+ function replaceEnv(reg = /process\.env(\.(\w*))?/g, envData) {
3
+ const stream = through.obj(function (file, enc, cb) {
4
+ if (file.isBuffer()) {
5
+ let contents = String(file.contents);
6
+ let resReg;
7
+ // eslint-disable-next-line
8
+ while ((resReg = reg.exec(contents)) !== null) {
9
+ const [reg1, reg2, reg3] = resReg;
10
+ if (reg1 && reg2 && reg3) {
11
+ const value = typeof envData[reg3] === 'object' ? JSON.stringify(envData[reg3]) : envData[reg3];
12
+ contents = contents.replace(reg1, value);
13
+ } else if (reg1 && !reg2 && !reg3) {
14
+ contents = contents.replace(reg1, JSON.stringify(envData));
15
+ }
16
+ }
17
+
18
+ // eslint-disable-next-line
19
+ file.contents = new Buffer(contents);
20
+ }
21
+
22
+ this.push(file);
23
+ cb();
24
+ });
25
+
26
+ return stream;
27
+ }
28
+
29
+ module.exports = replaceEnv;