@tarojs/helper 3.5.0-canary.0 → 3.5.0-theta.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.
@@ -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/constants.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TARO_BASE_CONFIG = exports.TARO_CONFIG_FLODER = exports.DEFAULT_TEMPLATE_SRC_GITEE = exports.DEFAULT_TEMPLATE_SRC = exports.isWindows = exports.DEVICE_RATIO_NAME = exports.taroJsMobxCommon = exports.taroJsMobx = exports.taroJsRedux = exports.taroJsFramework = exports.taroJsQuickAppComponents = exports.taroJsComponents = exports.META_TYPE = exports.UPDATE_PACKAGE_LIST = exports.FILE_PROCESSOR_MAP = exports.DEVICE_RATIO = exports.PROJECT_CONFIG = exports.NODE_MODULES_REG = exports.NODE_MODULES = exports.CSS_IMPORT_REG = exports.REG_URL = exports.REG_WXML_IMPORT = exports.REG_TEMPLATE = exports.REG_UX = exports.REG_JSON = exports.REG_FONT = exports.REG_IMAGE = exports.REG_MEDIA = exports.REG_CSS = exports.REG_STYLE = exports.REG_STYLUS = exports.REG_LESS = exports.REG_SASS_SCSS = exports.REG_SASS_SASS = exports.REG_SASS = exports.REG_VUE = exports.REG_SCRIPTS = exports.REG_TYPESCRIPT = exports.REG_SCRIPT = exports.REG_JS = exports.VUE_EXT = exports.SCRIPT_EXT = exports.UX_EXT = exports.TS_EXT = exports.JS_EXT = exports.SCSS_EXT = exports.CSS_EXT = exports.processTypeMap = exports.processTypeEnum = exports.PLATFORMS = void 0;
3
+ exports.TARO_BASE_CONFIG = exports.TARO_CONFIG_FOLDER = exports.DEFAULT_TEMPLATE_SRC_GITEE = exports.DEFAULT_TEMPLATE_SRC = exports.isWindows = exports.DEVICE_RATIO_NAME = exports.taroJsMobxCommon = exports.taroJsMobx = exports.taroJsRedux = exports.taroJsFramework = exports.taroJsQuickAppComponents = exports.taroJsComponents = exports.META_TYPE = exports.UPDATE_PACKAGE_LIST = exports.FILE_PROCESSOR_MAP = exports.DEVICE_RATIO = exports.PROJECT_CONFIG = exports.NODE_MODULES_REG = exports.NODE_MODULES = exports.CSS_IMPORT_REG = exports.REG_URL = exports.REG_WXML_IMPORT = exports.REG_TEMPLATE = exports.REG_UX = exports.REG_JSON = exports.REG_FONT = exports.REG_IMAGE = exports.REG_MEDIA = exports.REG_CSS = exports.REG_STYLE = exports.REG_STYLUS = exports.REG_LESS = exports.REG_SASS_SCSS = exports.REG_SASS_SASS = exports.REG_SASS = exports.REG_VUE = exports.REG_SCRIPTS = exports.REG_TYPESCRIPT = exports.REG_SCRIPT = exports.REG_JS = exports.VUE_EXT = exports.SCRIPT_EXT = exports.UX_EXT = exports.TS_EXT = exports.JS_EXT = exports.SCSS_EXT = exports.CSS_EXT = exports.processTypeMap = exports.processTypeEnum = exports.PLATFORMS = void 0;
4
4
  exports.FRAMEWORK_EXT_MAP = exports.FRAMEWORK_MAP = exports.ENTRY = exports.NPM_DIR = exports.TEMP_DIR = exports.SOURCE_DIR = exports.OUTPUT_DIR = void 0;
5
- const os = require("os");
6
5
  const chalk = require("chalk");
6
+ const os = require("os");
7
7
  // eslint-disable-next-line dot-notation
8
8
  exports.PLATFORMS = global['PLATFORMS'] = global['PLATFORMS'] || {};
9
9
  var processTypeEnum;
