1 | const gulp = require("gulp");
|
2 | const path = require("path");
|
3 | const fs = require("fs-extra");
|
4 | const { isDir } = require("./utils");
|
5 | const babelLoader = require("./loaders/babel");
|
6 | const eslintLoader = require("./loaders/eslint");
|
7 | const cssLoader = require("./loaders/css");
|
8 | const rawLoader = require("./loaders/raw");
|
9 |
|
10 | module.exports = async function compile(file, options) {
|
11 | const ignore =
|
12 | options.ignore ||
|
13 | function () {
|
14 | return false;
|
15 | };
|
16 | const absPath = path.resolve(options.appSrc, file);
|
17 | const outFile = path.resolve(options.outDir, file);
|
18 | let outDir = options.outDir;
|
19 |
|
20 | if (isDir(absPath)) {
|
21 | await fs.ensureDir(outFile);
|
22 | return Promise.resolve(file);
|
23 | }
|
24 |
|
25 | outDir = path.dirname(outFile);
|
26 |
|
27 | const meta = {
|
28 | outDir,
|
29 | outFile,
|
30 | filePath: file,
|
31 | };
|
32 |
|
33 | return new Promise(resolve => {
|
34 | let stream = gulp.src(absPath, {
|
35 | cwd: options.appSrc,
|
36 | allowEmpty: true,
|
37 | });
|
38 |
|
39 | if (!ignore(file)) {
|
40 | stream = eslintLoader(stream, options, meta);
|
41 | stream = cssLoader(stream, options, meta);
|
42 | stream = rawLoader(stream, options, meta);
|
43 | stream = babelLoader(stream, options, meta);
|
44 | if (options.transform) {
|
45 | stream = transform(stream, options, meta);
|
46 | }
|
47 | }
|
48 | stream = stream.pipe(gulp.dest(outDir));
|
49 |
|
50 | stream.on("finish", () => {
|
51 | resolve(file);
|
52 | });
|
53 | stream.on("error", () => {
|
54 | resolve(file);
|
55 | });
|
56 | });
|
57 | };
|