Skip to content

Commit

Permalink
WIP: webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
rhansen committed Aug 13, 2021
1 parent 118c66e commit 55a8673
Show file tree
Hide file tree
Showing 7 changed files with 521 additions and 94 deletions.
52 changes: 49 additions & 3 deletions src/node/hooks/express/specialpages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const fs = require('fs');
const fsp = fs.promises;
const toolbar = require('../../utils/toolbar');
const hooks = require('../../../static/js/pluginfw/hooks');
const plugins = require('../../../static/js/pluginfw/plugin_defs');
const settings = require('../../utils/Settings');
const util = require('util');
const webaccess = require('./webaccess');
const webpack = require('webpack');

exports.expressCreateServer = (hookName, args, cb) => {
exports.expressCreateServer = async (hookName, args) => {
// expose current stats
args.app.get('/stats', (req, res) => {
res.json(require('../../stats').toJSON());
Expand Down Expand Up @@ -46,6 +48,51 @@ exports.expressCreateServer = (hookName, args, cb) => {
});
});

await fsp.mkdir(path.join(settings.root, 'var/js'), {recursive: true});
await fsp.writeFile(
path.join(settings.root, 'var/js/padbootstrap.js'),
eejs.require('ep_etherpad-lite/templates/padbootstrap.js', {
pluginModules: (() => {
const pluginModules = new Set();
for (const part of plugins.parts) {
for (const [, hookFnName] of Object.entries(part.client_hooks || {})) {
pluginModules.add(hookFnName.split(':')[0]);
}
}
return [...pluginModules];
})(),
settings,
}));

console.log('Packaging client-side JavaScript...');
const compiler = webpack({
context: settings.root,
devtool: 'source-map',
entry: './var/js/padbootstrap.js',
mode: process.env.NODE_ENV || 'development',
module: {
parser: {
javascript: {
commonjsMagicComments: true,
},
},
},
output: {
path: path.join(settings.root, 'var/js'),
filename: '[name]-[contenthash].js',
},
});
const stats = await util.promisify(compiler.run.bind(compiler))();
console.log(`webpack stats:\n${stats}`);
const mainName = stats.toJson('minimal').assetsByChunkName.main;

args.app.get(`/${mainName}`, (req, res, next) => {
res.sendFile(path.join(settings.root, `var/js/${mainName}`));
});
args.app.get(`/${mainName}.map`, (req, res, next) => {
res.sendFile(path.join(settings.root, `var/js/${mainName}.map`));
});

// serve pad.html under /p
args.app.get('/p/:pad', (req, res, next) => {
// The below might break for pads being rewritten
Expand All @@ -62,6 +109,7 @@ exports.expressCreateServer = (hookName, args, cb) => {
req,
toolbar,
isReadOnly,
entrypoint: `../${mainName}`,
}));
});

Expand Down Expand Up @@ -97,6 +145,4 @@ exports.expressCreateServer = (hookName, args, cb) => {
next();
})().catch((err) => next(err || new Error(err)));
});

return cb();
};
Loading

0 comments on commit 55a8673

Please sign in to comment.