From 9cd49e725ae095dbb7a8934cd0131acb000c1ce6 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Sun, 18 Nov 2018 14:35:44 +0800 Subject: [PATCH] init --- LICENSE | 12 ++-- README.md | 5 +- generator.js | 90 ++++++++++++++++++++++++++ package.json | 18 ++++++ preset.json | 12 ++++ prompts.js | 43 ++++++++++++ template/common/postcss.config.js | 9 +++ template/common/public/index.html | 27 ++++++++ template/default/App.vue | 22 +++++++ template/default/main.js | 11 ++++ template/default/manifest.json | 55 ++++++++++++++++ template/default/pages.json | 16 +++++ template/default/pages/index/index.vue | 28 ++++++++ 13 files changed, 340 insertions(+), 8 deletions(-) mode change 100644 => 100755 LICENSE create mode 100644 generator.js create mode 100644 package.json create mode 100644 preset.json create mode 100644 prompts.js create mode 100644 template/common/postcss.config.js create mode 100644 template/common/public/index.html create mode 100644 template/default/App.vue create mode 100644 template/default/main.js create mode 100644 template/default/manifest.json create mode 100644 template/default/pages.json create mode 100644 template/default/pages/index/index.vue diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 index 77f865d..4e467aa --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ -MIT License +The MIT License (MIT) -Copyright (c) 2018 DCloud +Copyright (c) 2017-present, Yuxi (Evan) You Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md index 4bcbdb0..a9bc2e8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -# uni-preset-vue -uni-app preset for vue +# @uni-app/vue-cli-preset-uni-app + +> uni-app plugin for vue-cli \ No newline at end of file diff --git a/generator.js b/generator.js new file mode 100644 index 0000000..9d3cc60 --- /dev/null +++ b/generator.js @@ -0,0 +1,90 @@ +const fs = require('fs') +const path = require('path') + + +async function generate(dir, files, base = '') { + + const globby = require('globby') + + const _files = await globby(['**/*'], { + cwd: dir + }) + + for (const rawPath of _files) { + files[path.join(base, rawPath)] = fs.readFileSync(path.resolve(dir, rawPath), 'utf-8') + } +} + +module.exports = (api, options, rootOptions) => { + + api.render(async function(files) { + + api.extendPackage({ + dependencies: { + '@dcloudio/uni-h5': '^0.0.1' + }, + devDependencies: { + '@dcloudio/vue-cli-plugin-uni': '^0.0.1' + }, + babel: { + presets: [ + ['@vue/app', { + useBuiltIns: 'entry' + }] + ] + }, + browserslist: [ + 'last 3 versions', + 'Android >= 4.4', + 'ios >= 8' + ], + vue: { + baseUrl: '/', + assetsDir: 'static' + }, + vuePlugins: { + service: [path.resolve(__dirname, '../vue-cli-plugin-uni-app')] + } + }) + + Object.keys(files).forEach(name => { + delete files[name] + }) + + const template = options.repo || options.template + + const base = 'src' + + if (template === 'default') { + await generate(path.resolve(__dirname, './template/default'), files, base) + } else { + const ora = require('ora') + const home = require('user-home') + const download = require('download-git-repo') + + const spinner = ora('模板下载中...') + spinner.start() + + const tmp = path.join(home, '.uni-app/templates', template.replace(/[\/:]/g, '-'), 'src') + + if (fs.existsSync(tmp)) { + require('rimraf').sync.rm(tmp) + } + + await new Promise((resolve, reject) => { + download(template, tmp, err => { + spinner.stop() + if (err) { + return reject(err) + } + resolve() + }) + }) + + await generate(tmp, files, base) + } + + await generate(path.resolve(__dirname, './template/common'), files) + + }) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..cd5d9d2 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "uni-preset-vue", + "version": "0.0.1", + "description": "uni-app preset for vue-cli", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "fxy060608", + "license": "MIT", + "dependencies": { + "download-git-repo": "^1.1.0", + "globby": "^8.0.1", + "ora": "^3.0.0", + "rimraf": "^2.6.2", + "user-home": "^2.0.0" + } +} diff --git a/preset.json b/preset.json new file mode 100644 index 0000000..8e25c56 --- /dev/null +++ b/preset.json @@ -0,0 +1,12 @@ +{ + "useConfigFiles": false, + "plugins": { + "@vue/cli-plugin-babel": { + "presets": [ + ["@vue/app", { + "useBuiltIns": "entry" + }] + ] + } + } +} diff --git a/prompts.js b/prompts.js new file mode 100644 index 0000000..42e5d69 --- /dev/null +++ b/prompts.js @@ -0,0 +1,43 @@ +module.exports = [{ + type: 'list', + name: 'template', + message: '请选择 uni-app 模板', + choices: [{ + name: '默认模板', + value: 'default' + }, + { + name: 'Hello uni-app', + value: 'direct:https://www.inkercloud.com/masterkong/hello-uniapp.zip' + }, + { + name: '登录模板', + value: 'hello uni-app' + }, + { + name: '看图模板', + value: 'hello uni-app' + }, + { + name: '自定义模板', + value: 'custom' + } + ], + default: 'None', + }, + { + when: answers => answers.template === 'custom', + type: 'input', + name: 'repo', + message: '请输入自定义 uni-app 模板地址', + filter(input) { + return new Promise(function(resolve, reject) { + if (input) { + resolve(input) + } else { + reject('uni-app 模板地址不能为空') + } + }) + } + } +] diff --git a/template/common/postcss.config.js b/template/common/postcss.config.js new file mode 100644 index 0000000..26229ec --- /dev/null +++ b/template/common/postcss.config.js @@ -0,0 +1,9 @@ +const pkg = require('./package.json') +module.exports = { + plugins: [ + require("autoprefixer")({ + browsers: pkg.browserslist + }), + // require("./packages/@uni-app/postcss") + ] +} diff --git a/template/common/public/index.html b/template/common/public/index.html new file mode 100644 index 0000000..fc58adc --- /dev/null +++ b/template/common/public/index.html @@ -0,0 +1,27 @@ + + + + + + + + + <%= htmlWebpackPlugin.options.title %> + + + + + + + +
+ + + + diff --git a/template/default/App.vue b/template/default/App.vue new file mode 100644 index 0000000..56d2b92 --- /dev/null +++ b/template/default/App.vue @@ -0,0 +1,22 @@ + + + diff --git a/template/default/main.js b/template/default/main.js new file mode 100644 index 0000000..0656173 --- /dev/null +++ b/template/default/main.js @@ -0,0 +1,11 @@ +import Vue from 'vue' +import App from './App' + +Vue.config.productionTip = false + +App.mpType = 'app' + +const app = new Vue({ + ...App +}) +app.$mount() diff --git a/template/default/manifest.json b/template/default/manifest.json new file mode 100644 index 0000000..443cb85 --- /dev/null +++ b/template/default/manifest.json @@ -0,0 +1,55 @@ +{ + "name" : "test-uni-app", + "appid" : "__UNI__058126D", + "description": "", + "versionName": "1.0.0", + "versionCode": "100", + "transformPx":false, + "app-plus": { /* 5+App特有相关 */ + "modules": { /* 模块配置 */ + + }, + "distribute": { /* 应用发布信息 */ + "android": { /* android打包配置 */ + "permissions": ["", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" + ] + }, + "ios": { /* ios打包配置 */ + + }, + "sdkConfigs": { /* SDK配置 */ + + } + } + }, + "quickapp": { /* 快应用特有相关 */ + + }, + "mp-weixin": { /* 小程序特有相关 */ + "appid": "", + "setting" : { + "urlCheck" : true + } + } +} diff --git a/template/default/pages.json b/template/default/pages.json new file mode 100644 index 0000000..bf32f65 --- /dev/null +++ b/template/default/pages.json @@ -0,0 +1,16 @@ +{ + "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages + { + "path": "pages/index/index", + "style": { + "navigationBarTitleText": "uni-app" + } + } + ], + "globalStyle": { + "navigationBarTextStyle": "black", + "navigationBarTitleText": "uni-app", + "navigationBarBackgroundColor": "#F8F8F8", + "backgroundColor": "#F8F8F8" + } +} diff --git a/template/default/pages/index/index.vue b/template/default/pages/index/index.vue new file mode 100644 index 0000000..48e65bf --- /dev/null +++ b/template/default/pages/index/index.vue @@ -0,0 +1,28 @@ + + + + +