-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjitEnvSnowpackPlugin.js
98 lines (84 loc) · 2.77 KB
/
jitEnvSnowpackPlugin.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
const JitEnv = require('./JitEnv');
class JitEnvSnowpackPlugin {
name = "@weavedev/jit-env-snowpack-plugin";
JESP_isDev = undefined;
JESP_jitEnvInstance = undefined;
JESP_htmlFiles = [];
JESP_options = undefined;
constructor(options = {}) {
this.JESP_options = options;
}
/**
* Hook for snowpack to call on file changes
*/
transform({ contents, fileExt, srcPath, isDev }) {
// Filter file types
switch(fileExt) {
case '.html':
// Register HTML files to receive updates on env changes
this.JESP_registerHtml(srcPath);
// Get jitEnv instance
const jitEnv = this.JESP_jitEnv(isDev);
return { contents: jitEnv.transform(contents) };
default:
return;
}
}
/**
* Hook to be injected by snowpack to mark file changes
* @param {string} changed - Path to file changed
*/
markChanged() {
throw new Error(`Unexpected missing snowpack hook: markChanged`);
}
/**
* Get JitEnv instance
* @returns {JitEnv}
*/
JESP_jitEnv(isDev) {
// Set mode and ensure mode doesn't change
if (this.JESP_isDev !== isDev) {
if (this.JESP_isDev === undefined) {
this.JESP_isDev = isDev;
} else {
throw new Error(`Unexpected dev-mode change. Was ${this.JESP_isDev} but is now ${isDev}`);
}
}
// Create JitEnv instance if none exists
if (this.JESP_jitEnvInstance === undefined) {
this.JESP_jitEnvInstance = new JitEnv(
// Pass configuration
isDev ? this.JESP_options.dev : this.JESP_options.build,
// Pass update hook
this.JESP_requestUpdate,
);
}
// Return JitEnv
return this.JESP_jitEnvInstance;
}
/**
* Registers a HTML file to receive updates on changes in ENV data
* @param {string} path
*/
JESP_registerHtml(path) {
if (!this.JESP_htmlFiles.includes(path)) {
this.JESP_htmlFiles.push(path);
}
}
/**
* Hook for JitEnv to queue all HTML files for an update after a change in ENV data
*/
JESP_requestUpdate = () => {
this.JESP_htmlFiles.forEach((path) => {
this.markChanged(path);
});
// HACK: Watch generated type file as a fallback for snowpack's flaky on-html-change reload feature
if (this.JESP_options.dev && this.JESP_options.dev.emitTypes) {
this.markChanged(this.JESP_options.dev.emitTypes);
}
}
}
/**
* Snowpack plugin definition
*/
module.exports = (_, options) => new JitEnvSnowpackPlugin(options);