Skip to content

Commit 44b6540

Browse files
committed
:enhance: 增强功能, 增加插件适配等. 增加命令 micro-app-vue.
1 parent 4e4ae47 commit 44b6540

File tree

14 files changed

+554
-5718
lines changed

14 files changed

+554
-5718
lines changed

bin/micro-app-vue.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
const { cmd, argv, service } = require('@micro-app/cli/bin/base');
5+
6+
const ncmd = cmd && `vue-service-${cmd}` || cmd;
7+
8+
service.run(ncmd, argv);

micro-app.config.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ module.exports = {
44
name: '@micro-app/demo',
55
description: '',
66
version: '0.0.1',
7-
type: '', // types 类型
8-
webpack: { // webpack 配置 (只有自己使用)
9-
// output: {
10-
// path: path.resolve(__dirname, 'public'),
11-
// publicPath: '/public/',
12-
// },
7+
8+
entry: {
9+
main: [ './test/main.js' ],
1310
},
1411

1512
// staticPath: '',
@@ -37,29 +34,14 @@ module.exports = {
3734
server: {
3835
entry: '', // 服务端入口
3936
port: 8088, // 服务端口号
40-
contentBase: 'public', // 静态文件地址
4137
options: {
4238
// 服务端回调参数
4339
},
4440
},
4541

4642
plugins: [
47-
[{
48-
id: 'test',
49-
description: '这是test',
50-
link: __dirname + '/test/testPlugin',
51-
}, {
52-
a: 1,
53-
}],
43+
'@micro-app/plugin-webpack-adapter',
44+
// '@micro-app/plugin-vue-cli',
5445
],
5546

56-
// deploy: {
57-
// git: 'git+ssh://[email protected]',
58-
// branch: 'test',
59-
// // branch: {
60-
// // name: 'develop',
61-
// // extends: true,
62-
// // },
63-
// message: '', // 提交 message 中增加内容
64-
// },
6547
};

package.json

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
{
22
"name": "@micro-app/plugin-vue-cli",
3-
"version": "0.0.2",
3+
"version": "0.0.4",
44
"description": "[Plugin] adapter vue cli plugin.",
55
"main": "src/index.js",
6+
"bin": {
7+
"micro-app-vue": "./bin/micro-app-vue.js"
8+
},
69
"scripts": {
710
"test": "jest"
811
},
912
"files": [
10-
"src"
13+
"src",
14+
"bin"
1115
],
1216
"homepage": "https://github.com/MicrosApp/MicroApp-Plugin-Vue-CLI",
1317
"repository": {
@@ -28,15 +32,25 @@
2832
"email": "[email protected]"
2933
},
3034
"license": "MIT",
35+
"peerDependencies": {
36+
"@micro-app/cli": ">=0.1.4",
37+
"@micro-app/core": ">=0.1.4",
38+
"@micro-app/plugin-webpack-adapter": ">=0.0.4",
39+
"vue-template-compiler": "^2.0.0"
40+
},
3141
"devDependencies": {
32-
"@micro-app/cli": "0.1.1-rc.3",
33-
"@micro-app/core": "0.1.1",
42+
"@micro-app/cli": "^0.1.4-beta.1",
43+
"@micro-app/core": "^0.1.4-beta.1",
44+
"@micro-app/plugin-webpack-adapter": "^0.0.3",
3445
"@types/jest": "^24.0.18",
35-
"babel-eslint": "^10.0.2",
46+
"babel-eslint": "^10.0.3",
3647
"coveralls": "^3.0.6",
3748
"eslint": "^5.16.0",
3849
"eslint-config-2o3t": "^1.1.17",
39-
"jest": "^24.9.0"
50+
"jest": "^24.9.0",
51+
"vue-template-compiler": "^2.0.0"
4052
},
41-
"dependencies": {}
53+
"dependencies": {
54+
"@vue/cli-service": "^3.11.0"
55+
}
4256
}

src/buildInPlugins.js

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const fs = require('fs');
5+
const _ = require('lodash');
6+
const chalk = require('chalk');
7+
8+
const { defaults, validate } = require('@vue/cli-service/lib/options');
9+
10+
module.exports = function(api, opts = {}) {
11+
12+
const logger = api.logger;
13+
14+
// load user config
15+
const userOptions = loadUserOptions(api, opts);
16+
const defaultsDeep = require('lodash.defaultsdeep');
17+
const projectOptions = defaultsDeep(userOptions, defaults());
18+
19+
logger.debug('[vue:project-config]', projectOptions);
20+
21+
/**
22+
* Generate a cache identifier from a number of variables
23+
*/
24+
api.extendMethod('genCacheConfig', (id, partialIdentifier, configFiles = []) => {
25+
const fs = require('fs');
26+
const cacheDirectory = api.resolve(`node_modules/.cache/${id}`);
27+
28+
// replace \r\n to \n generate consistent hash
29+
const fmtFunc = conf => {
30+
if (typeof conf === 'function') {
31+
return conf.toString().replace(/\r\n?/g, '\n');
32+
}
33+
return conf;
34+
};
35+
36+
const variables = {
37+
partialIdentifier,
38+
'cli-service': require('../package.json').version,
39+
'cache-loader': require('cache-loader/package.json').version,
40+
env: process.env.NODE_ENV,
41+
test: !!process.env.VUE_CLI_TEST,
42+
config: [
43+
fmtFunc(projectOptions.chainWebpack),
44+
fmtFunc(projectOptions.configureWebpack),
45+
],
46+
};
47+
48+
if (!Array.isArray(configFiles)) {
49+
configFiles = [ configFiles ];
50+
}
51+
configFiles = configFiles.concat([
52+
'package-lock.json',
53+
'yarn.lock',
54+
'pnpm-lock.yaml',
55+
]);
56+
57+
const readConfig = file => {
58+
const absolutePath = api.resolve(file);
59+
if (!fs.existsSync(absolutePath)) {
60+
return;
61+
}
62+
63+
if (absolutePath.endsWith('.js')) {
64+
// should evaluate config scripts to reflect environment variable changes
65+
try {
66+
return JSON.stringify(require(absolutePath));
67+
} catch (e) {
68+
return fs.readFileSync(absolutePath, 'utf-8');
69+
}
70+
} else {
71+
return fs.readFileSync(absolutePath, 'utf-8');
72+
}
73+
};
74+
75+
for (const file of configFiles) {
76+
const content = readConfig(file);
77+
if (content) {
78+
variables.configFiles = content.replace(/\r\n?/g, '\n');
79+
break;
80+
}
81+
}
82+
83+
const hash = require('hash-sum');
84+
const cacheIdentifier = hash(variables);
85+
return { cacheDirectory, cacheIdentifier };
86+
}, {
87+
description: 'Generate a cache identifier from a number of variables',
88+
});
89+
90+
require('./methods.js')(api, projectOptions);
91+
92+
const vueRoot = path.dirname(require.resolve('@vue/cli-service'));
93+
94+
require('./plugins/commandPlugins.js')(api, vueRoot, projectOptions);
95+
require('./plugins/configPlugins.js')(api, vueRoot, projectOptions);
96+
require('./plugins/projectPlugins.js')(api, vueRoot, projectOptions);
97+
};
98+
99+
function loadUserOptions(api, opts) {
100+
const logger = api.logger;
101+
102+
// vue.config.js
103+
let fileConfig,
104+
pkgConfig,
105+
resolved,
106+
resolvedFrom;
107+
108+
const configPath = (
109+
process.env.VUE_CLI_SERVICE_CONFIG_PATH || path.resolve(api.getCwd(), 'vue.config.js')
110+
);
111+
if (fs.existsSync(configPath)) {
112+
try {
113+
fileConfig = require(configPath);
114+
115+
if (typeof fileConfig === 'function') {
116+
fileConfig = fileConfig();
117+
}
118+
119+
if (!fileConfig || typeof fileConfig !== 'object') {
120+
logger.error(
121+
`Error loading ${chalk.bold('vue.config.js')}: should export an object or a function that returns object.`
122+
);
123+
fileConfig = null;
124+
}
125+
} catch (e) {
126+
logger.error(`Error loading ${chalk.bold('vue.config.js')}:`);
127+
throw e;
128+
}
129+
}
130+
131+
// package.vue
132+
pkgConfig = api.pkg && api.pkg.vue;
133+
if (pkgConfig && typeof pkgConfig !== 'object') {
134+
logger.error(
135+
`Error loading vue-cli config in ${chalk.bold('package.json')}: ` +
136+
'the "vue" field should be an object.'
137+
);
138+
pkgConfig = null;
139+
}
140+
141+
if (fileConfig) {
142+
if (pkgConfig) {
143+
logger.warn(
144+
'"vue" field in package.json ignored ' +
145+
`due to presence of ${chalk.bold('vue.config.js')}.`
146+
);
147+
logger.warn(
148+
`You should migrate it into ${chalk.bold('vue.config.js')} ` +
149+
'and remove it from package.json.'
150+
);
151+
}
152+
resolved = fileConfig;
153+
resolvedFrom = 'vue.config.js';
154+
} else if (pkgConfig) {
155+
resolved = pkgConfig;
156+
resolvedFrom = '"vue" field in package.json';
157+
} else {
158+
resolved = opts.inlineOptions || {};
159+
resolvedFrom = 'inline options';
160+
}
161+
162+
if (resolved.css && typeof resolved.css.modules !== 'undefined') {
163+
if (typeof resolved.css.requireModuleExtension !== 'undefined') {
164+
logger.warn(
165+
`You have set both "css.modules" and "css.requireModuleExtension" in ${chalk.bold('vue.config.js')}, ` +
166+
'"css.modules" will be ignored in favor of "css.requireModuleExtension".'
167+
);
168+
} else {
169+
logger.warn(
170+
`"css.modules" option in ${chalk.bold('vue.config.js')} ` +
171+
'is deprecated now, please use "css.requireModuleExtension" instead.'
172+
);
173+
resolved.css.requireModuleExtension = !resolved.css.modules;
174+
}
175+
}
176+
177+
// normalize some options
178+
ensureSlash(resolved, 'publicPath');
179+
if (typeof resolved.publicPath === 'string') {
180+
resolved.publicPath = resolved.publicPath.replace(/^\.\//, '');
181+
}
182+
removeSlash(resolved, 'outputDir');
183+
184+
// validate options
185+
validate(resolved, msg => {
186+
logger.error(
187+
`Invalid options in ${chalk.bold(resolvedFrom)}: ${msg}`
188+
);
189+
});
190+
191+
return resolved;
192+
}
193+
194+
function ensureSlash(config, key) {
195+
let val = config[key];
196+
if (typeof val === 'string') {
197+
if (!/^https?:/.test(val)) {
198+
val = val.replace(/^([^/.])/, '/$1');
199+
}
200+
config[key] = val.replace(/([^/])$/, '$1/');
201+
}
202+
}
203+
204+
function removeSlash(config, key) {
205+
if (typeof config[key] === 'string') {
206+
config[key] = config[key].replace(/\/$/g, '');
207+
}
208+
}

src/index.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ const path = require('path');
44

55
module.exports = function VueCLIAdapter(api, opts = {}) {
66

7+
// api.assertVersion('>=0.1.4');
8+
79
// commands
810
require('./commands/version')(api);
911

1012
// Current working directory.
1113
api.extendMethod('getCwd', () => {
1214
return opts.root || api.root;
15+
}, {
16+
description: 'Current working directory.',
1317
});
1418

1519
/**
@@ -21,18 +25,8 @@ module.exports = function VueCLIAdapter(api, opts = {}) {
2125
api.extendMethod('resolve', _path => {
2226
const context = api.getCwd();
2327
return path.resolve(context, _path);
24-
});
25-
26-
api.extendMethod('assertVersion', range => {
27-
api.logger.warn(`assertVersion(${range}); 方法未真实实现 !`);
28-
return true;
29-
});
30-
31-
api.extendMethod('genCacheConfig', () => {
32-
api.logger.warn('genCacheConfig(); 方法未真实实现 !');
33-
const cacheDirectory = '';
34-
const cacheIdentifier = '';
35-
return { cacheDirectory, cacheIdentifier };
28+
}, {
29+
description: 'Resolve path for a project.',
3630
});
3731

3832
api.registerMethod('chainWebpack', {
@@ -48,11 +42,19 @@ module.exports = function VueCLIAdapter(api, opts = {}) {
4842
description: '适配 vue-cli 中 configureDevServer 事件',
4943
});
5044

51-
api.onChainWebpcakConfig(config => {
45+
api.modifyChainWebpcakConfig(config => {
5246
api.applyPluginHooks('chainWebpack', config);
47+
return config;
5348
});
5449

5550
api.modifyWebpcakConfig(config => {
5651
return api.applyPluginHooks('configureWebpack', config);
5752
});
53+
54+
const buildInPlugins = require('./buildInPlugins.js');
55+
buildInPlugins(api, opts);
56+
};
57+
58+
module.exports.configuration = {
59+
description: '针对 Vue Cli 适配器',
5860
};

0 commit comments

Comments
 (0)