Skip to content

Commit

Permalink
feat(cli): 调整文件编译
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 31, 2019
1 parent 3bc5450 commit f209936
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 33 deletions.
2 changes: 2 additions & 0 deletions packages/taro-cli/src/mini/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ async function buildWithWebpack ({ appPath }: { appPath: string }) {
entry: {
app: entryFilePath
},
sourceDir,
outputDir,
buildAdapter,
plugins: {
babel: babelConfig
},
isWatch: !isProduction,
constantsReplaceList,
designWidth: projectConfig.designWidth,
deviceRatio: projectConfig.deviceRatio
}
Expand Down
4 changes: 4 additions & 0 deletions packages/taro-mini-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@
"babel-types": "^6.26.0",
"better-babel-generator": "^6.26.1",
"chalk": "^2.4.2",
"file-loader": "^3.0.1",
"fs-extra": "^8.0.1",
"loader-utils": "^1.2.3",
"lodash": "^4.17.11",
"node-sass": "^4.12.0",
"ora": "^3.4.0",
"sass-loader": "^7.1.0",
"virtual-module-webpack-plugin": "^0.4.1",
"webpack": "^4.31.0",
"webpack-chain": "^6.0.0",
"webpack-format-messages": "^2.0.5"
Expand Down
22 changes: 21 additions & 1 deletion packages/taro-mini-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,27 @@ export default function build (config: IBuildConfig) {
fileTypeMap: MiniPlugin.getTaroFileTypeMap()
}
}]
}
},
{
test: /\.(scss|wxss|acss|)$/,
include: /src/,
use: [
{
loader: require.resolve('file-loader'),
options: {
useRelativePath: true,
name: `[path][name].wxss`,
context: config.sourceDir
}
},
{
loader: require.resolve('sass-loader'),
options: {

},
},
],
}
]
},
plugins: [
Expand Down
4 changes: 4 additions & 0 deletions packages/taro-mini-runner/src/loaders/loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function (source) {
console.log(source)
return source
}
15 changes: 4 additions & 11 deletions packages/taro-mini-runner/src/loaders/wxTransformerLoader.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import { getOptions } from 'loader-utils'
import wxTransformer from '@tarojs/transformer-wx'

import { REG_TYPESCRIPT, PARSE_AST_TYPE } from '../utils/constants'
import { REG_TYPESCRIPT } from '../utils/constants'

export default function wxTransformerLoader (source) {
const { buildAdapter, fileTypeMap } = getOptions(this)
const { buildAdapter } = getOptions(this)
const filePath = this.resourcePath
const wxTransformerParams: any = {
code: source,
sourcePath: filePath,
isTyped: REG_TYPESCRIPT.test(filePath),
adapter: buildAdapter
}
if (fileTypeMap[filePath]) {
const fileType = fileTypeMap[filePath].type
if (fileType === PARSE_AST_TYPE.ENTRY) {
wxTransformerParams.isApp = true
} else if (fileType === PARSE_AST_TYPE.PAGE) {
wxTransformerParams.isRoot = true
}
adapter: buildAdapter,
isNormal: true
}
const transformResult = wxTransformer(wxTransformerParams)
this.callback(null, transformResult.code, transformResult.ast)
Expand Down
22 changes: 19 additions & 3 deletions packages/taro-mini-runner/src/plugins/miniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 * as VirtualModulePlugin from 'virtual-module-webpack-plugin'
import { defaults } from 'lodash'
import * as t from 'babel-types'
import traverse from 'babel-traverse'
Expand All @@ -25,7 +26,8 @@ interface ITaroFileInfo {
[key: string]: {
type: PARSE_AST_TYPE,
config: IConfig,
wxml?: string
wxml?: string,
code?: string
}
}

Expand Down Expand Up @@ -200,7 +202,8 @@ export default class MiniPlugin {
taroFileTypeMap[this.appEntry] = {
type: PARSE_AST_TYPE.ENTRY,
config: configObj,
wxml: transformResult.template
wxml: transformResult.template,
code: transformResult.code
}
this.pages = new Set([
...appPages.map(item => {
Expand All @@ -225,7 +228,8 @@ export default class MiniPlugin {
taroFileTypeMap[file.path] = {
type: isRoot ? PARSE_AST_TYPE.PAGE : PARSE_AST_TYPE.COMPONENT,
config: configObj,
wxml: transformResult.template
wxml: transformResult.template,
code: transformResult.code
}
let depComponents = transformResult.components
if (depComponents && depComponents.length) {
Expand Down Expand Up @@ -277,11 +281,23 @@ export default class MiniPlugin {
})
}

transferFileContent (compiler: webpack.Compiler) {
Object.keys(taroFileTypeMap).forEach(item => {
const relativePath = item.replace(compiler.context, '')
const itemInfo = taroFileTypeMap[item]
new VirtualModulePlugin({
moduleName: relativePath,
contents: itemInfo.code
}).apply(compiler)
})
}

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

static getTaroFileTypeMap () {
Expand Down
3 changes: 3 additions & 0 deletions packages/taro-mini-runner/src/plugins/removeStyleImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class RemoveStyleImportPlugin {

}
Loading

0 comments on commit f209936

Please sign in to comment.