import { join } from 'path'; import glob from 'glob'; interface Options { cwd?: string; filename?: string; } /** * * @api public */ export class PagesPlugin { opts: any; constructor(opts: Options) { opts = opts || {}; this.opts = {}; this.opts.cwd = opts.cwd || join(process.cwd(), 'src', 'pages'); this.opts.filename = opts.filename || 'pages.json'; } apply(compiler: any) { compiler.hooks.emit.tapPromise('pages-plugin', this.emitStats.bind(this)); } emitStats(curCompiler: any, callback: any) { return Promise.resolve().then(() => { const json = JSON.stringify(glob.sync('**/*.tsx', { cwd: join(this.opts.cwd) })); curCompiler.assets[this.opts.filename] = { source() { return json; }, size() { return json.length; }, }; if (callback) { return void callback(); } }); } }