Skip to content

Commit

Permalink
only fix media
Browse files Browse the repository at this point in the history
  • Loading branch information
kongtiaowang committed Nov 18, 2024
1 parent 464dd8b commit ecaf768
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 108 deletions.
2 changes: 1 addition & 1 deletion modules/media/php/mediafile.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class MediaFile implements \LORIS\Data\DataInstance,
*/
public function getCenterID(): \CenterID
{
return \CenterID::singleton($this->DBRow['centerId']);
return \CenterID::singleton(intval($this->DBRow['centerId']));
}

/**
Expand Down
178 changes: 71 additions & 107 deletions webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ import webpack, {DefinePlugin, IgnorePlugin} from 'webpack';
import CopyPlugin from 'copy-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';

// Build mode (development or production)
const isDev = process.env.NODE_ENV === 'development';

// Target module to build (if there is one)
const target = process.env.target;

// A record mapping each LORIS module to its entry points
const lorisModules: Record<string, string[]> = {
// An object mapping each LORIS module to its entry points.
let lorisModules: { [x: string]: string[] } = {
media: ['CandidateMediaWidget', 'mediaIndex'],
issue_tracker: ['issueTrackerIndex', 'index', 'CandidateIssuesWidget'],
login: ['loginIndex'],
Expand Down Expand Up @@ -192,18 +186,35 @@ const plugins: webpack.WebpackPluginInstance[] = [];
plugins.push(new CopyPlugin({
patterns: [
{
from: `node_modules/react/umd/${
isDev ? 'react.development.js' : 'react.production.min.js'
}`,
to: 'htdocs/vendor/js/react',
from: path.resolve(__dirname, 'node_modules/react/umd'),
to: path.resolve(__dirname, 'htdocs/vendor/js/react'),
force: true,
globOptions: {
ignore: ['react.profiling.min.js'],
},
/** https://webpack.js.org/plugins/copy-webpack-plugin/#filter */
filter: async (path) => {
const file = path.split(/\\|\//).pop() as string;
const keep = [
'react.development.js',
'react.production.min.js',
];
return keep.includes(file);
},
},
{
from: `node_modules/react-dom/umd/${
isDev ? 'react-dom.development.js' : 'react-dom.production.min.js'
}`,
to: 'htdocs/vendor/js/react',
from: path.resolve(__dirname, 'node_modules/react-dom/umd'),
to: path.resolve(__dirname, 'htdocs/vendor/js/react'),
force: true,
/** https://webpack.js.org/plugins/copy-webpack-plugin/#filter */
filter: async (path) => {
const file = path.split(/\\|\//).pop() as string;
const keep = [
'react-dom.development.js',
'react-dom.production.min.js',
];
return keep.includes(file);
},
},
],
}));
Expand All @@ -221,110 +232,63 @@ if (EEGVisEnabled !== 'true' && EEGVisEnabled !== '1' ) {
}

