diff --git a/packages/taro-cli/src/mini/helper.ts b/packages/taro-cli/src/mini/helper.ts index 5cb749ef92a3..9e848742adb4 100644 --- a/packages/taro-cli/src/mini/helper.ts +++ b/packages/taro-cli/src/mini/helper.ts @@ -1,7 +1,10 @@ import * as fs from 'fs-extra' import * as path from 'path' +import { execSync } from 'child_process' +import chalk from 'chalk' import * as _ from 'lodash' +import * as ora from 'ora' import { Config } from '@tarojs/taro' import { IProjectConfig, ITaroManifestConfig } from '@tarojs/taro/types/compile' import wxTransformer from '@tarojs/transformer-wx' @@ -32,8 +35,12 @@ import { recursiveFindNodeModules, getBabelConfig, extnameExpRegOf, - generateAlipayPath + generateAlipayPath, + unzip, + shouldUseYarn, + shouldUseCnpm } from '../util' +import { downloadGithubRepoLatestRelease } from '../util/dowload' import { resolveNpmPkgMainPath } from '../util/resolve_npm_files' import { resolveNpmSync } from '../util/npm' @@ -393,3 +400,79 @@ export function getImportTaroSelfComponents (filePath, taroSelfComponents) { }) return importTaroSelfComponents } + +export async function prepareQuickAppEnvironment (buildData: IBuildData) { + let isReady = false + let needDownload = false + let needInstall = false + const originalOutputDir = buildData.originalOutputDir + console.log() + if (fs.existsSync(path.join(buildData.originalOutputDir, 'sign'))) { + needDownload = false + } else { + needDownload = true + } + if (needDownload) { + const getSpinner = ora('开始下载快应用运行容器...').start() + await downloadGithubRepoLatestRelease('NervJS/quickapp-container', buildData.appPath, originalOutputDir) + await unzip(path.join(originalOutputDir, 'download_temp.zip')) + getSpinner.succeed('快应用运行容器下载完成') + } else { + console.log(`${chalk.green('✔ ')} 快应用容器已经准备好`) + } + process.chdir(originalOutputDir) + console.log() + if (fs.existsSync(path.join(originalOutputDir, 'node_modules'))) { + needInstall = false + } else { + needInstall = true + } + if (needInstall) { + let command + if (shouldUseYarn()) { + command = 'NODE_ENV=development yarn install' + } else if (shouldUseCnpm()) { + command = 'NODE_ENV=development cnpm install' + } else { + command = 'NODE_ENV=development npm install' + } + const installSpinner = ora(`安装快应用依赖环境, 需要一会儿...`).start() + try { + const stdout = execSync(command) + installSpinner.color = 'green' + installSpinner.succeed('安装成功') + console.log(`${stdout}`) + isReady = true + } catch (error) { + installSpinner.color = 'red' + installSpinner.fail(chalk.red(`快应用依赖环境安装失败,请进入 ${path.basename(originalOutputDir)} 重新安装!`)) + console.log(`${error}`) + isReady = false + } + } else { + console.log(`${chalk.green('✔ ')} 快应用依赖已经安装好`) + isReady = true + } + return isReady +} + +export async function runQuickApp (isWatch: boolean | void, buildData: IBuildData, port?: number, release?: boolean) { + const originalOutputDir = buildData.originalOutputDir + const { compile } = require(require.resolve('hap-toolkit/lib/commands/compile', { paths: [originalOutputDir] })) + if (isWatch) { + const { launchServer } = require(require.resolve('@hap-toolkit/server', { paths: [originalOutputDir] })) + launchServer({ + port: port || 12306, + watch: isWatch, + clearRecords: false, + disableADB: false + }) + compile('native', 'dev', true) + } else { + if (!release) { + compile('native', 'dev', false) + } else { + compile('native', 'prod', false) + } + } +} diff --git a/packages/taro-mini-runner/package.json b/packages/taro-mini-runner/package.json index da9169188524..c8de00de343e 100644 --- a/packages/taro-mini-runner/package.json +++ b/packages/taro-mini-runner/package.json @@ -56,6 +56,7 @@ "ora": "^3.4.0", "postcss-loader": "^3.0.0", "postcss-pxtransform": "^1.3.2", + "request": "^2.88.0", "resolve": "^1.11.1", "sass-loader": "^7.1.0", "stylus-loader": "^3.0.2", @@ -64,7 +65,8 @@ "virtual-module-webpack-plugin": "^0.4.1", "webpack": "^4.31.0", "webpack-chain": "^6.0.0", - "webpack-format-messages": "^2.0.5" + "webpack-format-messages": "^2.0.5", + "yauzl": "2.10.0" }, "devDependencies": { "@types/babel-core": "^6.25.6", diff --git a/packages/taro-mini-runner/src/loaders/fileParseLoader.ts b/packages/taro-mini-runner/src/loaders/fileParseLoader.ts index 5b8f9b33f1f9..c341baa6c463 100644 --- a/packages/taro-mini-runner/src/loaders/fileParseLoader.ts +++ b/packages/taro-mini-runner/src/loaders/fileParseLoader.ts @@ -435,7 +435,8 @@ function processAst ( template(`import Taro from '${taroMiniAppFramework}'`, babylonConfig as any)() as any ) } - node.body.push(template(`export default require('${taroMiniAppFramework}').default.createApp(${exportVariableName})`, babylonConfig as any)() as any) + node.body.push(template(`exportRes = require('${taroMiniAppFramework}').default.createApp(${exportVariableName})`, babylonConfig as any)() as any) + node.body.push(template(`export default exportRes`, babylonConfig as any)() as any) } else { node.body.push(template(`App(require('${taroMiniAppFramework}').default.createApp(${exportVariableName}))`, babylonConfig as any)() as any) } @@ -451,7 +452,8 @@ function processAst ( template(`import Taro from '${taroMiniAppFramework}'`, babylonConfig as any)() as any ) } - node.body.push(template(`export default require('${taroMiniAppFramework}').default.createComponent(${exportVariableName}, '${pagePath}')`, babylonConfig as any)() as any) + node.body.push(template(`exportRes = require('${taroMiniAppFramework}').default.createComponent(${exportVariableName}, '${pagePath}')`, babylonConfig as any)() as any) + node.body.push(template(`export default exportRes`, babylonConfig as any)() as any) } else { node.body.push(template(`Page(require('${taroMiniAppFramework}').default.createComponent(${exportVariableName}, true))`, babylonConfig as any)() as any) } @@ -463,7 +465,8 @@ function processAst ( template(`import Taro from '${taroMiniAppFramework}'`, babylonConfig as any)() as any ) } - node.body.push(template(`export default require('${taroMiniAppFramework}').default.createComponent(${exportVariableName})`, babylonConfig as any)() as any) + node.body.push(template(`exportRes = require('${taroMiniAppFramework}').default.createComponent(${exportVariableName})`, babylonConfig as any)() as any) + node.body.push(template(`export default exportRes`, babylonConfig as any)() as any) } else { node.body.push(template(`Component(require('${taroMiniAppFramework}').default.createComponent(${exportVariableName}))`, babylonConfig as any)() as any) } @@ -484,7 +487,8 @@ export default function fileParseLoader (source, ast) { constantsReplaceList, buildAdapter, designWidth, - deviceRatio + deviceRatio, + sourceDir } = getOptions(this) const filePath = this.resourcePath const newAst = transformFromAst(ast, '', { @@ -494,7 +498,7 @@ export default function fileParseLoader (source, ast) { ] }).ast as t.File const miniType = this._module.miniType || PARSE_AST_TYPE.NORMAL - const result = processAst(newAst, buildAdapter, miniType, designWidth, deviceRatio, filePath, this.context) + const result = processAst(newAst, buildAdapter, miniType, designWidth, deviceRatio, filePath, sourceDir) const code = generate(result).code const res = transform(code, babelConfig) return res.code diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index 12172380577a..8b0b23e831fc 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -12,14 +12,14 @@ import * as LoaderTargetPlugin from 'webpack/lib/LoaderTargetPlugin' import { merge, defaults, kebabCase } from 'lodash' import * as t from 'babel-types' import traverse from 'babel-traverse' -import { Config as IConfig } from '@tarojs/taro' +import { Config as IConfig, PageConfig } from '@tarojs/taro' import * as _ from 'lodash' -import { REG_TYPESCRIPT, BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES, NODE_MODULES_REG, CONFIG_MAP, taroJsFramework, taroJsComponents, QUICKAPP_SPECIAL_COMPONENTS, taroJsQuickAppComponents } from '../utils/constants' +import { REG_TYPESCRIPT, BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES, NODE_MODULES_REG, CONFIG_MAP, taroJsFramework } from '../utils/constants' import { IComponentObj } from '../utils/types' import { resolveScriptPath, buildUsingComponents, isNpmPkg, resolveNpmSync, isEmptyObject, promoteRelativePath } from '../utils' import TaroSingleEntryDependency from '../dependencies/TaroSingleEntryDependency' -import { getTaroJsQuickAppComponentsPath, generateQuickAppUx, getImportTaroSelfComponents } from '../utils/helper' +import { getTaroJsQuickAppComponentsPath, generateQuickAppUx, getImportTaroSelfComponents, generateQuickAppManifest } from '../utils/helper' import parseAst from '../utils/parseAst' import TaroLoadChunksPlugin from './TaroLoadChunksPlugin' @@ -32,6 +32,8 @@ interface IMiniPluginOptions { nodeModulesPath: string, sourceDir: string, outputDir: string, + quickappJSON?: any, + designWidth: number, commonChunks: string[] } @@ -60,7 +62,7 @@ export const createTarget = function createTarget (name) { new JsonpTemplatePlugin().apply(compiler) new FunctionModulePlugin(options.output).apply(compiler) new NodeSourcePlugin(options.node).apply(compiler) - new LoaderTargetPlugin('web').apply(compiler) + new LoaderTargetPlugin('node').apply(compiler) } } @@ -133,6 +135,7 @@ export default class MiniPlugin { outputDir: string context: string appConfig: IConfig + pageConfigs: Map constructor (options = {}) { this.options = defaults(options || {}, { @@ -140,6 +143,7 @@ export default class MiniPlugin { nodeModulesPath: '', sourceDir: '', outputDir: '', + designWidth: 750, commonChunks: ['runtime', 'vendors'] }) this.sourceDir = this.options.sourceDir @@ -147,6 +151,7 @@ export default class MiniPlugin { this.pages = new Set() this.components = new Set() + this.pageConfigs = new Map() } tryAsync = fn => async (arg, callback) => { @@ -189,7 +194,7 @@ export default class MiniPlugin { new TaroLoadChunksPlugin({ commonChunks: this.options.commonChunks, - taroFileTypeMap + buildAdapter: this.options.buildAdapter }).apply(compiler) new TaroNormalModulesPlugin().apply(compiler) @@ -374,10 +379,11 @@ export default class MiniPlugin { } this.getSubPackages(configObj) this.generateTabBarFiles(compiler, configObj) + const template = '' taroFileTypeMap[this.appEntry] = { type: PARSE_AST_TYPE.ENTRY, config: configObj, - template: transformResult.template, + template, code: transformResult.code } this.pages = new Set([ @@ -403,7 +409,7 @@ export default class MiniPlugin { componentName = componentPath.replace(this.sourceDir, '').replace(/\\/g, '/').replace(path.extname(componentPath), '') } - return componentName + return componentName.replace(/^(\/|\\)/, '') } getComponents (fileList: Set, isRoot: boolean) { @@ -465,6 +471,10 @@ export default class MiniPlugin { }) let parseAstRes = parseAst(transformResult.ast, buildAdapter) configObj = parseAstRes.configObj + if (isRoot) { + const showPath = file.path.replace(this.sourceDir, '').replace(path.extname(file.path), '') + this.pageConfigs.set(showPath, configObj) + } taroSelfComponents = parseAstRes.taroSelfComponents const usingComponents = configObj.usingComponents if (usingComponents) { @@ -487,11 +497,7 @@ export default class MiniPlugin { if (isQuickApp) { const scriptPath = file.path const outputScriptPath = scriptPath.replace(this.sourceDir, this.outputDir).replace(path.extname(scriptPath), MINI_APP_FILES[buildAdapter].SCRIPT) - const stylePath = outputScriptPath.replace(path.extname(outputScriptPath), MINI_APP_FILES[buildAdapter].STYLE) - const templPath = outputScriptPath.replace(path.extname(outputScriptPath), MINI_APP_FILES[buildAdapter].TEMPL) - const styleRelativePath = promoteRelativePath(path.relative(outputScriptPath, stylePath)) - const scriptRelativePath = promoteRelativePath(path.relative(templPath, outputScriptPath)) - const importTaroSelfComponents = getImportTaroSelfComponents(outputScriptPath, this.options.nodeModulesPath, taroSelfComponents) + const importTaroSelfComponents = getImportTaroSelfComponents(outputScriptPath, this.options.nodeModulesPath, this.outputDir, taroSelfComponents) const usingComponents = configObj.usingComponents let importUsingComponent: any = new Set([]) if (usingComponents) { @@ -510,8 +516,6 @@ export default class MiniPlugin { })) template = generateQuickAppUx({ template, - script: scriptRelativePath, - style: styleRelativePath, imports: new Set([...importTaroSelfComponents, ...importUsingComponent, ...importCustomComponents]) }) } @@ -586,19 +590,50 @@ export default class MiniPlugin { const extname = path.extname(item) const templatePath = relativePath.replace(extname, MINI_APP_FILES[buildAdapter].TEMPL) const jsonPath = relativePath.replace(extname, MINI_APP_FILES[buildAdapter].CONFIG) + const scriptPath = relativePath.replace(extname, MINI_APP_FILES[buildAdapter].SCRIPT) + const stylePath = relativePath.replace(extname, MINI_APP_FILES[buildAdapter].STYLE) const itemInfo = taroFileTypeMap[item] - if (itemInfo.type !== PARSE_AST_TYPE.ENTRY) { - compilation.assets[templatePath] = { - size: () => itemInfo.template!.length, - source: () => itemInfo.template - } - } + let template = itemInfo.template if (!isQuickApp) { const jsonStr = JSON.stringify(itemInfo.config) compilation.assets[jsonPath] = { size: () => jsonStr.length, source: () => jsonStr } + } else { + let hitScriptItem + Object.keys(compilation.assets).forEach(item => { + if (stylePath.indexOf(item) >= 0) { + const relativeStylePath = promoteRelativePath(path.relative(scriptPath, stylePath)) + template = `\n` + template + } + if (scriptPath.indexOf(item) >= 0) { + let scriptContent = compilation.assets[item]._source.source() + scriptContent = `let exportRes;\n${scriptContent}\nexport default exportRes;` + hitScriptItem = item + template += `\n` + } + }) + if (hitScriptItem) { + delete compilation.assets[hitScriptItem] + } + const quickappJSON = generateQuickAppManifest({ + appConfig: this.appConfig, + designWidth: this.options.designWidth, + pageConfigs: this.pageConfigs, + quickappJSON: this.options.quickappJSON + }) + const quickappJSONStr = JSON.stringify(quickappJSON) + compilation.assets['./manifest.json'] = { + size: () => quickappJSONStr.length, + source: () => quickappJSONStr + } + } + if (template) { + compilation.assets[templatePath] = { + size: () => template!.length, + source: () => template + } } if (itemInfo.taroSelfComponents) { itemInfo.taroSelfComponents.forEach(item => { diff --git a/packages/taro-mini-runner/src/plugins/TaroLoadChunksPlugin.ts b/packages/taro-mini-runner/src/plugins/TaroLoadChunksPlugin.ts index 9b549f88018a..d0f51b0f4054 100644 --- a/packages/taro-mini-runner/src/plugins/TaroLoadChunksPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/TaroLoadChunksPlugin.ts @@ -4,59 +4,74 @@ import webpack, { compilation } from 'webpack' import { ConcatSource } from 'webpack-sources' import { urlToRequest } from 'loader-utils' -import { PARSE_AST_TYPE, REG_STYLE } from '../utils/constants' - -import { ITaroFileInfo } from './MiniPlugin' +import { PARSE_AST_TYPE, REG_STYLE, BUILD_TYPES } from '../utils/constants' +import { promoteRelativePath } from '../utils' const PLUGIN_NAME = 'TaroLoadChunksPlugin' interface IOptions { commonChunks: string[], - taroFileTypeMap: ITaroFileInfo + buildAdapter: BUILD_TYPES } export default class TaroLoadChunksPlugin { commonChunks: string[] - taroFileTypeMap: ITaroFileInfo + buildAdapter: BUILD_TYPES constructor (options: IOptions) { this.commonChunks = options.commonChunks + this.buildAdapter = options.buildAdapter } apply (compiler: webpack.Compiler) { compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation: compilation.Compilation) => { let commonChunks compilation.hooks.afterOptimizeChunks.tap(PLUGIN_NAME, (chunks: compilation.Chunk[]) => { - commonChunks = chunks.filter(chunk => this.commonChunks.includes(chunk.name)) + commonChunks = chunks.filter(chunk => this.commonChunks.includes(chunk.name)).reverse() }) compilation.chunkTemplate.hooks.renderWithEntry.tap(PLUGIN_NAME, (modules, chunk) => { - if (chunk.entryModule && chunk.entryModule.miniType === PARSE_AST_TYPE.ENTRY) { - compilation.hooks.afterOptimizeAssets.tap(PLUGIN_NAME, assets => { - const files = chunk.files - files.forEach(item => { - if (REG_STYLE.test(item)) { - const source = new ConcatSource() - const _source = assets[item]._source - Object.keys(assets).forEach(assetName => { - const fileName = path.basename(assetName, path.extname(assetName)) - if (REG_STYLE.test(assetName) && this.commonChunks.includes(fileName)) { - source.add(`@import ${JSON.stringify(urlToRequest(assetName))}`) - source.add('\n') - source.add(_source) - assets[item]._source = source - } - }) - } + if (chunk.entryModule) { + if (chunk.entryModule.miniType === PARSE_AST_TYPE.ENTRY) { + compilation.hooks.afterOptimizeAssets.tap(PLUGIN_NAME, assets => { + const files = chunk.files + files.forEach(item => { + if (REG_STYLE.test(item)) { + const source = new ConcatSource() + const _source = assets[item]._source + Object.keys(assets).forEach(assetName => { + const fileName = path.basename(assetName, path.extname(assetName)) + if (REG_STYLE.test(assetName) && this.commonChunks.includes(fileName)) { + source.add(`@import ${JSON.stringify(urlToRequest(assetName))}`) + source.add('\n') + source.add(_source) + assets[item]._source = source + } + }) + } + }) + }) + const source = new ConcatSource() + const id = chunk.id + commonChunks.forEach(chunkItem => { + source.add(`require(${JSON.stringify(promoteRelativePath(path.relative(id, chunkItem.name)))});\n`) + }) + source.add('\n') + source.add(modules) + source.add(';') + return source + } else if (this.buildAdapter === BUILD_TYPES.QUICKAPP && + (chunk.entryModule.miniType === PARSE_AST_TYPE.PAGE || + chunk.entryModule.miniType === PARSE_AST_TYPE.COMPONENT)) { + const source = new ConcatSource() + const id = chunk.id + commonChunks.forEach(chunkItem => { + source.add(`require(${JSON.stringify(promoteRelativePath(path.relative(id, chunkItem.name)))});\n`) }) - }) - const source = new ConcatSource() - commonChunks.reverse().forEach(chunkItem => { - source.add(`require(${JSON.stringify(urlToRequest(chunkItem.name))});\n`) - }) - source.add('\n') - source.add(modules) - source.add(';') - return source + source.add('\n') + source.add(modules) + source.add(';') + return source + } } }) }) diff --git a/packages/taro-mini-runner/src/utils/helper.ts b/packages/taro-mini-runner/src/utils/helper.ts index c58c3419bc76..f1375d7f2b9a 100644 --- a/packages/taro-mini-runner/src/utils/helper.ts +++ b/packages/taro-mini-runner/src/utils/helper.ts @@ -1,22 +1,8 @@ import * as path from 'path' -import * as fs from 'fs' -import { isEmptyObject, getInstalledNpmPkgPath, promoteRelativePath } from '.' -import { taroJsQuickAppComponents, REG_STYLE, REG_SCRIPT } from './constants' - -let quickappConfig = {} +import * as _ from 'lodash' -export function getQuickappConfig (appPath) { - if (!isEmptyObject(quickappConfig)) { - return quickappConfig - } - const configPath = path.join(appPath, 'project.quickapp.json') - if (!fs.existsSync(configPath)) { - quickappConfig = require('../config/manifest.default.json') - } else { - quickappConfig = JSON.parse(fs.readFileSync(configPath).toString()) - } - return quickappConfig -} +import { getInstalledNpmPkgPath, promoteRelativePath } from '.' +import { taroJsQuickAppComponents, REG_STYLE, REG_SCRIPT } from './constants' export function getTaroJsQuickAppComponentsPath (nodeModulesPath: string): string { const taroJsQuickAppComponentsPkg = getInstalledNpmPkgPath(taroJsQuickAppComponents, nodeModulesPath) @@ -27,13 +13,13 @@ export function getTaroJsQuickAppComponentsPath (nodeModulesPath: string): strin return path.join(path.dirname(taroJsQuickAppComponentsPkg as string), 'src/components') } -export function getImportTaroSelfComponents (filePath, nodeModulesPath, taroSelfComponents) { +export function getImportTaroSelfComponents (filePath, nodeModulesPath, outputDir, taroSelfComponents) { const importTaroSelfComponents = new Set<{ path: string, name: string }>() const taroJsQuickAppComponentsPath = getTaroJsQuickAppComponentsPath(nodeModulesPath) taroSelfComponents.forEach(c => { const cPath = path.join(taroJsQuickAppComponentsPath, c) const cMainPath = path.join(cPath, 'index') - const cRelativePath = promoteRelativePath(path.relative(filePath, cMainPath.replace(nodeModulesPath, 'npm'))) + const cRelativePath = promoteRelativePath(path.relative(filePath, cMainPath.replace(nodeModulesPath, path.join(outputDir, 'npm')))) importTaroSelfComponents.add({ path: cRelativePath, name: c @@ -81,3 +67,57 @@ export function generateQuickAppUx ({ } return uxTxt } + +export function generateQuickAppManifest ({ + appConfig, + quickappJSON, + pageConfigs, + designWidth +}) { + // 生成 router + const pages = (appConfig.pages as string[]).concat() + const routerPages = {} + const customPageConfig = quickappJSON.customPageConfig || {} + + pages.forEach(element => { + const customConfig = customPageConfig[element] + const pageConf: any = { + component: path.basename(element) + } + if (customConfig) { + const filter = customConfig.filter + const launchMode = customConfig.launchMode + if (filter) { + pageConf.filter = filter + } + if (launchMode) { + pageConf.launchMode = launchMode + } + } + routerPages[path.dirname(element)] = pageConf + }) + delete quickappJSON.customPageConfig + const routerEntry = pages.shift() + const router = { + entry: path.dirname(routerEntry as string), + pages: routerPages + } + // 生成 display + const display = JSON.parse(JSON.stringify(appConfig.window || {})) + display.pages = {} + pageConfigs.forEach((item, page) => { + if (item) { + display.pages[path.dirname(page)] = item + } + }) + quickappJSON.router = router + quickappJSON.display = display + quickappJSON.config = Object.assign({}, quickappJSON.config, { + designWidth: designWidth || 750 + }) + if (appConfig.window && appConfig.window.navigationStyle === 'custom') { + quickappJSON.display.titleBar = false + delete quickappJSON.display.navigationStyle + } + return quickappJSON +} diff --git a/packages/taro-mini-runner/src/utils/index.ts b/packages/taro-mini-runner/src/utils/index.ts index b5d40c4ad190..b3794a49e909 100644 --- a/packages/taro-mini-runner/src/utils/index.ts +++ b/packages/taro-mini-runner/src/utils/index.ts @@ -4,6 +4,7 @@ import * as fs from 'fs-extra' import * as resolvePath from 'resolve' import * as t from 'babel-types' import { mergeWith } from 'lodash' +import { Transform } from 'stream' import { CONFIG_MAP, JS_EXT, TS_EXT, NODE_MODULES_REG, MINI_APP_FILES, BUILD_TYPES, CSS_IMPORT_REG, REG_STYLE } from './constants' import { IOption, IComponentObj } from './types' diff --git a/packages/taro-mini-runner/src/utils/types.ts b/packages/taro-mini-runner/src/utils/types.ts index 478eb3ad0ca9..c4b5d8af72ee 100644 --- a/packages/taro-mini-runner/src/utils/types.ts +++ b/packages/taro-mini-runner/src/utils/types.ts @@ -23,5 +23,6 @@ export interface IBuildConfig extends IProjectBaseConfig, IMiniAppConfig { isWatch: boolean, port?: number, buildAdapter: BUILD_TYPES, - nodeModulesPath: string + nodeModulesPath: string, + quickappJSON: any } diff --git a/packages/taro-mini-runner/src/webpack/build.conf.ts b/packages/taro-mini-runner/src/webpack/build.conf.ts index 6519ac3cda7d..7fee552031e6 100644 --- a/packages/taro-mini-runner/src/webpack/build.conf.ts +++ b/packages/taro-mini-runner/src/webpack/build.conf.ts @@ -17,7 +17,6 @@ import { } from './chain' import { BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES } from '../utils/constants' import { Targets } from '../plugins/MiniPlugin' -import { getQuickappConfig } from '../utils/helper' const emptyObj = {} @@ -49,6 +48,7 @@ export default (appPath: string, mode, config: Partial): any => { postcss = emptyObj, nodeModulesPath, + quickappJSON, babel, csso, @@ -59,14 +59,21 @@ export default (appPath: string, mode, config: Partial): any => { const minimizer: any[] = [] const sourceDir = path.join(appPath, sourceRoot) const outputDir = path.join(appPath, outputRoot) - const isQuickapp = buildAdapter === BUILD_TYPES.QUICKAPP if (copy) { plugin.copyWebpackPlugin = getCopyWebpackPlugin({ copy, appPath }) } const constantsReplaceList = mergeOption([processEnvOption(env), defineConstants]) plugin.definePlugin = getDefinePlugin([constantsReplaceList]) - plugin.miniPlugin = getMiniPlugin({ sourceDir, outputDir, buildAdapter, constantsReplaceList, nodeModulesPath }) + plugin.miniPlugin = getMiniPlugin({ + sourceDir, + outputDir, + buildAdapter, + constantsReplaceList, + nodeModulesPath, + quickappJSON, + designWidth + }) plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{ filename: `[name]${MINI_APP_FILES[buildAdapter].STYLE}`, @@ -101,7 +108,7 @@ export default (appPath: string, mode, config: Partial): any => { output: getOutput(appPath, [{ outputRoot, publicPath: '/', - buildAdapter + buildAdapter, }, output]), target: Targets[buildAdapter], resolve: { @@ -161,19 +168,6 @@ export default (appPath: string, mode, config: Partial): any => { } } - // if (isQuickapp) { - // const quickappConfig = getQuickappConfig(appPath) - // const features = quickappConfig['features'] - // if (features && features.length) { - // const externals = {} - // features.forEach(item => { - // externals[`@${item.name}`] = { - // root: `@${item.name}` - // } - // }) - // mainConfig['externals'] = externals - // } - // } chain.merge(mainConfig) return chain } diff --git a/packages/taro-mini-runner/src/webpack/chain.ts b/packages/taro-mini-runner/src/webpack/chain.ts index 5c2c13ff0d73..4233441eb8fa 100644 --- a/packages/taro-mini-runner/src/webpack/chain.ts +++ b/packages/taro-mini-runner/src/webpack/chain.ts @@ -22,7 +22,8 @@ const globalObjectMap = { [BUILD_TYPES.ALIPAY]: 'my', [BUILD_TYPES.SWAN]: 'swan', [BUILD_TYPES.QQ]: 'qq', - [BUILD_TYPES.TT]: 'tt' + [BUILD_TYPES.TT]: 'tt', + [BUILD_TYPES.QUICKAPP]: 'global' } const defaultUglifyJsOption = { @@ -250,7 +251,8 @@ export const getModule = (appPath: string, { designWidth, deviceRatio, buildAdapter, - constantsReplaceList + constantsReplaceList, + sourceDir }]) const wxTransformerLoader = getWxTransformerLoader([{