Skip to content

Commit 0ec2ca3

Browse files
committed
feat: vue-function-api-extra v0.1.0
1 parent 996592b commit 0ec2ca3

File tree

8 files changed

+278
-0
lines changed

8 files changed

+278
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "vue-function-api-extra",
3+
"version": "0.1.0",
4+
"main": "dist/vue-function-api-extra.common.js",
5+
"unpkg": "dist/vue-function-api-extra.js",
6+
"repository": "https://github.com/chrisbing/vue-function-api-extra.git",
7+
"author": "Chris bingyuwuhen@gmail.com",
8+
"license": "MIT",
9+
"scripts": {
10+
"build-bundle": "rollup --config rollup.config.js",
11+
"build-files": "tsc",
12+
"build": "yarn build-bundle && yarn build-files"
13+
},
14+
"typings": "dist/index.d.ts",
15+
"module": "dist/vue-function-api-extra.esm.js",
16+
"devDependencies": {
17+
"rollup": "^1.19.4",
18+
"rollup-plugin-typescript": "^1.0.1",
19+
"typescript": "^3.5.3",
20+
"vue": "^2.6.10",
21+
"vue-function-api": "^2.2.0"
22+
},
23+
"peerDependencies": {
24+
"vue": "2",
25+
"vue-function-api": "2"
26+
}
27+
}

rollup.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import typescript from 'rollup-plugin-typescript';
2+
3+
const generateConfig = (format, filename) => {
4+
return {
5+
input: 'src/index.ts',
6+
output: {
7+
file: `dist/${filename}`,
8+
format: format,
9+
name: 'VueFunctionApiExtra',
10+
},
11+
plugins: [
12+
typescript()
13+
]
14+
}
15+
}
16+
17+
module.exports = [
18+
generateConfig('umd', 'vue-function-api-extra.js'),
19+
generateConfig('cjs', 'vue-function-api-extra.common.js'),
20+
generateConfig('esm', 'vue-function-api-extra.esm.js')
21+
]

src/getters.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {
2+
computed, SetupContext,
3+
} from 'vue-function-api'
4+
import {AnyObject} from "vue-function-api/dist/types/basic";
5+
6+
/**
7+
* helper function to use vuex getters
8+
* @param context
9+
* @param getters Name array of getters.
10+
*/
11+
export const useGetters = (context: SetupContext, getters:string[]) => {
12+
const computedObject:AnyObject = {}
13+
getters.forEach((key) => {
14+
computedObject[key] = computed(() => context.store.getters[key])
15+
})
16+
return computedObject
17+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './getters'
2+
export * from './plugin'

src/plugin.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {PluginObject, VueConstructor} from "vue"
2+
import {Vue} from "vue/types/vue";
3+
4+
declare module 'vue-function-api' {
5+
interface SetupContext {
6+
[key:string]: any,
7+
}
8+
}
9+
10+
let curVue: VueConstructor | null = null
11+
const DEFAULT_EXTRA_KEYS = ['router', 'route']
12+
13+
14+
export interface PluginOptions {
15+
extraKeys?: string[],
16+
}
17+
18+
19+
20+
export const plugin: PluginObject<PluginOptions> = {
21+
install(Vue, options = {}) {
22+
if (curVue) {
23+
if (process.env.NODE_ENV !== 'production') {
24+
// eslint-disable-next-line no-console
25+
console.warn('Vue function api helper init duplicated !')
26+
}
27+
}
28+
const pureVueProtoKeys = Object.keys(Vue.prototype)
29+
const pureVm = Object.keys(new Vue())
30+
31+
const extraKeys = (options.extraKeys || []).concat(DEFAULT_EXTRA_KEYS)
32+
33+
function wrapperSetup(this: Vue) {
34+
let vm = this
35+
let $options = vm.$options
36+
let setup = $options.setup
37+
if (!setup) {
38+
return
39+
}
40+
if (typeof setup !== 'function') {
41+
// eslint-disable-next-line no-console
42+
console.warn('The "setup" option should be a function that returns a object in component definitions.', vm)
43+
return
44+
}
45+
// wapper the setup option, so that we can use prototype properties and mixin properties in context
46+
$options.setup = function wrappedSetup(props, ctx) {
47+
// to extend context
48+
Object.keys(vm)
49+
.filter(x => /^\$/.test(x) && pureVm.indexOf(x) === -1)
50+
.forEach((x) => {
51+
// @ts-ignore
52+
ctx[x.replace(/^\$/, '')] = vm[x]
53+
})
54+
Object.keys(vm.$root.constructor.prototype)
55+
.filter(x => /^\$/.test(x) && pureVueProtoKeys.indexOf(x) === -1)
56+
.forEach((x) => {
57+
// @ts-ignore
58+
ctx[x.replace(/^\$/, '')] = vm[x]
59+
})
60+
// to extend context with router properties
61+
extraKeys.forEach((key) => {
62+
// @ts-ignore
63+
let value = vm['$' + key]
64+
if (value) {
65+
ctx[key] = value
66+
}
67+
})
68+
// @ts-ignore
69+
return setup(props, ctx)
70+
}
71+
}
72+
73+
Vue.mixin({
74+
beforeCreate: wrapperSetup,
75+
76+
})
77+
},
78+
}

tsconfig.json

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
// "incremental": true, /* Enable incremental compilation */
5+
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
6+
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
7+
// "lib": [], /* Specify library files to be included in the compilation. */
8+
// "allowJs": true, /* Allow javascript files to be compiled. */
9+
// "checkJs": true, /* Report errors in .js files. */
10+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
11+
"declaration": true, /* Generates corresponding '.d.ts' file. */
12+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
13+
// "sourceMap": true, /* Generates corresponding '.map' file. */
14+
// "outFile": "./", /* Concatenate and emit output to single file. */
15+
"outDir": "dist", /* Redirect output structure to the directory. */
16+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
17+
// "composite": true, /* Enable project compilation */
18+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
19+
// "removeComments": true, /* Do not emit comments to output. */
20+
// "noEmit": true, /* Do not emit outputs. */
21+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
22+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
23+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
24+
25+
/* Strict Type-Checking Options */
26+
"strict": true, /* Enable all strict type-checking options. */
27+
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
28+
// "strictNullChecks": true, /* Enable strict null checks. */
29+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
30+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
31+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
32+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
33+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
34+
35+
/* Additional Checks */
36+
"noUnusedLocals": true, /* Report errors on unused locals. */
37+
"noUnusedParameters": true, /* Report errors on unused parameters. */
38+
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
39+
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
40+
41+
/* Module Resolution Options */
42+
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
43+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
44+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
45+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
46+
// "typeRoots": [], /* List of folders to include type definitions from. */
47+
// "types": [], /* Type declaration files to be included in compilation. */
48+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
49+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
50+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
52+
53+
/* Source Map Options */
54+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
55+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
56+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
57+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
58+
59+
/* Experimental Options */
60+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
61+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
62+
}
63+
}

