-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.doc.mjs
More file actions
695 lines (596 loc) · 23.3 KB
/
webpack.config.doc.mjs
File metadata and controls
695 lines (596 loc) · 23.3 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
import * as fs from 'fs';
import * as path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import * as typedoc from 'typedoc';
import releaseConfig from './webpack.config.mjs';
import debugConfig from './webpack.config.mjs';
/**
* Webpack plugin for generating script documentation through Typedoc
*
* @remarks
*
* NOTICE: The plugin currently does not support caching, since it does not use
* the dependency graph, therefore when using webpack-dev-server, the
* webpack-dev-middleware is required to write to disk. This is the behavior I
* wanted from the get-go, so I doubt I'll be adding support for in-memory
* caching. webpack-dev-server is a DEVELOPMENT SERVER!!!! Why the hell would I
* want to optimize for production use-cases??
*
* {@link https://web.archive.org/web/20250421140139/https://typedoc.org/api/}
*/
class ScriptDocumentationPlugin {
/**
* Default options for the ScriptDocumentationPlugin.
*
* This is more to indicate the available options without going full-blown
* "I NEED A SCHEMA". They're supposed to be defined in the modules export
* directly though
*
* @type {Object}
* @property {string} configPath - Path to TypeDoc configuration
* @property {import('typedoc').Configuration.TypeDocOptions} -
* configOverrides - Object to override the loaded TypeDoc
* configuration
*/
static defaultOptions = {
configPath: './typedoc.json',
configOverrides: {}
}
constructor(options = {}) {
// set defaults
this.options = {
...ScriptDocumentationPlugin.defaultOptions,
...options,
};
this.application = null;
this.fileDependencies = [];
this.onWatchRun = this.onWatchRun.bind(this);
}
/**
* TODO: write JSDoc block comment
*/
apply(compiler) {
const pluginName = ScriptDocumentationPlugin.name;
this.logger = compiler.getInfrastructureLogger(pluginName);
this.logger.info('initializing Typedoc application...');
const optionsFromFile = JSON.parse(fs.readFileSync(
path.resolve(this.options.configPath)
));
this.typedoc = typedoc.Application.bootstrap({
...optionsFromFile,
...this.options.configOverrides
}).then((application) => {
this.logger.info('TypeDoc application initialized...');
// TypeDoc enforces POSIX paths, but webpack does (rightfully) not.
// Therfore I'm normalizing the paths, so we can do a comparison
// later on without having to normalize both the webpack paths and
// TypeDoc paths...
this.fileDependencies = application.getDefinedEntryPoints().map(
entrypoint => path.normalize(entrypoint.sourceFile.fileName)
);
// TODO: figure out how to get the entrypoints deduplicated directly
// through the TypeDoc API. Doing filtering myself shouldn't be
// necessary...
this.fileDependencies = this.fileDependencies.filter((v, i) => {
return this.fileDependencies.indexOf(v) == i;
});
this.logger.info(
'number of files dependent upon:',
this.fileDependencies.length
);
// well, this is a little awkward 🙈.... I resolve the encapsulating
// Promise later on, but since the watchRun hook is called multiple
// times and the Promise is only resolvable once, I set the instance
// explicitly. The resolve() method does not care, that's what it's
// there for...
this.typedoc = application;
// need to return application, so we can keep the Promise chain for
// later usage alive
return application;
})
.catch((error) => this.logger.error(error));
this.logger.info('registering compiler hooks...');
// NOTICE: Defining anonymous functions inside a hook callee just to be
// able to access a higher level object (e.g. defining a callee for a
// hook providing a compilation object inside a hook providing a
// compiler object) is very very bad (imo)... It makes it so hard to
// understand the actual lifecycle of the webpack compiler. If I can't
// access certain properties, it's a good indication that I'm hooking
// into the wrong spot of the lifecycle for defining whatever action I'm
// doing... I'm looking at you - MICROSOFT!!!
compiler.hooks.beforeRun.tap(pluginName, this.onWatchRun);
compiler.hooks.watchRun.tap(pluginName, this.onWatchRun);
}
/**
* hook callee, being executed before compilation starts
*
* if in watch mode, watches for file changes, determines if files
* applicable to typedoc have changed and generates documentation through
* typedoc, if that's the case
*
* @param {import('webpack').Compiler} compiler - Webpack compiler instance.
*/
onWatchRun(compiler) {
var shouldGenerate = true;
if (compiler.watchMode) {
if (compiler.modifiedFiles) {
for (const filePath of compiler.modifiedFiles) {
if (this.fileDependencies.includes(filePath)) {
shouldGenerate = true;
break
}
else {
shouldGenerate = false;
}
}
}
}
if (shouldGenerate) {
this.logger.info('dependendent files modified');
Promise.resolve(this.typedoc)
.then(async (application) => {
return [application, await application.convert()];
})
.then(async ([application, project]) => {
this.logger.info('generating TypeDoc output...');
await application.generateOutputs(project);
this.logger.info('TypeDoc output generated...');
})
.catch((error) => {
this.logger.error(error);
});
}
}
}
/**
* Webpack plugin for generating usability demonstration frames and and index
* file for documenting and testing styles of this reference implementation.
*
* TODO: explain in detail on how the usability demonstration is structured,
* meant to be used and implemented by this plugin.
*/
class StyleDocumentationPlugin {
static matchHtmlRegExp = /["'&<>]/;
/**
* Default options for the StyleDocumentationPlugin.
*
* @type {Object}
* @property {string} inputBasedir - Base directory for input files.
* @property {string} outputBasedir - Base directory for output files.
* @property {string} frameTemplatePath - Path to the HTML template for frames.
* @property {string} indexTemplatePath - Path to the HTML template for the index.
*/
static defaultOptions = {
inputBasedir: path.join('docs', 'style'),
outputBasedir: path.join('docs', 'style'),
frameTemplatePath: path.join('docs', 'style', '_templates', 'frame.html'),
indexTemplatePath: path.join('docs', 'style', '_templates', 'index.html'),
}
/**
* Constructor for the StyleDocumentationPlugin.
*
* @param {Object} [options={}] - Configuration options for the plugin.
*/
constructor(options = {}) {
const pluginName = StyleDocumentationPlugin.name;
// set defaults
this.options = {
...StyleDocumentationPlugin.defaultOptions,
...options,
};
this.framesOptions = [];
this._plugins = null;
this._fileDependencies = null;
this.indexOptions = null;
this.logger = null;
this.onWatchRun = this.onWatchRun.bind(this);
this.onAfterCompile = this.onAfterCompile.bind(this);
}
/**
* Webpack's `apply` method hooks into the compiler to set up the plugin.
*
* @param {import('webpack').Compiler} compiler - Webpack compiler instance.
*/
apply(compiler) {
const pluginName = StyleDocumentationPlugin.name;
const resolvePath = (path, compiler) => {
return path.resolve(compiler.config.output.path, path);
}
// normalize paths
this.options = {
...this.options,
...{
inputBasedir: path.resolve(this.options.inputBasedir),
outputBasedir: path.resolve(
compiler.options.output.path,
this.options.outputBasedir
),
frameTemplatePath: path.resolve(this.options.frameTemplatePath),
indexTemplatePath: path.resolve(this.options.indexTemplatePath)
}
}
this.logger = compiler.getInfrastructureLogger(pluginName);
this.logger.info('registering compiler hooks...');
compiler.hooks.beforeRun.tap(pluginName, this.onWatchRun);
compiler.hooks.watchRun.tap(pluginName, this.onWatchRun);
compiler.hooks.afterCompile.tap(pluginName, this.onAfterCompile);
this.logger.info('generating plugin options...');
for (const frameOptions of StyleDocumentationPlugin.getFramesOptions(
this.options.inputBasedir,
this.options.outputBasedir,
this.options.frameTemplatePath
)) {
this.framesOptions.push(frameOptions);
}
const indexFilename = path.join(this.options.outputBasedir, 'index.html');
this.indexOptions = {
title: "Tiara's HTML Theming Reference (Usability Demonstration)",
filename: indexFilename,
template: this.options.indexTemplatePath,
// don't inject styles, we don't want the index to look pretty as
// this could be make the frames be difficult to discern
inject: false,
templateParameters: {
// description is going to be rendered as HTML so the extra
// spaces are going to be dropped anyway.
description: `This usability demonstration serves both as a a
showcase, as well as a usability test. It mirrors
the Sass 7:1 pattern of the style's source code,
in order to have a structured approach for
maintaing coverage. To use hot-reloading during
development, open each iframe in a seperate
window.`,
framesMeta: StyleDocumentationPlugin.getGroupedFramesMeta(
this.framesOptions,
indexFilename,
)
}
}
this.logger.info('applying compiler to plugins...');
var pluginRegistrationCount = 0;
for (const [plugin, fileDependencies] of this.plugins()) {
plugin.apply(compiler);
pluginRegistrationCount += 1;
}
this.logger.info(
'number of files dependent upon:',
this._fileDependencies.length
);
this.logger.info(
'number of (child) plugins compiler was applied to:',
pluginRegistrationCount
);
}
/**
* Handles Webpack's `beforeRun` and `watchRun` hooks to apply the plugin
* logic.
*
* @param {import('webpack').Compiler} compiler - Webpack compiler instance.
*
* @remarks
* There is a bug in html-webpack-plugin, where it uses the wrong hook for
* watch events, therefore file changes aren't emitted properly. Therefore
* I'm applying the plugins directly to the compiler for each compilation,
* instead of enqueing it as a plugin during initialization.
*
* {@link https://github.com/webpack/webpack/issues/16312#issuecomment-1262993892
* | Issue report and workaround}
*/
onWatchRun(compiler) {
if (compiler.watchMode) {
if (compiler.modifiedFiles) {
const cwd = process.cwd();
compiler.modifiedFiles.forEach((filePath) => {
if (this._fileDependencies.includes(filePath)) {
const plugins = this.getPluginsForFileDependency(filePath);
this.logger.info(
`dependendent file of ${plugins.length} plugin(s) modified:`,
path.relative(cwd, filePath)
);
//compiler.watching.invalidate();
}
});
}
}
compiler.hooks.initialize.call(compiler);
}
/**
* get an iterator of (child) plugins applied to the compiler. If the
* plugins haven't been constructed yet, they will.
*
* @returns {Generator<[HtmlWebpackPlugin, string[]], void, undefined>} A
* generator that yields a tuple of a plugin and its dependent files
*/
*plugins() {
if(this._plugins) {
for (const [plugin, fileDependencies] of this._plugins) {
yield [ plugin, [...fileDependencies] ];
}
return
}
this._fileDependencies = [];
this._plugins = [];
const indexPluginData = [
new HtmlWebpackPlugin(this.indexOptions),
[this.options.indexTemplatePath]
];
this._fileDependencies.push(this.options.indexTemplatePath);
this._plugins.push(indexPluginData);
yield [
indexPluginData[0],
[...indexPluginData[1]]
];
this._fileDependencies.push(this.options.frameTemplatePath);
for (const frameOptions of this.framesOptions) {
const framePluginData = [
new HtmlWebpackPlugin(frameOptions),
[
frameOptions.templateParameters.sourcePath,
this.options.frameTemplatePath,
]
];
this._fileDependencies.push(
frameOptions.templateParameters.sourcePath
);
this._plugins.push(framePluginData);
yield [
framePluginData[0],
[...framePluginData[1]]
];
}
}
/**
* get plugins dependent upon a source file
*/
getPluginsForFileDependency(filePath) {
const matches = [];
for (const [plugin, fileDependencies] of this.plugins()) {
if (fileDependencies.includes(filePath)) matches.push(plugin);
}
return matches;
}
/**
* TODO: write JSDOC block comment
*/
onAfterCompile(compilation) {
const buildDependencyGroup = path.resolve(this.options.inputBasedir);
compilation.buildDependencies.add(buildDependencyGroup);
this.framesOptions.forEach(frameOptions => {
const sourcePath = frameOptions.templateParameters.sourcePath;
if (!compilation.fileDependencies.has(sourcePath)) {
compilation.fileDependencies.add(sourcePath);
this.logger.info(`manually added file dependency: ${sourcePath}`);
}
});
}
/**
* Reads a file and optionally escapes its HTML content.
*
* @param {string} filePath - Path to the file.
* @param {boolean} [escapeHtmlChars=false] - Whether to escape HTML special characters.
* @returns {string} The file's content.
*/
static readFile(filePath, escapeHtmlChars = false) {
const data = fs.readFileSync(filePath);
if (escapeHtmlChars) return StyleDocumentationPlugin.escapeHtml(data);
return data;
}
static escapeHtml (string) {
const str = '' + string; // typecasting to string
const match = StyleDocumentationPlugin.matchHtmlRegExp.exec(str);
if (!match) return str;
var escape;
var html = '';
var index = 0;
var lastIndex = 0;
for (index = match.index; index < str.length; index++) {
switch (str.charCodeAt(index)) {
case 34: // "
escape = '"'
break
case 38: // &
escape = '&'
break
case 39: // '
escape = '''
break
case 60: // <
escape = '<'
break
case 62: // >
escape = '>'
break
default:
continue
}
if (lastIndex !== index) html += str.substring(lastIndex, index)
lastIndex = index + 1;
html += escape;
}
return lastIndex !== index
? html + str.substring(lastIndex, index)
: html
}
/**
* Recursively searches for files with a specified suffix.
*
* @param {string} root - Root directory to start the search.
* @param {string} suffix - File suffix to filter for.
* @returns {Generator<string, void, undefined>} A generator that yields file paths.
*/
static *findFilesBySuffix(root, suffix) {
if (!fs.existsSync(root)) {
throw new Error(`path does not exist: '${root}'`);
}
var out = [];
var files = fs.readdirSync(root);
for (var i = 0; i < files.length; i++) {
let filename = path.join(root, files[i]);
let stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
yield* StyleDocumentationPlugin.findFilesBySuffix(filename, suffix)
}
else if (filename.endsWith(suffix)) { yield filename }
};
return out;
}
/**
* Generates basic options for a style documentation frame.
*
* @param {string} inputFilePath - Path to the source file (partial HTML document).
* @param {string} inputBaseDir - Base directory of input files.
* @param {string} outputBaseDir - Base directory for output files.
* @returns {Object} Basic options for HtmlWebpackPlugin.
*/
static getBasicFrameOptions(
inputFilePath,
inputBaseDir,
outputBaseDir,
) {
// canonical name of frame
const outputName = path.basename(
inputFilePath,
path.extname(inputFilePath)
);
// canonical directory name of frame, applicable to both input and output
const relDirname = path.dirname(
path.relative(inputBaseDir, inputFilePath)
);
return {
title: [
'Usability test for',
`${relDirname.replace('\\', '/')}/${outputName}`,
].join(' '),
filename: path.join(
outputBaseDir,
relDirname,
`${outputName}.html`
),
templateParameters: {
relDirname: relDirname.replace('\\', '/'),
sourcePath: inputFilePath,
}
};
}
/**
* Generates full configuration options for all style documentation frames.
*
* @param {string} inputBasedir - Base directory of input files.
* @param {string} outputBasedir - Base directory for output files.
* @param {string} templatePath - Path to the frame template.
* @returns {Generator<Object, void, undefined>} A generator yielding configuration options for HtmlWebpackPlugin.
*/
static *getFramesOptions(
inputBasedir,
outputBasedir,
templatePath,
) {
for (const filePath of StyleDocumentationPlugin.findFilesBySuffix(inputBasedir, '.htm')) {
const basicOptions = StyleDocumentationPlugin.getBasicFrameOptions(
filePath,
inputBasedir,
outputBasedir,
);
yield {
...basicOptions,
...{
inject: true,
isDevServer: process.env.WEBPACK_SERVE,
template: templatePath,
templateParameters: {
...basicOptions.templateParameters,
...{
getContent: StyleDocumentationPlugin.readFile,
}
},
}
};
}
}
/**
* Extracts metadata for a frame required by the index.
*
* @param {Object} frameOptions - Frame options object.
* @param {string} indexFilename - Path to the index file.
* @returns {Object} Metadata for the frame.
*/
static getFrameMeta(
frameOptions,
indexFilename,
) {
return {
href: path.relative(
path.dirname(indexFilename),
frameOptions.filename
).replace('\\', '/'),
sourcePath: frameOptions.templateParameters.sourcePath,
getContent: (path) => StyleDocumentationPlugin.readFile(path, true)
}
}
/**
* Groups frames metadata by relative directory paths.
*
* @param {Object[]} framesOptions - Array of frame options objects.
* @param {string} indexFilename - Path to the index file.
* @returns {Object} Grouped metadata.
*/
static getGroupedFramesMeta(framesOptions, indexFilename) {
const groups = {};
for (const frame of framesOptions) {
const relDirname = frame.templateParameters.relDirname;
if (!(relDirname in groups)) groups[relDirname] = [];
groups[relDirname].push(
StyleDocumentationPlugin.getFrameMeta(frame, indexFilename)
);
}
return groups;
}
}
export default (env, argv) => {
if (env.target && !['script', 'style'].includes(env.target)) {
throw new Error("target must be empty, 'script' or 'style'");
}
const parentConfig = argv.mode === 'development' ? debugConfig : releaseConfig;
const config = parentConfig(env, argv);
// doc is the build target (directory) and docs the subdirectory under the target
// directory, since all the other assets will have a directory in there as well.
// This is so that when I'm merging the release and docs output, all assets are
// deduplicated. See Makefile and scripts/npm-pack.ts for info on how that
// works.
config.output.path = path.resolve('build', 'doc');
// something different is specified in the tsconfig and I forgot about it
config.module.rules[1].use[0].options.compilerOptions = {
"outDir": path.join(config.output.path, 'script')
};
if (!env.target || env.target === 'script') {
config.plugins.push(new ScriptDocumentationPlugin({
configPath: './typedoc.json',
configOverrides: {
out: path.join('build', 'doc', 'docs', 'script'),
}
}));
}
if (!env.target || env.target === 'style') {
config.plugins.push(new StyleDocumentationPlugin());
}
return {
...config,
entry: {
'demo': [
"./src/script/main.ts",
"./src/style/demo.scss"
],
},
devServer: {
compress: true,
open: true,
hot: true,
liveReload: true,
watchFiles: ['src/**/*.scss', 'src/**/*.ts'],
devMiddleware: {
writeToDisk: true
},
static: 'build/doc/docs/style',
}
}
};