@@ -126,10 +126,13 @@ exports.UPDATE_PACKAGE_LIST = [
126
126
  '@tarojs/cli',
127
127
  '@tarojs/components',
128
128
  '@tarojs/taro-h5',
129
+ '@tarojs/taro-rn',
130
+ '@tarojs/rn-runner',
129
131
  '@tarojs/helper',
130
132
  '@tarojs/taro-loader',
131
133
  '@tarojs/mini-runner',
132
134
  '@tarojs/react',
135
+ '@tarojs/plugin-framework-react',
133
136
  '@tarojs/router',
134
137
  '@tarojs/runner-utils',
135
138
  '@tarojs/runtime',
@@ -143,6 +146,7 @@ exports.UPDATE_PACKAGE_LIST = [
143
146
  '@tarojs/plugin-platform-tt',
144
147
  '@tarojs/plugin-platform-qq',
145
148
  '@tarojs/plugin-platform-jd',
149
+ '@tarojs/plugin-html',
146
150
  'nervjs',
147
151
  'nerv-devtools'
148
152
  ];
@@ -164,9 +168,9 @@ exports.taroJsMobx = '@tarojs/mobx';
164
168
  exports.taroJsMobxCommon = '@tarojs/mobx-common';
165
169
  exports.DEVICE_RATIO_NAME = 'deviceRatio';
166
170
  exports.isWindows = os.platform() === 'win32';
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';
171
+ exports.DEFAULT_TEMPLATE_SRC = 'github:NervJS/taro-project-templates#v3.5';
172
+ exports.DEFAULT_TEMPLATE_SRC_GITEE = 'direct:https://gitee.com/o2team/taro-project-templates.git#v3.5';
173
+ exports.TARO_CONFIG_FOLDER = '.taro3.5';
170
174
  exports.TARO_BASE_CONFIG = 'index.json';
171
175
  exports.OUTPUT_DIR = 'dist';
172
176
  exports.SOURCE_DIR = 'src';
package/dist/index.js CHANGED
@@ -1,18 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.helper = void 0;
4
- const fs = require("fs-extra");
5
4
  const chalk = require("chalk");
6
5
  const chokidar = require("chokidar");
7
- const debug_1 = require("debug");
6
+ const fs = require("fs-extra");
7
+ const babelRegister_1 = require("./babelRegister");
8
8
  const constants = require("./constants");
9
- const utils = require("./utils");
10
9
  const npm = require("./npm");
11
- const babelRegister_1 = require("./babelRegister");
10
+ const swcRegister_1 = require("./swcRegister");
11
+ const utils = require("./utils");
12
12
  exports.helper = Object.assign(Object.assign(Object.assign({}, constants), utils), { npm,
13
13
  createBabelRegister: babelRegister_1.default,
14
+ injectDefineConfigHeader: babelRegister_1.injectDefineConfigHeader,
15
+ createSwcRegister: swcRegister_1.default,
14
16
  fs,
15
17
  chalk,
16
- chokidar,
17
- createDebug: debug_1.default });
18
+ chokidar, createDebug: id => require('debug')(id) });
18
19
  exports.default = exports.helper;
package/dist/npm.js CHANGED
@@ -10,8 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.getNpmPkg = exports.getNpmPkgSync = exports.callPluginSync = exports.callPlugin = exports.installNpmPkg = exports.resolveNpmSync = exports.resolveNpm = exports.taroPluginPrefix = void 0;
13
- const resolvePath = require("resolve");
14
- const spawn = require("cross-spawn");
15
13
  const chalk = require("chalk");
16
14
  const Util = require("./utils");
17
15
  const PEERS = /UNMET PEER DEPENDENCY ([a-z\-0-9.]+)@(.+)/gm;
@@ -23,6 +21,7 @@ const defaultInstallOptions = {
23
21
  };
24
22
  exports.taroPluginPrefix = '@tarojs/plugin-';
