Skip to content

Commit

Permalink
feat(cli): 将 app/页面/组件 添加到入口文件进行编译
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 31, 2019
1 parent e5a9fca commit 97cc0e1
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 34 deletions.
14 changes: 11 additions & 3 deletions packages/taro-cli/src/mini/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ export async function build (appPath: string, { watch, adapter = BUILD_TYPES.WEA
}

async function buildWithWebpack ({ appPath }: { appPath: string }) {
const { entryFilePath, outputDir, sourceDir, buildAdapter, projectConfig, isProduction } = getBuildData()
console.log(entryFilePath, outputDir)
const {
entryFilePath,
outputDir,
sourceDir,
buildAdapter,
projectConfig,
isProduction,
constantsReplaceList
} = getBuildData()
const miniRunner = await npmProcess.getNpmPkg('@tarojs/mini-runner', appPath)
const babelConfig = getBabelConfig(projectConfig!.plugins!.babel)
const miniRunnerOpts = {
Expand All @@ -41,7 +48,8 @@ async function buildWithWebpack ({ appPath }: { appPath: string }) {
plugins: {
babel: babelConfig
},
isWatch: !isProduction
isWatch: !isProduction,
constantsReplaceList
}
miniRunner(miniRunnerOpts)
}
3 changes: 3 additions & 0 deletions packages/taro-mini-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
"babel-core": "^6.26.3",
"babel-generator": "^6.26.1",
"babel-loader": "^8.0.6",
"babel-plugin-danger-remove-unused-import": "^1.1.2",
"babel-plugin-transform-define": "^1.3.1",
"babel-traverse": "^6.26.0",
"babel-types": "^6.26.0",
"better-babel-generator": "^6.26.1",
"chalk": "^2.4.2",
"fs-extra": "^8.0.1",
"loader-utils": "^1.2.3",
Expand Down
9 changes: 6 additions & 3 deletions packages/taro-mini-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ export default function build (config: IBuildConfig) {
use: [{
loader: path.resolve(__dirname, './loaders/fileParseLoader'),
options: {
babel
babel,
constantsReplaceList: config.constantsReplaceList
}
}, {
loader: path.resolve(__dirname, './loaders/wxTransformerLoader'),
options: {
buildAdapter: config.buildAdapter
buildAdapter: config.buildAdapter,
fileTypeMap: MiniPlugin.getTaroFileTypeMap()
}
}]
}
]
},
plugins: [
new MiniPlugin({
buildAdapter: config.buildAdapter
buildAdapter: config.buildAdapter,
constantsReplaceList: config.constantsReplaceList
})
]
}
Expand Down
14 changes: 10 additions & 4 deletions packages/taro-mini-runner/src/loaders/fileParseLoader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { getOptions } from 'loader-utils'
import { transform } from 'babel-core'
import { transform, transformFromAst } from 'babel-core'

const cannotRemoves = ['@tarojs/taro', 'react', 'nervjs']

export default function fileParseLoader (source, ast) {
const options = getOptions(this)
const babelConfig = options.babel
const res = transform(source, babelConfig)
const { babel: babelConfig, constantsReplaceList } = getOptions(this)
const newBabelConfig = Object.assign({}, babelConfig)
newBabelConfig.plugins = [
[require('babel-plugin-danger-remove-unused-import'), { ignore: cannotRemoves }],
[require('babel-plugin-transform-define').default, constantsReplaceList]
].concat(newBabelConfig.plugins)
const res = transformFromAst(ast, '', newBabelConfig)
return res.code
}
15 changes: 10 additions & 5 deletions packages/taro-mini-runner/src/loaders/wxTransformerLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ import { getOptions } from 'loader-utils'
import * as wxTransformer from '@tarojs/transformer-wx'

import { REG_TYPESCRIPT } from '../utils/constants'
import { TARO_FILE_TYPE } from '../plugins/miniPlugin'

export default function wxTransformerLoader (source) {
const options = getOptions(this)
const { buildAdapter, fileTypeMap } = getOptions(this)
const filePath = this.resourcePath
const { buildAdapter } = options
const transformResult = wxTransformer({
const wxTransformerParams: any = {
code: source,
sourcePath: filePath,
isTyped: REG_TYPESCRIPT.test(filePath),
adapter: buildAdapter
})
}
if (fileTypeMap[filePath] === TARO_FILE_TYPE.APP) {
wxTransformerParams.isApp = true
} else if (fileTypeMap[filePath] === TARO_FILE_TYPE.PAGE) {
wxTransformerParams.isRoot = true
}
const transformResult = wxTransformer(wxTransformerParams)
this.callback(null, transformResult.code, transformResult.ast)
console.log('wxTransformerLoader')
return transformResult.code
}
95 changes: 83 additions & 12 deletions packages/taro-mini-runner/src/plugins/miniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import * as fs from 'fs-extra'

