-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
208 lines (189 loc) · 6.84 KB
/
index.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const fs = require('fs-extra');
const internalLib = require('./lib/internals.js');
module.exports = {
before: '@apostrophecms/asset',
i18n: {
aposVite: {}
},
async init(self) {
self.buildSourceFolderName = 'src';
self.distFolderName = 'dist';
self.buildRoot = null;
self.buildRootSource = null;
self.distRoot = null;
self.buildManifestPath = null;
// Cached metadata for the current run
self.currentSourceMeta = null;
self.entrypointsManifest = [];
// Populated after a build has been triggered
self.buildOptions = {};
self.viteDevInstance = null;
self.shouldCreateDevServer = false;
// Populated when a watch is triggered
// UI folder -> index
self.currentSourceUiIndex = {};
// /absolute/path -> index
self.currentSourceFsIndex = {};
// ui/relative/path/file -> [ index1, index2 ]
self.currentSourceRelIndex = new Map();
// IMPORTANT: This should not be removed.
// Vite depends on both process.env.NODE_ENV and the `mode` config option.
// They should be in sync and ALWAYS set. We need to patch the environment
// and ensure it's set here.
// Read more at https://vite.dev/guide/env-and-mode.html#node-env-and-modes
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development';
}
},
handlers(self) {
return {
'@apostrophecms/asset:afterInit': {
async registerExternalBuild() {
self.apos.asset.configureBuildModule(self, {
alias: 'vite',
devServer: true,
hmr: true
});
await self.initWhenReady();
}
},
'@apostrophecms/express:afterListen': {
async prepareViteDevServer() {
if (self.shouldCreateDevServer) {
await self.createViteInstance(self.buildOptions);
}
}
},
'apostrophe:destroy': {
async destroyBuildWatcher() {
if (self.viteDevInstance) {
await self.viteDevInstance.close();
self.viteDevInstance = null;
}
}
}
};
},
middleware(self) {
if (process.env.NODE_ENV === 'production') {
return {};
}
return {
viteDevServer: {
before: '@apostrophecms/express',
url: '/__vite',
middleware: async (req, res, next) => {
if (!self.shouldCreateDevServer || !self.viteDevInstance) {
return res.status(403).send('forbidden');
}
// Do not provide `next` to the middleware, we want to stop the chain here
// if the request is handled by Vite. It provides its own 404 handler.
self.viteDevInstance.middlewares(req, res);
}
}
};
},
methods(self) {
return {
// see @apostrophecms/assset:getBuildOptions() for the options shape.
// A required interface for the asset module.
async build(options = {}) {
self.printDebug('build-options', { buildOptions: options });
self.buildOptions = options;
await self.buildBefore(options);
await self.buildPublic(options);
const ts = await self.buildApos(options);
const viteManifest = await self.getViteBuildManifest();
self.entrypointsManifest = await self.applyManifest(
self.entrypointsManifest, viteManifest
);
return {
entrypoints: self.entrypointsManifest,
sourceMapsRoot: self.distRoot,
devServerUrl: null,
ts
};
},
// A required interface for the asset module.
async startDevServer(options) {
self.printDebug('dev-server-build-options', { buildOptions: options });
self.buildOptions = options;
self.shouldCreateDevServer = true;
await self.buildBefore(options);
const {
scenes: currentScenes,
build: currentBuild
} = self.getCurrentMode(options.devServer);
self.ensureViteClientEntry(self.entrypointsManifest, currentScenes, options);
let ts;
if (currentBuild === 'public') {
await self.buildPublic(options);
}
if (currentBuild === 'apos') {
ts = await self.buildApos(options);
}
const viteManifest = await self.getViteBuildManifest(currentBuild);
self.entrypointsManifest = await self.applyManifest(
self.entrypointsManifest, viteManifest
);
return {
entrypoints: self.entrypointsManifest,
hmrTypes: [ ...new Set(
self.getBuildEntrypointsFor(options.devServer)
.map((entry) => entry.type)
) ],
ts,
devServerUrl: self.getDevServerUrl()
};
},
// A required interface for the asset module.
// Initialize the watcher for triggering vite HMR via file
// copy to the build source. This method is called always
// after the `startDevServer` method.
// `chokidar` is a chockidar `FSWatcher` or compatible instance.
async watch(chokidar, buildOptions) {
self.printDebug('watch-build-options', { buildOptions });
self.buildWatchIndex();
// Initialize our voting system to detect what entrypoints
// are concerned with a given source file change.
self.setWatchVoters(
self.getBuildEntrypointsFor(buildOptions.devServer)
);
chokidar
.on('add', (p) => self.onSourceAdd(p, false))
.on('addDir', (p) => self.onSourceAdd(p, true))
.on('change', (p) => self.onSourceChange(p))
.on('unlink', (p) => self.onSourceUnlink(p, false))
.on('unlinkDir', (p) => self.onSourceUnlink(p, true));
},
// A required interface for the asset module.
// This method is called when build and watch are not triggered.
// Enhance and return any entrypoints that are included in the manifest
// when an actual build/devServer is triggered.
// The options are same as the ones provided in the `build` and
// `startDevServer` methods.
async entrypoints(options) {
self.printDebug('entrypoints-build-options', { buildOptions: options });
const entrypoints = self.apos.asset.getBuildEntrypoints(options.types)
.filter(entrypoint => entrypoint.condition !== 'nomodule');
self.ensureInitEntry(entrypoints);
if (options.devServer) {
const { scenes } = self.getCurrentMode(options.devServer);
self.ensureViteClientEntry(entrypoints, scenes, options);
}
return entrypoints;
},
// A required interface for the asset module.
async clearCache() {
await fs.remove(self.cacheDirBase);
},
// A required interface for the asset module.
async reset() {
await fs.remove(self.buildRoot);
await fs.mkdir(self.buildRoot, { recursive: true });
},
// Internal implementation.
...internalLib(self)
};
}
};