Skip to content

Commit

Permalink
feat(taro): format by prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Simbachen committed Apr 22, 2018
1 parent c22a0e5 commit 163b7bb
Showing 1 changed file with 113 additions and 46 deletions.
159 changes: 113 additions & 46 deletions packages/taro-cli/src/h5.js
Expand Up @@ -23,7 +23,10 @@ const tempDir = '.temp'

const appPath = process.cwd()
const projectConfig = require(path.join(appPath, Util.PROJECT_CONFIG))(_.merge)
const sourceDir = path.join(appPath, projectConfig.sourceRoot || CONFIG.SOURCE_DIR)
const sourceDir = path.join(
appPath,
projectConfig.sourceRoot || CONFIG.SOURCE_DIR
)
const tempPath = path.join(appPath, tempDir)
const entryFilePath = path.join(sourceDir, CONFIG.ENTRY)

Expand All @@ -41,20 +44,36 @@ function processEntry (code) {
ClassDeclaration (astPath) {
const node = astPath.node
if (!node.superClass) return
if (node.superClass.type === 'MemberExpression' &&
node.superClass.object.name === taroImportDefaultName) {
if (
node.superClass.type === 'MemberExpression' &&
node.superClass.object.name === taroImportDefaultName
) {
node.superClass.object.name = nervJsImportDefaultName
if (node.id === null) {
const renameComponentClassName = '_TaroComponentClass'
astPath.replaceWith(t.classDeclaration(t.identifier(renameComponentClassName), node.superClass, node.body, node.decorators || []))
astPath.replaceWith(
t.classDeclaration(
t.identifier(renameComponentClassName),
node.superClass,
node.body,
node.decorators || []
)
)
componentClassName = renameComponentClassName
} else {
componentClassName = node.id.name
}
} else if (node.superClass.name === 'Component') {
if (node.id === null) {
const renameComponentClassName = '_TaroComponentClass'
astPath.replaceWith(t.classDeclaration(t.identifier(renameComponentClassName), node.superClass, node.body, node.decorators || []))
astPath.replaceWith(
t.classDeclaration(
t.identifier(renameComponentClassName),
node.superClass,
node.body,
node.decorators || []
)
)
componentClassName = renameComponentClassName
} else {
componentClassName = node.id.name
Expand Down Expand Up @@ -95,7 +114,9 @@ function processEntry (code) {
}
})
if (!defaultSpecifier) {
node.specifiers.unshift(t.importDefaultSpecifier(t.identifier(nervJsImportDefaultName)))
node.specifiers.unshift(
t.importDefaultSpecifier(t.identifier(nervJsImportDefaultName))
)
} else {
taroImportDefaultName = defaultSpecifier
node.specifiers[idx].local.name = nervJsImportDefaultName
Expand All @@ -112,25 +133,45 @@ function processEntry (code) {
const node = astPath.node
const key = node.key
if (key.name !== 'render') return
node.body = template(`{
node.body = template(
`{
return <${routerImportDefaultName}.Router />
}`, babylonConfig)()
}`,
babylonConfig
)()
}
})
},
Program: {
exit (astPath) {
const node = astPath.node
const routerPages = pages.map(v => {
const pageName = v.startsWith('/') ? v : `/${v}`
return `['${pageName}', () => import('.${pageName}.js')]`
}).join(',')
const routerPages = pages
.map(v => {
const pageName = v.startsWith('/') ? v : `/${v}`
return `['${pageName}', () => import('.${pageName}.js')]`
})
.join(',')

const importTaro = template(`import ${taroImportDefaultName} from '${taroJsFramework}'`, babylonConfig)()
const importTaroRouter = template(`import ${routerImportDefaultName} from '${taroRouterFramework}'`, babylonConfig)()
const initRouter = template(`${routerImportDefaultName}.initRouter([${routerPages}], ${taroImportDefaultName})`, babylonConfig)()
const initNativeApi = template(`${taroImportDefaultName}.initNativeApi(${taroImportDefaultName})`, babylonConfig)()
const renderApp = template(`${nervJsImportDefaultName}.render(<${componentClassName} />, document.getElementById('app'))`, babylonConfig)()
const importTaro = template(
`import ${taroImportDefaultName} from '${taroJsFramework}'`,
babylonConfig
)()
const importTaroRouter = template(
`import ${routerImportDefaultName} from '${taroRouterFramework}'`,
babylonConfig
)()
const initRouter = template(
`${routerImportDefaultName}.initRouter([${routerPages}], ${taroImportDefaultName})`,
babylonConfig
)()
const initNativeApi = template(
`${taroImportDefaultName}.initNativeApi(${taroImportDefaultName})`,
babylonConfig
)()
const renderApp = template(
`${nervJsImportDefaultName}.render(<${componentClassName} />, document.getElementById('app'))`,
babylonConfig
)()

node.body.unshift(importTaro)
node.body.unshift(importTaroRouter)
Expand Down Expand Up @@ -158,17 +199,33 @@ function processOthers (code) {
ClassDeclaration (astPath) {
const node = astPath.node
if (!node.superClass) return
if (node.superClass.type === 'MemberExpression' &&
node.superClass.object.name === taroImportDefaultName) {
if (
node.superClass.type === 'MemberExpression' &&
node.superClass.object.name === taroImportDefaultName
) {
node.superClass.object.name = nervJsImportDefaultName
if (node.id === null) {
const renameComponentClassName = '_TaroComponentClass'
astPath.replaceWith(t.classDeclaration(t.identifier(renameComponentClassName), node.superClass, node.body, node.decorators || []))
astPath.replaceWith(
t.classDeclaration(
t.identifier(renameComponentClassName),
node.superClass,
node.body,
node.decorators || []
)
)
}
} else if (node.superClass.name === 'Component') {
if (node.id === null) {
const renameComponentClassName = '_TaroComponentClass'
astPath.replaceWith(t.classDeclaration(t.identifier(renameComponentClassName), node.superClass, node.body, node.decorators || []))
astPath.replaceWith(
t.classDeclaration(
t.identifier(renameComponentClassName),
node.superClass,
node.body,
node.decorators || []
)
)
}
}
},
Expand All @@ -188,7 +245,9 @@ function processOthers (code) {
}
})
if (!defaultSpecifier) {
node.specifiers.unshift(t.importDefaultSpecifier(t.identifier(nervJsImportDefaultName)))
node.specifiers.unshift(
t.importDefaultSpecifier(t.identifier(nervJsImportDefaultName))
)
} else {
taroImportDefaultName = defaultSpecifier
node.specifiers[idx].local.name = nervJsImportDefaultName
Expand All @@ -207,9 +266,12 @@ function processOthers (code) {
exit (astPath) {
if (!taroImportDefaultName) return
const node = astPath.node
const importTaro = template(`
const importTaro = template(
`
import ${taroImportDefaultName} from '${taroJsFramework}'
`, babylonConfig)
`,
babylonConfig
)
node.body.unshift(importTaro())
}
}
Expand All @@ -223,32 +285,37 @@ function processOthers (code) {

function build ({ watch }) {
fs.ensureDirSync(tempPath)
vfs.src(path.join(sourceDir, '**'))
.pipe(through2.obj(function (file, enc, cb) {
if (file.isNull() || file.isStream()) {
return cb(null, file)
}
const filePath = file.path
const content = file.contents.toString()
if (entryFilePath === filePath) {
const transformResult = processEntry(content)
const jsCode = transformResult.code
file.contents = Buffer.from(jsCode)
} else if (Util.JS_EXT.indexOf(path.extname(filePath)) >= 0) {
const transformResult = processOthers(content)
let jsCode = transformResult.code
if (transformResult.taroImportDefaultName) {
jsCode = `import ${transformResult.taroImportDefaultName} from '${taroJsFramework}'\n${jsCode}`
vfs
.src(path.join(sourceDir, '**'))
.pipe(
through2.obj(function (file, enc, cb) {
if (file.isNull() || file.isStream()) {
return cb(null, file)
}
file.contents = Buffer.from(jsCode)
}
this.push(file)
cb()
}))
const filePath = file.path
const content = file.contents.toString()
if (entryFilePath === filePath) {
const transformResult = processEntry(content)
const jsCode = transformResult.code
file.contents = Buffer.from(jsCode)
} else if (Util.JS_EXT.indexOf(path.extname(filePath)) >= 0) {
const transformResult = processOthers(content)
let jsCode = transformResult.code
if (transformResult.taroImportDefaultName) {
jsCode = `import ${
transformResult.taroImportDefaultName
} from '${taroJsFramework}'\n${jsCode}`
}
file.contents = Buffer.from(jsCode)
}
this.push(file)
cb()
})
)
.pipe(vfs.dest(path.join(tempPath)))
.on('end', async () => {
const entry = {
'app': path.join(tempPath, CONFIG.ENTRY)
app: path.join(tempPath, CONFIG.ENTRY)
}
const h5Config = projectConfig.h5 || {}
h5Config.env = projectConfig.env
Expand Down

0 comments on commit 163b7bb

Please sign in to comment.