/**
* Add the project-specific modules and entry points to the record of main
* LORIS modules.
* Get the webpack entries of a given module, which is described by its name
* and its entry points.
*
* @returns A list of two-element tuples mapping each entry name (exemple
* 'login/loginIndex') to its webpack entry.
*/
function addProjectModules(
modules: Record<string, string[]>,
): Record<string, string[]> {
if (!fs.existsSync('./project/webpack-project.config.js')) {
return modules;
}
function makeModuleEntries(moduleName: string, files: string[]) {
// Check if a project override exists for the module.
const basePath = fs.existsSync(`./project/modules/${moduleName}`)
? `./project/modules/${moduleName}/`
: `./modules/${moduleName}/`;

const projectModules: Record<string, string[]>
return files.map((fileName) => ([moduleName + '/' + fileName, {
import: basePath + 'jsx/' + fileName,
filename: basePath + 'js/' + fileName + '.js',
library: {
name: ['lorisjs', moduleName, fileName],
type: 'window',
},
}]));
}

// Add entries for project overrides.
if (fs.existsSync('./project/webpack-project.config.js')) {
const projectModules: { [x: string]: string[] }
// eslint-disable-next-line @typescript-eslint/no-var-requires
= require('./project/webpack-project.config.js');

// Copy the record of LORIS modules
const allModules: Record<string, string[]> = {};
for (const [moduleName, moduleEntryPoints] of
Object.entries(projectModules)
) {
allModules[moduleName] = [...moduleEntryPoints];
}

// Add project-specific modules and overrides to the record of modules
for (const [moduleName, moduleEntryPoints] of
Object.entries(projectModules)
) {
if (moduleName in allModules) {
allModules[moduleName].push(...moduleEntryPoints);
for (const [moduleName, files] of Object.entries(projectModules)) {
if (moduleName in lorisModules) {
lorisModules[moduleName].push(...files);
} else {
allModules[moduleName] = moduleEntryPoints;
lorisModules[moduleName] = files;
}
}

return allModules;
}

/**
* Filter the record of LORIS modules to contain only the target module if a
* target is defined, or return the record unchanged if no target is defined.
*/
function filterTargetModules(
modules: Record<string, string[]>,
): Record<string, string[]> {
// If there is no target module, do not filter the modules
if (!target) {
// eslint-disable-next-line no-console
console.log('Building all modules');
return modules;
}

// Exit if the target module is not found in the list of modules
if (!(target in modules)) {
console.error(`Target module \''${target}'\' not found`);
// Only build the given target module if there is one in the current
// environment.
const target = process.env.target;
if (target) {
if (target in lorisModules) {
lorisModules = {
[target]: lorisModules[target],
};
} else {
console.error(`Target module '${target}' not found.`);
process.exit(1);
}

// Return a record containing only the target module files
// eslint-disable-next-line no-console
console.log(`Building module \'${target}\'`);
return {
[target]: modules[target],
};
}

/**
* Get the Webpack entries of a given module, with each entry being mapped to
* its name.
*/
function getModuleEntries(
moduleName: string,
moduleEntryPoints: string[],
): Record<string, webpack.EntryOptions>[] {
// Check if a project override exists for the module.
const basePath = fs.existsSync(`./project/modules/${moduleName}`)
? `./project/modules/${moduleName}/`
: `./modules/${moduleName}/`;

return moduleEntryPoints.map((moduleEntryPoint) => ({
[moduleName + '/' + moduleEntryPoint]: {
import: basePath + 'jsx/' + moduleEntryPoint,
filename: basePath + 'js/' + moduleEntryPoint + '.js',
library: {
name: ['lorisjs', moduleName, moduleEntryPoint],
type: 'window',
},
},
}));
}

/**
* Get the Webpack entries of the LORIS modules to build, with each entry being
* mapped to its name.
*/
function getModulesEntries(): Record<string, webpack.EntryOptions> {
const allModules = addProjectModules(lorisModules);
const targetModules = filterTargetModules(allModules);
const moduleEntries = Object.entries(targetModules)
.map(([moduleName, moduleEntryPoints]) =>
getModuleEntries(moduleName, moduleEntryPoints)
)
.flat();

return Object.assign({}, ...moduleEntries);
}
// Transform the mapping of LORIS modules to a mapping of webpack entry points.
const entries = Object.fromEntries(
Object.entries(lorisModules)
.map(([name, files]) => makeModuleEntries(name, files))
.flat()
);

const configs: webpack.Configuration[] = [];

Expand All @@ -336,7 +300,7 @@ configs.push({
Breadcrumbs: './jsx/Breadcrumbs.js',
CSSGrid: './jsx/CSSGrid.js',
Help: './jsx/Help.js',
...getModulesEntries(),
...entries,
},
output: {
path: __dirname,
Expand Down

0 comments on commit ecaf768

Please sign in to comment.