import * as wxTransformer from '@tarojs/transformer-wx'
import * as webpack from 'webpack'
import * as SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin'
import * as FunctionModulePlugin from 'webpack/lib/FunctionModulePlugin'
import * as NodeSourcePlugin from 'webpack/lib/node/NodeSourcePlugin'
import * as JsonpTemplatePlugin from 'webpack/lib/JsonpTemplatePlugin'
import * as LoaderTargetPlugin from 'webpack/lib/LoaderTargetPlugin'
import { defaults } from 'lodash'
import * as t from 'babel-types'
import traverse from 'babel-traverse'
Expand All @@ -16,18 +21,54 @@ interface IMiniPluginOptions {
buildAdapter: BUILD_TYPES
}

interface IComponent { name: string, path: string }

const PLUGIN_NAME = 'MiniPlugin'

const taroFileTypeMap = {}

export enum TARO_FILE_TYPE {
APP = 'APP',
PAGE = 'PAGE',
COMPONENT = 'COMPONENT'
}

export const createTarget = function createTarget(name) {
const target = compiler => {
const { options } = compiler
compiler.apply(
new JsonpTemplatePlugin(options.output),
new FunctionModulePlugin(options.output),
new NodeSourcePlugin(options.node),
new LoaderTargetPlugin('web')
)
}

const creater = new Function(
`var t = arguments[0]; return function ${name}(c) { return t(c); }`
);
return creater(target)
}

export const Targets = {
[BUILD_TYPES.WEAPP]: createTarget(BUILD_TYPES.WEAPP),
[BUILD_TYPES.ALIPAY]: createTarget(BUILD_TYPES.ALIPAY),
[BUILD_TYPES.SWAN]: createTarget(BUILD_TYPES.SWAN),
[BUILD_TYPES.TT]: createTarget(BUILD_TYPES.TT),
[BUILD_TYPES.QQ]: createTarget(BUILD_TYPES.QQ),
}

export default class MiniPlugin {
options: IMiniPluginOptions
appEntry: string
pages: Set<string>
components: Set<string>
pages: Set<IComponent>
components: Set<IComponent>
sourceDir: string

constructor (options = {}) {
this.options = defaults(options || {}, {
buildAdapter: BUILD_TYPES.WEAPP
buildAdapter: BUILD_TYPES.WEAPP,
commonLibName: 'lib.js'
})

this.pages = new Set()
Expand Down Expand Up @@ -68,6 +109,7 @@ export default class MiniPlugin {
}
const appEntryPath = getEntryPath(entry)
this.sourceDir = path.dirname(appEntryPath)
taroFileTypeMap[appEntryPath] = TARO_FILE_TYPE.APP
return appEntryPath
}

Expand Down Expand Up @@ -147,36 +189,65 @@ export default class MiniPlugin {
if (!appPages || appPages.length === 0) {
throw new Error('缺少页面')
}
this.pages = new Set([...appPages.map(item => resolveScriptPath(path.join(this.sourceDir, item)))])
this.pages = new Set([
...appPages.map(item => {
const pagePath = resolveScriptPath(path.join(this.sourceDir, item))
taroFileTypeMap[pagePath] = TARO_FILE_TYPE.PAGE
return { name: item, path: pagePath }
})
])
}

getComponents (fileList: Set<string>, isRoot: boolean) {
getComponents (fileList: Set<IComponent>, isRoot: boolean) {
const { buildAdapter } = this.options
fileList.forEach(file => {
const code = fs.readFileSync(file).toString()
const code = fs.readFileSync(file.path).toString()
const transformResult = wxTransformer({
code,
sourcePath: file,
isTyped: REG_TYPESCRIPT.test(file),
sourcePath: file.path,
isTyped: REG_TYPESCRIPT.test(file.path),
isRoot,
adapter: buildAdapter
})

let depComponents = transformResult.components
if (depComponents && depComponents.length) {
depComponents.forEach(item => {
const componentPath = resolveScriptPath(path.resolve(path.dirname(file), item.path))
if (fs.existsSync(componentPath)) {
this.components.add(componentPath)
this.getComponents(new Set([componentPath]), false)
const componentPath = resolveScriptPath(path.resolve(path.dirname(file.path), item.path))
if (fs.existsSync(componentPath) && !Array.from(this.components).some(item => item.path === componentPath)) {
taroFileTypeMap[componentPath] = TARO_FILE_TYPE.COMPONENT
const componentName = componentPath.replace(this.sourceDir, '').replace(/\\/g, '/').replace(path.extname(componentPath), '')
const componentObj = { name: componentName, path: componentPath }
this.components.add(componentObj)
this.getComponents(new Set([componentObj]), false)
}
})
}
})
}

addEntries (compiler: webpack.Compiler) {
const mainFiles = new Set([ ...this.pages, ...this.components ])
mainFiles.add({
name: 'app',
path: this.appEntry
})
mainFiles.forEach(item => {
compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation: webpack.compilation.Compilation, callback) => {
const dep = SingleEntryPlugin.createDependency(item.path, item.name)
compilation.addEntry(this.sourceDir, dep, item.name, callback)
})
})
}

run (compiler: webpack.Compiler) {
this.appEntry = this.getAppEntry(compiler)
this.getPages()
this.getComponents(this.pages, true)
this.addEntries(compiler)
}

static getTaroFileTypeMap () {
return taroFileTypeMap
}
}
3 changes: 2 additions & 1 deletion packages/taro-mini-runner/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ export interface ITaroBaseConfig {
}

