Skip to content

Commit

Permalink
feat(taro-runner-utils): 添加 @tarojs/runner-utils 包
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfield550 authored and luckyadam committed Dec 31, 2019
1 parent 4635bec commit 201a991
Show file tree
Hide file tree
Showing 7 changed files with 563 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/taro-runner-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@tarojs/runner-utils",
"version": "2.0.0-beta.6",
"description": "Taro runner utilities.",
"main": "dist/index.js",
"files": [
"dist",
"types"
],
"scripts": {
"build": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/NervJS/taro.git"
},
"keywords": [
"taro"
],
"author": "garfield550",
"license": "MIT",
"devDependencies": {
"typescript": "^3.7.3"
},
"dependencies": {
"scss-bundle": "^3.0.2"
}
}
7 changes: 7 additions & 0 deletions packages/taro-runner-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getBundleResult, getBundleContent, getSassLoaderOption } from './scss'

export {
getBundleResult,
getBundleContent,
getSassLoaderOption
}
85 changes: 85 additions & 0 deletions packages/taro-runner-utils/src/scss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Bundler, BundleResult } from 'scss-bundle'

interface LoaderOption {
data?: string
[key: string]: any
}

interface BuildConfig {
sass?: {
resource?: string | string[]
projectDirectory?: string
data?: string
}
sassLoaderOption?: LoaderOption
}

/**
* 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.
*/
export async function getBundleResult(url: string,
projectDirectory: string | undefined = undefined
): Promise<BundleResult> {
let bundler: Bundler = new Bundler()
if (projectDirectory) {
bundler = new Bundler(undefined, projectDirectory)
}
const res = await bundler.bundle(url)
return res
}

export async function getBundleContent(resource: string | string[],
projectDirectory: string | undefined = undefined
): Promise<string | undefined> {
let result: string | undefined = ''

try {
if (typeof resource === 'string') {
const res = await getBundleResult(resource, projectDirectory)
result = res.bundledContent
}

if (Array.isArray(resource)) {
for (const url of resource) {
const res = await getBundleResult(url, projectDirectory)
result += res.bundledContent || ''
}
}
} catch (error) {
throw new Error(error)
}

return result
}

export async function getSassLoaderOption<T extends BuildConfig>(
{ sass, sassLoaderOption }: T
): Promise<LoaderOption> {
sassLoaderOption = sassLoaderOption || {}

let bundledContent: string = ''

if (!sass) {
return sassLoaderOption
}

const { resource, projectDirectory } = sass
if (resource) {
const content = await getBundleContent(resource, projectDirectory)
bundledContent += content
}

if (sass.data) {
bundledContent += sass.data
}
return {
...sassLoaderOption,
data: sassLoaderOption.data ? `${sassLoaderOption.data}${bundledContent}` : bundledContent
}
}

export default getSassLoaderOption
31 changes: 31 additions & 0 deletions packages/taro-runner-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"compilerOptions": {
"allowJs": false,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"baseUrl": ".",
"declaration": true,
"declarationDir": "types/taro-runner-utils",
"experimentalDecorators": true,
"module": "CommonJS",
"moduleResolution": "node",
"noImplicitAny": false,
"noUnusedLocals": true,
"outDir": "dist",
"preserveConstEnums": true,
"removeComments": false,
"rootDir": "./src",
"skipLibCheck": true,
"sourceMap": false,
"strictNullChecks": true,
"target": "ES2015",
"traceResolution": false
},
"include": [
"./src",
"./types"
],
"exclude": [
"./src/__tests__"
]
}
2 changes: 2 additions & 0 deletions packages/taro-runner-utils/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { getBundleResult, getBundleContent, getSassLoaderOption } from './scss';
export { getBundleResult, getBundleContent, getSassLoaderOption };
24 changes: 24 additions & 0 deletions packages/taro-runner-utils/types/scss.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { BundleResult } from 'scss-bundle';
interface LoaderOption {
data?: string;
[key: string]: any;
}
interface BuildConfig {
sass?: {
resource?: string | string[];
projectDirectory?: string;
data?: string;
};
sassLoaderOption?: LoaderOption;
}
/**
* 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.
*/
export declare function getBundleResult(url: string, projectDirectory?: string | undefined): Promise<BundleResult>;
export declare function getBundleContent(resource: string | string[], projectDirectory?: string | undefined): Promise<string | undefined>;
export declare function getSassLoaderOption<T extends BuildConfig>({ sass, sassLoaderOption }: T): Promise<LoaderOption>;
export default getSassLoaderOption;
Loading

0 comments on commit 201a991

Please sign in to comment.