Skip to content

Commit da24cef

Browse files
committed
feat(build): support appConfig and processor for json config
1 parent fd5a58e commit da24cef

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

packages/mars-build/src/compiler/file/base.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
const buildInProcessors = require('./processor');
1010

11-
async function process(source, processors) {
11+
async function process(source, processors, file) {
1212
for (const processor of processors) {
1313
const {name, options = {}, process} = processor;
1414
if (typeof process === 'function') {
15-
source = await process(source, options);
15+
source = await process(source, options, file);
1616
}
1717
else if (buildInProcessors[name]) {
1818
source = await buildInProcessors[name](source, options);
@@ -54,14 +54,14 @@ function getFileCompiler(compile, config) {
5454
const lang = file.lang || file.type;
5555
let source = (file.contents && file.contents.toString()) || '';
5656
// preprocessors
57-
source = await process(source, getExtProcessors(preprocessors, lang));
57+
source = await process(source, getExtProcessors(preprocessors, lang), file);
5858
// compile
5959
options.path = file.path;
6060
options.file = file;
6161
const result = await compile(source, options, fileOptions);
6262
// postprocessors
6363
let {code, ...rest} = result;
64-
code = await process(code, getExtProcessors(postprocessors, lang));
64+
code = await process(code, getExtProcessors(postprocessors, lang), file);
6565
// overwrite file contents
6666
file.contents = Buffer.from(code || '');
6767
return rest;

packages/mars-build/src/compiler/sfc/compiler.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exports.compile = async function compile(file, options) {
1010
const {template, script, styles, config: configFile} = file;
1111
const blockConfig = configFile.$options.config;
1212
const mpConfig = blockConfig && blockConfig.config;
13+
const marsConfig = options._config;
1314
// const isComponent = mpConfig && mpConfig.component === true;
1415

1516
const {compilers, isApp, fPath, target, coreRelativePath, baseName} = options;
@@ -26,11 +27,16 @@ exports.compile = async function compile(file, options) {
2627
coreRelativePath,
2728
target,
2829
renderStr: !isApp ? renderFunctionName : null,
29-
dest: options._config.dest
30+
dest: marsConfig.dest
3031
});
3132

3233
// use configFile.$options.config first
3334
config = mpConfig ? mpConfig : config;
35+
// prefer appConfig in marsConfig
36+
if (isApp) {
37+
const appConfig = marsConfig.appConfig && marsConfig.appConfig.config;
38+
config = appConfig || config;
39+
}
3440

3541
// app.vue has no template
3642
if (!isApp) {

packages/mars-build/src/gulp-mars-h5.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ async function compile(file, opt) {
112112
fileSuffix,
113113
fPath
114114
}, opt);
115+
const marsConfig = options._config;
115116

116117
const {
117118
templateCompiler,
@@ -141,11 +142,17 @@ async function compile(file, opt) {
141142
mpConfig,
142143
target: options.target,
143144
path: fPath + '.vue',
144-
dest: options._config.dest,
145-
mars: options._config.h5 || {}
145+
dest: marsConfig.dest,
146+
mars: marsConfig.h5 || {}
146147
});
147148
// use blockConfig first
148149
config = mpConfig ? mpConfig : (scriptRet && scriptRet.config);
150+
// prefer appConfig in marsConfig
151+
if (isApp) {
152+
const appConfig = marsConfig.appConfig && marsConfig.appConfig.config;
153+
config = appConfig || config;
154+
}
155+
149156
// if (config && config.pages) {
150157
// config.pages = config.pages.filter(item => !/\.(swan|mp)$/.test(item));
151158
// }
@@ -226,7 +233,7 @@ async function compile(file, opt) {
226233
// let routerContent = fs.readFileSync(__dirname + '/h5/template/router.js');
227234
// routerContent = compileRouter(routerContent, {
228235
// config,
229-
// mars: options._config.h5 || null
236+
// mars: marsConfig.h5 || null
230237
// });
231238
// fs.writeFileSync(options.dest + '/router.js', routerContent);
232239

@@ -346,7 +353,7 @@ async function compile(file, opt) {
346353
// content = compileMain(content, {
347354
// mainOptions,
348355
// componentSet,
349-
// mars: options._config.h5 || {}
356+
// mars: marsConfig.h5 || {}
350357
// });
351358
// fs.writeFileSync(options.dest + '/main.js', content);
352359

@@ -387,6 +394,9 @@ ${styles.contents.toString() || ''}
387394
});
388395
templateFile.writeFileSync();
389396

397+
// just compile configFile
398+
// to involve processors
399+
await configCompiler(configFile, {components: scriptRet.components, config});
390400
return file;
391401
}
392402

0 commit comments

Comments
 (0)