@tmsfe/tmskit 0.0.5-beta.5 → 0.0.7
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 +27 -25
- package/dist/index.cjs.js +779 -664
- package/main.js +3 -3
- package/package.json +75 -69
- package/src/config/constant.js +71 -70
- package/src/config/defaultTmsConfig.js +16 -16
- package/src/entry.js +60 -60
- package/src/gulp/build.js +5 -8
- package/src/gulp/compile.js +81 -87
- package/src/gulp/dev.js +102 -108
- package/src/gulp/plugins/less.js +116 -116
- package/src/gulp/plugins/mpCommonDep.js +131 -131
- package/src/gulp/plugins/mpJsonDep.js +108 -108
- package/src/gulp/plugins/mpWxmlDep.js +194 -194
- package/src/gulp/plugins/postcss-font-base64.js +72 -72
- package/src/gulp/{replaceEnv.js → plugins/replaceEnv.js} +29 -29
- package/src/gulp/plugins/utils/pluginError.js +25 -25
- package/src/index.js +62 -62
- package/src/init.js +33 -33
- package/src/scripts/create/ask.js +63 -63
- package/src/scripts/create/generator.js +25 -25
- package/src/scripts/create/ignoreFiles.js +7 -7
- package/src/scripts/create/index.js +72 -72
- package/src/scripts/create/render.js +19 -19
- package/src/scripts/run/build/index.js +17 -16
- package/src/scripts/run/dev/index.js +84 -67
- package/src/scripts/run/index.js +68 -63
- package/src/scripts/run/init/index.js +87 -80
- package/src/scripts/run/install/index.js +29 -31
- package/src/utils/buildAppJson.js +221 -200
- package/src/utils/checkDependencies.js +77 -77
- package/src/utils/cliUtils.js +35 -35
- package/src/utils/cloneModules.js +116 -91
- package/src/utils/findCssImport.js +30 -30
- package/src/utils/global.js +36 -36
- package/src/utils/handleError.js +16 -0
- package/src/utils/io.js +106 -106
- package/src/utils/log.js +44 -44
- package/src/utils/mpCiUtils.js +73 -74
- package/src/utils/npmUtils.js +166 -148
- package/src/utils/tkitUtils.js +158 -84
- package/src/utils/widgets.js +167 -173
|
@@ -1,194 +1,194 @@
|
|
|
1
|
-
/* eslint-disable no-param-reassign */
|
|
2
|
-
const through = require('through2');
|
|
3
|
-
const htmlparser2 = require('htmlparser2');
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const { diffContentCopyFile, ext, fileInDir } = require('../../utils/io');
|
|
7
|
-
const { resolve } = require('../../utils/widgets');
|
|
8
|
-
const { dfsFindCommonDep } = require('./mpCommonDep');
|
|
9
|
-
const { pluginError } = require('./utils/pluginError');
|
|
10
|
-
|
|
11
|
-
// 处理后缀(源码引入依赖时,不带后缀的情况)
|
|
12
|
-
const extFile = function (name, file) {
|
|
13
|
-
const extMap = {
|
|
14
|
-
import: '.wxml',
|
|
15
|
-
include: '.wxml',
|
|
16
|
-
wxs: '.wxs',
|
|
17
|
-
};
|
|
18
|
-
let extObj = {};
|
|
19
|
-
|
|
20
|
-
if (Object.keys(extMap).indexOf(name) > -1 && !file.endsWith(extMap[name])) {
|
|
21
|
-
extObj = ext(file, [extMap[name]]);
|
|
22
|
-
}
|
|
23
|
-
return {
|
|
24
|
-
extPath: extObj.extPath || '',
|
|
25
|
-
ext: extObj.ext || '',
|
|
26
|
-
file: extObj.file || file,
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const dfsFindWxmlDep = function (anaFileOriginFile, anaFileDestFile, isWatch = true) {
|
|
31
|
-
const resDep = {
|
|
32
|
-
image: new Map(),
|
|
33
|
-
wxml: new Map(),
|
|
34
|
-
wxs: new Map(),
|
|
35
|
-
};
|
|
36
|
-
function dfs(anaFileOriginFile, anaFileDestFile) {
|
|
37
|
-
let contents = '';
|
|
38
|
-
try {
|
|
39
|
-
contents = fs.readFileSync(anaFileOriginFile, 'utf8');
|
|
40
|
-
} catch (e) {
|
|
41
|
-
pluginError(e, isWatch);
|
|
42
|
-
}
|
|
43
|
-
const parser = new htmlparser2.Parser({
|
|
44
|
-
onopentag(name, attributes) {
|
|
45
|
-
if (attributes?.src?.startsWith('.')) {
|
|
46
|
-
const depOriginPath = path.join(path.dirname(anaFileOriginFile), attributes.src);
|
|
47
|
-
// 被依赖文件加上后缀
|
|
48
|
-
const { ext, file: depOriginFile, extPath } = extFile(name, depOriginPath);
|
|
49
|
-
|
|
50
|
-
if (!fs.existsSync(depOriginFile)) {
|
|
51
|
-
pluginError(
|
|
52
|
-
new Error(`${anaFileOriginFile}引用的路径${depOriginFile}找不到应用文件,请检查引用路径`),
|
|
53
|
-
isWatch,
|
|
54
|
-
);
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const depDestPath = path.join(path.dirname(anaFileDestFile), attributes.src);
|
|
59
|
-
const depDestFile = depDestPath.endsWith(ext) ? depDestPath : depDestPath + extPath;
|
|
60
|
-
|
|
61
|
-
// 收集wxml依赖
|
|
62
|
-
if (['import', 'include'].indexOf(name) > -1) {
|
|
63
|
-
resDep.wxml.set(depDestFile, {
|
|
64
|
-
anaFileOriginFile,
|
|
65
|
-
anaFileDestFile,
|
|
66
|
-
depDestFile,
|
|
67
|
-
depOriginFile,
|
|
68
|
-
});
|
|
69
|
-
dfs(depOriginFile, depDestFile);
|
|
70
|
-
}
|
|
71
|
-
// 收集image依赖
|
|
72
|
-
// if (name === 'image') {
|
|
73
|
-
// resDep.image.set(attributes.src, {
|
|
74
|
-
// anaFileOriginFile,
|
|
75
|
-
// anaFileDestFile,
|
|
76
|
-
// depDestFile,
|
|
77
|
-
// depOriginFile,
|
|
78
|
-
// });
|
|
79
|
-
// dfs(depOriginFile, depDestFile);
|
|
80
|
-
// }
|
|
81
|
-
// 收集wxs依赖
|
|
82
|
-
if (name === 'wxs') {
|
|
83
|
-
resDep.wxs.set(depDestFile, {
|
|
84
|
-
anaFileOriginFile,
|
|
85
|
-
anaFileDestFile,
|
|
86
|
-
depDestFile,
|
|
87
|
-
depOriginFile,
|
|
88
|
-
});
|
|
89
|
-
const defs = dfsFindCommonDep(depOriginFile, depDestFile, ['.wxs']);
|
|
90
|
-
defs.forEach((item, key) => {
|
|
91
|
-
resDep.wxs.set(key, item);
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
parser.write(contents);
|
|
98
|
-
parser.end();
|
|
99
|
-
}
|
|
100
|
-
dfs(anaFileOriginFile, anaFileDestFile);
|
|
101
|
-
|
|
102
|
-
return resDep;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
function mpWxmlDep(tmsConfig, module, isWatch) {
|
|
106
|
-
const stream = through.obj(function (file, enc, cb) {
|
|
107
|
-
// 依赖分析的文件
|
|
108
|
-
const anaFileOriginFile = file.history[0];
|
|
109
|
-
const anaFileRelativeModule = path.relative(resolve(module.from), anaFileOriginFile);
|
|
110
|
-
const anaFileDestFile = resolve(tmsConfig.outputDir, module.to, anaFileRelativeModule);
|
|
111
|
-
|
|
112
|
-
if (file.isBuffer()) {
|
|
113
|
-
let contents = String(file.contents);
|
|
114
|
-
const copyModules = new Map();
|
|
115
|
-
const parser = new htmlparser2.Parser({
|
|
116
|
-
onopentag(name, attributes) {
|
|
117
|
-
const nameFilter = ['import', 'include', 'wxs'];
|
|
118
|
-
if (nameFilter.indexOf(name) > -1 && attributes.src) {
|
|
119
|
-
const depOriginPath = path.join(path.dirname(anaFileOriginFile), attributes.src);
|
|
120
|
-
// 处理后缀(源码引入依赖时,后缀不全的情况)
|
|
121
|
-
const { ext, file: depOriginFile, extPath } = extFile(name, depOriginPath);
|
|
122
|
-
|
|
123
|
-
Object.keys(tmsConfig.dependencies).forEach((includeName) => {
|
|
124
|
-
if (attributes.src.indexOf(includeName) > -1) {
|
|
125
|
-
const includePath = tmsConfig.dependencies[includeName];
|
|
126
|
-
|
|
127
|
-
if (!fileInDir(includePath, depOriginFile)) {
|
|
128
|
-
pluginError(
|
|
129
|
-
new Error(`${anaFileOriginFile}引用路径${depOriginFile}不在${includePath}不在文件夹内, 请检查应用路径`),
|
|
130
|
-
isWatch,
|
|
131
|
-
);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// eslint-disable-next-line
|
|
136
|
-
const reg = new RegExp(`^(\.\.\/)+.*\/${includeName}\/(.*)`);
|
|
137
|
-
const regRes = attributes.src.match(reg) || [];
|
|
138
|
-
if (regRes[2]) {
|
|
139
|
-
const depDestPath = resolve(tmsConfig.outputDir, module.to, includeName, regRes[2]);
|
|
140
|
-
const depDestFile = depDestPath.endsWith(ext) ? depDestPath : depDestPath + extPath;
|
|
141
|
-
if (!copyModules.has(depDestFile)) {
|
|
142
|
-
copyModules.set(depDestFile, {
|
|
143
|
-
depOriginFile,
|
|
144
|
-
depDestFile,
|
|
145
|
-
beforeDepPath: attributes.src,
|
|
146
|
-
afterDepPath: path.relative(path.dirname(anaFileDestFile), depDestFile),
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
},
|
|
154
|
-
});
|
|
155
|
-
parser.write(contents);
|
|
156
|
-
parser.end();
|
|
157
|
-
|
|
158
|
-
// console.log('wxml copyModules', copyModules);
|
|
159
|
-
copyModules.forEach(({ depOriginFile, depDestFile, beforeDepPath, afterDepPath }) => {
|
|
160
|
-
if (fs.existsSync(depOriginFile)) {
|
|
161
|
-
diffContentCopyFile(depOriginFile, depDestFile);
|
|
162
|
-
const reg = new RegExp(`['"]${beforeDepPath}["']`, 'g');
|
|
163
|
-
contents = contents.replace(reg, `"${afterDepPath}"`);
|
|
164
|
-
}
|
|
165
|
-
if (depOriginFile.endsWith('.wxml')) {
|
|
166
|
-
const defs = dfsFindWxmlDep(depOriginFile, depDestFile, isWatch);
|
|
167
|
-
// console.log('wxml defs', defs);
|
|
168
|
-
[...defs.wxml, ...defs.wxs].forEach(([, item]) => {
|
|
169
|
-
diffContentCopyFile(item.depOriginFile, item.depDestFile);
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
if (depOriginFile.endsWith('.wxs')) {
|
|
173
|
-
const defs = dfsFindCommonDep(depOriginFile, depDestFile, ['.wxs'], isWatch);
|
|
174
|
-
// console.log('wxs defs', defs);
|
|
175
|
-
defs.forEach((item) => {
|
|
176
|
-
diffContentCopyFile(item.depOriginFile, item.depDestFile);
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
file.contents =
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
this.push(file);
|
|
185
|
-
cb();
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
return stream;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
module.exports = {
|
|
192
|
-
mpWxmlDep,
|
|
193
|
-
dfsFindWxmlDep,
|
|
194
|
-
};
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
const through = require('through2');
|
|
3
|
+
const htmlparser2 = require('htmlparser2');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const { diffContentCopyFile, ext, fileInDir } = require('../../utils/io');
|
|
7
|
+
const { resolve } = require('../../utils/widgets');
|
|
8
|
+
const { dfsFindCommonDep } = require('./mpCommonDep');
|
|
9
|
+
const { pluginError } = require('./utils/pluginError');
|
|
10
|
+
|
|
11
|
+
// 处理后缀(源码引入依赖时,不带后缀的情况)
|
|
12
|
+
const extFile = function (name, file) {
|
|
13
|
+
const extMap = {
|
|
14
|
+
import: '.wxml',
|
|
15
|
+
include: '.wxml',
|
|
16
|
+
wxs: '.wxs',
|
|
17
|
+
};
|
|
18
|
+
let extObj = {};
|
|
19
|
+
|
|
20
|
+
if (Object.keys(extMap).indexOf(name) > -1 && !file.endsWith(extMap[name])) {
|
|
21
|
+
extObj = ext(file, [extMap[name]]);
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
extPath: extObj.extPath || '',
|
|
25
|
+
ext: extObj.ext || '',
|
|
26
|
+
file: extObj.file || file,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const dfsFindWxmlDep = function (anaFileOriginFile, anaFileDestFile, isWatch = true) {
|
|
31
|
+
const resDep = {
|
|
32
|
+
image: new Map(),
|
|
33
|
+
wxml: new Map(),
|
|
34
|
+
wxs: new Map(),
|
|
35
|
+
};
|
|
36
|
+
function dfs(anaFileOriginFile, anaFileDestFile) {
|
|
37
|
+
let contents = '';
|
|
38
|
+
try {
|
|
39
|
+
contents = fs.readFileSync(anaFileOriginFile, 'utf8');
|
|
40
|
+
} catch (e) {
|
|
41
|
+
pluginError(e, isWatch);
|
|
42
|
+
}
|
|
43
|
+
const parser = new htmlparser2.Parser({
|
|
44
|
+
onopentag(name, attributes) {
|
|
45
|
+
if (attributes?.src?.startsWith('.')) {
|
|
46
|
+
const depOriginPath = path.join(path.dirname(anaFileOriginFile), attributes.src);
|
|
47
|
+
// 被依赖文件加上后缀
|
|
48
|
+
const { ext, file: depOriginFile, extPath } = extFile(name, depOriginPath);
|
|
49
|
+
|
|
50
|
+
if (!fs.existsSync(depOriginFile)) {
|
|
51
|
+
pluginError(
|
|
52
|
+
new Error(`${anaFileOriginFile}引用的路径${depOriginFile}找不到应用文件,请检查引用路径`),
|
|
53
|
+
isWatch,
|
|
54
|
+
);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const depDestPath = path.join(path.dirname(anaFileDestFile), attributes.src);
|
|
59
|
+
const depDestFile = depDestPath.endsWith(ext) ? depDestPath : depDestPath + extPath;
|
|
60
|
+
|
|
61
|
+
// 收集wxml依赖
|
|
62
|
+
if (['import', 'include'].indexOf(name) > -1) {
|
|
63
|
+
resDep.wxml.set(depDestFile, {
|
|
64
|
+
anaFileOriginFile,
|
|
65
|
+
anaFileDestFile,
|
|
66
|
+
depDestFile,
|
|
67
|
+
depOriginFile,
|
|
68
|
+
});
|
|
69
|
+
dfs(depOriginFile, depDestFile);
|
|
70
|
+
}
|
|
71
|
+
// 收集image依赖
|
|
72
|
+
// if (name === 'image') {
|
|
73
|
+
// resDep.image.set(attributes.src, {
|
|
74
|
+
// anaFileOriginFile,
|
|
75
|
+
// anaFileDestFile,
|
|
76
|
+
// depDestFile,
|
|
77
|
+
// depOriginFile,
|
|
78
|
+
// });
|
|
79
|
+
// dfs(depOriginFile, depDestFile);
|
|
80
|
+
// }
|
|
81
|
+
// 收集wxs依赖
|
|
82
|
+
if (name === 'wxs') {
|
|
83
|
+
resDep.wxs.set(depDestFile, {
|
|
84
|
+
anaFileOriginFile,
|
|
85
|
+
anaFileDestFile,
|
|
86
|
+
depDestFile,
|
|
87
|
+
depOriginFile,
|
|
88
|
+
});
|
|
89
|
+
const defs = dfsFindCommonDep(depOriginFile, depDestFile, ['.wxs']);
|
|
90
|
+
defs.forEach((item, key) => {
|
|
91
|
+
resDep.wxs.set(key, item);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
parser.write(contents);
|
|
98
|
+
parser.end();
|
|
99
|
+
}
|
|
100
|
+
dfs(anaFileOriginFile, anaFileDestFile);
|
|
101
|
+
|
|
102
|
+
return resDep;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
function mpWxmlDep(tmsConfig, module, isWatch) {
|
|
106
|
+
const stream = through.obj(function (file, enc, cb) {
|
|
107
|
+
// 依赖分析的文件
|
|
108
|
+
const anaFileOriginFile = file.history[0];
|
|
109
|
+
const anaFileRelativeModule = path.relative(resolve(module.from), anaFileOriginFile);
|
|
110
|
+
const anaFileDestFile = resolve(tmsConfig.outputDir, module.to, anaFileRelativeModule);
|
|
111
|
+
|
|
112
|
+
if (file.isBuffer()) {
|
|
113
|
+
let contents = String(file.contents);
|
|
114
|
+
const copyModules = new Map();
|
|
115
|
+
const parser = new htmlparser2.Parser({
|
|
116
|
+
onopentag(name, attributes) {
|
|
117
|
+
const nameFilter = ['import', 'include', 'wxs'];
|
|
118
|
+
if (nameFilter.indexOf(name) > -1 && attributes.src) {
|
|
119
|
+
const depOriginPath = path.join(path.dirname(anaFileOriginFile), attributes.src);
|
|
120
|
+
// 处理后缀(源码引入依赖时,后缀不全的情况)
|
|
121
|
+
const { ext, file: depOriginFile, extPath } = extFile(name, depOriginPath);
|
|
122
|
+
|
|
123
|
+
Object.keys(tmsConfig.dependencies).forEach((includeName) => {
|
|
124
|
+
if (attributes.src.indexOf(includeName) > -1) {
|
|
125
|
+
const includePath = tmsConfig.dependencies[includeName];
|
|
126
|
+
|
|
127
|
+
if (!fileInDir(includePath, depOriginFile)) {
|
|
128
|
+
pluginError(
|
|
129
|
+
new Error(`${anaFileOriginFile}引用路径${depOriginFile}不在${includePath}不在文件夹内, 请检查应用路径`),
|
|
130
|
+
isWatch,
|
|
131
|
+
);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// eslint-disable-next-line
|
|
136
|
+
const reg = new RegExp(`^(\.\.\/)+.*\/${includeName}\/(.*)`);
|
|
137
|
+
const regRes = attributes.src.match(reg) || [];
|
|
138
|
+
if (regRes[2]) {
|
|
139
|
+
const depDestPath = resolve(tmsConfig.outputDir, module.to, includeName, regRes[2]);
|
|
140
|
+
const depDestFile = depDestPath.endsWith(ext) ? depDestPath : depDestPath + extPath;
|
|
141
|
+
if (!copyModules.has(depDestFile)) {
|
|
142
|
+
copyModules.set(depDestFile, {
|
|
143
|
+
depOriginFile,
|
|
144
|
+
depDestFile,
|
|
145
|
+
beforeDepPath: attributes.src,
|
|
146
|
+
afterDepPath: path.relative(path.dirname(anaFileDestFile), depDestFile).replace(/\\/g, '/'),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
parser.write(contents);
|
|
156
|
+
parser.end();
|
|
157
|
+
|
|
158
|
+
// console.log('wxml copyModules', copyModules);
|
|
159
|
+
copyModules.forEach(({ depOriginFile, depDestFile, beforeDepPath, afterDepPath }) => {
|
|
160
|
+
if (fs.existsSync(depOriginFile)) {
|
|
161
|
+
diffContentCopyFile(depOriginFile, depDestFile);
|
|
162
|
+
const reg = new RegExp(`['"]${beforeDepPath}["']`, 'g');
|
|
163
|
+
contents = contents.replace(reg, `"${afterDepPath}"`);
|
|
164
|
+
}
|
|
165
|
+
if (depOriginFile.endsWith('.wxml')) {
|
|
166
|
+
const defs = dfsFindWxmlDep(depOriginFile, depDestFile, isWatch);
|
|
167
|
+
// console.log('wxml defs', defs);
|
|
168
|
+
[...defs.wxml, ...defs.wxs].forEach(([, item]) => {
|
|
169
|
+
diffContentCopyFile(item.depOriginFile, item.depDestFile);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if (depOriginFile.endsWith('.wxs')) {
|
|
173
|
+
const defs = dfsFindCommonDep(depOriginFile, depDestFile, ['.wxs'], isWatch);
|
|
174
|
+
// console.log('wxs defs', defs);
|
|
175
|
+
defs.forEach((item) => {
|
|
176
|
+
diffContentCopyFile(item.depOriginFile, item.depDestFile);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
file.contents = Buffer.from(contents);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
this.push(file);
|
|
185
|
+
cb();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return stream;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
module.exports = {
|
|
192
|
+
mpWxmlDep,
|
|
193
|
+
dfsFindWxmlDep,
|
|
194
|
+
};
|
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
/* eslint-disable no-param-reassign */
|
|
2
|
-
const postcss = require('postcss');
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const { fail } = require('../../utils/log');
|
|
6
|
-
|
|
7
|
-
module.exports = postcss.plugin('postcss-font-base64', (options) => {
|
|
8
|
-
options = {
|
|
9
|
-
...options,
|
|
10
|
-
...{
|
|
11
|
-
match: { Scrabble: ['fakefont'] },
|
|
12
|
-
format: ['eot', 'woff', 'woff2', 'ttf'],
|
|
13
|
-
},
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
return function (css, result) {
|
|
17
|
-
css.walkAtRules('font-face', (fontFace) => {
|
|
18
|
-
const fileTypeRegex = getRegexStringForFileTypes(options.format);
|
|
19
|
-
|
|
20
|
-
fontFace.replaceValues(new RegExp(`url\\(["']?.+\\.${fileTypeRegex}["']?\\)`), (attr) => {
|
|
21
|
-
const fontRePath = attr.replace(/(url|'|"|\(|\)|\?#iefix)/g, '');
|
|
22
|
-
const fontAbPath = path.join(path.dirname(result.opts.from), fontRePath);
|
|
23
|
-
const res64 = base64Encode(fontAbPath);
|
|
24
|
-
const newUrlStr = 'url(data:'.concat(getMimeType(attr)).concat(';charset=utf-8;base64,')
|
|
25
|
-
.concat(res64)
|
|
26
|
-
.concat(')');
|
|
27
|
-
return res64 ? newUrlStr : attr;
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
function getRegexStringForFileTypes(fileTypes) {
|
|
32
|
-
const regex = fileTypes.map(fileType => (
|
|
33
|
-
fileType === 'eot'
|
|
34
|
-
? fileType.concat('(\\?#iefix)?')
|
|
35
|
-
: fileType)).join('|');
|
|
36
|
-
return regex ? `(${regex})` : '';
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// helper functions
|
|
40
|
-
function getMimeType(attribute) {
|
|
41
|
-
const formats = {
|
|
42
|
-
'.woff': 'application/font-woff',
|
|
43
|
-
'.woff2': 'font/woff2',
|
|
44
|
-
'.ttf': 'application/font-sfnt',
|
|
45
|
-
'.eot': 'application/vnd.ms-fontobject',
|
|
46
|
-
'.otf': 'application/font-sfnt',
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
let match = '';
|
|
50
|
-
const extension = attribute.match(/\.[a-z]{3,4}/)[0];
|
|
51
|
-
|
|
52
|
-
if (extension in formats) {
|
|
53
|
-
match = formats[extension];
|
|
54
|
-
};
|
|
55
|
-
return match;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
function base64Encode(file) {
|
|
59
|
-
if (fs.existsSync(file)) {
|
|
60
|
-
return readAndEncodeFile(file);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
fail(`${file} does not exist.`);
|
|
64
|
-
return '';
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function readAndEncodeFile(file) {
|
|
68
|
-
const bitmap = fs.readFileSync(file);
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
});
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
const postcss = require('postcss');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const { fail } = require('../../utils/log');
|
|
6
|
+
|
|
7
|
+
module.exports = postcss.plugin('postcss-font-base64', (options) => {
|
|
8
|
+
options = {
|
|
9
|
+
...options,
|
|
10
|
+
...{
|
|
11
|
+
match: { Scrabble: ['fakefont'] },
|
|
12
|
+
format: ['eot', 'woff', 'woff2', 'ttf'],
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
return function (css, result) {
|
|
17
|
+
css.walkAtRules('font-face', (fontFace) => {
|
|
18
|
+
const fileTypeRegex = getRegexStringForFileTypes(options.format);
|
|
19
|
+
|
|
20
|
+
fontFace.replaceValues(new RegExp(`url\\(["']?.+\\.${fileTypeRegex}["']?\\)`), (attr) => {
|
|
21
|
+
const fontRePath = attr.replace(/(url|'|"|\(|\)|\?#iefix)/g, '');
|
|
22
|
+
const fontAbPath = path.join(path.dirname(result.opts.from), fontRePath);
|
|
23
|
+
const res64 = base64Encode(fontAbPath);
|
|
24
|
+
const newUrlStr = 'url(data:'.concat(getMimeType(attr)).concat(';charset=utf-8;base64,')
|
|
25
|
+
.concat(res64)
|
|
26
|
+
.concat(')');
|
|
27
|
+
return res64 ? newUrlStr : attr;
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
function getRegexStringForFileTypes(fileTypes) {
|
|
32
|
+
const regex = fileTypes.map(fileType => (
|
|
33
|
+
fileType === 'eot'
|
|
34
|
+
? fileType.concat('(\\?#iefix)?')
|
|
35
|
+
: fileType)).join('|');
|
|
36
|
+
return regex ? `(${regex})` : '';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// helper functions
|
|
40
|
+
function getMimeType(attribute) {
|
|
41
|
+
const formats = {
|
|
42
|
+
'.woff': 'application/font-woff',
|
|
43
|
+
'.woff2': 'font/woff2',
|
|
44
|
+
'.ttf': 'application/font-sfnt',
|
|
45
|
+
'.eot': 'application/vnd.ms-fontobject',
|
|
46
|
+
'.otf': 'application/font-sfnt',
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
let match = '';
|
|
50
|
+
const extension = attribute.match(/\.[a-z]{3,4}/)[0];
|
|
51
|
+
|
|
52
|
+
if (extension in formats) {
|
|
53
|
+
match = formats[extension];
|
|
54
|
+
};
|
|
55
|
+
return match;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
function base64Encode(file) {
|
|
59
|
+
if (fs.existsSync(file)) {
|
|
60
|
+
return readAndEncodeFile(file);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fail(`${file} does not exist.`);
|
|
64
|
+
return '';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function readAndEncodeFile(file) {
|
|
68
|
+
const bitmap = fs.readFileSync(file);
|
|
69
|
+
return Buffer.from(bitmap).toString('base64');
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
});
|
|
@@ -1,29 +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 =
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
this.push(file);
|
|
23
|
-
cb();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
return stream;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
module.exports = replaceEnv;
|
|
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 = Buffer.from(contents);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
this.push(file);
|
|
23
|
+
cb();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return stream;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = replaceEnv;
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
/* eslint-disable no-param-reassign */
|
|
2
|
-
const { fail } = require('../../../utils/log');
|
|
3
|
-
|
|
4
|
-
function pluginError(error, isWatch) {
|
|
5
|
-
const errorOptions = { showStack: true };
|
|
6
|
-
|
|
7
|
-
errorOptions.error = error;
|
|
8
|
-
errorOptions.fileName = error.file;
|
|
9
|
-
errorOptions.lineNumber = error.line;
|
|
10
|
-
errorOptions.showProperties = false;
|
|
11
|
-
errorOptions.showStack = false;
|
|
12
|
-
|
|
13
|
-
const errMsg = error.message;
|
|
14
|
-
|
|
15
|
-
if (isWatch) {
|
|
16
|
-
fail(errMsg);
|
|
17
|
-
} else {
|
|
18
|
-
fail(errMsg);
|
|
19
|
-
process.exit(1);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
module.exports = {
|
|
24
|
-
pluginError,
|
|
25
|
-
};
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
const { fail } = require('../../../utils/log');
|
|
3
|
+
|
|
4
|
+
function pluginError(error, isWatch) {
|
|
5
|
+
const errorOptions = { showStack: true };
|
|
6
|
+
|
|
7
|
+
errorOptions.error = error;
|
|
8
|
+
errorOptions.fileName = error.file;
|
|
9
|
+
errorOptions.lineNumber = error.line;
|
|
10
|
+
errorOptions.showProperties = false;
|
|
11
|
+
errorOptions.showStack = false;
|
|
12
|
+
|
|
13
|
+
const errMsg = error.message;
|
|
14
|
+
|
|
15
|
+
if (isWatch) {
|
|
16
|
+
fail(errMsg);
|
|
17
|
+
} else {
|
|
18
|
+
fail(errMsg);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = {
|
|
24
|
+
pluginError,
|
|
25
|
+
};
|