Repository navigation
Expand file tree
/
Copy pathbuild.js
More file actions
159 lines (142 loc) · 3.91 KB
/
Copy pathbuild.js
File metadata and controls
159 lines (142 loc) · 3.91 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-console */
/* eslint-disable no-param-reassign */
// TODO
// Add cleanup function for when an errror is encountered or user terminates process
const { cwd } = require("process");
const tsup = require("tsup");
const { program } = require("commander");
const { execFileSync } = require("child_process");
const fs = require("fs");
const { npmDir, getBuildEntries } = require("./utils.js");
const path = require("path");
const { reactDir } = require("./utils.js");
const packageJsonDir = `${process.cwd()}/package.json`;
const commanderOpts = program
.option("-w --watch")
.option("-p, --prefix <string>")
.parse()
.opts();
const bundleDirectories = [npmDir];
async function nextTask(label, action) {
try {
console.log(label, "...");
await action();
} catch (err) {
console.error(err);
if (err.stdout) console.error(err.stdout);
if (err.stderr) console.error(err.stderr);
process.exit(1);
}
}
nextTask("Cleaning up previous build", () => {
bundleDirectories.map((dir) =>
fs.rmSync(dir, { force: true, recursive: true })
);
bundleDirectories.map((dir) =>
fs.mkdirSync(dir, {
recursive: true,
})
);
});
nextTask("verifying dependencies", async () => {
const packageJson = JSON.parse(fs.readFileSync(packageJsonDir, "utf-8"));
const dependencies = [
"@lit/react",
"react",
"commander",
"custom-element-jet-brains-integration",
"custom-element-vs-code-integration",
];
dependencies.forEach((dep) => {
if (!packageJson.devDependencies[dep] && !packageJson.dependencies[dep]) {
console.error(`${dep} is not installed, please install and try again`);
console.log(
`You should be able to do this by adding "${dep}":"*" to your devDependencies`
);
bundleDirectories.map((dir) =>
fs.rmSync(dir, { force: true, recursive: true })
);
process.exit(1);
}
});
});
nextTask("Generating CEM config", () => {
execFileSync(process.execPath, [path.join(__dirname, "make-CEMconfig.js")], {
stdio: "inherit",
});
});
nextTask("Generating component metadata", () => {
bundleDirectories.map((dir) =>
execFileSync(
process.execPath,
[path.join(__dirname, "make-metadata.js"), "--outdir", dir],
{
stdio: "inherit",
}
)
);
});
nextTask("Wrapping components for React", async () => {
execFileSync(
process.execPath,
[path.join(__dirname, "make-react.js"), "-p", `${commanderOpts.prefix}`],
{
stdio: "inherit",
}
);
});
nextTask("Running the TypeScript compiler", () => {
execFileSync(
"tsc",
["--project", "./tsconfig.prod.json", "--outdir", npmDir],
{
stdio: "inherit",
}
);
});
// TODO
// allow for custom tsup config (might not be necessary)
nextTask("Building source", async () => {
const sourceDir = path.join(cwd(), "src");
const config = {
format: "esm",
target: "es2017",
platform: "browser",
entry: getBuildEntries(sourceDir),
splitting: true,
treeshake: true,
bundle: true,
outDir: npmDir,
external: ["@lit/react", "react"],
watch: commanderOpts.watch,
};
tsup
.build({
...config,
esbuildOptions(buildOptions) {
buildOptions.chunkNames = "chunks/[name].[hash]";
buildOptions.platform = "browser";
},
})
.catch((err) => {
console.error(err);
nextTask("Cleaning up failed build", () => {
bundleDirectories.map((dir) =>
fs.rmSync(dir, { force: true, recursive: true })
);
});
})
.finally(() => {
// Skip cleanup if watch mode is enabled to preserve the react source for incremental builds
if (!commanderOpts.watch) {
nextTask("Cleaning up react source", () => {
fs.rmSync(reactDir, {
force: true,
recursive: true,
});
});
}
});
});
module.exports = {};