-
Notifications
You must be signed in to change notification settings - Fork 1
/
arc-plugin-marko.js
101 lines (84 loc) · 2.29 KB
/
arc-plugin-marko.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const path = require("path");
const fs = require("fs-extra");
const markoCompiler = require("marko/compiler");
const markoCompile = require("@marko/compile");
const markoPrebuild = require("@marko/prebuild");
const getDefaultBuildConfig = require("./getDefaultBuildConfig");
const ok = require("assert").ok;
async function prebuildPage(page, config, extend) {
if (extend) {
config = Object.assign(
{},
getDefaultBuildConfig(path.dirname(page)),
config
);
}
await fs.remove(config.cacheDir);
return markoPrebuild.run({
pages: [page],
config
});
}
function findPlugin(pluginName, plugins) {
for (const plugin of plugins) {
if (typeof plugin === "string") {
if (plugin === pluginName) return true;
} else if (typeof plugin === "object") {
if (plugin.plugin === pluginName) return true;
}
}
return false;
}
async function build(config) {
const pluginConfig = config.pluginConfig;
ok(
pluginConfig,
'"pages" and "config" are required properties to use arc-plugin-marko'
);
let { pages, config: buildConfig, bucket } = pluginConfig;
ok(pages, '"pages" is a required property to use arc-plugin-marko');
markoCompiler.registerTaglib(
require.resolve("@lasso/marko-taglib/marko.json")
);
const cwd = process.cwd();
let extendDefault = false;
if (buildConfig) {
buildConfig = require(require.resolve(path.resolve(cwd, buildConfig)));
} else {
buildConfig = {};
extendDefault = true;
}
if (!buildConfig.plugins) {
buildConfig.plugins = ["lasso-marko"];
} else {
if (!findPlugin("lasso-marko", buildConfig.plugins)) {
buildConfig.plugins.push("lasso-marko");
}
}
if (bucket && !findPlugin("lasso-s3-writer", buildConfig.plugins)) {
buildConfig.plugins.push({
plugin: "lasso-s3-writer",
config: {
bucket,
logger: console.log
}
});
}
const fileGlob = path.resolve(cwd, "./src/**/*.marko");
await markoCompile.run({
dir: cwd,
server: true,
files: fileGlob
});
if (Array.isArray(pages)) {
for (const page of pages) {
await prebuildPage(page, buildConfig, extendDefault);
}
} else {
await prebuildPage(pages, buildConfig, extendDefault);
}
}
module.exports = {
beforeCreate: build,
beforeDeploy: build
};