-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ziyi.hzy
committed
Mar 26, 2018
0 parents
commit fc57943
Showing
15 changed files
with
19,042 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,6 @@ | ||
node_modules | ||
.cache | ||
.vscode | ||
.temp | ||
built | ||
coverage |
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 @@ | ||
node_modules | ||
.cache | ||
.vscode | ||
.temp | ||
built | ||
test | ||
coverage |
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,12 @@ | ||
{ | ||
"bracketSpacing": true, | ||
"printWidth": 120, | ||
"proseWrap": "never", | ||
"requirePragma": false, | ||
"semi": false, | ||
"singleQuote": false, | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"useTabs": false, | ||
"overrides": [{ "files": "*.json", "options": { "printWidth": 200 } }] | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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,15 @@ | ||
{ | ||
"name": "pri-plugin-dob", | ||
"version": "0.0.0", | ||
"types": "src/index.ts", | ||
"main": "built/index.js", | ||
"scripts": { | ||
"start": "pri plugin-watch", | ||
"prepublishOnly": "pri plugin-build", | ||
"release": "rimraf built && npm publish", | ||
"test": "pri test" | ||
}, | ||
"dependencies": { | ||
"pri": "*" | ||
} | ||
} |
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,155 @@ | ||
import * as fs from "fs-extra" | ||
import * as _ from "lodash" | ||
import * as normalizePath from "normalize-path" | ||
import * as path from "path" | ||
import * as prettier from "prettier" | ||
import { helperPath, pri, storesPath, tempJsEntryPath } from "pri" | ||
import { md5 } from "./utils/md5" | ||
|
||
const LAYOUT_TEMP = "LayoutTempComponent" | ||
const LAYOUT = "LayoutComponent" | ||
|
||
const MARKDOWN_LAYOUT_TEMP = "MarkdownLayoutTempComponent" | ||
const MARKDOWN_LAYOUT = "MarkdownLayoutComponent" | ||
|
||
const safeName = (str: string) => _.upperFirst(_.camelCase(str)) | ||
|
||
interface IResult { | ||
projectAnalyseDob: { | ||
storeFiles: Array<{ | ||
name: string | ||
file: path.ParsedPath | ||
}> | ||
} | ||
} | ||
|
||
export default (instance: typeof pri) => { | ||
const projectRootPath = instance.project.getProjectRootPath() | ||
|
||
instance.project.onAnalyseProject(files => { | ||
return { | ||
projectAnalyseDob: { | ||
storeFiles: files | ||
.filter(file => { | ||
const relativePath = path.relative(projectRootPath, path.join(file.dir, file.name)) | ||
|
||
if (!relativePath.startsWith(storesPath.dir)) { | ||
return false | ||
} | ||
|
||
return true | ||
}) | ||
.map(file => { | ||
return { file, name: safeName(file.name) } | ||
}) | ||
} | ||
} as IResult | ||
}) | ||
|
||
instance.project.onCreateEntry((analyseInfo: IResult, entry, env, projectConfig) => { | ||
const helperAbsolutePath = path.join(projectRootPath, path.format(helperPath)) | ||
|
||
if (analyseInfo.projectAnalyseDob.storeFiles.length === 0) { | ||
if (fs.existsSync(helperAbsolutePath)) { | ||
fs.removeSync(helperAbsolutePath) | ||
} | ||
|
||
return | ||
} | ||
|
||
// Connect normal pages | ||
entry.pipe.set("normalPagesImportEnd", importEnd => { | ||
return ` | ||
${importEnd}.then(component => Connect()(component.default)) | ||
` | ||
}) | ||
|
||
// Connect layout | ||
entry.pipe.set("analyseLayoutImportName", text => LAYOUT_TEMP) | ||
entry.pipe.set("analyseLayoutBody", body => { | ||
return ` | ||
${body} | ||
const ${LAYOUT} = Connect()(${LAYOUT_TEMP}) | ||
` | ||
}) | ||
|
||
// Connect markdown layout | ||
entry.pipe.set("analyseMarkdownLayoutImportName", text => MARKDOWN_LAYOUT_TEMP) | ||
entry.pipe.set("analyseMarkdownLayoutBody", body => { | ||
return ` | ||
${body} | ||
const ${MARKDOWN_LAYOUT} = Connect()(${MARKDOWN_LAYOUT_TEMP}) | ||
` | ||
}) | ||
|
||
const entryRelativeToHelper = path.relative( | ||
path.join(tempJsEntryPath.dir), | ||
path.join(helperPath.dir, helperPath.name) | ||
) | ||
|
||
entry.pipeHeader(header => { | ||
return ` | ||
${header} | ||
import { useStrict } from "dob" | ||
import { Connect, Provider } from "dob-react" | ||
import { stores } from "${normalizePath(entryRelativeToHelper)}" | ||
` | ||
}) | ||
|
||
entry.pipeBody(body => { | ||
return ` | ||
${body} | ||
useStrict() | ||
` | ||
}) | ||
|
||
entry.pipeRenderRouter(router => { | ||
return ` | ||
<Provider {...stores}> | ||
${router} | ||
</Provider> | ||
` | ||
}) | ||
|
||
const storesHelper = ` | ||
import { combineStores } from "dob" | ||
${analyseInfo.projectAnalyseDob.storeFiles | ||
.map(storeFile => { | ||
const importAbsolutePath = path.join(storeFile.file.dir, storeFile.file.name) | ||
const importRelativePath = path.relative(path.join(projectRootPath, helperPath.dir), importAbsolutePath) | ||
return `import { ${storeFile.name}Action, ${storeFile.name}Store } from "${normalizePath( | ||
importRelativePath | ||
)}"` | ||
}) | ||
.join("\n")} | ||
const stores = combineStores({${analyseInfo.projectAnalyseDob.storeFiles | ||
.map(storeFile => { | ||
return `${storeFile.name}Action, ${storeFile.name}Store` | ||
}) | ||
.join(",")}}) | ||
export { stores } | ||
` | ||
|
||
// If has stores, create helper.ts | ||
fs.outputFileSync( | ||
helperAbsolutePath, | ||
prettier.format(getHelperContent(storesHelper), { | ||
semi: false, | ||
parser: "typescript" | ||
}) | ||
) | ||
}) | ||
} | ||
|
||
function getHelperContent(str: string) { | ||
return ` | ||
/** | ||
* Do not edit this file. | ||
* This file is automatic generated to get type help. | ||
*/ | ||
${str} | ||
` | ||
} |
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,4 @@ | ||
import { env, setCustomEnv, setEnvLocal, setEnvProd } from "../utils/env"; | ||
import { IProjectConfig as ProjectConfig } from "../utils/project-config-interface"; | ||
export { ProjectConfig }; | ||
export { env, setEnvLocal, setEnvProd, setCustomEnv }; |
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,11 @@ | ||
export declare class GlobalEnv { | ||
isLocal: boolean; | ||
isProd: boolean; | ||
customEnv: any; | ||
get: (name: string) => any; | ||
} | ||
declare let env: GlobalEnv; | ||
export { env }; | ||
export declare function setEnvLocal(): void; | ||
export declare function setEnvProd(): void; | ||
export declare function setCustomEnv(info: any): void; |
47 changes: 47 additions & 0 deletions
47
src/utils/declare/client/utils/project-config-interface.d.ts
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,47 @@ | ||
export declare class IProjectConfig { | ||
/** | ||
* Title for html <title>. | ||
*/ | ||
title?: string; | ||
/** | ||
* Dist dir path. | ||
* Only take effect on `npm run build` | `pri build`. | ||
*/ | ||
distDir?: string; | ||
/** | ||
* Dist main file name. | ||
* Only take effect on `npm run build` | `pri build`. | ||
*/ | ||
distFileName?: string; | ||
/** | ||
* Assets public path. `"//some.com"`, `"//some.com/somePath"`, `"/somePath"`. | ||
* If not set, result: `/<distPath>`. | ||
* If set /somePath for example, result: `/somePath/<distPath>`. | ||
* If set some.com for example, result: `//some.com/<distPath>`. | ||
* If set some.com/somePath for example, result: `//some.com/somePath/<distPath>`. | ||
* Only take effect on `npm run build` | `pri build`. | ||
*/ | ||
publicPath?: string | null; | ||
/** | ||
* Base href for all pages. | ||
* For example, `/admin` is the root path after deploy, you should set baseHref to `/admin`. | ||
* There is no need to modify the code, routing `/` can automatically maps to `/admin`. | ||
* Only take effect on `npm run build` | `pri build` | ||
*/ | ||
baseHref?: string; | ||
/** | ||
* Generate static index file for each route, when building. | ||
* Usefal for static service who don't serve fallback html, like github-pages. | ||
*/ | ||
staticBuild: boolean; | ||
/** | ||
* Custom env. | ||
*/ | ||
customEnv?: { | ||
[key: string]: any; | ||
}; | ||
/** | ||
* Using https for server. | ||
*/ | ||
useHttps: boolean; | ||
} |
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 @@ | ||
declare module "*.json" |
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,17 @@ | ||
declare module "@babel/preset-env" | ||
declare module "@babel/preset-react" | ||
declare module "opn" | ||
declare module "walk" | ||
declare module "colors" | ||
declare module "koa-static" | ||
declare module "koa-mount" | ||
declare module "koa-compress" | ||
declare module "node-forge" | ||
declare module "@koa/cors" | ||
declare module "react-router-dom/StaticRouter" | ||
declare module "markdown-it" | ||
declare module "react-loadable" | ||
declare module "extract-text-webpack-plugin" | ||
declare module "html-webpack-plugin" | ||
declare module "normalize-path" | ||
declare module "preload-webpack-plugin" |
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,8 @@ | ||
import * as crypto from "crypto" | ||
|
||
export function md5(str: string) { | ||
return crypto | ||
.createHash("md5") | ||
.update(str, "utf8") | ||
.digest("hex") | ||
} |
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,9 @@ | ||
import test from "ava" | ||
|
||
function sum(a: number, b: number) { | ||
return a + b | ||
} | ||
|
||
test("adds 1 + 2 to equal 3", t => { | ||
t.true(sum(1, 2) === 3) | ||
}) |
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,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"strict": true, | ||
"strictNullChecks": false, | ||
"jsx": "react", | ||
"target": "esnext", | ||
"experimentalDecorators": true, | ||
"skipLibCheck": true, | ||
"outDir": "built", | ||
"lib": ["dom", "es5", "es6", "scripthost"] | ||
}, | ||
"exclude": ["node_modules", "built", "lib"] | ||
} |
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,26 @@ | ||
{ | ||
"extends": "tslint:latest", | ||
"defaultSeverity": "error", | ||
"rules": { | ||
"semicolon": [ | ||
false | ||
], | ||
"object-literal-sort-keys": false, | ||
"max-classes-per-file": [ | ||
true, | ||
5 | ||
], | ||
"trailing-comma": [ | ||
false | ||
], | ||
"no-string-literal": false, | ||
"max-line-length": [ | ||
true, | ||
200 | ||
], | ||
"arrow-parens": false, | ||
"no-implicit-dependencies": false, | ||
"no-object-literal-type-assertion": false, | ||
"no-submodule-imports": false | ||
} | ||
} |