From ecaf76865b9eb0dca79badb019d2fabaef216f2b Mon Sep 17 00:00:00 2001 From: Shen Date: Mon, 18 Nov 2024 14:20:39 -0500 Subject: [PATCH] only fix media --- modules/media/php/mediafile.class.inc | 2 +- webpack.config.ts | 178 ++++++++++---------------- 2 files changed, 72 insertions(+), 108 deletions(-) diff --git a/modules/media/php/mediafile.class.inc b/modules/media/php/mediafile.class.inc index f7c1e39c689..0d7503a09e3 100644 --- a/modules/media/php/mediafile.class.inc +++ b/modules/media/php/mediafile.class.inc @@ -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'])); } /** diff --git a/webpack.config.ts b/webpack.config.ts index 19337311c27..1e350381bd5 100644 --- a/webpack.config.ts +++ b/webpack.config.ts @@ -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 = { +// 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'], @@ -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); + }, }, ], })); @@ -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, -): Record { - 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 + 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 = {}; - 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, -): Record { - // 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[] { - // 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 { - 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[] = []; @@ -336,7 +300,7 @@ configs.push({ Breadcrumbs: './jsx/Breadcrumbs.js', CSSGrid: './jsx/CSSGrid.js', Help: './jsx/Help.js', - ...getModulesEntries(), + ...entries, }, output: { path: __dirname,