diff --git a/packages/taro-cli/src/convertor/index.ts b/packages/taro-cli/src/convertor/index.ts index 0f55b6c1d1dc..68c22766c756 100644 --- a/packages/taro-cli/src/convertor/index.ts +++ b/packages/taro-cli/src/convertor/index.ts @@ -7,7 +7,7 @@ import * as prettier from 'prettier' import traverse, { NodePath } from 'babel-traverse' import * as t from 'babel-types' import * as taroize from '@tarojs/taroize' -import * as wxTransformer from '@tarojs/transformer-wx' +import wxTransformer from '@tarojs/transformer-wx' import * as postcss from 'postcss' import * as unitTransform from 'postcss-taro-unit-transform' @@ -407,8 +407,6 @@ export default class Convertor { const transformResult = wxTransformer({ code, sourcePath: file, - outputPath: outputFilePath, - isNormal: true, isTyped: REG_TYPESCRIPT.test(file) }) const { ast, scriptFiles } = this.parseAst({ diff --git a/packages/taro-cli/src/h5/index.ts b/packages/taro-cli/src/h5/index.ts index 92197c37f01e..e37935b6b688 100644 --- a/packages/taro-cli/src/h5/index.ts +++ b/packages/taro-cli/src/h5/index.ts @@ -1,5 +1,5 @@ import { PageConfig } from '@tarojs/taro' -import * as wxTransformer from '@tarojs/transformer-wx' +import wxTransformer from '@tarojs/transformer-wx' import * as babel from 'babel-core' import traverse, { NodePath, TraverseOptions } from 'babel-traverse' import * as t from 'babel-types' @@ -295,7 +295,6 @@ class Compiler { let ast = wxTransformer({ code, sourcePath: filePath, - isNormal: true, isTyped: REG_TYPESCRIPT.test(filePath), adapter: 'h5' }).ast diff --git a/packages/taro-cli/src/mini/compileScript.ts b/packages/taro-cli/src/mini/compileScript.ts index cc37c99a9293..95df8fe21e21 100644 --- a/packages/taro-cli/src/mini/compileScript.ts +++ b/packages/taro-cli/src/mini/compileScript.ts @@ -1,7 +1,7 @@ import * as fs from 'fs-extra' import * as path from 'path' -import * as wxTransformer from '@tarojs/transformer-wx' +import wxTransformer from '@tarojs/transformer-wx' import { printLog, @@ -83,9 +83,6 @@ export function compileDepScripts (scriptFiles: string[], needUseBabel?: boolean const transformResult = wxTransformer({ code, sourcePath: item, - sourceDir, - outputPath: outputItem, - isNormal: true, isTyped: REG_TYPESCRIPT.test(item), adapter: buildAdapter, env: constantsReplaceList, diff --git a/packages/taro-cli/src/mini/component.ts b/packages/taro-cli/src/mini/component.ts index 27f356461939..49e52b8e618a 100644 --- a/packages/taro-cli/src/mini/component.ts +++ b/packages/taro-cli/src/mini/component.ts @@ -2,7 +2,7 @@ import * as fs from 'fs-extra' import * as path from 'path' import { Config as IConfig } from '@tarojs/taro' -import * as wxTransformer from '@tarojs/transformer-wx' +import wxTransformer from '@tarojs/transformer-wx' import * as _ from 'lodash' import traverse from 'babel-traverse' @@ -55,12 +55,10 @@ export function getComponentsNamedMap () { export function isFileToBeTaroComponent ( code: string, - sourcePath: string, - outputPath: string + sourcePath: string ) { const { buildAdapter, - sourceDir, constantsReplaceList, jsxAttributeNameReplace, alias @@ -68,9 +66,6 @@ export function isFileToBeTaroComponent ( const transformResult: IWxTransformResult = wxTransformer({ code, sourcePath: sourcePath, - sourceDir, - outputPath: outputPath, - isNormal: true, isTyped: REG_TYPESCRIPT.test(sourcePath), adapter: buildAdapter, env: constantsReplaceList, diff --git a/packages/taro-cli/src/mini/entry.ts b/packages/taro-cli/src/mini/entry.ts index 980b85508757..4c7842cca495 100644 --- a/packages/taro-cli/src/mini/entry.ts +++ b/packages/taro-cli/src/mini/entry.ts @@ -2,7 +2,7 @@ import * as fs from 'fs-extra' import * as path from 'path' import { AppConfig } from '@tarojs/taro' -import * as wxTransformer from '@tarojs/transformer-wx' +import wxTransformer from '@tarojs/transformer-wx' import { REG_SCRIPTS, @@ -104,8 +104,6 @@ export async function buildEntry (): Promise { const transformResult: IWxTransformResult = wxTransformer({ code: entryFileCode, sourcePath: entryFilePath, - sourceDir, - outputPath: outputEntryFilePath, isApp: true, isTyped: REG_TYPESCRIPT.test(entryFilePath), adapter: buildAdapter, diff --git a/packages/taro-cli/src/mini/helper.ts b/packages/taro-cli/src/mini/helper.ts index de8f4f50dc26..4d998cd8dbb7 100644 --- a/packages/taro-cli/src/mini/helper.ts +++ b/packages/taro-cli/src/mini/helper.ts @@ -3,7 +3,7 @@ import * as path from 'path' import * as _ from 'lodash' import { Config } from '@tarojs/taro' -import * as wxTransformer from '@tarojs/transformer-wx' +import wxTransformer from '@tarojs/transformer-wx' import getHashName from '../util/hash' import { @@ -376,9 +376,6 @@ export function getImportTaroSelfComponents (filePath, taroSelfComponents) { const transformResult: IWxTransformResult = wxTransformer({ code: scriptContent, sourcePath: sourceFilePath, - sourceDir: getBuildData().sourceDir, - outputPath: outputFilePath, - isNormal: true, isTyped: false, adapter: BUILD_TYPES.QUICKAPP }) diff --git a/packages/taro-cli/src/mini/page.ts b/packages/taro-cli/src/mini/page.ts index e0846ac19ab3..845f2fa4494a 100644 --- a/packages/taro-cli/src/mini/page.ts +++ b/packages/taro-cli/src/mini/page.ts @@ -2,7 +2,7 @@ import * as fs from 'fs-extra' import * as path from 'path' import { Config as IConfig } from '@tarojs/taro' -import * as wxTransformer from '@tarojs/transformer-wx' +import wxTransformer from '@tarojs/transformer-wx' import * as _ from 'lodash' import { @@ -105,8 +105,6 @@ export async function buildSinglePage (page: string) { const aheadTransformResult: IWxTransformResult = wxTransformer({ code: pageJsContent, sourcePath: pageJs, - sourceDir, - outputPath: outputPageJSPath, isRoot: true, isTyped: REG_TYPESCRIPT.test(pageJs), adapter: buildAdapter, diff --git a/packages/taro-cli/src/mini/webpack.ts b/packages/taro-cli/src/mini/webpack.ts index 0c002258716f..551493052e81 100644 --- a/packages/taro-cli/src/mini/webpack.ts +++ b/packages/taro-cli/src/mini/webpack.ts @@ -49,7 +49,8 @@ async function buildWithWebpack ({ appPath }: { appPath: string }) { babel: babelConfig }, isWatch: !isProduction, - constantsReplaceList + designWidth: projectConfig.designWidth, + deviceRatio: projectConfig.deviceRatio } miniRunner(miniRunnerOpts) } diff --git a/packages/taro-cli/src/rn/transformJS.ts b/packages/taro-cli/src/rn/transformJS.ts index 52d2040085fc..ed19a7951b99 100644 --- a/packages/taro-cli/src/rn/transformJS.ts +++ b/packages/taro-cli/src/rn/transformJS.ts @@ -4,12 +4,11 @@ import traverse, { NodePath } from 'babel-traverse' import * as t from 'babel-types' import * as _ from 'lodash' import generate from 'babel-generator' -import * as wxTransformer from '@tarojs/transformer-wx' +import wxTransformer from '@tarojs/transformer-wx' import * as Util from '../util' import babylonConfig from '../config/babylon' -import { convertSourceStringToAstExpression as toAst } from '../util/astConvert' +import { convertSourceStringToAstExpression as toAst, convertAstExpressionToVariable as toVar } from '../util/astConvert' import { REG_STYLE, REG_TYPESCRIPT, BUILD_TYPES, REG_SCRIPTS } from '../util/constants' -import { convertAstExpressionToVariable as toVar } from '../util/astConvert' const template = require('babel-template') @@ -137,7 +136,6 @@ function getJSAst (code, filePath) { return wxTransformer({ code, sourcePath: filePath, - isNormal: true, isTyped: REG_TYPESCRIPT.test(filePath), adapter: 'rn' }).ast diff --git a/packages/taro-cli/src/ui/common.ts b/packages/taro-cli/src/ui/common.ts index 6ca2cb599757..a2f39a7f8172 100644 --- a/packages/taro-cli/src/ui/common.ts +++ b/packages/taro-cli/src/ui/common.ts @@ -5,11 +5,9 @@ import * as path from 'path' import * as wxTransformer from '@tarojs/transformer-wx' import generate from 'babel-generator' import * as fs from 'fs-extra' -import { parseAst } from '../mini/astProcess' import { IBuildData } from './ui.types' import { cssImports, printLog, resolveScriptPath, resolveStylePath, isNpmPkg } from '../util' -import { PARSE_AST_TYPE, processTypeEnum, REG_STYLE, REG_TYPESCRIPT, CSS_EXT } from '../util/constants' -import { IComponentObj } from '../mini/interface' +import { processTypeEnum, REG_STYLE, REG_TYPESCRIPT, REG_SCRIPT, REG_JSON, REG_FONT, REG_IMAGE, REG_MEDIA, CSS_EXT } from '../util/constants' let processedScriptFiles: Set = new Set() @@ -20,6 +18,85 @@ export const RN_OUTPUT_NAME = 'rn' export const TEMP_DIR = '.temp' export const RN_TEMP_DIR = 'rn_temp' +interface IComponentObj { + name?: string, + path: string | null, + type?: string +} + +interface IParseAstReturn { + styleFiles: string[], + scriptFiles: string[], + jsonFiles: string[], + mediaFiles: string[] +} + +function parseAst ( + ast: t.File, + sourceFilePath: string +): IParseAstReturn { + const styleFiles: string[] = [] + const scriptFiles: string[] = [] + const jsonFiles: string[] = [] + const mediaFiles: string[] = [] + + traverse(ast, { + Program: { + exit (astPath) { + astPath.traverse({ + ImportDeclaration (astPath) { + const node = astPath.node + const source = node.source + const value = source.value + const valueExtname = path.extname(value) + if (value.indexOf('.') === 0) { + let importPath = path.resolve(path.dirname(sourceFilePath), value) + importPath = resolveScriptPath(importPath) + if (REG_SCRIPT.test(valueExtname) || REG_TYPESCRIPT.test(valueExtname)) { + const vpath = path.resolve(sourceFilePath, '..', value) + let fPath = value + if (fs.existsSync(vpath) && vpath !== sourceFilePath) { + fPath = vpath + } + if (scriptFiles.indexOf(fPath) < 0) { + scriptFiles.push(fPath) + } + } else if (REG_JSON.test(valueExtname)) { + const vpath = path.resolve(sourceFilePath, '..', value) + if (fs.existsSync(vpath) && jsonFiles.indexOf(vpath) < 0) { + jsonFiles.push(vpath) + } + } else if (REG_FONT.test(valueExtname) || REG_IMAGE.test(valueExtname) || REG_MEDIA.test(valueExtname)) { + const vpath = path.resolve(sourceFilePath, '..', value) + if (fs.existsSync(vpath) && mediaFiles.indexOf(vpath) < 0) { + mediaFiles.push(vpath) + } + } else if (REG_STYLE.test(valueExtname)) { + const vpath = path.resolve(path.dirname(sourceFilePath), value) + if (fs.existsSync(vpath) && styleFiles.indexOf(vpath) < 0) { + styleFiles.push(vpath) + } + } else { + const vpath = resolveScriptPath(path.resolve(sourceFilePath, '..', value)) + if (fs.existsSync(vpath) && scriptFiles.indexOf(vpath) < 0) { + scriptFiles.push(vpath) + } + } + } + } + }) + } + } + }) + + return { + styleFiles, + scriptFiles, + jsonFiles, + mediaFiles + } +} + export function parseEntryAst (ast: t.File, relativeFile: string) { const styleFiles: string[] = [] const components: IComponentObj[] = [] @@ -157,7 +234,7 @@ export function analyzeFiles (files: string[], sourceDir: string, outputDir: str scriptFiles, jsonFiles, mediaFiles - } = parseAst(PARSE_AST_TYPE.NORMAL, transformResult.ast, [], file, file, true) + } = parseAst(transformResult.ast, file) const resFiles = styleFiles.concat(scriptFiles, jsonFiles, mediaFiles) diff --git a/packages/taro-cli/src/util/resolve_npm_files.ts b/packages/taro-cli/src/util/resolve_npm_files.ts index dd99b33ee1a0..355258ef73b1 100644 --- a/packages/taro-cli/src/util/resolve_npm_files.ts +++ b/packages/taro-cli/src/util/resolve_npm_files.ts @@ -1,7 +1,7 @@ import * as fs from 'fs-extra' import * as path from 'path' import * as resolvePath from 'resolve' -import * as wxTransformer from '@tarojs/transformer-wx' +import wxTransformer from '@tarojs/transformer-wx' import * as babel from 'babel-core' import * as t from 'babel-types' import traverse from 'babel-traverse' @@ -435,8 +435,6 @@ async function recursiveRequire ({ const transformResult = wxTransformer({ code: fileContent, sourcePath: filePath, - outputPath: outputNpmPath, - isNormal: true, adapter: buildAdapter, isTyped: REG_TYPESCRIPT.test(filePath), env: constantsReplaceList