@tmsfe/tmskit 0.0.4 → 0.0.5-beta.3
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 +1065 -915
- package/package.json +11 -6
- package/src/config/constant.js +1 -1
- package/src/config/defaultTmsConfig.js +5 -4
- package/src/entry.js +48 -8
- package/src/gulp/build.js +8 -0
- package/src/gulp/compile.js +68 -0
- package/src/gulp/dev.js +107 -0
- package/src/gulp/jsDep.js +150 -0
- package/src/gulp/mpJsonDep.js +122 -0
- package/src/gulp/replaceEnv.js +29 -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/io.js +45 -0
- package/src/utils/npmUtils.js +26 -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
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
|
-
};
|