From 991588dd24936f5f6d05e6972f7c313e54823c3a Mon Sep 17 00:00:00 2001 From: Garfield Lee Date: Fri, 6 Dec 2019 11:26:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20webpack-runner=20=E5=92=8C=20mini-runne?= =?UTF-8?q?r=20=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lerna.json | 1 + packages/taro-mini-runner/package.json | 1 + packages/taro-mini-runner/src/index.ts | 56 +------------------ .../taro-mini-runner/src/utils/bundler.ts | 24 -------- packages/taro-webpack-runner/package.json | 2 +- .../taro-webpack-runner/src/util/chain.ts | 56 +------------------ 6 files changed, 8 insertions(+), 132 deletions(-) delete mode 100644 packages/taro-mini-runner/src/utils/bundler.ts diff --git a/lerna.json b/lerna.json index a15cc4e896e7..9e4f32c74828 100644 --- a/lerna.json +++ b/lerna.json @@ -30,6 +30,7 @@ "packages/taro-quickapp", "packages/taro-qq", "packages/taro-jd", + "packages/taro-runner-utils", "packages/taro-webpack-runner", "packages/taro-mini-runner", "packages/postcss-plugin-constparse", diff --git a/packages/taro-mini-runner/package.json b/packages/taro-mini-runner/package.json index fc2b37694d11..5c73afddf9a2 100644 --- a/packages/taro-mini-runner/package.json +++ b/packages/taro-mini-runner/package.json @@ -32,6 +32,7 @@ }, "homepage": "https://github.com/NervJS/taro#readme", "dependencies": { + "@tarojs/runner-utils": "2.0.0-beta.6", "@tarojs/taro": "2.0.0-beta.6", "@tarojs/transformer-wx": "2.0.0-beta.6", "babel-core": "^6.26.3", diff --git a/packages/taro-mini-runner/src/index.ts b/packages/taro-mini-runner/src/index.ts index 1e49b3d76d91..b82751b56af4 100644 --- a/packages/taro-mini-runner/src/index.ts +++ b/packages/taro-mini-runner/src/index.ts @@ -1,9 +1,9 @@ import * as webpack from 'webpack' +import { getSassLoaderOption } from '@tarojs/runner-utils' -import { IBuildConfig } from './utils/types' +import { IBuildConfig, IOption } from './utils/types' import { BUILD_TYPES } from './utils/constants' import { printBuildError, bindProdLogger, bindDevLogger } from './utils/logHelper' -import Bundler from './utils/bundler' import buildConf from './webpack/build.conf' const customizeChain = (chain, customizeFunc: Function) => { @@ -12,58 +12,8 @@ const customizeChain = (chain, customizeFunc: Function) => { } } -const getSassLoaderOption = async ({ sass, sassLoaderOption }: IBuildConfig) => { - let bundledContent = '' - sassLoaderOption = sassLoaderOption || {} - if (!sass) { - return sassLoaderOption - } - if (sass.resource && !sass.projectDirectory) { - const { resource } = sass - try { - if (typeof resource === 'string') { - const res = await Bundler(resource) - bundledContent += res.bundledContent - if (Array.isArray(resource)) { - for (const url of resource) { - const res = await Bundler(url) - bundledContent += res.bundledContent - } - } - } - } catch (e) { - console.log(e) - } - } - - if (sass.resource && sass.projectDirectory) { - const { resource, projectDirectory } = sass - try { - if (typeof resource === 'string') { - const res = await Bundler(resource, projectDirectory) - bundledContent += res.bundledContent - } - if (Array.isArray(resource)) { - for (const url of resource) { - const res = await Bundler(url, projectDirectory) - bundledContent += res.bundledContent - } - } - } catch (e) { - console.log(e) - } - } - if (sass.data) { - bundledContent += sass.data - } - return { - ...sassLoaderOption, - data: sassLoaderOption.data ? `${sassLoaderOption.data}${bundledContent}` : bundledContent - } -} - const makeConfig = async (buildConfig: IBuildConfig) => { - const sassLoaderOption = await getSassLoaderOption(buildConfig) + const sassLoaderOption: IOption = await getSassLoaderOption(buildConfig) return { ...buildConfig, sassLoaderOption diff --git a/packages/taro-mini-runner/src/utils/bundler.ts b/packages/taro-mini-runner/src/utils/bundler.ts deleted file mode 100644 index 7b970d0731c5..000000000000 --- a/packages/taro-mini-runner/src/utils/bundler.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Bundler } from 'scss-bundle' - -/** - * Return bundled sass content. - * - * @param {string} url Absolute file path. - * @param {(string | undefined)} projectDirectory Absolute project location, where node_modules are located. Used for resolving tilde imports. - * @returns Bundle result. - */ -async function getBundleContent(url, projectDirectory?) { - let bundler: any - if (projectDirectory) { - bundler = new Bundler(undefined, projectDirectory) - } else { - bundler = new Bundler() - } - if (!bundler) { - throw new ReferenceError(`'bundler' is not defined.`) - } - const res = await bundler.Bundle(url) - return res -} - -export default getBundleContent diff --git a/packages/taro-webpack-runner/package.json b/packages/taro-webpack-runner/package.json index 8b6632be2132..f6de80dc5df4 100644 --- a/packages/taro-webpack-runner/package.json +++ b/packages/taro-webpack-runner/package.json @@ -33,6 +33,7 @@ "homepage": "https://github.com/NervJS/taro#readme", "dependencies": { "@tarojs/plugin-sass": "2.0.0-beta.6", + "@tarojs/runner-utils": "2.0.0-beta.6", "@tarojs/taro": "2.0.0-beta.6", "@tarojs/taro-h5": "2.0.0-beta.6", "autoprefixer": "8.6.4", @@ -63,7 +64,6 @@ "resolve-url-loader": "2.3.0", "sass": "^1.23.1", "sass-loader": "7.1.0", - "scss-bundle": "^2.5.1", "style-loader": "0.21.0", "stylus": "0.54.5", "stylus-loader": "3.0.2", diff --git a/packages/taro-webpack-runner/src/util/chain.ts b/packages/taro-webpack-runner/src/util/chain.ts index e33799c266de..61e5a4c16251 100644 --- a/packages/taro-webpack-runner/src/util/chain.ts +++ b/packages/taro-webpack-runner/src/util/chain.ts @@ -1,5 +1,5 @@ import * as apis from '@tarojs/taro-h5/dist/taroApis' -import * as Bundler from '@tarojs/plugin-sass/bundler' +import { getSassLoaderOption } from '@tarojs/runner-utils' import * as CopyWebpackPlugin from 'copy-webpack-plugin' import CssoWebpackPlugin from 'csso-webpack-plugin' import * as sass from 'sass' @@ -16,60 +16,8 @@ import { recursiveMerge } from '.' import { getPostcssPlugins } from '../config/postcss.conf' import { Option, BuildConfig } from './types' -const getSassLoaderOption = async ({ sass, sassLoaderOption }: BuildConfig) => { - let bundledContent = '' - sassLoaderOption = sassLoaderOption || {} - if (!sass) { - return sassLoaderOption - } - if (sass.resource && !sass.projectDirectory) { - const { resource } = sass - try { - if (typeof resource === 'string') { - const res = await Bundler(resource) - bundledContent += res.bundledContent - if (Array.isArray(resource)) { - for (const url of resource) { - const res = await Bundler(url) - bundledContent += res.bundledContent - } - } - } - } catch (e) { - console.log(e) - } - } - - // check resource & projectDirectory property - // projectDirectory used for resolving tilde imports - if (sass.resource && sass.projectDirectory) { - const { resource, projectDirectory } = sass - try { - if (typeof resource === 'string') { - const res = await Bundler(resource, projectDirectory) - bundledContent += res.bundledContent - } - if (Array.isArray(resource)) { - for (const url of resource) { - const res = await Bundler(url, projectDirectory) - bundledContent += res.bundledContent - } - } - } catch (e) { - console.log(e) - } - } - if (sass.data) { - bundledContent += sass.data - } - return { - ...sassLoaderOption, - data: sassLoaderOption.data ? `${sassLoaderOption.data}${bundledContent}` : bundledContent - } -} - const makeConfig = async (buildConfig: BuildConfig) => { - const sassLoaderOption = await getSassLoaderOption(buildConfig) + const sassLoaderOption: Option = await getSassLoaderOption(buildConfig) return { ...buildConfig, sassLoaderOption