@tarojs/helper 3.5.0-canary.0 → 3.5.0-canary.1
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/babelRegister.js +37 -0
- package/dist/index.js +1 -0
- package/dist/utils.js +134 -3
- package/package.json +3 -3
- package/types/babelRegister.d.ts +3 -0
- package/types/index.d.ts +3 -1
- package/types/utils.d.ts +1 -0
package/dist/babelRegister.js
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.injectDefineConfigHeader = void 0;
|
|
3
4
|
const path = require("path");
|
|
5
|
+
/**
|
|
6
|
+
* Inject `defineAppConfig` and `definePageConfig`
|
|
7
|
+
* require header at the top of a config file,
|
|
8
|
+
* without the need to specifically require them
|
|
9
|
+
* if they are used
|
|
10
|
+
*/
|
|
11
|
+
function injectDefineConfigHeader(babel) {
|
|
12
|
+
const appConfig = 'function defineAppConfig(config) { return config }';
|
|
13
|
+
const pageConfig = 'function definePageConfig(config) { return config }';
|
|
14
|
+
const prependHeader = (nodePath, header) => {
|
|
15
|
+
const parsedHeader = babel.parse(header, { filename: '' }).program.body[0];
|
|
16
|
+
nodePath.node.body.unshift(parsedHeader);
|
|
17
|
+
};
|
|
18
|
+
const enterHandler = (nodePath) => {
|
|
19
|
+
const { scope, node } = nodePath;
|
|
20
|
+
scope.traverse(node, {
|
|
21
|
+
CallExpression(p) {
|
|
22
|
+
const callee = p.node.callee;
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
switch (callee.name) {
|
|
25
|
+
case 'defineAppConfig':
|
|
26
|
+
return prependHeader(nodePath, appConfig);
|
|
27
|
+
case 'definePageConfig':
|
|
28
|
+
return prependHeader(nodePath, pageConfig);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
visitor: {
|
|
35
|
+
Program: { enter: enterHandler }
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
exports.injectDefineConfigHeader = injectDefineConfigHeader;
|
|
4
40
|
function createBabelRegister({ only }) {
|
|
5
41
|
require('@babel/register')({
|
|
6
42
|
only: Array.from(new Set([...only])),
|
|
@@ -9,6 +45,7 @@ function createBabelRegister({ only }) {
|
|
|
9
45
|
require.resolve('@babel/preset-typescript')
|
|
10
46
|
],
|
|
11
47
|
plugins: [
|
|
48
|
+
injectDefineConfigHeader,
|
|
12
49
|
[require.resolve('@babel/plugin-proposal-decorators'), {
|
|
13
50
|
legacy: true
|
|
14
51
|
}],
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,7 @@ const npm = require("./npm");
|
|
|
11
11
|
const babelRegister_1 = require("./babelRegister");
|
|
12
12
|
exports.helper = Object.assign(Object.assign(Object.assign({}, constants), utils), { npm,
|
|
13
13
|
createBabelRegister: babelRegister_1.default,
|
|
14
|
+
injectDefineConfigHeader: babelRegister_1.injectDefineConfigHeader,
|
|
14
15
|
fs,
|
|
15
16
|
chalk,
|
|
16
17
|
chokidar,
|
package/dist/utils.js
CHANGED
|
@@ -9,12 +9,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.readConfig = exports.removeHeadSlash = exports.getModuleDefaultExport = exports.addPlatforms = exports.extnameExpRegOf = exports.readDirWithFileTypes = exports.getAllFilesInFloder = exports.unzip = exports.applyArrayedVisitors = exports.mergeVisitors = exports.recursiveMerge = exports.getInstalledNpmPkgVersion = exports.getInstalledNpmPkgPath = exports.pascalCase = exports.emptyDirectory = exports.cssImports = exports.generateConstantsList = exports.generateEnvList = exports.resolveScriptPath = exports.resolveMainFilePath = exports.isEmptyObject = exports.shouldUseCnpm = exports.shouldUseYarn = exports.getSystemUsername = exports.getConfig = exports.getTaroPath = exports.getUserHomeDir = exports.recursiveFindNodeModules = exports.printLog = exports.resolveStylePath = exports.promoteRelativePath = exports.replaceAliasPath = exports.isAliasPath = exports.isQuickAppPkg = exports.isNpmPkg = exports.isNodeModule = exports.normalizePath = void 0;
|
|
12
|
+
exports.readConfig = exports.readPageConfig = exports.removeHeadSlash = exports.getModuleDefaultExport = exports.addPlatforms = exports.extnameExpRegOf = exports.readDirWithFileTypes = exports.getAllFilesInFloder = exports.unzip = exports.applyArrayedVisitors = exports.mergeVisitors = exports.recursiveMerge = exports.getInstalledNpmPkgVersion = exports.getInstalledNpmPkgPath = exports.pascalCase = exports.emptyDirectory = exports.cssImports = exports.generateConstantsList = exports.generateEnvList = exports.resolveScriptPath = exports.resolveMainFilePath = exports.isEmptyObject = exports.shouldUseCnpm = exports.shouldUseYarn = exports.getSystemUsername = exports.getConfig = exports.getTaroPath = exports.getUserHomeDir = exports.recursiveFindNodeModules = exports.printLog = exports.resolveStylePath = exports.promoteRelativePath = exports.replaceAliasPath = exports.isAliasPath = exports.isQuickAppPkg = exports.isNpmPkg = exports.isNodeModule = exports.normalizePath = void 0;
|
|
13
13
|
const fs = require("fs-extra");
|
|
14
14
|
const path = require("path");
|
|
15
15
|
const os = require("os");
|
|
16
16
|
const stream_1 = require("stream");
|
|
17
17
|
const child_process = require("child_process");
|
|
18
|
+
const parser = require("@babel/parser");
|
|
19
|
+
const traverse_1 = require("@babel/traverse");
|
|
18
20
|
const chalk = require("chalk");
|
|
19
21
|
const findWorkspaceRoot = require("find-yarn-workspace-root");
|
|
20
22
|
const lodash_1 = require("lodash");
|
|
@@ -489,15 +491,144 @@ function removeHeadSlash(str) {
|
|
|
489
491
|
return str.replace(/^(\/|\\)/, '');
|
|
490
492
|
}
|
|
491
493
|
exports.removeHeadSlash = removeHeadSlash;
|
|
494
|
+
function analyzeImport(filePath) {
|
|
495
|
+
const code = fs.readFileSync(filePath).toString();
|
|
496
|
+
let importPaths = [];
|
|
497
|
+
filePath = path.dirname(filePath);
|
|
498
|
+
const ast = parser.parse(code, {
|
|
499
|
+
sourceType: 'module',
|
|
500
|
+
plugins: [
|
|
501
|
+
'typescript',
|
|
502
|
+
'classProperties',
|
|
503
|
+
'objectRestSpread',
|
|
504
|
+
'optionalChaining'
|
|
505
|
+
]
|
|
506
|
+
});
|
|
507
|
+
traverse_1.default(ast, {
|
|
508
|
+
ImportDeclaration({ node }) {
|
|
509
|
+
const list = [];
|
|
510
|
+
const source = node.source.value;
|
|
511
|
+
if (path.extname(source)) {
|
|
512
|
+
const importPath = path.resolve(filePath, source);
|
|
513
|
+
list.push(importPath);
|
|
514
|
+
}
|
|
515
|
+
else {
|
|
516
|
+
['.js', '.ts', '.json'].forEach(ext => {
|
|
517
|
+
const importPath = path.resolve(filePath, source + ext);
|
|
518
|
+
list.push(importPath);
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
const dep = list.find(importPath => fs.existsSync(importPath));
|
|
522
|
+
if (!dep)
|
|
523
|
+
return;
|
|
524
|
+
importPaths.push(dep);
|
|
525
|
+
importPaths = importPaths.concat(analyzeImport(dep));
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
return importPaths;
|
|
529
|
+
}
|
|
530
|
+
// converts ast nodes to js object
|
|
531
|
+
function exprToObject(node) {
|
|
532
|
+
const types = ['BooleanLiteral', 'StringLiteral', 'NumericLiteral'];
|
|
533
|
+
if (types.includes(node.type)) {
|
|
534
|
+
return node.value;
|
|
535
|
+
}
|
|
536
|
+
if (node.name === 'undefined' && !node.value) {
|
|
537
|
+
return undefined;
|
|
538
|
+
}
|
|
539
|
+
if (node.type === 'NullLiteral') {
|
|
540
|
+
return null;
|
|
541
|
+
}
|
|
542
|
+
if (node.type === 'ObjectExpression') {
|
|
543
|
+
return genProps(node.properties);
|
|
544
|
+
}
|
|
545
|
+
if (node.type === 'ArrayExpression') {
|
|
546
|
+
return node.elements.reduce((acc, el) => [
|
|
547
|
+
...acc,
|
|
548
|
+
...(el.type === 'SpreadElement'
|
|
549
|
+
? exprToObject(el.argument)
|
|
550
|
+
: [exprToObject(el)])
|
|
551
|
+
], []);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
// converts ObjectExpressions to js object
|
|
555
|
+
function genProps(props) {
|
|
556
|
+
return props.reduce((acc, prop) => {
|
|
557
|
+
if (prop.type === 'SpreadElement') {
|
|
558
|
+
return Object.assign(Object.assign({}, acc), exprToObject(prop.argument));
|
|
559
|
+
}
|
|
560
|
+
else if (prop.type !== 'ObjectMethod') {
|
|
561
|
+
const v = exprToObject(prop.value);
|
|
562
|
+
if (v !== undefined) {
|
|
563
|
+
return Object.assign(Object.assign({}, acc), { [prop.key.name || prop.key.value]: v });
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return acc;
|
|
567
|
+
}, {});
|
|
568
|
+
}
|
|
569
|
+
// read page config from a sfc file instead of the regular config file
|
|
570
|
+
function readSFCPageConfig(configPath) {
|
|
571
|
+
if (!fs.existsSync(configPath))
|
|
572
|
+
return {};
|
|
573
|
+
const sfcSource = fs.readFileSync(configPath, 'utf8');
|
|
574
|
+
const dpcReg = /definePageConfig\(\{[\w\W]+?\}\)/g;
|
|
575
|
+
const matches = sfcSource.match(dpcReg);
|
|
576
|
+
let result = {};
|
|
577
|
+
if (matches && matches.length === 1) {
|
|
578
|
+
const callExprHandler = (p) => {
|
|
579
|
+
const { callee } = p.node;
|
|
580
|
+
if (!callee.name)
|
|
581
|
+
return;
|
|
582
|
+
if (callee.name && callee.name !== 'definePageConfig')
|
|
583
|
+
return;
|
|
584
|
+
const configNode = p.node.arguments[0];
|
|
585
|
+
result = exprToObject(configNode);
|
|
586
|
+
p.stop();
|
|
587
|
+
};
|
|
588
|
+
const configSource = matches[0];
|
|
589
|
+
const babel = require('@babel/core');
|
|
590
|
+
const ast = babel.parse(configSource, { filename: '' });
|
|
591
|
+
babel.traverse(ast.program, { CallExpression: callExprHandler });
|
|
592
|
+
}
|
|
593
|
+
return result;
|
|
594
|
+
}
|
|
595
|
+
function readPageConfig(configPath) {
|
|
596
|
+
let result = {};
|
|
597
|
+
const extNames = ['.js', '.jsx', '.ts', '.tsx', '.vue'];
|
|
598
|
+
// check source file extension
|
|
599
|
+
extNames.some(ext => {
|
|
600
|
+
const tempPath = configPath.replace('.config', ext);
|
|
601
|
+
if (fs.existsSync(tempPath)) {
|
|
602
|
+
try {
|
|
603
|
+
result = readSFCPageConfig(tempPath);
|
|
604
|
+
}
|
|
605
|
+
catch (error) {
|
|
606
|
+
result = {};
|
|
607
|
+
}
|
|
608
|
+
return true;
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
return result;
|
|
612
|
+
}
|
|
613
|
+
exports.readPageConfig = readPageConfig;
|
|
492
614
|
function readConfig(configPath) {
|
|
493
615
|
let result = {};
|
|
494
616
|
if (fs.existsSync(configPath)) {
|
|
617
|
+
const importPaths = constants_1.REG_SCRIPTS.test(configPath) ? analyzeImport(configPath) : [];
|
|
495
618
|
babelRegister_1.default({
|
|
496
|
-
only: [
|
|
619
|
+
only: [
|
|
620
|
+
configPath,
|
|
621
|
+
filepath => importPaths.includes(filepath)
|
|
622
|
+
]
|
|
623
|
+
});
|
|
624
|
+
importPaths.concat([configPath]).forEach(item => {
|
|
625
|
+
delete require.cache[item];
|
|
497
626
|
});
|
|
498
|
-
delete require.cache[configPath];
|
|
499
627
|
result = exports.getModuleDefaultExport(require(configPath));
|
|
500
628
|
}
|
|
629
|
+
else {
|
|
630
|
+
result = readPageConfig(configPath);
|
|
631
|
+
}
|
|
501
632
|
return result;
|
|
502
633
|
}
|
|
503
634
|
exports.readConfig = readConfig;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tarojs/helper",
|
|
3
|
-
"version": "3.5.0-canary.
|
|
3
|
+
"version": "3.5.0-canary.1",
|
|
4
4
|
"description": "Taro Helper",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"@babel/preset-typescript": "^7.14.5",
|
|
41
41
|
"@babel/register": "^7.14.5",
|
|
42
42
|
"@babel/runtime": "^7.14.5",
|
|
43
|
-
"@tarojs/taro": "3.5.0-canary.
|
|
43
|
+
"@tarojs/taro": "3.5.0-canary.1",
|
|
44
44
|
"chalk": "3.0.0",
|
|
45
45
|
"chokidar": "^3.3.1",
|
|
46
46
|
"cross-spawn": "^7.0.3",
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"resolve": "^1.6.0",
|
|
52
52
|
"yauzl": "2.10.0"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "c61624d2f763e6d31e67d6cf9c564efc8b0d0887"
|
|
55
55
|
}
|
package/types/babelRegister.d.ts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import chalk from 'chalk';
|
|
|
5
5
|
import { processTypeEnum, IProcessTypeMap } from './constants';
|
|
6
6
|
import * as utils from './utils';
|
|
7
7
|
import * as npm from './npm';
|
|
8
|
-
import createBabelRegister from './babelRegister';
|
|
8
|
+
import createBabelRegister, { injectDefineConfigHeader } from './babelRegister';
|
|
9
9
|
|
|
10
10
|
export declare enum META_TYPE {
|
|
11
11
|
ENTRY = 'ENTRY',
|
|
@@ -27,6 +27,7 @@ export declare enum FRAMEWORK_MAP {
|
|
|
27
27
|
declare interface helper {
|
|
28
28
|
npm: typeof npm;
|
|
29
29
|
createBabelRegister: typeof createBabelRegister;
|
|
30
|
+
injectDefineConfigHeader: typeof injectDefineConfigHeader;
|
|
30
31
|
fs: typeof fs;
|
|
31
32
|
chokidar: typeof chokidar;
|
|
32
33
|
chalk: typeof chalk;
|
|
@@ -74,6 +75,7 @@ declare interface helper {
|
|
|
74
75
|
getModuleDefaultExport: (exports: any) => any;
|
|
75
76
|
removeHeadSlash: (str: string) => string;
|
|
76
77
|
readConfig: (configPath: string) => any;
|
|
78
|
+
readPageConfig: (configPath: string) => any;
|
|
77
79
|
PLATFORMS: any;
|
|
78
80
|
processTypeEnum: typeof processTypeEnum;
|
|
79
81
|
processTypeMap: IProcessTypeMap;
|
package/types/utils.d.ts
CHANGED
|
@@ -43,3 +43,4 @@ export declare function addPlatforms(platform: string): void;
|
|
|
43
43
|
export declare const getModuleDefaultExport: (exports: any) => any;
|
|
44
44
|
export declare const removeHeadSlash: (str: string) => string;
|
|
45
45
|
export declare const readConfig: (configPath: string) => any;
|
|
46
|
+
export declare const readPageConfig: (configPath: string) => any;
|