-
Notifications
You must be signed in to change notification settings - Fork 33
/
gulp-mars-base.js
85 lines (76 loc) · 2.33 KB
/
gulp-mars-base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* @file 共用编译配置
* @author meixuguang
*/
/* eslint-disable fecs-no-require */
/* eslint-disable no-native-reassign */
/* eslint-disable fecs-min-vars-per-destructure */
const through = require('through2');
const gutil = require('gulp-util');
const PluginError = gutil.PluginError;
const PLUGIN_NAME = 'gulp-mars';
const path = require('path');
const mkdirp = require('mkdirp');
const slash = require('slash');
const {parse: sfcParser} = require('./compiler/sfc/parser');
const {compile: sfcCompiler} = require('./compiler/sfc/compiler');
const log = require('./helper/log');
function compile(file, opt, compilers) {
const {
target = 'swan',
fileSuffix = {
html: 'swan',
js: 'js',
css: 'css',
json: 'json'
}
} = opt;
const rPath = path.relative(file.base, file.path);
const fPath = slash(path.resolve(file.cwd, opt.dest, rPath).replace(/\.vue$/, ''));
const baseName = path.basename(fPath);
let coreRelativePath = path.join(
path.relative(
path.parse(fPath).dir,
path.resolve(file.cwd, opt.dest)
) || '.',
opt.coreDir || 'common'
);
coreRelativePath = coreRelativePath[0] === '.' ? coreRelativePath : './' + coreRelativePath;
coreRelativePath = slash(coreRelativePath + '/index');
try {
const dirPath = fPath.replace(/[^/]+$/, '');
mkdirp.sync(dirPath);
}
catch (e) {}
const isApp = path.basename(fPath) === 'app';
const options = Object.assign({
isApp,
coreRelativePath,
compilers,
fileSuffix,
fPath,
baseName
}, opt);
const sfcFile = sfcParser(file, options);
return sfcCompiler(sfcFile, options);
}
function gulpPlugin(opt = {dest: './dist'}, compilers) {
const stream = through.obj(function (file, enc, cb) {
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return cb();
}
if (file.isBuffer()) {
compile(file, opt, compilers)
.then(_ => cb(null, file))
.catch(err => {
log.error('[COMPILE ERROR]:', err);
cb(null, file);
});
}
});
return stream;
}
module.exports = {
gulpPlugin
};