Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work in progress: webpack #5150

Draft
wants to merge 14 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions src/node/hooks/express/specialpages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ 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.expressPreSession = async (hookName, {app}) => {
// This endpoint is intended to conform to:
Expand Down Expand Up @@ -69,12 +71,62 @@ exports.expressPreSession = async (hookName, {app}) => {
});
};

exports.expressCreateServer = (hookName, args, cb) => {
exports.expressCreateServer = async (hookName, args) => {
// serve index.html under /
args.app.get('/', (req, res) => {
res.send(eejs.require('ep_etherpad-lite/templates/index.html', {req}));
});

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',
},
resolve: {
alias: {
'ep_etherpad-lite': path.join(settings.root, 'src'),
},
},
});
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 @@ -91,6 +143,7 @@ exports.expressCreateServer = (hookName, args, cb) => {
req,
toolbar,
isReadOnly,
entrypoint: `../${mainName}`,
}));
});

Expand All @@ -112,6 +165,4 @@ exports.expressCreateServer = (hookName, args, cb) => {
// express-session automatically calls req.session.touch() so we don't need to do it here.
res.json({status: 'ok'});
});

return cb();
};
Loading