25
23
  function resolveNpm(pluginName, root) {
24
+ const resolvePath = require('resolve');
26
25
  if (!npmCached[pluginName]) {
27
26
  return new Promise((resolve, reject) => {
28
27
  resolvePath(`${pluginName}`, { basedir: root }, (err, res) => {
@@ -38,6 +37,7 @@ function resolveNpm(pluginName, root) {
38
37
  }
39
38
  exports.resolveNpm = resolveNpm;
40
39
  function resolveNpmSync(pluginName, root) {
40
+ const resolvePath = require('resolve');
41
41
  try {
42
42
  if (!npmCached[pluginName]) {
43
43
  const res = resolvePath.sync(pluginName, { basedir: root });
@@ -103,6 +103,7 @@ function installNpmPkg(pkgList, options) {
103
103
  args.push('--save');
104
104
  }
105
105
  }
106
+ const spawn = require('cross-spawn');
106
107
  const output = spawn.sync(installer, args, {
107
108
  stdio: ['ignore', 'pipe', 'inherit']
108
109
  });
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InjectDefineConfigHeader = void 0;
4
+ const Visitor_js_1 = require("@swc/core/Visitor.js");
5
+ class InjectDefineConfigHeader extends Visitor_js_1.Visitor {
6
+ visitTsType(expression) {
7
+ return expression;
8
+ }
9
+ visitCallExpression(expression) {
10
+ const callee = expression.callee;
11
+ if (callee.type === 'Identifier' && (callee.value === 'definePageConfig' || callee.value === 'defineAppConfig')) {
12
+ return expression.arguments[0].expression;
13
+ }
14
+ return expression;
15
+ }
16
+ }
17
+ exports.InjectDefineConfigHeader = InjectDefineConfigHeader;
18
+ function createSwcRegister({ only, plugin }) {
19
+ const config = {
20
+ only: Array.from(new Set([...only])),
21
+ jsc: {
22
+ parser: {
23
+ syntax: 'typescript',
24
+ decorators: true
25
+ },
26
+ transform: {
27
+ legacyDecorator: true
28
+ }
29
+ },
30
+ module: {
31
+ type: 'commonjs'
32
+ }
33
+ };
34
+ if (plugin)
35
+ config.plugin = plugin;
36
+ require('@swc/register')(config);
37
+ }
38
+ exports.default = createSwcRegister;
package/dist/utils.js CHANGED
@@ -9,18 +9,15 @@ 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;
13
- const fs = require("fs-extra");
14
- const path = require("path");
15
- const os = require("os");
16
- const stream_1 = require("stream");
17
- const child_process = require("child_process");
12
+ exports.readConfig = exports.readPageConfig = exports.removeHeadSlash = exports.getModuleDefaultExport = exports.addPlatforms = exports.extnameExpRegOf = exports.readDirWithFileTypes = exports.getAllFilesInFolder = 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;
18
13
  const chalk = require("chalk");
19
- const findWorkspaceRoot = require("find-yarn-workspace-root");
14
+ const child_process = require("child_process");
15
+ const fs = require("fs-extra");
20
16
  const lodash_1 = require("lodash");
21
- const yauzl = require("yauzl");
17
+ const os = require("os");
18
+ const path = require("path");
22
19
  const constants_1 = require("./constants");
23
- const babelRegister_1 = require("./babelRegister");
20
+ const swcRegister_1 = require("./swcRegister");
24
21
  const execSync = child_process.execSync;
25
22
  function normalizePath(path) {
26
23
  return path.replace(/\\/g, '/').replace(/\/{2,}/g, '/');
@@ -119,6 +116,7 @@ function printLog(type, tag, filePath) {
119
116
  }
120
117
  exports.printLog = printLog;
121
118
  function recursiveFindNodeModules(filePath, lastFindPath) {
119
+ const findWorkspaceRoot = require('find-yarn-workspace-root');
122
120
  if (lastFindPath && (normalizePath(filePath) === normalizePath(lastFindPath))) {
123
121
  return filePath;
124
122
  }
@@ -155,7 +153,7 @@ function getUserHomeDir() {
155
153
  }
156
154
  exports.getUserHomeDir = getUserHomeDir;
157
155
  function getTaroPath() {
158
- const taroPath = path.join(getUserHomeDir(), constants_1.TARO_CONFIG_FLODER);
156
+ const taroPath = path.join(getUserHomeDir(), constants_1.TARO_CONFIG_FOLDER);
159
157
  if (!fs.existsSync(taroPath)) {
160
158
  fs.ensureDirSync(taroPath);
161
159
  }
@@ -257,7 +255,7 @@ function generateConstantsList(constants) {
257
255
  const res = {};
258
256
  if (constants && !isEmptyObject(constants)) {
259
257
  for (const key in constants) {
260
- if (lodash_1.isPlainObject(constants[key])) {
258
+ if ((0, lodash_1.isPlainObject)(constants[key])) {
261
259
  res[key] = generateConstantsList(constants[key]);
262
260
  }
263
261
  else {
@@ -317,7 +315,7 @@ function emptyDirectory(dirPath, opts = { excludes: [] }) {
317
315
  }
318
316
  exports.emptyDirectory = emptyDirectory;
319
317
  /* eslint-enable */
320
- const pascalCase = (str) => str.charAt(0).toUpperCase() + lodash_1.camelCase(str.substr(1));
318
+ const pascalCase = (str) => str.charAt(0).toUpperCase() + (0, lodash_1.camelCase)(str.substr(1));
321
319
  exports.pascalCase = pascalCase;
322
320
  function getInstalledNpmPkgPath(pkgName, basedir) {
323
321
  const resolvePath = require('resolve');
@@ -338,7 +336,7 @@ function getInstalledNpmPkgVersion(pkgName, basedir) {
338
336
  }
339
337
  exports.getInstalledNpmPkgVersion = getInstalledNpmPkgVersion;
340
338
  const recursiveMerge = (src, ...args) => {
341
- return lodash_1.mergeWith(src, ...args, (value, srcValue) => {
339
+ return (0, lodash_1.mergeWith)(src, ...args, (value, srcValue) => {
342
340
  const typeValue = typeof value;
343
341
  const typeSrcValue = typeof srcValue;
344
342
  if (typeValue !== typeSrcValue)
@@ -347,20 +345,20 @@ const recursiveMerge = (src, ...args) => {
347
345
  return value.concat(srcValue);
348
346
  }
349
347
  if (typeValue === 'object') {
350
- return exports.recursiveMerge(value, srcValue);
348
+ return (0, exports.recursiveMerge)(value, srcValue);
351
349
  }
352
350
  });
353
351
  };
354
352
  exports.recursiveMerge = recursiveMerge;
355
353
  const mergeVisitors = (src, ...args) => {
356
354
  const validFuncs = ['exit', 'enter'];
357
- return lodash_1.mergeWith(src, ...args, (value, srcValue, key, object, srcObject) => {
355
+ return (0, lodash_1.mergeWith)(src, ...args, (value, srcValue, key, object, srcObject) => {
358
356
  if (!object.hasOwnProperty(key) || !srcObject.hasOwnProperty(key)) {
359
357
  return undefined;
360
358
  }
361
359
  const shouldMergeToArray = validFuncs.indexOf(key) > -1;
362
360
  if (shouldMergeToArray) {
363
- return lodash_1.flatMap([value, srcValue]);
361
+ return (0, lodash_1.flatMap)([value, srcValue]);
364
362
  }
365
363
  const [newValue, newSrcValue] = [value, srcValue].map(v => {
366
364
  if (typeof v === 'function') {
@@ -372,7 +370,7 @@ const mergeVisitors = (src, ...args) => {
372
370
  return v;
373
371
  }
374
372
  });
375
- return exports.mergeVisitors(newValue, newSrcValue);
373
+ return (0, exports.mergeVisitors)(newValue, newSrcValue);
376
374
  });
377
375
  };
378
376
  exports.mergeVisitors = mergeVisitors;
@@ -388,13 +386,15 @@ const applyArrayedVisitors = obj => {
388
386
  };
389
387
  }
390
388
  else if (typeof funcs === 'object') {
391
- exports.applyArrayedVisitors(funcs);
389
+ (0, exports.applyArrayedVisitors)(funcs);
392
390
  }
393
391
  }
394
392
  return obj;
395
393
  };
396
394
  exports.applyArrayedVisitors = applyArrayedVisitors;
397
395
  function unzip(zipPath) {
396
+ const Transform = require('stream').Transform;
397
+ const yauzl = require('yauzl');
398
398
  return new Promise((resolve, reject) => {
399
399
  yauzl.open(zipPath, { lazyEntries: true }, (err, zipfile) => {
400
400
  if (err || !zipfile)
@@ -419,7 +419,7 @@ function unzip(zipPath) {
419
419
  zipfile.openReadStream(entry, (err, readStream) => {
420
420
  if (err || !readStream)
421
421
  throw err;
422
- const filter = new stream_1.Transform();
422
+ const filter = new Transform();
423
423
  filter._transform = function (chunk, _encoding, cb) {
424
424
  cb(undefined, chunk);
425
425
  };
@@ -442,13 +442,13 @@ function unzip(zipPath) {
442
442
  });
443
443
  }
444
444
  exports.unzip = unzip;
445
- const getAllFilesInFloder = (floder, filter = []) => __awaiter(void 0, void 0, void 0, function* () {
445
+ const getAllFilesInFolder = (folder, filter = []) => __awaiter(void 0, void 0, void 0, function* () {
446
446
  let files = [];
447
- const list = readDirWithFileTypes(floder);
447
+ const list = readDirWithFileTypes(folder);
448
448
  yield Promise.all(list.map((item) => __awaiter(void 0, void 0, void 0, function* () {
449
- const itemPath = path.join(floder, item.name);
449
+ const itemPath = path.join(folder, item.name);
450
450
  if (item.isDirectory) {
451
- const _files = yield exports.getAllFilesInFloder(itemPath, filter);
451
+ const _files = yield (0, exports.getAllFilesInFolder)(itemPath, filter);
452
452
  files = [...files, ..._files];
453
453
  }
454
454
  else if (item.isFile) {
@@ -458,11 +458,11 @@ const getAllFilesInFloder = (floder, filter = []) => __awaiter(void 0, void 0, v
458
458
  })));
459
459
  return files;
460
460
  });
461
- exports.getAllFilesInFloder = getAllFilesInFloder;
462
- function readDirWithFileTypes(floder) {
463
- const list = fs.readdirSync(floder);
461
+ exports.getAllFilesInFolder = getAllFilesInFolder;
462
+ function readDirWithFileTypes(folder) {
463
+ const list = fs.readdirSync(folder);
464
464
  const res = list.map(name => {
465
- const stat = fs.statSync(path.join(floder, name));
465
+ const stat = fs.statSync(path.join(folder, name));
466
466
  return {
467
467
  name,
468
468
  isDirectory: stat.isDirectory(),
@@ -489,14 +489,148 @@ function removeHeadSlash(str) {
489
489
  return str.replace(/^(\/|\\)/, '');
490
490
  }
491
491
  exports.removeHeadSlash = removeHeadSlash;
492
+ function analyzeImport(filePath) {
493
+ const parser = require('@babel/parser');
494
+ const traverse = require('@babel/traverse').default;
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(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
+ if (path.extname(dep) !== '.json') {
526
+ importPaths = importPaths.concat(analyzeImport(dep));
527
+ }
528
+ }
529
+ });
530
+ return importPaths;
531
+ }
532
+ // converts ast nodes to js object
533
+ function exprToObject(node) {
534
+ const types = ['BooleanLiteral', 'StringLiteral', 'NumericLiteral'];
535
+ if (types.includes(node.type)) {
536
+ return node.value;
537
+ }
538
+ if (node.name === 'undefined' && !node.value) {
539
+ return undefined;
540
+ }
541
+ if (node.type === 'NullLiteral') {
542
+ return null;
543
+ }
544
+ if (node.type === 'ObjectExpression') {
545
+ return genProps(node.properties);
546
+ }
547
+ if (node.type === 'ArrayExpression') {
548
+ return node.elements.reduce((acc, el) => [
549
+ ...acc,
550
+ ...(el.type === 'SpreadElement'
551
+ ? exprToObject(el.argument)
552
+ : [exprToObject(el)])
553
+ ], []);
554
+ }
555
+ }
556
+ // converts ObjectExpressions to js object
557
+ function genProps(props) {
558
+ return props.reduce((acc, prop) => {
559
+ if (prop.type === 'SpreadElement') {
560
+ return Object.assign(Object.assign({}, acc), exprToObject(prop.argument));
561
+ }
562
+ else if (prop.type !== 'ObjectMethod') {
563
+ const v = exprToObject(prop.value);
564
+ if (v !== undefined) {
565
+ return Object.assign(Object.assign({}, acc), { [prop.key.name || prop.key.value]: v });
566
+ }
567
+ }
568
+ return acc;
569
+ }, {});
570
+ }
571
+ // read page config from a sfc file instead of the regular config file
572
+ function readSFCPageConfig(configPath) {
573
+ if (!fs.existsSync(configPath))
574
+ return {};
575
+ const sfcSource = fs.readFileSync(configPath, 'utf8');
576
+ const dpcReg = /definePageConfig\(\{[\w\W]+?\}\)/g;
577
+ const matches = sfcSource.match(dpcReg);
578
+ let result = {};
579
+ if (matches && matches.length === 1) {
580
+ const callExprHandler = (p) => {
581
+ const { callee } = p.node;
582
+ if (!callee.name)
583
+ return;
584
+ if (callee.name && callee.name !== 'definePageConfig')
585
+ return;
586
+ const configNode = p.node.arguments[0];
587
+ result = exprToObject(configNode);
588
+ p.stop();
589
+ };
590
+ const configSource = matches[0];
591
+ const babel = require('@babel/core');
592
+ const ast = babel.parse(configSource, { filename: '' });
593
+ babel.traverse(ast.program, { CallExpression: callExprHandler });
594
+ }
595
+ return result;
596
+ }
597
+ function readPageConfig(configPath) {
598
+ let result = {};
599
+ const extNames = ['.js', '.jsx', '.ts', '.tsx', '.vue'];
600
+ // check source file extension
601
+ extNames.some(ext => {
602
+ const tempPath = configPath.replace('.config', ext);
603
+ if (fs.existsSync(tempPath)) {
604
+ try {
605
+ result = readSFCPageConfig(tempPath);
606
+ }
607
+ catch (error) {
608
+ result = {};
609
+ }
610
+ return true;
611
+ }
612
+ });
613
+ return result;
614
+ }
615
+ exports.readPageConfig = readPageConfig;
492
616
  function readConfig(configPath) {
493
617
  let result = {};
494
618
  if (fs.existsSync(configPath)) {
495
- babelRegister_1.default({
496
- only: [configPath]
619
+ const importPaths = constants_1.REG_SCRIPTS.test(configPath) ? analyzeImport(configPath) : [];
620
+ (0, swcRegister_1.default)({
621
+ only: [
622
+ configPath,
623
+ filepath => importPaths.includes(filepath)
624
+ ],
625
+ plugin: m => new swcRegister_1.InjectDefineConfigHeader().visitProgram(m)
626
+ });
627
+ importPaths.concat([configPath]).forEach(item => {
628
+ delete require.cache[item];
497
629
  });
498
- delete require.cache[configPath];
499
- result = exports.getModuleDefaultExport(require(configPath));
630
+ result = (0, exports.getModuleDefaultExport)(require(configPath));
631
+ }
632
+ else {
633
+ result = readPageConfig(configPath);
500
634
  }
501
635
  return result;
502
636
  }
package/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  module.exports = require('./dist/index.js').default
2
+
2
3
  module.exports.default = module.exports
package/package.json CHANGED
@@ -1,18 +1,9 @@
1
1
  {
2
2
  "name": "@tarojs/helper",
3
- "version": "3.5.0-canary.0",
3
+ "version": "3.5.0-theta.1",
4
4
  "description": "Taro Helper",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
7
- "scripts": {
8
- "build": "run-s clean prod",
9
- "dev": "tsc -w",
10
- "prod": "tsc",
11
- "clean": "rimraf dist",
12
- "prepack": "npm run build",
13
- "test": "jest",
14
- "test:dev": "jest --watch"
15
- },
16
7
  "repository": {
17
8
  "type": "git",
18
9
  "url": "git+https://github.com/NervJS/taro.git"
@@ -40,7 +31,8 @@
40
31
  "@babel/preset-typescript": "^7.14.5",
41
32
  "@babel/register": "^7.14.5",
42
33
  "@babel/runtime": "^7.14.5",
43
- "@tarojs/taro": "3.5.0-canary.0",
34
+ "@swc/core": "^1.2.204",
35
+ "@swc/register": "^0.1.10",
44
36
  "chalk": "3.0.0",
45
37
  "chokidar": "^3.3.1",
46
38
  "cross-spawn": "^7.0.3",
@@ -48,8 +40,15 @@
48
40
  "find-yarn-workspace-root": "2.0.0",
49
41
  "fs-extra": "^8.0.1",
50
42
  "lodash": "^4.17.21",
51
- "resolve": "^1.6.0",
43
+ "resolve": "^1.22.0",
52
44
  "yauzl": "2.10.0"
53
45
  },
54
- "gitHead": "a0222bc41bc05b0e34413d6db3de963d777a5015"
55
- }
46
+ "scripts": {
47
+ "build": "run-s clean prod",
48
+ "dev": "tsc -w",
49
+ "prod": "tsc",
50
+ "clean": "rimraf dist",
51
+ "test": "jest",
52
+ "test:dev": "jest --watch"
53
+ }
54
+ }
@@ -1,3 +1,6 @@
1
+ import type { PluginItem } from '@babel/core'
2
+
1
3
  export default function createBabelRegister({ only }: {
2
4
  only: any;
3
5
  }): void;
6
+ export function injectDefineConfigHeader (babel: any): PluginItem;
@@ -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.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";
76
+ export declare const DEFAULT_TEMPLATE_SRC = "github:NervJS/taro-project-templates#v3.5";
77
+ export declare const DEFAULT_TEMPLATE_SRC_GITEE = "direct:https://gitee.com/o2team/taro-project-templates.git#v3.5";
78
+ export declare const TARO_CONFIG_FOLDER = ".taro3.5";
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";
package/types/index.d.ts CHANGED
@@ -1,11 +1,13 @@
1
- import fs from 'fs-extra';
2
- import * as chokidar from 'chokidar';
3
- import createDebug from 'debug';
4
- import chalk from 'chalk';
5
- import { processTypeEnum, IProcessTypeMap } from './constants';
6
- import * as utils from './utils';
7
- import * as npm from './npm';
8
- import createBabelRegister from './babelRegister';
1
+ import fs from 'fs-extra'
2
+ import * as chokidar from 'chokidar'
3
+ import createDebug from 'debug'
4
+ import chalk from 'chalk'
5
+
6
+ import { processTypeEnum, IProcessTypeMap } from './constants'
7
+ import * as utils from './utils'
8
+ import * as npm from './npm'
9
+ import createBabelRegister, { injectDefineConfigHeader } from './babelRegister'
10
+ import createSwcRegister from './swcRegister'
9
11
 
10
12
  export declare enum META_TYPE {
11
13
  ENTRY = 'ENTRY',
@@ -25,121 +27,127 @@ export declare enum FRAMEWORK_MAP {
25
27
  }
26
28
 
27
29
  declare interface helper {
28
- npm: typeof npm;
29
- createBabelRegister: typeof createBabelRegister;
30
- fs: typeof fs;
31
- chokidar: typeof chokidar;
32
- chalk: typeof chalk;
30
+ npm: typeof npm
31
+ createSwcRegister: typeof createSwcRegister
32
+ createBabelRegister: typeof createBabelRegister
33
+ injectDefineConfigHeader: typeof injectDefineConfigHeader
34
+ fs: typeof fs
35
+ chokidar: typeof chokidar
36
+ chalk: typeof chalk
33
37
  createDebug: createDebug.Debug & {
34
- debug: createDebug.Debug;
35
- default: createDebug.Debug;
36
- };
37
- normalizePath(path: string): string;
38
- isNpmPkg(name: string): boolean;
39
- isQuickAppPkg(name: string): boolean;
40
- isAliasPath(name: string, pathAlias?: object): boolean;
41
- replaceAliasPath(filePath: string, name: string, pathAlias?: object): string;
42
- promoteRelativePath(fPath: string): string;
43
- resolveStylePath(p: string): string;
44
- printLog(type: processTypeEnum, tag: string, filePath?: string | undefined): void;
45
- recursiveFindNodeModules(filePath: string): string;
46
- getUserHomeDir(): string;
47
- getTaroPath(): string;
48
- getConfig(): object;
49
- getSystemUsername(): string;
50
- shouldUseYarn(): boolean;
51
- shouldUseCnpm(): boolean;
52
- isEmptyObject(obj: any): boolean;
53
- resolveScriptPath(p: string): string;
38
+ debug: createDebug.Debug
39
+ default: createDebug.Debug
40
+ }
41
+ normalizePath(path: string): string
42
+ isNpmPkg(name: string): boolean
43
+ isQuickAppPkg(name: string): boolean
44
+ isAliasPath(name: string, pathAlias?: object): boolean
45
+ replaceAliasPath(filePath: string, name: string, pathAlias?: object): string
46
+ promoteRelativePath(fPath: string): string
47
+ resolveStylePath(p: string): string
48
+ printLog(type: processTypeEnum, tag: string, filePath?: string | undefined): void
49
+ recursiveFindNodeModules(filePath: string): string
50
+ getUserHomeDir(): string
51
+ getTaroPath(): string
52
+ getConfig(): object
53
+ getSystemUsername(): string
54
+ shouldUseYarn(): boolean
55
+ shouldUseCnpm(): boolean
56
+ isEmptyObject(obj: any): boolean
57
+ resolveScriptPath(p: string): string
54
58
  resolveMainFilePath(p: string, extArrs?: string[]): string
55
- generateEnvList(env: object): object;
56
- generateConstantsList(constants: object): object;
57
- cssImports(content: string): string[];
58
- emptyDirectory(dirPath: string, opts?: {
59
- excludes: string[];
60
- }): void;
61
- getInstalledNpmPkgPath(pkgName: string, basedir: string): string | null;
62
- getInstalledNpmPkgVersion(pkgName: string, basedir: string): string | null;
63
- unzip(zipPath: any): Promise<unknown>;
64
- getBabelConfig(babel: any): any;
65
- readDirWithFileTypes(floder: string): utils.FileStat[];
66
- extnameExpRegOf(filePath: string): RegExp;
67
- addPlatforms(platform: string): void;
68
- isNodeModule: (filename: string) => boolean;
69
- pascalCase: (str: string) => string;
70
- recursiveMerge: <T = any>(src: Partial<T>, ...args: (Partial<T> | undefined)[]) => any;
71
- mergeVisitors: (src: any, ...args: any[]) => any;
72
- applyArrayedVisitors: (obj: any) => any;
73
- getAllFilesInFloder: (floder: string, filter?: string[]) => Promise<string[]>;
74
- getModuleDefaultExport: (exports: any) => any;
75
- removeHeadSlash: (str: string) => string;
76
- readConfig: (configPath: string) => any;
77
- PLATFORMS: any;
78
- processTypeEnum: typeof processTypeEnum;
79
- processTypeMap: IProcessTypeMap;
80
- CSS_EXT: string[];
81
- SCSS_EXT: string[];
82
- JS_EXT: string[];
83
- TS_EXT: string[];
84
- UX_EXT: string[];
85
- SCRIPT_EXT: string[];
86
- VUE_EXT: string[];
87
- REG_JS: RegExp;
88
- REG_SCRIPT: RegExp;
89
- REG_TYPESCRIPT: RegExp;
90
- REG_SCRIPTS: RegExp;
91
- REG_VUE: RegExp;
92
- REG_SASS: RegExp;
93
- REG_SASS_SASS: RegExp;
94
- REG_SASS_SCSS: RegExp;
95
- REG_LESS: RegExp;
96
- REG_STYLUS: RegExp;
97
- REG_STYLE: RegExp;
98
- REG_CSS: RegExp;
99
- REG_MEDIA: RegExp;
100
- REG_IMAGE: RegExp;
101
- REG_FONT: RegExp;
102
- REG_JSON: RegExp;
103
- REG_UX: RegExp;
104
- REG_TEMPLATE: RegExp;
105
- REG_WXML_IMPORT: RegExp;
106
- REG_URL: RegExp;
107
- CSS_IMPORT_REG: RegExp;
108
- NODE_MODULES: "node_modules";
109
- NODE_MODULES_REG: RegExp;
110
- PROJECT_CONFIG: "config/index";
59
+ generateEnvList(env: object): object
60
+ generateConstantsList(constants: object): object
61
+ cssImports(content: string): string[]
62
+ emptyDirectory(
63
+ dirPath: string,
64
+ opts?: {
65
+ excludes: string[]
66
+ }
67
+ ): void
68
+ getInstalledNpmPkgPath(pkgName: string, basedir: string): string | null
69
+ getInstalledNpmPkgVersion(pkgName: string, basedir: string): string | null
70
+ unzip(zipPath: any): Promise<unknown>
71
+ getBabelConfig(babel: any): any
72
+ readDirWithFileTypes(folder: string): utils.FileStat[]
73
+ extnameExpRegOf(filePath: string): RegExp
74
+ addPlatforms(platform: string): void
75
+ isNodeModule: (filename: string) => boolean
76
+ pascalCase: (str: string) => string
77
+ recursiveMerge: <T = any>(src: Partial<T>, ...args: (Partial<T> | undefined)[]) => any
78
+ mergeVisitors: (src: any, ...args: any[]) => any
79
+ applyArrayedVisitors: (obj: any) => any
80
+ getAllFilesInFolder: (folder: string, filter?: string[]) => Promise<string[]>
81
+ getModuleDefaultExport: (exports: any) => any
82
+ removeHeadSlash: (str: string) => string
83
+ readConfig: (configPath: string) => any
84
+ readPageConfig: (configPath: string) => any
85
+ PLATFORMS: any
86
+ processTypeEnum: typeof processTypeEnum
87
+ processTypeMap: IProcessTypeMap
88
+ CSS_EXT: string[]
89
+ SCSS_EXT: string[]
90
+ JS_EXT: string[]
91
+ TS_EXT: string[]
92
+ UX_EXT: string[]
93
+ SCRIPT_EXT: string[]
94
+ VUE_EXT: string[]
95
+ REG_JS: RegExp
96
+ REG_SCRIPT: RegExp
97
+ REG_TYPESCRIPT: RegExp
98
+ REG_SCRIPTS: RegExp
99
+ REG_VUE: RegExp
100
+ REG_SASS: RegExp
101
+ REG_SASS_SASS: RegExp
102
+ REG_SASS_SCSS: RegExp
103
+ REG_LESS: RegExp
104
+ REG_STYLUS: RegExp
105
+ REG_STYLE: RegExp
106
+ REG_CSS: RegExp
107
+ REG_MEDIA: RegExp
108
+ REG_IMAGE: RegExp
109
+ REG_FONT: RegExp
110
+ REG_JSON: RegExp
111
+ REG_UX: RegExp
112
+ REG_TEMPLATE: RegExp
113
+ REG_WXML_IMPORT: RegExp
114
+ REG_URL: RegExp
115
+ CSS_IMPORT_REG: RegExp
116
+ NODE_MODULES: 'node_modules'
117
+ NODE_MODULES_REG: RegExp
118
+ PROJECT_CONFIG: 'config/index'
111
119
  DEVICE_RATIO: {
112
- 640: number;
113
- 750: number;
114
- 828: number;
115
- };
120
+ 640: number
121
+ 750: number
122
+ 828: number
123
+ }
116
124
  FILE_PROCESSOR_MAP: {
117
- '.js': string;
118
- '.scss': string;
119
- '.sass': string;
120
- '.less': string;
121
- '.styl': string;
122
- };
123
- META_TYPE: typeof META_TYPE;
124
- FRAMEWORK_MAP: typeof FRAMEWORK_MAP;
125
- UPDATE_PACKAGE_LIST: string[];
126
- taroJsComponents: "@tarojs/components";
127
- taroJsQuickAppComponents: "@tarojs/components-qa";
128
- taroJsFramework: "@tarojs/taro";
129
- taroJsRedux: "@tarojs/redux";
130
- taroJsMobx: "@tarojs/mobx";
131
- taroJsMobxCommon: "@tarojs/mobx-common";
132
- DEVICE_RATIO_NAME: "deviceRatio";
133
- isWindows: boolean;
134
- DEFAULT_TEMPLATE_SRC: "github:NervJS/taro-project-templates#v3.1";
135
- DEFAULT_TEMPLATE_SRC_GITEE: "direct:https://gitee.com/o2team/taro-project-templates.git#v3.1";
136
- TARO_CONFIG_FLODER: ".taro3.1";
137
- TARO_BASE_CONFIG: "index.json";
138
- OUTPUT_DIR: "dist";
139
- SOURCE_DIR: "src";
140
- TEMP_DIR: ".temp";
141
- NPM_DIR: "npm";
142
- ENTRY: "app";
125
+ '.js': string
126
+ '.scss': string
127
+ '.sass': string
128
+ '.less': string
129
+ '.styl': string
130
+ }
131
+ META_TYPE: typeof META_TYPE
132
+ FRAMEWORK_MAP: typeof FRAMEWORK_MAP
133
+ UPDATE_PACKAGE_LIST: string[]
134
+ taroJsComponents: '@tarojs/components'
135
+ taroJsQuickAppComponents: '@tarojs/components-qa'
136
+ taroJsFramework: '@tarojs/taro'
137
+ taroJsRedux: '@tarojs/redux'
138
+ taroJsMobx: '@tarojs/mobx'
139
+ taroJsMobxCommon: '@tarojs/mobx-common'
140
+ DEVICE_RATIO_NAME: 'deviceRatio'
141
+ isWindows: boolean
142
+ DEFAULT_TEMPLATE_SRC: 'github:NervJS/taro-project-templates#v3.5'
143
+ DEFAULT_TEMPLATE_SRC_GITEE: 'direct:https://gitee.com/o2team/taro-project-templates.git#v3.5'
144
+ TARO_CONFIG_FOLDER: '.taro3.5'
145
+ TARO_BASE_CONFIG: 'index.json'
146
+ OUTPUT_DIR: 'dist'
147
+ SOURCE_DIR: 'src'
148
+ TEMP_DIR: '.temp'
149
+ NPM_DIR: 'npm'
150
+ ENTRY: 'app'
143
151
  FRAMEWORK_EXT_MAP: Record<FRAMEWORK_MAP, string[]>
144
152
  }
145
153
  declare const helper: helper
@@ -0,0 +1,3 @@
1
+ export default function createSwcRegister({ only }: {
2
+ only: any;
3
+ }): void;
package/types/utils.d.ts CHANGED
@@ -31,15 +31,16 @@ export declare const mergeVisitors: (src: any, ...args: any[]) => any;
31
31
  export declare const applyArrayedVisitors: (obj: any) => any;
32
32
  export declare function unzip(zipPath: any): Promise<unknown>;
33
33
  export declare function getBabelConfig(babel: any): any;
34
- export declare const getAllFilesInFloder: (floder: string, filter?: string[]) => Promise<string[]>;
34
+ export declare const getAllFilesInFolder: (folder: string, filter?: string[]) => Promise<string[]>;
35
35
  export interface FileStat {
36
36
  name: string;
37
37
  isDirectory: boolean;
38
38
  isFile: boolean;
39
39
  }
40
- export declare function readDirWithFileTypes(floder: string): FileStat[];
40
+ export declare function readDirWithFileTypes(folder: string): FileStat[];
41
41
  export declare function extnameExpRegOf(filePath: string): RegExp;
42
42
  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;