export interface IBuildConfig extends ITaroBaseConfig, ITaroMiniConfig {
isWatch: boolean
isWatch: boolean,
constantsReplaceList: IOption
}
25 changes: 19 additions & 6 deletions packages/taro-mini-runner/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ babel-messages@^6.23.0, babel-messages@^6.8.0:
dependencies:
babel-runtime "^6.22.0"

babel-plugin-danger-remove-unused-import@^1.1.1:
babel-plugin-danger-remove-unused-import@^1.1.1, babel-plugin-danger-remove-unused-import@^1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/babel-plugin-danger-remove-unused-import/-/babel-plugin-danger-remove-unused-import-1.1.2.tgz#ac39c30edfe524ef8cfc411fec5edc479d19e132"

Expand Down Expand Up @@ -1074,7 +1074,7 @@ babel-plugin-transform-class-properties@^6.24.1:
babel-runtime "^6.22.0"
babel-template "^6.24.1"

babel-plugin-transform-define@^1.3.0:
babel-plugin-transform-define@^1.3.0, babel-plugin-transform-define@^1.3.1:
version "1.3.1"
resolved "https://registry.npmjs.org/babel-plugin-transform-define/-/babel-plugin-transform-define-1.3.1.tgz#b21b7bad3b84cf8e3f07cdc8c660b99cbbc01213"
dependencies:
Expand Down Expand Up @@ -1201,6 +1201,19 @@ bcrypt-pbkdf@^1.0.0:
dependencies:
tweetnacl "^0.14.3"

better-babel-generator@^6.26.1:
version "6.26.1"
resolved "https://registry.npmjs.org/better-babel-generator/-/better-babel-generator-6.26.1.tgz#7c26035f32d8d55d06dbc81b410378a6230a515e"
dependencies:
babel-messages "^6.23.0"
babel-runtime "^6.26.0"
babel-types "^6.26.0"
detect-indent "^4.0.0"
jsesc "2"
lodash "^4.17.4"
source-map "^0.5.7"
trim-right "^1.0.1"

big.js@^5.2.2:
version "5.2.2"
resolved "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz#65f0af382f578bcdc742bd9c281e9cb2d7768328"
Expand Down Expand Up @@ -3166,14 +3179,14 @@ jsdom@^11.5.1:
ws "^5.2.0"
xml-name-validator "^3.0.0"

jsesc@2, jsesc@^2.5.1:
version "2.5.2"
resolved "https://registry.npmjs.org/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"

jsesc@^1.3.0:
version "1.3.0"
resolved "https://registry.npmjs.org/jsesc/-/jsesc-1.3.0.tgz#46c3fec8c1892b12b0833db9bc7622176dbab34b"

jsesc@^2.5.1:
version "2.5.2"
resolved "https://registry.npmjs.org/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"

json-parse-better-errors@^1.0.1, json-parse-better-errors@^1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"
Expand Down

0 comments on commit 97cc0e1

Please sign in to comment.