yarn.lock

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@types/estree@0.0.39":
6+
version "0.0.39"
7+
resolved "https://registry.npm.taobao.org/@types/estree/download/@types/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
8+
9+
"@types/node@^12.6.9":
10+
version "12.7.2"
11+
resolved "https://registry.npm.taobao.org/@types/node/download/@types/node-12.7.2.tgz#c4e63af5e8823ce9cc3f0b34f7b998c2171f0c44"
12+
13+
acorn@^6.2.1:
14+
version "6.3.0"
15+
resolved "https://registry.npm.taobao.org/acorn/download/acorn-6.3.0.tgz#0087509119ffa4fc0a0041d1e93a417e68cb856e"
16+
17+
estree-walker@^0.6.1:
18+
version "0.6.1"
19+
resolved "https://registry.npm.taobao.org/estree-walker/download/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362"
20+
21+
path-parse@^1.0.6:
22+
version "1.0.6"
23+
resolved "https://registry.npm.taobao.org/path-parse/download/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
24+
25+
resolve@^1.10.0:
26+
version "1.12.0"
27+
resolved "https://registry.npm.taobao.org/resolve/download/resolve-1.12.0.tgz?cache=0&sync_timestamp=1564641434608&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fresolve%2Fdownload%2Fresolve-1.12.0.tgz#3fc644a35c84a48554609ff26ec52b66fa577df6"
28+
dependencies:
29+
path-parse "^1.0.6"
30+
31+
rollup-plugin-typescript@^1.0.1:
32+
version "1.0.1"
33+
resolved "https://registry.npm.taobao.org/rollup-plugin-typescript/download/rollup-plugin-typescript-1.0.1.tgz#86565033b714c3d1f3aba510aad3dc519f7091e9"
34+
dependencies:
35+
resolve "^1.10.0"
36+
rollup-pluginutils "^2.5.0"
37+
38+
rollup-pluginutils@^2.5.0:
39+
version "2.8.1"
40+
resolved "https://registry.npm.taobao.org/rollup-pluginutils/download/rollup-pluginutils-2.8.1.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Frollup-pluginutils%2Fdownload%2Frollup-pluginutils-2.8.1.tgz#8fa6dd0697344938ef26c2c09d2488ce9e33ce97"
41+
dependencies:
42+
estree-walker "^0.6.1"
43+
44+
rollup@^1.19.4:
45+
version "1.19.4"
46+
resolved "https://registry.npm.taobao.org/rollup/download/rollup-1.19.4.tgz#0cb4e4d6fa127adab59b11d0be50e8dd1c78123a"
47+
dependencies:
48+
"@types/estree" "0.0.39"
49+
"@types/node" "^12.6.9"
50+
acorn "^6.2.1"
51+
52+
tslib@^1.9.3:
53+
version "1.10.0"
54+
resolved "https://registry.npm.taobao.org/tslib/download/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
55+
56+
typescript@^3.5.3:
57+
version "3.5.3"
58+
resolved "https://registry.npm.taobao.org/typescript/download/typescript-3.5.3.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypescript%2Fdownload%2Ftypescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977"
59+
60+
vue-function-api@^2.2.0:
61+
version "2.2.0"
62+
resolved "https://registry.npm.taobao.org/vue-function-api/download/vue-function-api-2.2.0.tgz#06aeb76fcbcf27d8591a1f87cee8d3d771d7feed"
63+
dependencies:
64+
tslib "^1.9.3"
65+
66+
vue@^2.6.10:
67+
version "2.6.10"
68+
resolved "https://registry.npm.taobao.org/vue/download/vue-2.6.10.tgz#a72b1a42a4d82a721ea438d1b6bf55e66195c637"

0 commit comments

Comments
 (0)