@tmsfe/tmskit 0.0.4 → 0.0.5-beta.0
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/dist/index.cjs.js +703 -948
- package/package.json +9 -5
- package/src/config/constant.js +1 -1
- package/src/config/defaultTmsConfig.js +3 -4
- package/src/entry.js +48 -8
- package/src/gulp/build.js +78 -0
- package/src/gulp/dev.js +94 -0
- package/src/gulp/replaceEnv.js +28 -0
- package/src/index.js +26 -10
- package/src/init.js +0 -5
- package/src/scripts/run/build/index.js +3 -3
- package/src/scripts/run/dev/index.js +3 -3
- package/src/scripts/run/index.js +7 -7
- package/src/scripts/run/init/index.js +12 -29
- package/src/scripts/run/install/index.js +21 -29
- package/src/utils/buildAppJson.js +1 -1
- package/src/utils/entry.js +150 -0
- package/src/utils/npmUtils.js +23 -4
- package/src/webpack/base.js +0 -65
- package/src/webpack/build.js +0 -21
- package/src/webpack/buildServer.js +0 -34
- package/src/webpack/dev.js +0 -31
- package/src/webpack/devServer.js +0 -37
- package/src/webpack/plugins/entryExtractPlugin/index.js +0 -28
- package/src/webpack/utils.js +0 -244
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const { resolve, unique } = require('../utils/widgets');
|
|
4
|
+
const { tmsModulesMergeLocalModuleCfg } = require('../utils/tkitUtils');
|
|
5
|
+
const { fail } = require('../utils/log');
|
|
6
|
+
const replaceExt = require('replace-ext');
|
|
7
|
+
|
|
8
|
+
const extensions = ['.js', '.ts'];
|
|
9
|
+
function ext(entry, extensions) {
|
|
10
|
+
let newEntry = entry;
|
|
11
|
+
try {
|
|
12
|
+
const stat = fs.lstatSync(newEntry);
|
|
13
|
+
if (stat.isDirectory()) {
|
|
14
|
+
newEntry += (newEntry[newEntry.length - 1] === '/') ? 'index' : '/index';
|
|
15
|
+
}
|
|
16
|
+
} catch (e) {}
|
|
17
|
+
|
|
18
|
+
for (const ext of extensions) {
|
|
19
|
+
const file = replaceExt(newEntry, ext);
|
|
20
|
+
if (fs.existsSync(file)) {
|
|
21
|
+
return {
|
|
22
|
+
file,
|
|
23
|
+
ext,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 根据用户选择的modules,找到module.config.json的配置信息,找到所有的page
|
|
31
|
+
function getPageEntry(modules, tmsConfig, moduleDir) {
|
|
32
|
+
let entry = [];
|
|
33
|
+
// tms.config.js的modules 合并 module.config.json的配置项
|
|
34
|
+
const newModules = tmsModulesMergeLocalModuleCfg(modules, tmsConfig.appName, moduleDir);
|
|
35
|
+
newModules.forEach(({ path: relativePath, pages, root }, index) => {
|
|
36
|
+
pages.forEach((page) => {
|
|
37
|
+
const pageJsonPath = `${resolve(relativePath, page)}.json`;
|
|
38
|
+
if (fs.existsSync(pageJsonPath)) {
|
|
39
|
+
const pageJsonContent = JSON.parse(fs.readFileSync(pageJsonPath, 'utf-8'));
|
|
40
|
+
const pageDir = path.dirname(pageJsonPath); // 该页面所在的目录
|
|
41
|
+
const extValue = ext(resolve(relativePath, page), extensions);
|
|
42
|
+
if (!extValue) {
|
|
43
|
+
fail(`当前${page}找不到入口.js或.ts文件`);
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const entryKey = `${root}/${page}${extValue.ext}`;
|
|
48
|
+
entry = entry.concat(
|
|
49
|
+
[{
|
|
50
|
+
key: entryKey,
|
|
51
|
+
filePath: extValue.file,
|
|
52
|
+
type: 'page',
|
|
53
|
+
moduleInfo: {
|
|
54
|
+
path: relativePath,
|
|
55
|
+
root,
|
|
56
|
+
},
|
|
57
|
+
}],
|
|
58
|
+
getComponentEntry(pageJsonContent, pageDir, path.dirname(entryKey), newModules[index]),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
return entry;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 根据pageJson,filePath,获取页面引入的所有component
|
|
67
|
+
function getComponentEntry(pageJson, pagePath, pageKey, moduleInfo) {
|
|
68
|
+
const componentEntry = [];
|
|
69
|
+
function task(json, dir, rootKey) {
|
|
70
|
+
if (!json.usingComponents) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const componentKeys = Object.keys(json.usingComponents);
|
|
75
|
+
|
|
76
|
+
// 如果存在依赖的组件
|
|
77
|
+
componentKeys.forEach((key) => {
|
|
78
|
+
if (json.usingComponents[key].startsWith('.')) {
|
|
79
|
+
// 拼出组件所在位置的绝对路径
|
|
80
|
+
const comValue = path.join(dir, json.usingComponents[key]);
|
|
81
|
+
const extValue = ext(comValue, extensions);
|
|
82
|
+
const comKey = path.resolve('/', rootKey, json.usingComponents[key]);
|
|
83
|
+
if (!extValue) {
|
|
84
|
+
fail(`当前page: ${pagePath} component: ${comValue}找不到入口.js或.ts文件`);
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
componentEntry.push({
|
|
89
|
+
key: `${comKey.slice(1)}${extValue.ext}`,
|
|
90
|
+
filePath: extValue.file,
|
|
91
|
+
type: 'component',
|
|
92
|
+
moduleInfo: {
|
|
93
|
+
path: moduleInfo.path,
|
|
94
|
+
root: moduleInfo.root,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const comJsonPath = `${comValue}.json`;
|
|
99
|
+
if (fs.existsSync(comJsonPath)) {
|
|
100
|
+
const comJsonContent = JSON.parse(fs.readFileSync(comJsonPath, 'utf-8'));
|
|
101
|
+
|
|
102
|
+
const comDir = path.dirname(comJsonPath); // 该页面所在的目录
|
|
103
|
+
task(comJsonContent, comDir, path.dirname(comKey));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
task(pageJson, pagePath, pageKey);
|
|
110
|
+
|
|
111
|
+
return componentEntry;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 收集依赖在非模块内的目录
|
|
115
|
+
function getDepDirOfNotInModule(modules, tmsConfig, moduleDir) {
|
|
116
|
+
const result = [];
|
|
117
|
+
const pageInfoArr = getPageEntry(modules, tmsConfig, moduleDir);
|
|
118
|
+
// const pageInfoArr = [{
|
|
119
|
+
// type: 'component',
|
|
120
|
+
// filePath: '/Users/odile/workspace/tms-frontend1/miniprogram/modules/home/common/components/favorite/favorite.js',
|
|
121
|
+
// moduleInfo: {
|
|
122
|
+
// root: 'modules/home/welfare',
|
|
123
|
+
// path: '../../miniprogram/modules/home/welfare',
|
|
124
|
+
// },
|
|
125
|
+
// }];
|
|
126
|
+
pageInfoArr.filter(item => item.type === 'component').forEach((item) => {
|
|
127
|
+
const { moduleInfo, filePath } = item;
|
|
128
|
+
// 文件路径 相对于 模块path的路径 ../../common/data
|
|
129
|
+
const moduleFileRelative = path.relative(resolve(moduleInfo.path), filePath);
|
|
130
|
+
|
|
131
|
+
// '../../common/data' => '../../common'
|
|
132
|
+
const fileRelativeDir = moduleFileRelative.match(/^(\.\.\/)+([^/]*)/);
|
|
133
|
+
// eslint-disable-next-line
|
|
134
|
+
if (fileRelativeDir && fileRelativeDir[0]) {
|
|
135
|
+
const fileDir = resolve(moduleInfo.path, fileRelativeDir[0]);
|
|
136
|
+
if (fs.existsSync(fileDir)) {
|
|
137
|
+
result.push({
|
|
138
|
+
from: fileDir,
|
|
139
|
+
to: resolve(tmsConfig.webpack.outputDir, moduleInfo.root, fileRelativeDir[0]),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
return unique(result, 'from');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
module.exports = {
|
|
149
|
+
getDepDirOfNotInModule,
|
|
150
|
+
};
|
package/src/utils/npmUtils.js
CHANGED
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
6
|
const shell = require('shelljs');
|
|
7
|
+
const glob = require('glob-ignore');
|
|
7
8
|
const LOG = require('./log');
|
|
8
9
|
|
|
9
10
|
const dirpath = process.cwd(); // 项目根目录
|
|
10
11
|
|
|
11
|
-
const getTarNpmFilename = targetDir => `${targetDir.
|
|
12
|
+
const getTarNpmFilename = targetDir => `${targetDir.replace(/\//g, '-')}.tar.gz`;
|
|
12
13
|
|
|
13
14
|
// 缓存npm包
|
|
14
15
|
const npmCache = function (targetDir, cacheDir) {
|
|
@@ -38,9 +39,8 @@ const getNpmCache = function (targetDir, cacheDir) {
|
|
|
38
39
|
|
|
39
40
|
|
|
40
41
|
// 遍历安装指定目录下所有项目的npm依赖
|
|
41
|
-
const
|
|
42
|
+
const mpNpmInstallAll = async (modules, contextDir, cacheDir) => {
|
|
42
43
|
const packageJsonFiles = await findAllPackageJson(modules, contextDir);
|
|
43
|
-
|
|
44
44
|
await Promise.all(packageJsonFiles.map(file => new Promise((resolve) => {
|
|
45
45
|
const dir = path.dirname(file);
|
|
46
46
|
shell.cd(dir);
|
|
@@ -109,7 +109,9 @@ const findAllPackageJson = (subRoots = [], contextDir) => {
|
|
|
109
109
|
const cwd = contextDir || dirpath;
|
|
110
110
|
const result = [path.join(cwd, packageJsonName)]; // 默认填充根目录下的package.json
|
|
111
111
|
|
|
112
|
+
|
|
112
113
|
subRoots.forEach((subRoot) => {
|
|
114
|
+
console.log('>>>>>>>>>>>>', subRoot);
|
|
113
115
|
const toppath = path.join(cwd, subRoot.root); // 从该目录开始查找package.json文件
|
|
114
116
|
const list = findFilesByFilter(toppath, packageJsonName);
|
|
115
117
|
|
|
@@ -119,8 +121,25 @@ const findAllPackageJson = (subRoots = [], contextDir) => {
|
|
|
119
121
|
return result;
|
|
120
122
|
};
|
|
121
123
|
|
|
124
|
+
function cloudNpmInstall(contextDir) {
|
|
125
|
+
return new Promise((resolve, reject) => {
|
|
126
|
+
glob(`${contextDir}/**/package.json`, ['node_modules', 'miniprogram_npm'], (err, files) => {
|
|
127
|
+
if (err) {
|
|
128
|
+
reject(err);
|
|
129
|
+
}
|
|
130
|
+
files.forEach((file) => {
|
|
131
|
+
const dir = path.dirname(file);
|
|
132
|
+
shell.cd(dir);
|
|
133
|
+
shell.exec('npx npm install --production --registry http://mirrors.tencent.com/npm/', { silent: false });
|
|
134
|
+
});
|
|
135
|
+
resolve();
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
122
140
|
|
|
123
141
|
module.exports = {
|
|
124
|
-
|
|
142
|
+
cloudNpmInstall,
|
|
143
|
+
mpNpmInstallAll,
|
|
125
144
|
findAllPackageJson,
|
|
126
145
|
};
|
package/src/webpack/base.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
const WebpackChain = require('webpack-chain');
|
|
2
|
-
const webpack = require('webpack');
|
|
3
|
-
const { getEntry, stringified, getAlias } = require('./utils');
|
|
4
|
-
const { resolve } = require('../utils/widgets');
|
|
5
|
-
const { DEFAULT_WEBPACK_ENTRY, DEFAULT_MODULE_DIR } = require('../config/constant');
|
|
6
|
-
|
|
7
|
-
module.exports = (tmsConfig, modules) => {
|
|
8
|
-
const { envData = {}, webpack: tmsWebpack } = tmsConfig;
|
|
9
|
-
let webpackConfig = new WebpackChain();
|
|
10
|
-
|
|
11
|
-
const alias = getAlias(modules);
|
|
12
|
-
const entry = getEntry(DEFAULT_WEBPACK_ENTRY, modules, tmsConfig, DEFAULT_MODULE_DIR);
|
|
13
|
-
webpackConfig.merge({
|
|
14
|
-
entry,
|
|
15
|
-
output: {
|
|
16
|
-
path: resolve(`./${tmsWebpack.outputDir}`),
|
|
17
|
-
libraryTarget: 'commonjs2',
|
|
18
|
-
filename: '[name]',
|
|
19
|
-
},
|
|
20
|
-
// TODO 有报错,先注释掉
|
|
21
|
-
// cache: {
|
|
22
|
-
// type: 'filesystem',
|
|
23
|
-
// cacheDirectory: resolve(`./${tmsConfig.webpack.outputDir}/node_modules/.cache`),
|
|
24
|
-
// },
|
|
25
|
-
resolve: {
|
|
26
|
-
extensions: ['.tsx', '.ts', '.js'],
|
|
27
|
-
alias: Object.assign(tmsWebpack.alias, alias),
|
|
28
|
-
},
|
|
29
|
-
devtool: tmsWebpack.sourceMap ? 'source-map' : false,
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
webpackConfig.module
|
|
33
|
-
.rule('ts-loader')
|
|
34
|
-
.test(/\.ts$/)
|
|
35
|
-
.use('ts-loader')
|
|
36
|
-
.loader(require.resolve('ts-loader'))
|
|
37
|
-
.options({
|
|
38
|
-
configFile: resolve('./tsconfig.json'),
|
|
39
|
-
// 只进行语法转换,不进行类型校验,提高构建速度
|
|
40
|
-
transpileOnly: true,
|
|
41
|
-
})
|
|
42
|
-
.end();
|
|
43
|
-
|
|
44
|
-
// webpackConfig.module
|
|
45
|
-
// .rule('babel')
|
|
46
|
-
// .test(/\.(js|mjs|jsx|ts|tsx)$/)
|
|
47
|
-
// .pre()
|
|
48
|
-
// .exclude.add(/(node_modules|miniprogram_npm)/).end()
|
|
49
|
-
// .use(require.resolve('babel-loader'))
|
|
50
|
-
// .loader(require.resolve('babel-loader'));
|
|
51
|
-
|
|
52
|
-
webpackConfig
|
|
53
|
-
.plugin('definePlugin')
|
|
54
|
-
.use(webpack.DefinePlugin, [stringified(envData)])
|
|
55
|
-
.end();
|
|
56
|
-
|
|
57
|
-
// 执行tms.config.js自定义的webpackChain
|
|
58
|
-
if (tmsWebpack.webpackChain) {
|
|
59
|
-
webpackConfig = tmsWebpack.webpackChain(webpackConfig, {
|
|
60
|
-
modules,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return webpackConfig;
|
|
65
|
-
};
|
package/src/webpack/build.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
const webpackConfig = require('./base');
|
|
2
|
-
const { DEFAULT_COPY_CONFIG } = require('../config/constant');
|
|
3
|
-
const { getCopyPlugin } = require('./utils');
|
|
4
|
-
|
|
5
|
-
module.exports = (...args) => {
|
|
6
|
-
const [tmsConfig, modules] = args;
|
|
7
|
-
const webpackBuildConfig = webpackConfig(...args);
|
|
8
|
-
|
|
9
|
-
webpackBuildConfig.mode('production');
|
|
10
|
-
|
|
11
|
-
const copyPluginParams = getCopyPlugin(DEFAULT_COPY_CONFIG, modules, tmsConfig, 'build');
|
|
12
|
-
// console.log('copyPluginParams', copyPluginParams);
|
|
13
|
-
webpackBuildConfig
|
|
14
|
-
.plugin('copy-webpack-plugin')
|
|
15
|
-
.use(require('copy-webpack-plugin'), [{
|
|
16
|
-
patterns: copyPluginParams,
|
|
17
|
-
}])
|
|
18
|
-
.end();
|
|
19
|
-
|
|
20
|
-
return webpackBuildConfig;
|
|
21
|
-
};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
const webpack = require('webpack');
|
|
2
|
-
const webBuildConfig = require('./build');
|
|
3
|
-
const { createTask } = require('../utils/widgets');
|
|
4
|
-
const { fail } = require('../utils/log');
|
|
5
|
-
|
|
6
|
-
function compilerRun(compiler) {
|
|
7
|
-
return new Promise((resolve) => {
|
|
8
|
-
compiler.run((err, stats) => {
|
|
9
|
-
resolve({ err, stats });
|
|
10
|
-
});
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = async (...args) => {
|
|
15
|
-
const config = webBuildConfig(...args);
|
|
16
|
-
const compiler = webpack(config.toConfig());
|
|
17
|
-
|
|
18
|
-
const { err, stats } = await createTask(compilerRun, '开始webpack打包编译', 'webpack打包编译完成')(compiler);;
|
|
19
|
-
|
|
20
|
-
if (err) {
|
|
21
|
-
fail(err);
|
|
22
|
-
console.log('详细的错误信息:', err);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (stats.hasErrors() || stats.hasWarnings()) {
|
|
26
|
-
console.log(stats.toString({
|
|
27
|
-
// 增加控制台颜色开关
|
|
28
|
-
colors: true,
|
|
29
|
-
errorDetails: true,
|
|
30
|
-
}));
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
return compiler;
|
|
34
|
-
};
|
package/src/webpack/dev.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
const webpackConfig = require('./base');
|
|
2
|
-
const { DEFAULT_COPY_CONFIG } = require('../config/constant');
|
|
3
|
-
const { getCopyPlugin } = require('./utils');
|
|
4
|
-
|
|
5
|
-
module.exports = (...args) => {
|
|
6
|
-
const [tmsConfig, modules] = args;
|
|
7
|
-
const webpackDevConfig = webpackConfig(...args);
|
|
8
|
-
|
|
9
|
-
webpackDevConfig.devtool('source-map');
|
|
10
|
-
|
|
11
|
-
webpackDevConfig.mode('development');
|
|
12
|
-
|
|
13
|
-
const copyPluginParams = getCopyPlugin(DEFAULT_COPY_CONFIG, modules, tmsConfig, 'dev');
|
|
14
|
-
// console.log('copyPluginParams', copyPluginParams);
|
|
15
|
-
webpackDevConfig
|
|
16
|
-
.plugin('copy-webpack-plugin')
|
|
17
|
-
.use(require('copy-webpack-plugin'), [{
|
|
18
|
-
patterns: copyPluginParams,
|
|
19
|
-
}])
|
|
20
|
-
.end();
|
|
21
|
-
|
|
22
|
-
webpackDevConfig
|
|
23
|
-
.plugin('ExtractPlugin')
|
|
24
|
-
.use(require('./plugins/entryExtractPlugin'), [{
|
|
25
|
-
tmsConfig,
|
|
26
|
-
modules,
|
|
27
|
-
}])
|
|
28
|
-
.end();
|
|
29
|
-
|
|
30
|
-
return webpackDevConfig;
|
|
31
|
-
};
|
package/src/webpack/devServer.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
const webpack = require('webpack');
|
|
2
|
-
const webDevConfig = require('./dev');
|
|
3
|
-
const { setupDevWebPackHooks } = require('./utils');
|
|
4
|
-
const { fail } = require('../utils/log');
|
|
5
|
-
|
|
6
|
-
module.exports = (...args) => {
|
|
7
|
-
const config = webDevConfig(...args);
|
|
8
|
-
const compiler = webpack(config.toConfig());
|
|
9
|
-
|
|
10
|
-
setupDevWebPackHooks({
|
|
11
|
-
compiler,
|
|
12
|
-
}, () => {
|
|
13
|
-
// TODO 判断open参数,打开微信开发者工具
|
|
14
|
-
// openDevtool(api.resolve(config.outputDir || 'dist'))
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
compiler.watch(
|
|
18
|
-
{
|
|
19
|
-
aggregateTimeout: 1000,
|
|
20
|
-
poll: undefined,
|
|
21
|
-
},
|
|
22
|
-
(err, stats) => {
|
|
23
|
-
if (err) {
|
|
24
|
-
fail(err);
|
|
25
|
-
console.log('详细的错误信息:', err);
|
|
26
|
-
}
|
|
27
|
-
if (stats.hasErrors() || stats.hasWarnings()) {
|
|
28
|
-
console.log(stats.toString({
|
|
29
|
-
// 增加控制台颜色开关
|
|
30
|
-
colors: true,
|
|
31
|
-
}));
|
|
32
|
-
};
|
|
33
|
-
},
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
return compiler;
|
|
37
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
|
|
2
|
-
const { DEFAULT_WEBPACK_ENTRY, DEFAULT_MODULE_DIR } = require('../../../config/constant');
|
|
3
|
-
const { getEntry } = require('../../utils');
|
|
4
|
-
|
|
5
|
-
class EntryExtraPlugin {
|
|
6
|
-
constructor(options = {}) {
|
|
7
|
-
this.options = options;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
applyEntry(compiler, entry) {
|
|
11
|
-
const { context } = compiler.options;
|
|
12
|
-
|
|
13
|
-
Object.keys(entry).forEach((key) => {
|
|
14
|
-
new SingleEntryPlugin(context, entry[key], key).apply(compiler);
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
apply(compiler) {
|
|
19
|
-
const { tmsConfig = {}, modules = [] } = this.options;
|
|
20
|
-
|
|
21
|
-
compiler.hooks.watchRun.tap('EntryExtraPlugin', () => {
|
|
22
|
-
const entry = getEntry(DEFAULT_WEBPACK_ENTRY, modules, tmsConfig, DEFAULT_MODULE_DIR);
|
|
23
|
-
this.applyEntry(compiler, entry);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
module.exports = EntryExtraPlugin;
|
package/src/webpack/utils.js
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const { resolve } = require('../utils/widgets');
|
|
4
|
-
const { buildOutputAppJson } = require('../utils/buildAppJson');
|
|
5
|
-
const { tmsModulesMergeLocalModuleCfg } = require('../utils/tkitUtils');
|
|
6
|
-
const { findAllPackageJson } = require('../utils/npmUtils');
|
|
7
|
-
const { fail } = require('../utils/log');
|
|
8
|
-
const replaceExt = require('replace-ext');
|
|
9
|
-
|
|
10
|
-
const extensions = ['.ts', '.js'];
|
|
11
|
-
function ext(entry, extensions) {
|
|
12
|
-
let newEntry = entry;
|
|
13
|
-
try {
|
|
14
|
-
const stat = fs.lstatSync(newEntry);
|
|
15
|
-
if (stat.isDirectory()) {
|
|
16
|
-
newEntry += (newEntry[newEntry.length - 1] === '/') ? 'index' : '/index';
|
|
17
|
-
}
|
|
18
|
-
} catch (e) {}
|
|
19
|
-
|
|
20
|
-
for (const ext of extensions) {
|
|
21
|
-
const file = replaceExt(newEntry, ext);
|
|
22
|
-
if (fs.existsSync(file)) {
|
|
23
|
-
return {
|
|
24
|
-
file,
|
|
25
|
-
ext,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// 获取所有的package.json里的依赖包Dir
|
|
33
|
-
function getAlias(modules) {
|
|
34
|
-
const alias = {};
|
|
35
|
-
const allPackages = findAllPackageJson(modules, resolve('./dist'));
|
|
36
|
-
allPackages.forEach((packageFilePath) => {
|
|
37
|
-
const { dependencies } = require(packageFilePath);
|
|
38
|
-
Object.keys(dependencies).forEach((dependence) => {
|
|
39
|
-
alias[dependence] = path.join(path.dirname(packageFilePath), `./miniprogram_npm/${dependence}`);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
return alias;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
// 根据用户选择的modules,找到module.config.json的配置信息,找到所有的page
|
|
47
|
-
function getPageEntry(modules, tmsConfig, moduleDir) {
|
|
48
|
-
const entry = {};
|
|
49
|
-
// tms.config.js的modules 合并 module.config.json的配置项
|
|
50
|
-
const newModules = tmsModulesMergeLocalModuleCfg(modules, tmsConfig.appName, moduleDir);
|
|
51
|
-
newModules.forEach(({ path: relativePath, pages, root }) => {
|
|
52
|
-
pages.forEach((page) => {
|
|
53
|
-
const pageJsonPath = `${resolve(relativePath, page)}.json`;
|
|
54
|
-
if (fs.existsSync(pageJsonPath)) {
|
|
55
|
-
const pageJsonContent = JSON.parse(fs.readFileSync(pageJsonPath, 'utf-8'));
|
|
56
|
-
const pageDir = path.dirname(pageJsonPath); // 该页面所在的目录
|
|
57
|
-
const extValue = ext(resolve(relativePath, page), extensions);
|
|
58
|
-
if (!extValue) {
|
|
59
|
-
fail(`当前${page}找不到入口.js或.ts文件`);
|
|
60
|
-
process.exit(1);
|
|
61
|
-
}
|
|
62
|
-
const entryKey = `${root}/${page}${extValue.ext}`;
|
|
63
|
-
|
|
64
|
-
Object.assign(
|
|
65
|
-
entry,
|
|
66
|
-
{ [entryKey]: extValue.file },
|
|
67
|
-
getComponentEntry(pageJsonContent, pageDir, path.dirname(entryKey)),
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
return entry;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// 根据appJson,获取所有的page
|
|
76
|
-
function getEntry(defaultWebpackEntry, modules, tmsConfig, moduleDir) {
|
|
77
|
-
const defaultEntry = {};
|
|
78
|
-
Object.keys(defaultWebpackEntry).forEach((key) => {
|
|
79
|
-
const extValue = ext(defaultWebpackEntry[key], extensions);
|
|
80
|
-
defaultEntry[key + extValue.ext] = extValue.file;
|
|
81
|
-
});
|
|
82
|
-
return {
|
|
83
|
-
...defaultEntry,
|
|
84
|
-
...getPageEntry(modules, tmsConfig, moduleDir),
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// 根据pageJson,filePath,获取页面引入的所有component
|
|
89
|
-
function getComponentEntry(pageJson, pagePath, pageKey) {
|
|
90
|
-
const componentEntry = {};
|
|
91
|
-
function task(json, dir, rootKey) {
|
|
92
|
-
if (!json.usingComponents) {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const componentKeys = Object.keys(json.usingComponents);
|
|
97
|
-
|
|
98
|
-
// 如果存在依赖的组件
|
|
99
|
-
componentKeys.forEach((key) => {
|
|
100
|
-
if (json.usingComponents[key].startsWith('.')) {
|
|
101
|
-
// 拼出组件所在位置的绝对路径
|
|
102
|
-
const comValue = path.join(dir, json.usingComponents[key]);
|
|
103
|
-
const extValue = ext(comValue, extensions);
|
|
104
|
-
const comKey = path.resolve('/', rootKey, json.usingComponents[key]);
|
|
105
|
-
if (!extValue) {
|
|
106
|
-
fail(`当前page: ${pagePath} component: ${comValue}找不到入口.js或.ts文件`);
|
|
107
|
-
process.exit(1);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
componentEntry[`${comKey.slice(1)}${extValue.ext}`] = extValue.file;
|
|
111
|
-
const comJsonPath = `${comValue}.json`;
|
|
112
|
-
if (fs.existsSync(comJsonPath)) {
|
|
113
|
-
const comJsonContent = JSON.parse(fs.readFileSync(comJsonPath, 'utf-8'));
|
|
114
|
-
|
|
115
|
-
const comDir = path.dirname(comJsonPath); // 该页面所在的目录
|
|
116
|
-
task(comJsonContent, comDir, path.dirname(comKey));
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
task(pageJson, pagePath, pageKey);
|
|
123
|
-
|
|
124
|
-
return componentEntry;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// 根据modules处理需要拷贝的模块,copy-webpack-plugin需要的参数
|
|
128
|
-
function getCopyPlugin(defaultCopyConfig, modules, tmsConfig, env) {
|
|
129
|
-
const toPath = tmsConfig.webpack.outputDir;
|
|
130
|
-
function generatorAppJson(tmsConfig, modules) {
|
|
131
|
-
try {
|
|
132
|
-
return buildOutputAppJson(tmsConfig, modules);
|
|
133
|
-
} catch (e) {
|
|
134
|
-
fail(`动态生成app.json出现错误${e} 请检查你的配置项`);
|
|
135
|
-
return {};
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const patterns = [];
|
|
140
|
-
// 默认的一些配置拷贝文件 package.json、sitemap.json等
|
|
141
|
-
defaultCopyConfig.forEach((item) => {
|
|
142
|
-
if (fs.existsSync(resolve(item))) {
|
|
143
|
-
patterns.push({
|
|
144
|
-
from: resolve(item),
|
|
145
|
-
to: resolve(`./${toPath}/${item}`),
|
|
146
|
-
transform: {
|
|
147
|
-
cache: true,
|
|
148
|
-
},
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
// 拷贝模块的代码
|
|
154
|
-
modules.forEach((item) => {
|
|
155
|
-
patterns.push({
|
|
156
|
-
from: resolve(item.path),
|
|
157
|
-
to: resolve(`./${toPath}/${item.root}`),
|
|
158
|
-
globOptions: {
|
|
159
|
-
ignore: ['**/*.js', '*.js', '**/*.ts', '*.ts'],
|
|
160
|
-
},
|
|
161
|
-
transform: {
|
|
162
|
-
cache: true,
|
|
163
|
-
transformer: env === 'dev'
|
|
164
|
-
? (content, absoluteFrom) => {
|
|
165
|
-
// 监听module.config.json的修改, 自动生成编译后的app.json
|
|
166
|
-
if (absoluteFrom.indexOf('module.config.json') > -1) {
|
|
167
|
-
generatorAppJson(tmsConfig, modules);
|
|
168
|
-
}
|
|
169
|
-
return content;
|
|
170
|
-
}
|
|
171
|
-
: content => content,
|
|
172
|
-
},
|
|
173
|
-
});
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
if (env === 'dev') {
|
|
177
|
-
// 拷贝app.json时,自动生成编译后的app.json
|
|
178
|
-
const appJsonConfig = ['app.json'];
|
|
179
|
-
appJsonConfig.forEach((item) => {
|
|
180
|
-
patterns.push({
|
|
181
|
-
from: resolve(item),
|
|
182
|
-
to: resolve(`./${toPath}/${item}`),
|
|
183
|
-
transform: {
|
|
184
|
-
cache: true,
|
|
185
|
-
transformer: () => {
|
|
186
|
-
const appJson = generatorAppJson(tmsConfig, modules);
|
|
187
|
-
return JSON.stringify(appJson, null, 2);
|
|
188
|
-
},
|
|
189
|
-
},
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return patterns;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// dev时,给webpack注入相应的事件
|
|
198
|
-
const setupDevWebPackHooks = (context, firstDone) => {
|
|
199
|
-
let tempFirstDone = true;
|
|
200
|
-
|
|
201
|
-
const invalid = () => {
|
|
202
|
-
// eslint-disable-next-line
|
|
203
|
-
context.stats = undefined;
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
const done = (stats) => {
|
|
207
|
-
// eslint-disable-next-line
|
|
208
|
-
context.stats = stats;
|
|
209
|
-
|
|
210
|
-
process.nextTick(() => {
|
|
211
|
-
const { stats } = context;
|
|
212
|
-
if (!stats) return;
|
|
213
|
-
|
|
214
|
-
if (tempFirstDone) {
|
|
215
|
-
tempFirstDone = false;
|
|
216
|
-
firstDone();
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
};
|
|
220
|
-
const { compiler } = context;
|
|
221
|
-
|
|
222
|
-
compiler.hooks.watchRun.tap('miniprogram-dev', invalid);
|
|
223
|
-
compiler.hooks.invalid.tap('miniprogram-dev', invalid);
|
|
224
|
-
compiler.hooks.done.tap('miniprogram-dev', done);
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
const stringified = raw => ({
|
|
228
|
-
'process.env': Object.keys(raw).reduce(
|
|
229
|
-
(env, key) => {
|
|
230
|
-
// eslint-disable-next-line
|
|
231
|
-
env[key] = JSON.stringify(raw[key]);
|
|
232
|
-
return env;
|
|
233
|
-
},
|
|
234
|
-
{},
|
|
235
|
-
),
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
module.exports = {
|
|
239
|
-
getCopyPlugin,
|
|
240
|
-
setupDevWebPackHooks,
|
|
241
|
-
getEntry,
|
|
242
|
-
stringified,
|
|
243
|
-
getAlias,
|
|
244
|
-
};
|