@tarojs/helper 3.3.20 → 3.4.0-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.
@@ -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,10 +43,10 @@ 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
  }],
15
- require.resolve('@babel/plugin-proposal-class-properties'),
16
50
  require.resolve('@babel/plugin-proposal-object-rest-spread'),
17
51
  [require.resolve('@babel/plugin-transform-runtime'), {
18
52
  corejs: false,
package/dist/constants.js CHANGED
@@ -164,9 +164,9 @@ exports.taroJsMobx = '@tarojs/mobx';
164
164
  exports.taroJsMobxCommon = '@tarojs/mobx-common';
165
165
  exports.DEVICE_RATIO_NAME = 'deviceRatio';
166
166
  exports.isWindows = os.platform() === 'win32';
167
- exports.DEFAULT_TEMPLATE_SRC = 'github:NervJS/taro-project-templates#v3.1';
168
- exports.DEFAULT_TEMPLATE_SRC_GITEE = 'direct:https://gitee.com/o2team/taro-project-templates.git#v3.1';
169
- exports.TARO_CONFIG_FLODER = '.taro3.1';
167
+ exports.DEFAULT_TEMPLATE_SRC = 'github:NervJS/taro-project-templates#v3.4';
168
+ exports.DEFAULT_TEMPLATE_SRC_GITEE = 'direct:https://gitee.com/o2team/taro-project-templates.git#v3.4';
169
+ exports.TARO_CONFIG_FLODER = '.taro3.4';
170
170
  exports.TARO_BASE_CONFIG = 'index.json';
171
171
  exports.OUTPUT_DIR = 'dist';
172
172
  exports.SOURCE_DIR = 'src';
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");
@@ -57,7 +59,7 @@ function replaceAliasPath(filePath, name, pathAlias = {}) {
57
59
  return promoteRelativePath(path.relative(filePath, fs.realpathSync(resolveScriptPath(pathAlias[name]))));
58
60
  }
59
61
  const reg = new RegExp(`^(${prefixs.join('|')})/(.*)`);
60
- name = name.replace(reg, function (m, $1, $2) {
62
+ name = name.replace(reg, function (_m, $1, $2) {
61
63
  return promoteRelativePath(path.relative(filePath, path.join(pathAlias[$1], $2)));
62
64
  });
63
65
  return name;
@@ -397,7 +399,7 @@ exports.applyArrayedVisitors = applyArrayedVisitors;
397
399
  function unzip(zipPath) {
398
400
  return new Promise((resolve, reject) => {
399
401
  yauzl.open(zipPath, { lazyEntries: true }, (err, zipfile) => {
400
- if (err)
402
+ if (err || !zipfile)
401
403
  throw err;
402
404
  zipfile.on('close', () => {
403
405
  fs.removeSync(zipPath);
@@ -417,10 +419,10 @@ function unzip(zipPath) {
417
419
  }
418
420
  else {
419
421
  zipfile.openReadStream(entry, (err, readStream) => {
420
- if (err)
422
+ if (err || !readStream)
421
423
  throw err;
422
424
  const filter = new stream_1.Transform();
423
- filter._transform = function (chunk, encoding, cb) {
425
+ filter._transform = function (chunk, _encoding, cb) {
424
426
  cb(undefined, chunk);
425
427
  };
426
428
  filter._flush = function (cb) {
@@ -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.3.20",
3
+ "version": "3.4.0-beta.3",
4
4
  "description": "Taro Helper",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -32,25 +32,24 @@
32
32
  },
33
33
  "homepage": "https://github.com/NervJS/taro#readme",
34
34
  "dependencies": {
35
- "@babel/core": "^7.11.1",
36
- "@babel/plugin-proposal-class-properties": "7.10.4",
37
- "@babel/plugin-proposal-decorators": "7.10.5",
38
- "@babel/plugin-proposal-object-rest-spread": "7.14.7",
39
- "@babel/plugin-transform-runtime": "7.11.0",
40
- "@babel/preset-env": "^7.11.0",
41
- "@babel/preset-typescript": "7.12.17",
42
- "@babel/register": "7.14.5",
43
- "@babel/runtime": "^7.9.2",
44
- "@tarojs/taro": "3.3.20",
35
+ "@babel/core": "^7.14.5",
36
+ "@babel/plugin-proposal-decorators": "^7.14.5",
37
+ "@babel/plugin-proposal-object-rest-spread": "^7.14.5",
38
+ "@babel/plugin-transform-runtime": "^7.14.5",
39
+ "@babel/preset-env": "^7.14.5",
40
+ "@babel/preset-typescript": "^7.14.5",
41
+ "@babel/register": "^7.14.5",
42
+ "@babel/runtime": "^7.14.5",
43
+ "@tarojs/taro": "3.4.0-beta.3",
45
44
  "chalk": "3.0.0",
46
- "chokidar": "3.3.1",
47
- "cross-spawn": "7.0.3",
45
+ "chokidar": "^3.3.1",
46
+ "cross-spawn": "^7.0.3",
48
47
  "debug": "4.1.1",
49
48
  "find-yarn-workspace-root": "2.0.0",
50
- "fs-extra": "8.1.0",
51
- "lodash": "4.17.21",
52
- "resolve": "1.15.1",
49
+ "fs-extra": "^8.0.1",
50
+ "lodash": "^4.17.21",
51
+ "resolve": "^1.6.0",
53
52
  "yauzl": "2.10.0"
54
53
  },
55
- "gitHead": "f98ceefaa3e419db5a6c3bcd3bf123ce5a0a0124"
54
+ "gitHead": "2ef7e8c10c9e5784e98bb1ad3a803d5fd6a46c8b"
56
55
  }
@@ -73,9 +73,9 @@ export declare const taroJsMobx = "@tarojs/mobx";
73
73
  export declare const taroJsMobxCommon = "@tarojs/mobx-common";
74
74
  export declare const DEVICE_RATIO_NAME = "deviceRatio";
75
75
  export declare const isWindows: boolean;
76
- export declare const DEFAULT_TEMPLATE_SRC = "github:NervJS/taro-project-templates#v3";
77
- export declare const DEFAULT_TEMPLATE_SRC_GITEE = "direct:https://gitee.com/o2team/taro-project-templates.git#v3";
78
- export declare const TARO_CONFIG_FLODER = ".taro3";
76
+ export declare const DEFAULT_TEMPLATE_SRC = "github:NervJS/taro-project-templates#v3.4";
77
+ export declare const DEFAULT_TEMPLATE_SRC_GITEE = "direct:https://gitee.com/o2team/taro-project-templates.git#v3.4";
78
+ export declare const TARO_CONFIG_FLODER = ".taro3.4";
79
79
  export declare const TARO_BASE_CONFIG = "index.json";
80
80
  export declare const OUTPUT_DIR = "dist";
81
81
  export declare const SOURCE_DIR = "src";