Skip to content

Commit ff05d45

Browse files
committed
Adds docs generation to build/watch
1 parent d0379f0 commit ff05d45

File tree

1 file changed

+61
-22
lines changed

1 file changed

+61
-22
lines changed

webpack.config.mjs

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ function getExtensionConfig(target, mode, env) {
105105

106106
plugins.push(
107107
new ContributionsPlugin(),
108+
new DocsPlugin(),
108109
new FantasticonPlugin({
109110
configPath: '.fantasticonrc.js',
110111
onBefore:
@@ -724,32 +725,54 @@ const schema = {
724725
},
725726
};
726727

727-
class ContributionsPlugin {
728-
constructor() {
729-
this.pluginName = 'contributions';
728+
class FileGeneratorPlugin {
729+
/**
730+
*
731+
* @param {string} pluginName
732+
* @param {string[]} pathsToWatch
733+
* @param {{ name: string; command: string; args: string[] }} command
734+
*/
735+
constructor(pluginName, pathsToWatch, command) {
736+
this.pluginName = pluginName;
737+
this.pathsToWatch = pathsToWatch;
738+
this.command = command;
730739
this.lastModified = 0;
731740
}
732741

733742
/**
734-
* @param {import("webpack").Compiler} compiler
743+
* @private
744+
* @param {string[]} paths
735745
*/
746+
pathsChanged(paths) {
747+
let changed = false;
748+
for (const path of paths) {
749+
try {
750+
const stats = fs.statSync(path);
751+
if (stats.mtimeMs > this.lastModified) {
752+
changed = true;
753+
break;
754+
}
755+
} catch {}
756+
}
757+
758+
return changed;
759+
}
760+
736761
apply(compiler) {
737-
const contributesPath = path.join(__dirname, 'contributions.json');
738762
let pendingGeneration = false;
739763

740-
// Add file dependency for watching
764+
// Add dependent paths for watching
741765
compiler.hooks.thisCompilation.tap(this.pluginName, compilation => {
742-
// Only watch the source file
743-
compilation.fileDependencies.add(contributesPath);
766+
this.pathsToWatch.map(path => compilation.fileDependencies.add(path));
744767
});
745768

746769
// Run generation when needed
747770
compiler.hooks.make.tapAsync(this.pluginName, async (compilation, callback) => {
748771
const logger = compiler.getInfrastructureLogger(this.pluginName);
749772
try {
750-
const stats = fs.statSync(contributesPath);
773+
const changed = this.pathsChanged(this.pathsToWatch);
751774
// Only regenerate if the file has changed since last time
752-
if (stats.mtimeMs <= this.lastModified) {
775+
if (!changed) {
753776
callback();
754777
return;
755778
}
@@ -763,38 +786,54 @@ class ContributionsPlugin {
763786
pendingGeneration = true;
764787

765788
try {
766-
logger.log(`[${compiler.name}] Generating 'package.json' contributions...`);
789+
logger.log(`Generating ${this.command.name}...`);
767790
const start = Date.now();
768791

769-
const result = spawnSync('pnpm', ['run', 'generate:contributions'], {
792+
const result = spawnSync(this.command.command, this.command.args, {
770793
cwd: __dirname,
771794
encoding: 'utf8',
772795
shell: true,
773796
});
774797

775798
if (result.status === 0) {
776799
this.lastModified = Date.now();
777-
logger.log(
778-
`[${compiler.name}] Generated 'package.json' contributions in \x1b[32m${
779-
Date.now() - start
780-
}ms\x1b[0m`,
781-
);
800+
logger.log(`Generated ${this.command.name} in \x1b[32m${Date.now() - start}ms\x1b[0m`);
782801
} else {
783-
logger.error(`[${this.pluginName}] Failed to generate contributions: ${result.stderr}`);
802+
logger.error(`[${this.pluginName}] Failed to run ${this.command.name}: ${result.stderr}`);
784803
}
785804
} finally {
786805
pendingGeneration = false;
787806
}
788-
} catch (err) {
807+
} catch (ex) {
789808
// File doesn't exist or other error
790-
logger.error(`[${this.pluginName}] Error checking contributions file: ${err}`);
809+
logger.error(`[${this.pluginName}] Error checking source file: ${ex}`);
791810
}
792811

793812
callback();
794813
});
795814
}
796815
}
797816

817+
class ContributionsPlugin extends FileGeneratorPlugin {
818+
constructor() {
819+
super('contributions', [path.join(__dirname, 'contributions.json')], {
820+
name: "'package.json' contributions",
821+
command: 'pnpm',
822+
args: ['run', 'generate:contributions'],
823+
});
824+
}
825+
}
826+
827+
class DocsPlugin extends FileGeneratorPlugin {
828+
constructor() {
829+
super('docs', [path.join(__dirname, 'src', 'constants.telemetry.ts')], {
830+
name: 'docs',
831+
command: 'pnpm',
832+
args: ['run', 'generate:docs:telemetry'],
833+
});
834+
}
835+
}
836+
798837
class FantasticonPlugin {
799838
alreadyRun = false;
800839

@@ -854,7 +893,7 @@ class FantasticonPlugin {
854893
}
855894

856895
const logger = compiler.getInfrastructureLogger(this.pluginName);
857-
logger.log(`[${compiler.name}] Generating icon font...`);
896+
logger.log(`Generating icon font...`);
858897

859898
const start = Date.now();
860899

@@ -883,7 +922,7 @@ class FantasticonPlugin {
883922
})`;
884923
}
885924

886-
logger.log(`[${compiler.name}] Generated icon font in \x1b[32m${Date.now() - start}ms\x1b[0m${suffix}`);
925+
logger.log(`Generated icon font in \x1b[32m${Date.now() - start}ms\x1b[0m${suffix}`);
887926
}
888927

889928
const generateFn = generate.bind(this);

0 commit comments

Comments
 (0)