@doubao-dev/taro-plugin-platform-doubao 0.10.3 → 0.11.0-rc.10
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 +57 -152
- package/dist/compiler.d.ts +23 -13
- package/dist/compiler.js +44 -45
- package/dist/emit/index.d.ts +11 -0
- package/dist/emit/index.js +63 -0
- package/dist/index.js +8 -16
- package/dist/plans/host-deps.d.ts +45 -0
- package/dist/plans/host-deps.js +246 -0
- package/dist/plans/project-files.d.ts +8 -0
- package/dist/plans/project-files.js +309 -0
- package/dist/plans/pxtransform.d.ts +6 -0
- package/dist/{pxtransform.js → plans/pxtransform.js} +14 -17
- package/dist/plans/scaffold.d.ts +10 -0
- package/dist/plans/scaffold.js +114 -0
- package/dist/plans/sources/class-components.d.ts +11 -0
- package/dist/{class-components.js → plans/sources/class-components.js} +171 -59
- package/dist/plans/sources/dead-branches.d.ts +36 -0
- package/dist/plans/sources/dead-branches.js +415 -0
- package/dist/plans/sources/index.d.ts +36 -0
- package/dist/plans/sources/index.js +57 -0
- package/dist/plans/sources/styles.d.ts +7 -0
- package/dist/plans/sources/styles.js +111 -0
- package/dist/plans/sources/transform.d.ts +9 -0
- package/dist/plans/sources/transform.js +396 -0
- package/dist/plans/sources/vendor/bundle.d.ts +23 -0
- package/dist/plans/sources/vendor/bundle.js +221 -0
- package/dist/plans/sources/vendor/class-loader.d.ts +9 -0
- package/dist/plans/sources/vendor/class-loader.js +41 -0
- package/dist/plans/sources/vendor/index.d.ts +34 -0
- package/dist/plans/sources/vendor/index.js +62 -0
- package/dist/plans/sources/vendor/manifest.d.ts +43 -0
- package/dist/plans/sources/vendor/manifest.js +182 -0
- package/dist/plans/sources/vendor/output.d.ts +4 -0
- package/dist/plans/sources/vendor/output.js +68 -0
- package/dist/project/app.d.ts +3 -0
- package/dist/project/app.js +160 -0
- package/dist/project/defines.d.ts +3 -0
- package/dist/project/defines.js +89 -0
- package/dist/project/imports.d.ts +95 -0
- package/dist/project/imports.js +122 -0
- package/dist/project/index.d.ts +82 -0
- package/dist/project/index.js +182 -0
- package/dist/project/program.d.ts +15 -0
- package/dist/project/program.js +60 -0
- package/dist/project/resolver.d.ts +24 -0
- package/dist/project/resolver.js +96 -0
- package/dist/runner.d.ts +3 -2
- package/dist/runner.js +6 -4
- package/dist/shared/errors.d.ts +6 -0
- package/dist/shared/errors.js +16 -0
- package/dist/shared/layout.d.ts +32 -0
- package/dist/shared/layout.js +85 -0
- package/dist/shared/paths.d.ts +16 -0
- package/dist/shared/paths.js +83 -0
- package/dist/shared/syntax.d.ts +31 -0
- package/dist/shared/syntax.js +276 -0
- package/dist/verify.d.ts +4 -0
- package/dist/verify.js +35 -0
- package/package.json +3 -1
- package/dist/class-components.d.ts +0 -6
- package/dist/config.d.ts +0 -8
- package/dist/config.js +0 -274
- package/dist/file-system.d.ts +0 -21
- package/dist/file-system.js +0 -42
- package/dist/files.d.ts +0 -24
- package/dist/files.js +0 -256
- package/dist/platform-branches.d.ts +0 -4
- package/dist/platform-branches.js +0 -122
- package/dist/project-files.d.ts +0 -7
- package/dist/project-files.js +0 -129
- package/dist/project.d.ts +0 -48
- package/dist/project.js +0 -314
- package/dist/pxtransform.d.ts +0 -17
- package/dist/scaffold.d.ts +0 -15
- package/dist/scaffold.js +0 -103
- package/dist/transform.d.ts +0 -11
- package/dist/transform.js +0 -318
- package/dist/validate.d.ts +0 -4
- package/dist/validate.js +0 -27
- package/templates/taro.defines.ts +0 -2
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.compileDefines = compileDefines;
|
|
7
|
+
exports.analyzeSourceBranches = analyzeSourceBranches;
|
|
8
|
+
exports.applyDefinesAndPruneBranches = applyDefinesAndPruneBranches;
|
|
9
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
10
|
+
const builtinDefineKeys = new Set([
|
|
11
|
+
'process.env.TARO_ENV',
|
|
12
|
+
'process.env.TARO_PLATFORM',
|
|
13
|
+
'process.env.NODE_ENV'
|
|
14
|
+
]);
|
|
15
|
+
function unwrapParens(expr) {
|
|
16
|
+
while (typescript_1.default.isParenthesizedExpression(expr)) {
|
|
17
|
+
expr = expr.expression;
|
|
18
|
+
}
|
|
19
|
+
return expr;
|
|
20
|
+
}
|
|
21
|
+
function parseExpressionNode(rawExpression) {
|
|
22
|
+
const sf = typescript_1.default.createSourceFile('__define__.ts', `(\n${rawExpression}\n)`, typescript_1.default.ScriptTarget.Latest, true);
|
|
23
|
+
const stmt = sf.statements[0];
|
|
24
|
+
if (!stmt || !typescript_1.default.isExpressionStatement(stmt)) {
|
|
25
|
+
return typescript_1.default.factory.createIdentifier('undefined');
|
|
26
|
+
}
|
|
27
|
+
const result = typescript_1.default.transform(unwrapParens(stmt.expression), [
|
|
28
|
+
(context) => {
|
|
29
|
+
const visit = (node) => {
|
|
30
|
+
const visited = typescript_1.default.visitEachChild(node, visit, context);
|
|
31
|
+
typescript_1.default.setTextRange(visited, { pos: -1, end: -1 });
|
|
32
|
+
return visited;
|
|
33
|
+
};
|
|
34
|
+
return (node) => visit(node);
|
|
35
|
+
}
|
|
36
|
+
]);
|
|
37
|
+
const cleaned = result.transformed[0];
|
|
38
|
+
result.dispose();
|
|
39
|
+
return cleaned;
|
|
40
|
+
}
|
|
41
|
+
function evaluateLiteralNode(expr) {
|
|
42
|
+
const node = unwrapParens(expr);
|
|
43
|
+
if (typescript_1.default.isStringLiteral(node) || typescript_1.default.isNoSubstitutionTemplateLiteral(node)) {
|
|
44
|
+
return { known: true, value: node.text, typeofResult: 'string' };
|
|
45
|
+
}
|
|
46
|
+
if (typescript_1.default.isNumericLiteral(node)) {
|
|
47
|
+
return { known: true, value: Number(node.text), typeofResult: 'number' };
|
|
48
|
+
}
|
|
49
|
+
if (node.kind === typescript_1.default.SyntaxKind.TrueKeyword || node.kind === typescript_1.default.SyntaxKind.FalseKeyword) {
|
|
50
|
+
return { known: true, value: node.kind === typescript_1.default.SyntaxKind.TrueKeyword, typeofResult: 'boolean' };
|
|
51
|
+
}
|
|
52
|
+
if (node.kind === typescript_1.default.SyntaxKind.NullKeyword) {
|
|
53
|
+
return { known: true, value: null, typeofResult: 'object' };
|
|
54
|
+
}
|
|
55
|
+
if (typescript_1.default.isIdentifier(node) && node.text === 'undefined') {
|
|
56
|
+
return { known: true, value: undefined, typeofResult: 'undefined' };
|
|
57
|
+
}
|
|
58
|
+
if (typescript_1.default.isObjectLiteralExpression(node) || typescript_1.default.isArrayLiteralExpression(node)) {
|
|
59
|
+
return { known: false, typeofResult: 'object' };
|
|
60
|
+
}
|
|
61
|
+
return { known: false };
|
|
62
|
+
}
|
|
63
|
+
function compileDefines(defines) {
|
|
64
|
+
const identifiers = new Map();
|
|
65
|
+
const chains = new Map();
|
|
66
|
+
const chainPrefixes = new Set();
|
|
67
|
+
const customKeys = new Set();
|
|
68
|
+
for (const [key, rawExpression] of Object.entries(defines)) {
|
|
69
|
+
if (!builtinDefineKeys.has(key)) {
|
|
70
|
+
customKeys.add(key);
|
|
71
|
+
}
|
|
72
|
+
const sf = typescript_1.default.createSourceFile('__eval__.ts', `(${rawExpression})`, typescript_1.default.ScriptTarget.Latest, true);
|
|
73
|
+
const stmt = sf.statements[0];
|
|
74
|
+
const staticValue = stmt && typescript_1.default.isExpressionStatement(stmt) ? evaluateLiteralNode(stmt.expression) : { known: false };
|
|
75
|
+
if (/^[A-Za-z_$][\w$]*$/.test(key)) {
|
|
76
|
+
identifiers.set(key, { rawExpression, staticValue });
|
|
77
|
+
}
|
|
78
|
+
else if (/^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/.test(key)) {
|
|
79
|
+
chains.set(key, { rawExpression, staticValue });
|
|
80
|
+
const segments = key.split('.');
|
|
81
|
+
for (let i = 1; i < segments.length; i++) {
|
|
82
|
+
chainPrefixes.add(segments.slice(0, i).join('.'));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return { identifiers, chains, chainPrefixes, customKeys };
|
|
87
|
+
}
|
|
88
|
+
function isWriteTarget(node) {
|
|
89
|
+
let cur = node;
|
|
90
|
+
while (cur.parent && typescript_1.default.isParenthesizedExpression(cur.parent)) {
|
|
91
|
+
cur = cur.parent;
|
|
92
|
+
}
|
|
93
|
+
const parent = cur.parent;
|
|
94
|
+
if (!parent) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return Boolean((typescript_1.default.isBinaryExpression(parent) &&
|
|
98
|
+
parent.left === cur &&
|
|
99
|
+
parent.operatorToken.kind >= typescript_1.default.SyntaxKind.FirstAssignment &&
|
|
100
|
+
parent.operatorToken.kind <= typescript_1.default.SyntaxKind.LastAssignment) ||
|
|
101
|
+
((typescript_1.default.isPrefixUnaryExpression(parent) || typescript_1.default.isPostfixUnaryExpression(parent)) &&
|
|
102
|
+
(parent.operator === typescript_1.default.SyntaxKind.PlusPlusToken ||
|
|
103
|
+
parent.operator === typescript_1.default.SyntaxKind.MinusMinusToken)) ||
|
|
104
|
+
typescript_1.default.isDeleteExpression(parent) ||
|
|
105
|
+
((typescript_1.default.isForInStatement(parent) || typescript_1.default.isForOfStatement(parent)) && parent.initializer === cur));
|
|
106
|
+
}
|
|
107
|
+
function isDeclarationOrMemberKey(node) {
|
|
108
|
+
const parent = node.parent;
|
|
109
|
+
if (!parent) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
if (((typescript_1.default.isPropertyAccessExpression(parent) ||
|
|
113
|
+
typescript_1.default.isPropertyAssignment(parent) ||
|
|
114
|
+
typescript_1.default.isMethodDeclaration(parent) ||
|
|
115
|
+
typescript_1.default.isPropertyDeclaration(parent) ||
|
|
116
|
+
typescript_1.default.isGetAccessorDeclaration(parent) ||
|
|
117
|
+
typescript_1.default.isSetAccessorDeclaration(parent) ||
|
|
118
|
+
typescript_1.default.isEnumMember(parent) ||
|
|
119
|
+
typescript_1.default.isJsxAttribute(parent) ||
|
|
120
|
+
typescript_1.default.isVariableDeclaration(parent) ||
|
|
121
|
+
typescript_1.default.isParameter(parent) ||
|
|
122
|
+
typescript_1.default.isFunctionLike(parent) ||
|
|
123
|
+
typescript_1.default.isClassLike(parent)) &&
|
|
124
|
+
parent.name === node) ||
|
|
125
|
+
(typescript_1.default.isBindingElement(parent) && (parent.name === node || parent.propertyName === node)) ||
|
|
126
|
+
typescript_1.default.isImportSpecifier(parent) ||
|
|
127
|
+
typescript_1.default.isImportClause(parent) ||
|
|
128
|
+
typescript_1.default.isNamespaceImport(parent) ||
|
|
129
|
+
typescript_1.default.isExportSpecifier(parent) ||
|
|
130
|
+
typescript_1.default.isLabeledStatement(parent) ||
|
|
131
|
+
typescript_1.default.isBreakOrContinueStatement(parent)) {
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
for (let cur = node; cur; cur = cur.parent) {
|
|
135
|
+
if (typescript_1.default.isTypeNode(cur) ||
|
|
136
|
+
typescript_1.default.isInterfaceDeclaration(cur) ||
|
|
137
|
+
typescript_1.default.isTypeAliasDeclaration(cur) ||
|
|
138
|
+
typescript_1.default.isTypeParameterDeclaration(cur)) {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
function isUnshadowedGlobal(identifier, checker) {
|
|
145
|
+
if (!checker) {
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
const symbol = identifier.parent && typescript_1.default.isShorthandPropertyAssignment(identifier.parent)
|
|
149
|
+
? checker.getShorthandAssignmentValueSymbol(identifier.parent)
|
|
150
|
+
: checker.getSymbolAtLocation(identifier);
|
|
151
|
+
return (!symbol?.declarations?.length || symbol.declarations.every((d) => d.getSourceFile().isDeclarationFile));
|
|
152
|
+
}
|
|
153
|
+
function extractPropertyChain(node) {
|
|
154
|
+
const segments = [node.name.text];
|
|
155
|
+
let cur = node.expression;
|
|
156
|
+
while (typescript_1.default.isPropertyAccessExpression(cur)) {
|
|
157
|
+
segments.push(cur.name.text);
|
|
158
|
+
cur = cur.expression;
|
|
159
|
+
}
|
|
160
|
+
if (!typescript_1.default.isIdentifier(cur)) {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
segments.push(cur.text);
|
|
164
|
+
return { root: cur, path: segments.reverse().join('.') };
|
|
165
|
+
}
|
|
166
|
+
function matchDefineEntry(node, compiled, checker) {
|
|
167
|
+
if (typescript_1.default.isIdentifier(node)) {
|
|
168
|
+
const entry = compiled.identifiers.get(node.text);
|
|
169
|
+
return entry &&
|
|
170
|
+
!isDeclarationOrMemberKey(node) &&
|
|
171
|
+
!isWriteTarget(node) &&
|
|
172
|
+
isUnshadowedGlobal(node, checker)
|
|
173
|
+
? entry
|
|
174
|
+
: undefined;
|
|
175
|
+
}
|
|
176
|
+
if (typescript_1.default.isPropertyAccessExpression(node) && !isWriteTarget(node)) {
|
|
177
|
+
const chain = extractPropertyChain(node);
|
|
178
|
+
const entry = chain ? compiled.chains.get(chain.path) : undefined;
|
|
179
|
+
return chain && entry && isUnshadowedGlobal(chain.root, checker) ? entry : undefined;
|
|
180
|
+
}
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
function markSubtreeDead(root, deadNodes, deadNames) {
|
|
184
|
+
if (!root) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const walk = (node) => {
|
|
188
|
+
deadNodes.add(node);
|
|
189
|
+
if (typescript_1.default.isIdentifier(node)) {
|
|
190
|
+
deadNames.add(node.text);
|
|
191
|
+
}
|
|
192
|
+
typescript_1.default.forEachChild(node, walk);
|
|
193
|
+
};
|
|
194
|
+
walk(root);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Stage 1 (Read-Only): Evaluates `defineConstants`/`env` and marks dead conditional branches in application
|
|
198
|
+
* `src/` files directly on the original `ts.SourceFile` while AST nodes stay bound to `ts.TypeChecker`.
|
|
199
|
+
*/
|
|
200
|
+
function analyzeSourceBranches(source, compiled, checker) {
|
|
201
|
+
const deadNodes = new Set();
|
|
202
|
+
const branchDecisions = new Map();
|
|
203
|
+
const defineReplacements = new Map();
|
|
204
|
+
const deadIdentifierNames = new Set();
|
|
205
|
+
if (source.isDeclarationFile) {
|
|
206
|
+
return { deadNodes, branchDecisions, defineReplacements, deadIdentifierNames };
|
|
207
|
+
}
|
|
208
|
+
function evaluate(expr) {
|
|
209
|
+
const node = unwrapParens(expr);
|
|
210
|
+
const matched = matchDefineEntry(node, compiled, checker);
|
|
211
|
+
if (matched) {
|
|
212
|
+
return matched.staticValue;
|
|
213
|
+
}
|
|
214
|
+
const lit = evaluateLiteralNode(node);
|
|
215
|
+
if (lit.known || lit.typeofResult) {
|
|
216
|
+
if (typescript_1.default.isIdentifier(node) && node.text === 'undefined' && !isUnshadowedGlobal(node, checker)) {
|
|
217
|
+
return { known: false };
|
|
218
|
+
}
|
|
219
|
+
return lit;
|
|
220
|
+
}
|
|
221
|
+
if (typescript_1.default.isTypeOfExpression(node)) {
|
|
222
|
+
const target = unwrapParens(node.expression);
|
|
223
|
+
const inner = evaluate(target);
|
|
224
|
+
if (inner.typeofResult) {
|
|
225
|
+
return { known: true, value: inner.typeofResult, typeofResult: 'string' };
|
|
226
|
+
}
|
|
227
|
+
const chain = typescript_1.default.isPropertyAccessExpression(target) ? extractPropertyChain(target) : undefined;
|
|
228
|
+
if (chain && compiled.chainPrefixes.has(chain.path) && isUnshadowedGlobal(chain.root, checker)) {
|
|
229
|
+
return { known: true, value: 'object', typeofResult: 'string' };
|
|
230
|
+
}
|
|
231
|
+
return { known: false };
|
|
232
|
+
}
|
|
233
|
+
if (typescript_1.default.isPrefixUnaryExpression(node) && node.operator === typescript_1.default.SyntaxKind.ExclamationToken) {
|
|
234
|
+
const sub = evaluate(node.operand);
|
|
235
|
+
return sub.known ? { known: true, value: !sub.value, typeofResult: 'boolean' } : { known: false };
|
|
236
|
+
}
|
|
237
|
+
if (typescript_1.default.isBinaryExpression(node)) {
|
|
238
|
+
const op = node.operatorToken.kind;
|
|
239
|
+
if (op === typescript_1.default.SyntaxKind.AmpersandAmpersandToken || op === typescript_1.default.SyntaxKind.BarBarToken) {
|
|
240
|
+
const left = evaluate(node.left);
|
|
241
|
+
if (!left.known) {
|
|
242
|
+
return { known: false };
|
|
243
|
+
}
|
|
244
|
+
const shortCircuits = op === typescript_1.default.SyntaxKind.AmpersandAmpersandToken ? !left.value : Boolean(left.value);
|
|
245
|
+
return shortCircuits ? left : evaluate(node.right);
|
|
246
|
+
}
|
|
247
|
+
if (op === typescript_1.default.SyntaxKind.EqualsEqualsEqualsToken || op === typescript_1.default.SyntaxKind.ExclamationEqualsEqualsToken) {
|
|
248
|
+
const left = evaluate(node.left);
|
|
249
|
+
const right = evaluate(node.right);
|
|
250
|
+
if (left.known && right.known) {
|
|
251
|
+
const eq = left.value === right.value;
|
|
252
|
+
return {
|
|
253
|
+
known: true,
|
|
254
|
+
value: op === typescript_1.default.SyntaxKind.EqualsEqualsEqualsToken ? eq : !eq,
|
|
255
|
+
typeofResult: 'boolean'
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (typescript_1.default.isConditionalExpression(node)) {
|
|
261
|
+
const cond = evaluate(node.condition);
|
|
262
|
+
if (cond.known) {
|
|
263
|
+
return evaluate(cond.value ? node.whenTrue : node.whenFalse);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return { known: false };
|
|
267
|
+
}
|
|
268
|
+
function visitLive(node) {
|
|
269
|
+
if (typescript_1.default.isImportDeclaration(node) ||
|
|
270
|
+
typescript_1.default.isTypeNode(node) ||
|
|
271
|
+
typescript_1.default.isInterfaceDeclaration(node) ||
|
|
272
|
+
typescript_1.default.isTypeAliasDeclaration(node)) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (typescript_1.default.isIfStatement(node)) {
|
|
276
|
+
const cond = evaluate(node.expression);
|
|
277
|
+
if (cond.known) {
|
|
278
|
+
const outcome = Boolean(cond.value);
|
|
279
|
+
branchDecisions.set(node, outcome);
|
|
280
|
+
markSubtreeDead(node.expression, deadNodes, deadIdentifierNames);
|
|
281
|
+
markSubtreeDead(outcome ? node.elseStatement : node.thenStatement, deadNodes, deadIdentifierNames);
|
|
282
|
+
const kept = outcome ? node.thenStatement : node.elseStatement;
|
|
283
|
+
if (kept) {
|
|
284
|
+
visitLive(kept);
|
|
285
|
+
}
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (typescript_1.default.isConditionalExpression(node)) {
|
|
290
|
+
const cond = evaluate(node.condition);
|
|
291
|
+
if (cond.known) {
|
|
292
|
+
const outcome = Boolean(cond.value);
|
|
293
|
+
branchDecisions.set(node, outcome);
|
|
294
|
+
markSubtreeDead(node.condition, deadNodes, deadIdentifierNames);
|
|
295
|
+
markSubtreeDead(outcome ? node.whenFalse : node.whenTrue, deadNodes, deadIdentifierNames);
|
|
296
|
+
visitLive(outcome ? node.whenTrue : node.whenFalse);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (typescript_1.default.isBinaryExpression(node) &&
|
|
301
|
+
(node.operatorToken.kind === typescript_1.default.SyntaxKind.AmpersandAmpersandToken ||
|
|
302
|
+
node.operatorToken.kind === typescript_1.default.SyntaxKind.BarBarToken)) {
|
|
303
|
+
const left = evaluate(node.left);
|
|
304
|
+
if (left.known) {
|
|
305
|
+
const shortCircuits = node.operatorToken.kind === typescript_1.default.SyntaxKind.AmpersandAmpersandToken
|
|
306
|
+
? !left.value
|
|
307
|
+
: Boolean(left.value);
|
|
308
|
+
branchDecisions.set(node, shortCircuits);
|
|
309
|
+
if (shortCircuits) {
|
|
310
|
+
visitLive(node.left);
|
|
311
|
+
markSubtreeDead(node.right, deadNodes, deadIdentifierNames);
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
markSubtreeDead(node.left, deadNodes, deadIdentifierNames);
|
|
315
|
+
visitLive(node.right);
|
|
316
|
+
}
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (typescript_1.default.isTypeOfExpression(node)) {
|
|
321
|
+
const folded = evaluate(node);
|
|
322
|
+
if (folded.known && typeof folded.value === 'string') {
|
|
323
|
+
defineReplacements.set(node, JSON.stringify(folded.value));
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (typescript_1.default.isPropertyAccessExpression(node) || typescript_1.default.isIdentifier(node)) {
|
|
328
|
+
if (node.parent &&
|
|
329
|
+
typescript_1.default.isPropertyAccessExpression(node.parent) &&
|
|
330
|
+
node.parent.expression === node &&
|
|
331
|
+
matchDefineEntry(node.parent, compiled, checker)) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const matched = matchDefineEntry(node, compiled, checker);
|
|
335
|
+
if (matched) {
|
|
336
|
+
defineReplacements.set(node, matched.rawExpression);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
typescript_1.default.forEachChild(node, visitLive);
|
|
341
|
+
}
|
|
342
|
+
typescript_1.default.forEachChild(source, visitLive);
|
|
343
|
+
return { deadNodes, branchDecisions, defineReplacements, deadIdentifierNames };
|
|
344
|
+
}
|
|
345
|
+
/** Stage 3: Replaces global constants, prunes dead branches, and strips dead imports across all modules. */
|
|
346
|
+
function applyDefinesAndPruneBranches(source, analysis, liveReferencedNames) {
|
|
347
|
+
const { branchDecisions, defineReplacements, deadIdentifierNames } = analysis;
|
|
348
|
+
if (!branchDecisions.size && !defineReplacements.size) {
|
|
349
|
+
return source;
|
|
350
|
+
}
|
|
351
|
+
const result = typescript_1.default.transform(source, [
|
|
352
|
+
(context) => {
|
|
353
|
+
const visit = (node) => {
|
|
354
|
+
if (typescript_1.default.isIfStatement(node) && branchDecisions.has(node)) {
|
|
355
|
+
const selected = branchDecisions.get(node) ? node.thenStatement : node.elseStatement;
|
|
356
|
+
return selected
|
|
357
|
+
? (typescript_1.default.visitNode(selected, visit) ?? typescript_1.default.factory.createEmptyStatement())
|
|
358
|
+
: typescript_1.default.factory.createEmptyStatement();
|
|
359
|
+
}
|
|
360
|
+
if (typescript_1.default.isConditionalExpression(node) && branchDecisions.has(node)) {
|
|
361
|
+
return typescript_1.default.visitNode(branchDecisions.get(node) ? node.whenTrue : node.whenFalse, visit);
|
|
362
|
+
}
|
|
363
|
+
if (typescript_1.default.isBinaryExpression(node) && branchDecisions.has(node)) {
|
|
364
|
+
return typescript_1.default.visitNode(branchDecisions.get(node) ? node.left : node.right, visit);
|
|
365
|
+
}
|
|
366
|
+
if (typescript_1.default.isShorthandPropertyAssignment(node) && defineReplacements.has(node.name)) {
|
|
367
|
+
return typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier(node.name.text), parseExpressionNode(defineReplacements.get(node.name)));
|
|
368
|
+
}
|
|
369
|
+
if (defineReplacements.has(node)) {
|
|
370
|
+
return parseExpressionNode(defineReplacements.get(node));
|
|
371
|
+
}
|
|
372
|
+
return typescript_1.default.visitEachChild(node, visit, context);
|
|
373
|
+
};
|
|
374
|
+
return (file) => typescript_1.default.visitNode(file, visit, typescript_1.default.isSourceFile);
|
|
375
|
+
}
|
|
376
|
+
]);
|
|
377
|
+
const transformed = result.transformed[0];
|
|
378
|
+
result.dispose();
|
|
379
|
+
if (!branchDecisions.size) {
|
|
380
|
+
return transformed;
|
|
381
|
+
}
|
|
382
|
+
const isPruned = (name) => deadIdentifierNames.has(name) && !liveReferencedNames.has(name);
|
|
383
|
+
return typescript_1.default.factory.updateSourceFile(transformed, transformed.statements.flatMap((statement) => {
|
|
384
|
+
if (typescript_1.default.isEmptyStatement(statement)) {
|
|
385
|
+
return [];
|
|
386
|
+
}
|
|
387
|
+
if (!typescript_1.default.isImportDeclaration(statement) || !statement.importClause) {
|
|
388
|
+
return [statement];
|
|
389
|
+
}
|
|
390
|
+
const clause = statement.importClause;
|
|
391
|
+
const keepDefault = clause.name && !isPruned(clause.name.text) ? clause.name : undefined;
|
|
392
|
+
let keepNamed = clause.namedBindings;
|
|
393
|
+
if (keepNamed && typescript_1.default.isNamespaceImport(keepNamed) && isPruned(keepNamed.name.text)) {
|
|
394
|
+
keepNamed = undefined;
|
|
395
|
+
}
|
|
396
|
+
else if (keepNamed && typescript_1.default.isNamedImports(keepNamed)) {
|
|
397
|
+
const kept = keepNamed.elements.filter((el) => !isPruned(el.name.text));
|
|
398
|
+
keepNamed = kept.length
|
|
399
|
+
? kept.length === keepNamed.elements.length
|
|
400
|
+
? keepNamed
|
|
401
|
+
: typescript_1.default.factory.updateNamedImports(keepNamed, kept)
|
|
402
|
+
: undefined;
|
|
403
|
+
}
|
|
404
|
+
if (!keepDefault && !keepNamed) {
|
|
405
|
+
return [];
|
|
406
|
+
}
|
|
407
|
+
if (keepDefault === clause.name && keepNamed === clause.namedBindings) {
|
|
408
|
+
return [statement];
|
|
409
|
+
}
|
|
410
|
+
return [
|
|
411
|
+
typescript_1.default.factory.updateImportDeclaration(statement, statement.modifiers, typescript_1.default.factory.updateImportClause(clause, clause.isTypeOnly, keepDefault, keepNamed), statement.moduleSpecifier, statement.attributes)
|
|
412
|
+
];
|
|
413
|
+
}));
|
|
414
|
+
}
|
|
415
|
+
//# sourceMappingURL=dead-branches.js.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { IPluginContext } from '@tarojs/service';
|
|
2
|
+
import type { ProjectModel } from '../../project';
|
|
3
|
+
import { type SourceImport, type VendorPackage } from './vendor';
|
|
4
|
+
import type { VendorManifest } from './vendor/manifest';
|
|
5
|
+
/**
|
|
6
|
+
* The generated application sources, computed without writing anything. Paths are relative to `targetSource`
|
|
7
|
+
* (`src/` or `src/modules/<moduleName>/`), so the same graph can be emitted into a staging directory.
|
|
8
|
+
*/
|
|
9
|
+
export interface SourceGraph {
|
|
10
|
+
/** Add a vendor back-import to the same discovery, skipping previously collected modules. */
|
|
11
|
+
collect(filename: string): string;
|
|
12
|
+
has(filename: string): boolean;
|
|
13
|
+
/** Generated text of each rewritten module, rewritten stylesheet, setup file and ambient declarations. */
|
|
14
|
+
files: Map<string, string>;
|
|
15
|
+
/** Reachable files copied unchanged from the source root (styles, images and other assets), keyed like `files`. */
|
|
16
|
+
copies: Map<string, string>;
|
|
17
|
+
/** Reachable script files, relative to the source root. */
|
|
18
|
+
sourceFiles: string[];
|
|
19
|
+
/** Reachable non-script style and static asset files, relative to the source root. */
|
|
20
|
+
assetFiles: string[];
|
|
21
|
+
/** Third-party modules to bundle into the vendor directory. */
|
|
22
|
+
vendor: VendorManifest;
|
|
23
|
+
/** Discovery warnings for the generation report. */
|
|
24
|
+
warnings: string[];
|
|
25
|
+
}
|
|
26
|
+
/** The application sources and their bundled packages, plus what the generation report lists about them. */
|
|
27
|
+
export interface SourcesPlan {
|
|
28
|
+
warnings: string[];
|
|
29
|
+
writeSource(stagingDirectory: string): void;
|
|
30
|
+
sourceFiles: string[];
|
|
31
|
+
assetFiles: string[];
|
|
32
|
+
vendor: VendorPackage[];
|
|
33
|
+
sourceImports: SourceImport[];
|
|
34
|
+
}
|
|
35
|
+
export declare function planSources(ctx: IPluginContext, model: ProjectModel): Promise<SourcesPlan>;
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.planSources = planSources;
|
|
7
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const emit_1 = require("../../emit");
|
|
9
|
+
const paths_1 = require("../../shared/paths");
|
|
10
|
+
const transform_1 = require("./transform");
|
|
11
|
+
const vendor_1 = require("./vendor");
|
|
12
|
+
/**
|
|
13
|
+
* Discovers the application sources and bundles their vendor imports, in memory. Bundled packages may
|
|
14
|
+
* import application modules that no page reaches (for example through `~/utils/...`); those join the
|
|
15
|
+
* discovery without transforming previously collected modules again.
|
|
16
|
+
*/
|
|
17
|
+
async function discoverAndBundle(ctx, model) {
|
|
18
|
+
const graph = (0, transform_1.discoverSources)(ctx, model);
|
|
19
|
+
let vendor;
|
|
20
|
+
for (;;) {
|
|
21
|
+
vendor = await (0, vendor_1.buildVendor)(ctx, model, graph.vendor, vendor);
|
|
22
|
+
const missing = vendor.sourceFiles.filter((file) => !graph.has(file));
|
|
23
|
+
if (!missing.length) {
|
|
24
|
+
return { graph, vendor };
|
|
25
|
+
}
|
|
26
|
+
const missingScripts = missing.filter((file) => paths_1.sourcePattern.test(file));
|
|
27
|
+
if (missingScripts.length) {
|
|
28
|
+
model.program.ensureFiles(missingScripts);
|
|
29
|
+
}
|
|
30
|
+
missing.forEach(graph.collect);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async function planSources(ctx, model) {
|
|
34
|
+
const { targetSource } = model.layout;
|
|
35
|
+
const { graph, vendor } = await discoverAndBundle(ctx, model);
|
|
36
|
+
const sourcePaths = new Map(vendor.sourceFiles.map((file) => [
|
|
37
|
+
(0, paths_1.posixPath)(node_path_1.default.relative(ctx.paths.sourcePath, file)),
|
|
38
|
+
node_path_1.default.relative(targetSource, graph.collect(file))
|
|
39
|
+
]));
|
|
40
|
+
return {
|
|
41
|
+
warnings: graph.warnings,
|
|
42
|
+
sourceFiles: graph.sourceFiles,
|
|
43
|
+
assetFiles: graph.assetFiles,
|
|
44
|
+
vendor: vendor.packages,
|
|
45
|
+
sourceImports: vendor.sourceImports,
|
|
46
|
+
writeSource(stagingDirectory) {
|
|
47
|
+
for (const [relative, source] of graph.copies) {
|
|
48
|
+
(0, emit_1.copyFile)(source, node_path_1.default.join(stagingDirectory, relative));
|
|
49
|
+
}
|
|
50
|
+
for (const [relative, content] of graph.files) {
|
|
51
|
+
(0, emit_1.writeText)(node_path_1.default.join(stagingDirectory, relative), content);
|
|
52
|
+
}
|
|
53
|
+
vendor.write(stagingDirectory, sourcePaths);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LocalResolver } from '../../project/resolver';
|
|
2
|
+
/**
|
|
3
|
+
* Rewrites Taro alias specifiers (`@/...`, `~/...`) inside stylesheets to relative paths within `targetSource`,
|
|
4
|
+
* leaving native relative `@import "variables"`, Less `@import (reference)`, and `url(bg.png)` untouched.
|
|
5
|
+
*/
|
|
6
|
+
export declare function rewriteStylesheetAliases(filename: string, outputFile: string, sourcePath: string, outputFor: (targetFile: string) => string, resolveLocal: LocalResolver): string | undefined;
|
|
7
|
+
//# sourceMappingURL=styles.d.ts.map
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.rewriteStylesheetAliases = rewriteStylesheetAliases;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const imports_1 = require("../../project/imports");
|
|
10
|
+
const errors_1 = require("../../shared/errors");
|
|
11
|
+
const paths_1 = require("../../shared/paths");
|
|
12
|
+
const styleExts = ['.scss', '.sass', '.less', '.styl', '.stylus', '.pcss', '.css'];
|
|
13
|
+
function resolveAliasedStyleTarget(fromStyleFile, clean, kind, resolveLocal) {
|
|
14
|
+
const isFile = (p) => Boolean(p && node_fs_1.default.statSync(p, { throwIfNoEntry: false })?.isFile());
|
|
15
|
+
const direct = resolveLocal(fromStyleFile, clean);
|
|
16
|
+
if (isFile(direct)) {
|
|
17
|
+
return direct;
|
|
18
|
+
}
|
|
19
|
+
if (kind === 'url') {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
const dir = node_path_1.default.posix.dirname(clean);
|
|
23
|
+
const base = node_path_1.default.posix.basename(clean);
|
|
24
|
+
const partial = dir === '.' ? `_${base}` : `${dir}/${base.startsWith('_') ? base : `_${base}`}`;
|
|
25
|
+
if (node_path_1.default.posix.extname(clean)) {
|
|
26
|
+
const hit = resolveLocal(fromStyleFile, partial);
|
|
27
|
+
return isFile(hit) ? hit : undefined;
|
|
28
|
+
}
|
|
29
|
+
for (const ext of styleExts) {
|
|
30
|
+
for (const candidate of [`${clean}${ext}`, `${partial}${ext}`]) {
|
|
31
|
+
const hit = resolveLocal(fromStyleFile, candidate);
|
|
32
|
+
if (isFile(hit)) {
|
|
33
|
+
return hit;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Rewrites Taro alias specifiers (`@/...`, `~/...`) inside stylesheets to relative paths within `targetSource`,
|
|
41
|
+
* leaving native relative `@import "variables"`, Less `@import (reference)`, and `url(bg.png)` untouched.
|
|
42
|
+
*/
|
|
43
|
+
function rewriteStylesheetAliases(filename, outputFile, sourcePath, outputFor, resolveLocal) {
|
|
44
|
+
const raw = node_fs_1.default.readFileSync(filename, 'utf8');
|
|
45
|
+
const isCss = node_path_1.default.extname(filename).toLowerCase() === '.css';
|
|
46
|
+
const pattern = isCss
|
|
47
|
+
? /\/\*[\s\S]*?\*\/|@(?:import|use|forward)\s+(?:\([^)]*\)\s*)*(?:url\(\s*)?(["'])((?:\\.|(?!\1)[^\\])*)\1|(?<![\w-])url\(\s*(?:(["'])((?:\\.|(?!\3)[^\\])*)\3|([^)\s"']+))|(["'])(?:\\.|(?!\6)[^\\])*\6/gi
|
|
48
|
+
: /\/\*[\s\S]*?\*\/|\/\/[^\n]*|@(?:import|use|forward)\s+(?:\([^)]*\)\s*)*(?:url\(\s*)?(["'])((?:\\.|(?!\1)[^\\])*)\1|(?<![\w-])url\(\s*(?:(["'])((?:\\.|(?!\3)[^\\])*)\3|([^)\s"']+))|(["'])(?:\\.|(?!\6)[^\\])*\6/gi;
|
|
49
|
+
const edits = [];
|
|
50
|
+
for (const match of raw.matchAll(pattern)) {
|
|
51
|
+
const full = match[0];
|
|
52
|
+
const index = match.index;
|
|
53
|
+
let kind;
|
|
54
|
+
let spec = '';
|
|
55
|
+
let start = 0;
|
|
56
|
+
if (match[2] !== undefined) {
|
|
57
|
+
kind = 'import';
|
|
58
|
+
spec = match[2];
|
|
59
|
+
start = index + full.lastIndexOf(match[1] + spec) + 1;
|
|
60
|
+
}
|
|
61
|
+
else if (match[4] !== undefined) {
|
|
62
|
+
kind = 'url';
|
|
63
|
+
spec = match[4];
|
|
64
|
+
start = index + full.lastIndexOf(match[3] + spec) + 1;
|
|
65
|
+
}
|
|
66
|
+
else if (match[5] !== undefined) {
|
|
67
|
+
kind = 'url';
|
|
68
|
+
spec = match[5];
|
|
69
|
+
start = index + full.lastIndexOf(spec);
|
|
70
|
+
}
|
|
71
|
+
if (!kind || !spec || /^(?:data:|sass:|https?:|\/\/|#)/i.test(spec.trim())) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const stripped = spec.startsWith('~') ? spec.slice(1) : spec;
|
|
75
|
+
const [, clean = '', suffix = ''] = stripped.match(/^([^?#]*)(.*)$/) ?? [];
|
|
76
|
+
if (!clean || !(0, imports_1.isBareSpecifier)(clean)) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
// Skip if a same-directory relative file or `_partial` exists natively.
|
|
80
|
+
const localCandidate = node_path_1.default.resolve(node_path_1.default.dirname(filename), clean);
|
|
81
|
+
const localPartial = node_path_1.default.join(node_path_1.default.dirname(localCandidate), `_${node_path_1.default.basename(localCandidate)}`);
|
|
82
|
+
if (!spec.startsWith('~') &&
|
|
83
|
+
[
|
|
84
|
+
localCandidate,
|
|
85
|
+
localPartial,
|
|
86
|
+
...styleExts.flatMap((e) => [`${localCandidate}${e}`, `${localPartial}${e}`])
|
|
87
|
+
].some((p) => node_fs_1.default.statSync(p, { throwIfNoEntry: false })?.isFile())) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const target = resolveAliasedStyleTarget(filename, clean, kind, resolveLocal);
|
|
91
|
+
if (!target) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (!(0, paths_1.inside)(sourcePath, target)) {
|
|
95
|
+
(0, errors_1.fail)(`Stylesheet dependency leaves sourceRoot: ${target} (from ${filename})`);
|
|
96
|
+
}
|
|
97
|
+
const replacement = (0, paths_1.relativeAsset)(outputFile, outputFor(target)) + suffix;
|
|
98
|
+
if (replacement !== spec) {
|
|
99
|
+
edits.push({ start, end: start + spec.length, replacement });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!edits.length) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
let updated = raw;
|
|
106
|
+
for (let i = edits.length - 1; i >= 0; i--) {
|
|
107
|
+
updated = updated.slice(0, edits[i].start) + edits[i].replacement + updated.slice(edits[i].end);
|
|
108
|
+
}
|
|
109
|
+
return updated;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=styles.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { IPluginContext } from '@tarojs/service';
|
|
2
|
+
import type { ProjectModel } from '../../project';
|
|
3
|
+
import type { SourceGraph } from '.';
|
|
4
|
+
/**
|
|
5
|
+
* Discovers the application sources, styles and static assets reachable from the pages (and `app.doubao.*`)
|
|
6
|
+
* using the 3-stage `ts.Program + TypeChecker` pipeline, without writing anything to disk.
|
|
7
|
+
*/
|
|
8
|
+
export declare function discoverSources(ctx: IPluginContext, model: ProjectModel): SourceGraph;
|
|
9
|
+
//# sourceMappingURL=transform.d.ts.map
|