@tarojs/helper 3.4.0-beta.0 → 3.4.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.
@@ -1,6 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const path = require("path");
4
+ /**
5
+ * Inject `defineAppConfig` and `definePageConfig`
6
+ * require header at the top of a config file,
7
+ * without the need to specifically require them
8
+ * if they are used
9
+ */
10
+ function injectDefineConfigHeader(babel) {
11
+ const appConfig = 'const { defineAppConfig } = require("@tarojs/helper")';
12
+ const pageConfig = 'const { definePageConfig } = require("@tarojs/helper")';
13
+ const prependHeader = (nodePath, header) => {
14
+ const parsedHeader = babel.parse(header, { filename: '' }).program.body[0];
15
+ nodePath.node.body.unshift(parsedHeader);
16
+ };
17
+ const enterHandler = (nodePath) => {
18
+ const { scope, node } = nodePath;
19
+ scope.traverse(node, {
20
+ CallExpression(p) {
21
+ const callee = p.node.callee;
22
+ // @ts-ignore
23
+ switch (callee.name) {
24
+ case 'defineAppConfig':
25
+ return prependHeader(nodePath, appConfig);
26
+ case 'definePageConfig':
27
+ return prependHeader(nodePath, pageConfig);
28
+ }
29
+ }
30
+ });
31
+ };
32
+ return {
33
+ visitor: {
34
+ Program: { enter: enterHandler }
35
+ }
36
+ };
37
+ }
4
38
  function createBabelRegister({ only }) {
5
39
  require('@babel/register')({
6
40
  only: Array.from(new Set([...only])),
@@ -9,6 +43,7 @@ function createBabelRegister({ only }) {
9
43
  require.resolve('@babel/preset-typescript')
10
44
  ],
11
45
  plugins: [
46
+ injectDefineConfigHeader,
12
47
  [require.resolve('@babel/plugin-proposal-decorators'), {
13
48
  legacy: true
14
49
  }],
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.definePageConfig = exports.defineAppConfig = 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,146 @@ function removeHeadSlash(str) {
489
491
  return str.replace(/^(\/|\\)/, '');
490
492
  }
491
493
  exports.removeHeadSlash = removeHeadSlash;
494
+ function defineAppConfig(config) {
495
+ return config;
496
+ }
497
+ exports.defineAppConfig = defineAppConfig;
498
+ function definePageConfig(config) {
499
+ return config;
500
+ }
501
+ exports.definePageConfig = definePageConfig;
502
+ function analyzeImport(filePath) {
503
+ const code = fs.readFileSync(filePath).toString();
504
+ let importPaths = [];
505
+ filePath = path.dirname(filePath);
506
+ const ast = parser.parse(code, {
507
+ sourceType: 'module',
508
+ plugins: [
509
+ 'typescript',
510
+ 'classProperties',
511
+ 'objectRestSpread',
512
+ 'optionalChaining'
513
+ ]
514
+ });
515
+ traverse_1.default(ast, {
516
+ ImportDeclaration({ node }) {
517
+ const list = [];
518
+ const source = node.source.value;
519
+ if (path.extname(source)) {
520
+ const importPath = path.resolve(filePath, source);
521
+ list.push(importPath);
522
+ }
523
+ else {
524
+ ['.js', '.ts', '.json'].forEach(ext => {
525
+ const importPath = path.resolve(filePath, source + ext);
526
+ list.push(importPath);
527
+ });
528
+ }
529
+ const dep = list.find(importPath => fs.existsSync(importPath));
530
+ if (!dep)
531
+ return;
532
+ importPaths.push(dep);
533
+ importPaths = importPaths.concat(analyzeImport(dep));
534
+ }
535
+ });
536
+ return importPaths;
537
+ }
538
+ // converts ast nodes to js object
539
+ function exprToObject(node) {
540
+ const types = ['BooleanLiteral', 'StringLiteral', 'NumericLiteral'];
541
+ if (types.includes(node.type)) {
542
+ return node.value;
543
+ }
544
+ if (node.name === 'undefined' && !node.value) {
545
+ return undefined;
546
+ }
547
+ if (node.type === 'NullLiteral') {
548
+ return null;
549
+ }
550
+ if (node.type === 'ObjectExpression') {
551
+ return genProps(node.properties);
552
+ }
553
+ if (node.type === 'ArrayExpression') {
554
+ return node.elements.reduce((acc, el) => [
555
+ ...acc,
556
+ ...(el.type === 'SpreadElement'
557
+ ? exprToObject(el.argument)
558
+ : [exprToObject(el)])
559
+ ], []);
560
+ }
561
+ }
562
+ // converts ObjectExpressions to js object
563
+ function genProps(props) {
564
+ return props.reduce((acc, prop) => {
565
+ if (prop.type === 'SpreadElement') {
566
+ return Object.assign(Object.assign({}, acc), exprToObject(prop.argument));
567
+ }
568
+ else if (prop.type !== 'ObjectMethod') {
569
+ const v = exprToObject(prop.value);
570
+ if (v !== undefined) {
571
+ return Object.assign(Object.assign({}, acc), { [prop.key.name || prop.key.value]: v });
572
+ }
573
+ }
574
+ return acc;
575
+ }, {});
576
+ }
577
+ // read page config from a sfc file instead of the regular config file
578
+ function readSFCPageConfig(configPath) {
579
+ if (!fs.existsSync(configPath))
580
+ return {};
581
+ const sfcSource = fs.readFileSync(configPath, 'utf8');
582
+ const dpcReg = /definePageConfig\(\{[\w\W]+?\}\)/g;
583
+ const matches = sfcSource.match(dpcReg);
584
+ let result = {};
585
+ if (matches && matches.length === 1) {
586
+ const callExprHandler = (p) => {
587
+ const { callee } = p.node;
588
+ if (!callee.name)
589
+ return;
590
+ if (callee.name && callee.name !== 'definePageConfig')
591
+ return;
592
+ const configNode = p.node.arguments[0];
593
+ result = exprToObject(configNode);
594
+ p.stop();
595
+ };
596
+ const configSource = matches[0];
597
+ const babel = require('@babel/core');
598
+ const ast = babel.parse(configSource, { filename: '' });
599
+ babel.traverse(ast.program, { CallExpression: callExprHandler });
600
+ }
601
+ return result;
602
+ }
492
603
  function readConfig(configPath) {
493
604
  let result = {};
494
605
  if (fs.existsSync(configPath)) {
606
+ const importPaths = constants_1.REG_SCRIPTS.test(configPath) ? analyzeImport(configPath) : [];
495
607
  babelRegister_1.default({
496
- only: [configPath]
608
+ only: [
609
+ configPath,
610
+ filepath => importPaths.includes(filepath)
611
+ ]
612
+ });
613
+ importPaths.concat([configPath]).forEach(item => {
614
+ delete require.cache[item];
497
615
  });
498
- delete require.cache[configPath];
499
616
  result = exports.getModuleDefaultExport(require(configPath));
500
617
  }
618
+ else {
619
+ const extNames = ['.js', '.jsx', '.ts', '.tsx', '.vue'];
620
+ // check source file extension
621
+ extNames.some(ext => {
622
+ const tempPath = configPath.replace('.config', ext);
623
+ if (fs.existsSync(tempPath)) {
624
+ try {
625
+ result = readSFCPageConfig(tempPath);
626
+ }
627
+ catch (error) {
628
+ result = {};
629
+ }
630
+ return true;
631
+ }
632
+ });
633
+ }
501
634
  return result;
502
635
  }
503
636
  exports.readConfig = readConfig;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tarojs/helper",
3
- "version": "3.4.0-beta.0",
3
+ "version": "3.4.0",
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.4.0-beta.0",
43
+ "@tarojs/taro": "3.4.0",
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": "107670bc2360ee9136a0e558e6d5fad1db640fba"
54
+ "gitHead": "a0e97bc937f878cc059f54748c9a3829fa3f4664"
55
55
  }