diff --git a/packages/taro-mini-runner/src/loaders/fileParseLoader.ts b/packages/taro-mini-runner/src/loaders/fileParseLoader.ts index ac4fd54c4a9a..5b8f9b33f1f9 100644 --- a/packages/taro-mini-runner/src/loaders/fileParseLoader.ts +++ b/packages/taro-mini-runner/src/loaders/fileParseLoader.ts @@ -28,6 +28,8 @@ const template = require('babel-template') const cannotRemoves = ['@tarojs/taro', 'react', 'nervjs'] +const NON_WEBPACK_REQUIRE = '__non_webpack_require__' + function processAst ( ast: t.File, buildAdapter: BUILD_TYPES, @@ -173,10 +175,31 @@ function processAst ( const source = node.source let value = source.value const specifiers = node.specifiers + if (isQuickApp && isQuickAppPkg(value)) { + let defaultSpecifier: string = 'LOCAL' + specifiers.forEach(item => { + if (item.type === 'ImportDefaultSpecifier') { + defaultSpecifier = item.local.name + } + }) + astPath.replaceWith( + t.variableDeclaration('const', [ + t.variableDeclarator( + t.identifier(defaultSpecifier), + t.callExpression( + t.identifier(NON_WEBPACK_REQUIRE),[ + t.stringLiteral(value) + ] + ) + ) + ]) + ) + return + } if (NODE_MODULES_REG.test(sourceFilePath) && sourceFilePath.indexOf(taroMiniAppFramework) >= 0) { return } - if (isNpmPkg(value) && !isQuickAppPkg(value)) { + if (isNpmPkg(value)) { if (value === taroJsComponents) { if (isQuickApp) { specifiers.forEach(specifier => { @@ -227,10 +250,14 @@ function processAst ( const args = node.arguments as t.StringLiteral[] let value = args[0].value const parentNode = astPath.parentPath.parentPath.node as t.VariableDeclaration + if (isQuickApp && isQuickAppPkg(value)) { + callee.name = NON_WEBPACK_REQUIRE + return + } if (NODE_MODULES_REG.test(sourceFilePath) && sourceFilePath.indexOf(taroMiniAppFramework) >= 0) { return } - if (isNpmPkg(value) && !isQuickAppPkg(value)) { + if (isNpmPkg(value)) { if (value === taroJsComponents) { if (isQuickApp) { if (parentNode.declarations.length === 1 && parentNode.declarations[0].init) { diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index 705fbba1deae..13c559b0ec37 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -60,6 +60,7 @@ export const Targets = { [BUILD_TYPES.SWAN]: createTarget(BUILD_TYPES.SWAN), [BUILD_TYPES.TT]: createTarget(BUILD_TYPES.TT), [BUILD_TYPES.QQ]: createTarget(BUILD_TYPES.QQ), + [BUILD_TYPES.QUICKAPP]: createTarget(BUILD_TYPES.QUICKAPP) } export function isFileToBeTaroComponent ( diff --git a/packages/taro-mini-runner/src/utils/helper.ts b/packages/taro-mini-runner/src/utils/helper.ts new file mode 100644 index 000000000000..b8c3f4bfb016 --- /dev/null +++ b/packages/taro-mini-runner/src/utils/helper.ts @@ -0,0 +1,18 @@ +import * as path from 'path' +import * as fs from 'fs' +import { isEmptyObject } from '.' + +let quickappConfig = {} + +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 +} diff --git a/packages/taro-mini-runner/src/utils/index.ts b/packages/taro-mini-runner/src/utils/index.ts index d944d600b075..1b3b5cdf8964 100644 --- a/packages/taro-mini-runner/src/utils/index.ts +++ b/packages/taro-mini-runner/src/utils/index.ts @@ -18,7 +18,7 @@ export function isNpmPkg (name: string): boolean { } export function isQuickAppPkg (name: string): boolean { - return /@system\./.test(name) + return /^@(system|service)\.[a-zA-Z]{1,}/.test(name) } export function isEmptyObject (obj: any): boolean { diff --git a/packages/taro-mini-runner/src/webpack/build.conf.ts b/packages/taro-mini-runner/src/webpack/build.conf.ts index cbdeeecc2228..48478f9f9d8d 100644 --- a/packages/taro-mini-runner/src/webpack/build.conf.ts +++ b/packages/taro-mini-runner/src/webpack/build.conf.ts @@ -13,10 +13,11 @@ import { getModule, mergeOption, getMiniPlugin, - getMiniCssExtractPlugin + getMiniCssExtractPlugin, } 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 = {} @@ -56,6 +57,7 @@ export default (appPath: string, mode, config: Partial): any => { const plugin: any = {} const minimizer: any[] = [] const sourceDir = path.join(appPath, sourceRoot) + const isQuickapp = buildAdapter === BUILD_TYPES.QUICKAPP if (copy) { plugin.copyWebpackPlugin = getCopyWebpackPlugin({ copy, appPath }) @@ -90,8 +92,7 @@ export default (appPath: string, mode, config: Partial): any => { plugin.cssoWebpackPlugin = getCssoWebpackPlugin([csso ? csso.config : {}]) } } - - chain.merge({ + const mainConfig = { mode, devtool: getDevtool(enableSourceMap), entry, @@ -156,6 +157,21 @@ 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 }