Skip to content

Commit

Permalink
fix(cli): 编译器参数调整
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 31, 2019
1 parent 62f71a5 commit 4771c21
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 38 deletions.
4 changes: 1 addition & 3 deletions packages/taro-cli/src/convertor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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({
Expand Down
3 changes: 1 addition & 2 deletions packages/taro-cli/src/h5/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -295,7 +295,6 @@ class Compiler {
let ast = wxTransformer({
code,
sourcePath: filePath,
isNormal: true,
isTyped: REG_TYPESCRIPT.test(filePath),
adapter: 'h5'
}).ast
Expand Down
5 changes: 1 addition & 4 deletions packages/taro-cli/src/mini/compileScript.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 2 additions & 7 deletions packages/taro-cli/src/mini/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -55,22 +55,17 @@ export function getComponentsNamedMap () {

export function isFileToBeTaroComponent (
code: string,
sourcePath: string,
outputPath: string
sourcePath: string
) {
const {
buildAdapter,
sourceDir,
constantsReplaceList,
jsxAttributeNameReplace,
alias
} = getBuildData()
const transformResult: IWxTransformResult = wxTransformer({
code,
sourcePath: sourcePath,
sourceDir,
outputPath: outputPath,
isNormal: true,
isTyped: REG_TYPESCRIPT.test(sourcePath),
adapter: buildAdapter,
env: constantsReplaceList,
Expand Down
4 changes: 1 addition & 3 deletions packages/taro-cli/src/mini/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -104,8 +104,6 @@ export async function buildEntry (): Promise<AppConfig> {
const transformResult: IWxTransformResult = wxTransformer({
code: entryFileCode,
sourcePath: entryFilePath,
sourceDir,
outputPath: outputEntryFilePath,
isApp: true,
isTyped: REG_TYPESCRIPT.test(entryFilePath),
adapter: buildAdapter,
Expand Down
5 changes: 1 addition & 4 deletions packages/taro-cli/src/mini/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
})
Expand Down
4 changes: 1 addition & 3 deletions packages/taro-cli/src/mini/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-cli/src/mini/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ async function buildWithWebpack ({ appPath }: { appPath: string }) {
babel: babelConfig
},
isWatch: !isProduction,
constantsReplaceList
designWidth: projectConfig.designWidth,
deviceRatio: projectConfig.deviceRatio
}
miniRunner(miniRunnerOpts)
}
6 changes: 2 additions & 4 deletions packages/taro-cli/src/rn/transformJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -137,7 +136,6 @@ function getJSAst (code, filePath) {
return wxTransformer({
code,
sourcePath: filePath,
isNormal: true,
isTyped: REG_TYPESCRIPT.test(filePath),
adapter: 'rn'
}).ast
Expand Down
85 changes: 81 additions & 4 deletions packages/taro-cli/src/ui/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = new Set()

Expand All @@ -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[] = []
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 1 addition & 3 deletions packages/taro-cli/src/util/resolve_npm_files.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4771c21

Please sign in to comment.