-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(taro-runner-utils): 添加 @tarojs/runner-utils 包
- Loading branch information
1 parent
4635bec
commit 201a991
Showing
7 changed files
with
563 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { getBundleResult, getBundleContent, getSassLoaderOption } from './scss' | ||
|
||
export { | ||
getBundleResult, | ||
getBundleContent, | ||
getSassLoaderOption | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { getBundleResult, getBundleContent, getSassLoaderOption } from './scss'; | ||
export { getBundleResult, getBundleContent, getSassLoaderOption }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.