Skip to content

Commit 335f2f3

Browse files
committed
:enhance: 增加虚拟文件处理.
1 parent 126be1b commit 335f2f3

File tree

6 files changed

+9517
-68
lines changed

6 files changed

+9517
-68
lines changed

micro-app.config.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ module.exports = {
99
main: [ './test/main.js' ],
1010
},
1111

12-
// staticPath: '',
13-
14-
// dlls: [
15-
// {
16-
// context: __dirname,
17-
// },
18-
// ],
19-
2012
alias: { // 前端
2113
api: 'abc',
2214
config: {
@@ -39,9 +31,9 @@ module.exports = {
3931
},
4032
},
4133

42-
plugins: [
43-
'@micro-app/plugin-webpack-adapter',
44-
// '@micro-app/plugin-vue-cli',
45-
],
34+
// plugins: [
35+
// '@micro-app/plugin-webpack-adapter',
36+
// '@micro-app/plugin-vue-cli',
37+
// ],
4638

4739
};

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
},
3434
"license": "MIT",
3535
"peerDependencies": {
36-
"@micro-app/cli": ">=0.1.6",
37-
"@micro-app/core": ">=0.1.7",
38-
"@micro-app/plugin-webpack-adapter": ">=0.0.5",
36+
"@micro-app/cli": ">=0.2.0",
37+
"@micro-app/core": ">=0.2.0",
38+
"@micro-app/plugin-webpack-adapter": ">=0.1.0",
3939
"vue-template-compiler": "^2.0.0"
4040
},
4141
"devDependencies": {
42-
"@micro-app/cli": "^0.1.6",
43-
"@micro-app/core": "^0.1.7",
44-
"@micro-app/plugin-webpack-adapter": "^0.0.5",
42+
"@micro-app/cli": "^0.2.0-beta.2",
43+
"@micro-app/core": "^0.2.0-beta.2",
44+
"@micro-app/plugin-webpack-adapter": "^0.1.0-beta.2",
4545
"@types/jest": "^24.0.18",
4646
"babel-eslint": "^10.0.3",
4747
"coveralls": "^3.0.6",

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const path = require('path');
44

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

7-
// api.assertVersion('>=0.1.4');
7+
api.assertVersion('>=0.2.0');
88

99
// commands
1010
require('./commands/version')(api);

src/methods.js

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
22

33
const assert = require('assert');
4-
const fs = require('fs');
54

65
module.exports = function Methods(api, projectOptions) {
76

7+
const fixedPluginFile = require('./utils/fixedPluginFile.js');
8+
89
// 针对 vue plugin 的注册
910
api.extendMethod('registerVuePlugin', {
1011
description: '针对 vue-cli plugin 的注册方法.',
@@ -86,51 +87,3 @@ module.exports = function Methods(api, projectOptions) {
8687

8788
};
8889

89-
// 重新适配文件
90-
function fixedPluginFile(id, link) {
91-
const newLink = link.replace(/\.js$/ig, '-for-micro-app.js');
92-
const jsText = fs.readFileSync(require.resolve(link), 'utf8');
93-
let newJsText = jsText
94-
.replace(/registerCommand/gm, 'registerVueCommand')
95-
.replace(new RegExp('vue-cli-service (\\w+)', 'gm'), 'micro-app-vue $1')
96-
.replace(new RegExp('api.service.context', 'gm'), 'api.getCwd()')
97-
.replace('const webpackConfig = api.resolveWebpackConfig()', [
98-
'const _webpackConfig = api.resolveWebpackConfig()',
99-
`const { webpackConfig } = api.applyPluginHooks('modifyWebpackConfig', {
100-
args: args,
101-
webpackConfig: _webpackConfig,
102-
});
103-
104-
if (!webpackConfig) {
105-
api.logger.error('[Plugin] modifyWebpackConfig must return { webpackConfig }');
106-
return process.exit(1);
107-
}`,
108-
].join('\n'))
109-
;
110-
111-
if (id === 'vue-service:plugins-command-serve') {
112-
// 注入事件
113-
newJsText = newJsText
114-
.replace('api.service.devServerConfigFns.forEach(fn => fn(app, server))', [
115-
'const config = api.serverConfig;',
116-
"api.applyPluginHooks('onServerInit', { app, server, options, config });",
117-
"api.applyPluginHooks('beforeServerEntry', { app, server, options, config });",
118-
].join('\n\n'))
119-
.replace('before (app, server) {', [
120-
`after (app, server) {
121-
const config = api.serverConfig;
122-
api.applyPluginHooks('afterServerEntry', { app, server, options, config });
123-
api.applyPluginHooks('onServerInitDone', { app, server, options, config });
124-
projectDevServerOptions.after && projectDevServerOptions.after(app, server);
125-
},`,
126-
'before (app, server) {',
127-
].join('\n'));
128-
}
129-
130-
131-
// TODO change module.exports.defeaultConfig
132-
133-
fs.writeFileSync(newLink, newJsText, 'utf8');
134-
135-
return newLink;
136-
}

src/utils/fixedPluginFile.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
const virtualFile = require('@micro-app/core').virtualFile;
4+
5+
// 重新适配文件
6+
module.exports = function fixedPluginFile(id, link) {
7+
virtualFile.register(link, content => {
8+
9+
let newJsText = content
10+
.replace(/registerCommand/gm, 'registerVueCommand')
11+
.replace(new RegExp('vue-cli-service (\\w+)', 'gm'), 'micro-app-vue $1')
12+
.replace(new RegExp('api.service.context', 'gm'), 'api.getCwd()')
13+
.replace('const webpackConfig = api.resolveWebpackConfig()', [
14+
'const _webpackConfig = api.resolveWebpackConfig()',
15+
`const { webpackConfig } = api.applyPluginHooks('modifyWebpackConfig', {
16+
args: args,
17+
webpackConfig: _webpackConfig,
18+
});
19+
20+
if (!webpackConfig) {
21+
api.logger.error('[Plugin] modifyWebpackConfig must return { webpackConfig }');
22+
return process.exit(1);
23+
}`,
24+
].join('\n'))
25+
;
26+
27+
if (id === 'vue-service:plugins-command-serve') {
28+
// 注入事件
29+
newJsText = newJsText
30+
.replace('api.service.devServerConfigFns.forEach(fn => fn(app, server))', [
31+
'const config = api.serverConfig;',
32+
"api.applyPluginHooks('onServerInit', { app, server, options, config });",
33+
"api.applyPluginHooks('beforeServerEntry', { app, server, options, config });",
34+
].join('\n\n'))
35+
.replace('before (app, server) {', [
36+
`after (app, server) {
37+
const config = api.serverConfig;
38+
api.applyPluginHooks('afterServerEntry', { app, server, options, config });
39+
api.applyPluginHooks('onServerInitWillDone', { app, server, options, config });
40+
api.applyPluginHooks('onServerInitDone', { app, server, options, config });
41+
projectDevServerOptions.after && projectDevServerOptions.after(app, server);
42+
},`,
43+
'before (app, server) {',
44+
].join('\n'));
45+
}
46+
47+
// TODO change module.exports.defeaultConfig
48+
49+
50+
return newJsText;
51+
});
52+
53+
return link;
54+
};

0 commit comments

Comments
 (0)