Skip to content

Commit

Permalink
fix: 修复小程序插件编译错误,close #6627
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Jun 14, 2020
1 parent 52a3241 commit 5524f8b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
38 changes: 27 additions & 11 deletions packages/taro-cli/src/presets/platforms/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default (ctx) => {
isWatch
} = ctx.runOpts
const { sourcePath, outputPath } = ctx.paths
const { chalk, fs, PLATFORMS } = ctx.helper
const { emptyDirectory, chalk, fs, PLATFORMS } = ctx.helper
const { WEAPP, ALIPAY } = PLATFORMS

const PLUGIN_JSON = 'plugin.json'
Expand All @@ -20,6 +20,8 @@ export default (ctx) => {
[WEAPP]: '微信',
[ALIPAY]: '支付宝'
}
emptyDirectory(outputPath)

if (plugin !== WEAPP && plugin !== ALIPAY) {
console.log(chalk.red('目前插件编译仅支持 微信/支付宝 小程序!'))
return
Expand All @@ -30,19 +32,29 @@ export default (ctx) => {
await ctx.applyPlugins({
name: 'build',
opts: {
platform: 'weapp',
isBuildPlugin: true,
isWatch,
outputRoot: `${config.outputRoot}/miniprogram`
config: {
...config,
isBuildPlugin: true,
isWatch,
outputRoot: `${config.outputRoot}/miniprogram`,
platform: 'weapp',
needClearOutput: false
},
platform: 'weapp'
}
})
await ctx.applyPlugins({
name: 'build',
opts: {
platform: 'weapp',
isBuildPlugin: false,
isWatch,
outputRoot: `${config.outputRoot}/miniprogram`
config: {
...config,
isBuildPlugin: false,
isWatch,
outputRoot: `${config.outputRoot}`,
platform: 'weapp',
needClearOutput: false
},
platform: 'weapp'
}
})
}
Expand All @@ -51,8 +63,12 @@ export default (ctx) => {
await ctx.applyPlugins({
name: 'build',
opts: {
platform: 'alipay',
isWatch
config: {
...config,
isWatch,
platform: 'alipay'
},
platform: 'alipay'
}
})
const pluginJson = path.join(sourcePath, PLUGIN_JSON)
Expand Down
8 changes: 6 additions & 2 deletions packages/taro-cli/src/presets/platforms/weapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export default (ctx) => {
async fn ({ config }) {
const { appPath, nodeModulesPath, outputPath } = ctx.paths
const { npm, emptyDirectory } = ctx.helper
emptyDirectory(outputPath)
const isBuildPlugin = config.isBuildPlugin || false

if (!('needClearOutput' in config) || config.needClearOutput) {
emptyDirectory(outputPath)
}

// 生成 project.config.json
ctx.generateProjectConfig({
Expand All @@ -42,7 +46,7 @@ export default (ctx) => {
...config,
nodeModulesPath,
buildAdapter: config.platform,
isBuildPlugin: false,
isBuildPlugin,
globalObject: 'wx',
fileType: {
templ: '.wxml',
Expand Down
16 changes: 15 additions & 1 deletion packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ export default class MiniPlugin {
}

async generateMiniFiles (compilation: webpack.compilation.Compilation, commonStyles: Set<string>) {
const { isBuildQuickapp, fileType, buildAdapter, commonChunks, modifyBuildTempFileContent, modifyBuildAssets } = this.options
const { isBuildQuickapp, fileType, buildAdapter, commonChunks, modifyBuildTempFileContent, modifyBuildAssets, isBuildPlugin } = this.options
if (typeof modifyBuildTempFileContent === 'function') {
await modifyBuildTempFileContent(taroFileTypeMap)
}
Expand Down Expand Up @@ -1069,6 +1069,20 @@ export default class MiniPlugin {
}
}
})
if (isBuildPlugin) {
const pluginJSONPath = path.join(this.sourceDir, 'plugin', 'plugin.json')
if (pluginJSONPath) {
const pluginJSON = fs.readJSONSync(pluginJSONPath)
if (pluginJSON.main) {
pluginJSON.main = path.basename(pluginJSON.main, path.extname(pluginJSON.main)) + '.js'
}
const relativePath = this.getRelativePath(pluginJSONPath).replace(/\\/g, '/')
compilation.assets[relativePath] = {
size: () => JSON.stringify(pluginJSON).length,
source: () => JSON.stringify(pluginJSON)
}
}
}
if (typeof modifyBuildAssets === 'function') {
await modifyBuildAssets(compilation.assets)
}
Expand Down
4 changes: 0 additions & 4 deletions packages/taro-mini-runner/src/webpack/build.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>, chain: any
from: path.join(sourceRoot, 'plugin', 'doc'),
to: path.join(outputRoot, 'doc')
})
patterns.push({
from: path.join(sourceRoot, 'plugin', 'plugin.json'),
to: path.join(outputRoot, 'plugin', 'plugin.json')
})
copy = Object.assign({}, copy, { patterns })
}
if (copy) {
Expand Down

0 comments on commit 5524f8b

Please sign in to comment.