Skip to content

Commit 47fbb39

Browse files
committed
fix(build): fix process binary files
1 parent 4300147 commit 47fbb39

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

packages/mars-build/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"gulp": "^4.0.2",
2020
"gulp-changed": "^3.2.0",
2121
"gulp-intercept": "^0.1.0",
22+
"gulp-is-binary": "^0.1.2",
2223
"gulp-util": "^3.0.8",
2324
"less": "^3.9.0",
2425
"lodash.merge": "^4.6.1",

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,26 @@ function getExtProcessors(processors, lang) {
4646
return ret;
4747
}
4848

49+
function defaultCompile(bufferOrString) {
50+
return { code: bufferOrString };
51+
}
52+
53+
function getFileSource(file) {
54+
return file.isBinary && file.isBinary()
55+
? file.contents
56+
: file.contents
57+
? file.contents.toString()
58+
: '';
59+
}
60+
4961
function getFileCompiler(compile, config) {
5062
const {preprocessors = {}, postprocessors = {}} = config;
63+
compile = compile || defaultCompile;
5164

5265
return async function fileCompiler(file, options) {
5366
const fileOptions = file.$options;
5467
const lang = file.lang || file.type;
55-
let source = (file.contents && file.contents.toString()) || '';
68+
let source = getFileSource(file);
5669
// preprocessors
5770
source = await process(source, getExtProcessors(preprocessors, lang), file);
5871
// compile
@@ -63,7 +76,10 @@ function getFileCompiler(compile, config) {
6376
let {code, ...rest} = result;
6477
code = await process(code, getExtProcessors(postprocessors, lang), file);
6578
// overwrite file contents
66-
file.contents = Buffer.from(code || '');
79+
file.contents = typeof code === 'string'
80+
? Buffer.from(code || '')
81+
: code;
82+
6783
return rest;
6884
};
6985
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ async function compileJS(content, options) {
7676
return res;
7777
}
7878

79-
function compileDefault(code, options) {
80-
return {
81-
code
82-
};
83-
}
84-
8579
async function compile(file, options) {
8680
const {fileSuffix, target} = options;
8781
const buildConfig = options._config || {};
@@ -104,7 +98,7 @@ async function compile(file, options) {
10498
}
10599
else {
106100
// for other files, use default compiler
107-
const compiler = getFileCompiler(compileDefault, buildConfig);
101+
const compiler = getFileCompiler(null, buildConfig);
108102
await compiler(file, options);
109103
}
110104

packages/mars-build/src/scripts/gulpTasks.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ function getTaskSFC(config, options) {
9696
* @return {Function}
9797
*/
9898
function getTaskCompileAssets(config, options) {
99+
const isBinary = require('gulp-is-binary');
99100
const {source} = config;
100101
const {target} = options;
101102
const dest = config.dest.path;
@@ -118,6 +119,7 @@ function getTaskCompileAssets(config, options) {
118119
file.isBuffer() && logger('[compile:assets]:', getPathToCWD(file.path));
119120
return file;
120121
}))
122+
.pipe(isBinary())
121123
.pipe(compileFile(options))
122124
.pipe(gulp.dest(dest));
123125
};

0 commit comments

Comments
 (0)