From af5ab770ec9c7ba175cefc6bceb133f5119a94f6 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Sat, 26 Jan 2019 02:30:46 +0100 Subject: [PATCH] More cleaning, deps in routes.json, module autodetection --- Dockerfile | 2 - front/core/lib/index.js | 1 - front/core/lib/module-loader.js | 12 ++-- front/core/lib/specs/module-loader.spec.js | 3 +- front/core/lib/spliting-register.js | 12 ---- front/module-import.js | 7 +++ front/salix/routes.js | 6 -- front/spliting.js | 18 ------ gulpfile.js | 17 +++--- loopback/server/middleware.json | 7 --- loopback/server/middleware/cors.js | 5 -- loopback/server/server.js | 8 +-- modules.yml | 8 --- modules/claim/front/routes.json | 1 + modules/item/front/routes.json | 1 + modules/order/front/routes.json | 1 + modules/ticket/front/routes.json | 1 + package-lock.json | 69 +++++----------------- package.json | 4 +- webpack.config.js | 3 +- 20 files changed, 48 insertions(+), 138 deletions(-) delete mode 100644 front/core/lib/spliting-register.js create mode 100755 front/module-import.js delete mode 100644 front/spliting.js delete mode 100644 loopback/server/middleware/cors.js delete mode 100644 modules.yml diff --git a/Dockerfile b/Dockerfile index 510faa06b..12708bcc2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,10 +25,8 @@ RUN npm install --only=prod COPY loopback loopback COPY back back COPY modules modules -COPY dist/webpack-assets.json dist/ COPY print print COPY \ - modules.yml \ LICENSE \ README.md \ ./ diff --git a/front/core/lib/index.js b/front/core/lib/index.js index e7f610e57..3aab29228 100644 --- a/front/core/lib/index.js +++ b/front/core/lib/index.js @@ -3,7 +3,6 @@ import './crud'; import './acl-service'; import './storage-services'; import './template'; -import './spliting-register'; import './interpolate'; import './copy'; import './equals'; diff --git a/front/core/lib/module-loader.js b/front/core/lib/module-loader.js index 4ecc179b4..163d103bb 100644 --- a/front/core/lib/module-loader.js +++ b/front/core/lib/module-loader.js @@ -1,5 +1,5 @@ import ngModule from '../module'; -import splitingRegister from './spliting-register'; +import moduleImport from 'module-import'; factory.$inject = ['$http', '$window', '$ocLazyLoad', '$translatePartialLoader', '$translate', '$q']; export function factory($http, $window, $ocLazyLoad, $translatePartialLoader, $translate, $q) { @@ -8,6 +8,10 @@ export function factory($http, $window, $ocLazyLoad, $translatePartialLoader, $t this._loaded = {}; } load(moduleName, validations) { + let moduleConf = $window.routes.find(i => i && i.module == moduleName); + if (!moduleConf) + return $q.reject(new Error(`Module not found: ${moduleName}`)); + let loaded = this._loaded; if (loaded[moduleName] === true) @@ -19,8 +23,8 @@ export function factory($http, $window, $ocLazyLoad, $translatePartialLoader, $t loaded[moduleName] = false; - let deps = splitingRegister.getDependencies(moduleName); let depPromises = []; + let deps = moduleConf.dependencies; if (deps) { for (let dep of deps) @@ -48,9 +52,7 @@ export function factory($http, $window, $ocLazyLoad, $translatePartialLoader, $t })); } - promises.push(new Promise(resolve => { - splitingRegister.modules[moduleName](resolve); - })); + promises.push(moduleImport(moduleName)); Promise.all(promises).then(() => { loaded[moduleName] = true; diff --git a/front/core/lib/specs/module-loader.spec.js b/front/core/lib/specs/module-loader.spec.js index d63148d34..c8def1634 100644 --- a/front/core/lib/specs/module-loader.spec.js +++ b/front/core/lib/specs/module-loader.spec.js @@ -4,9 +4,10 @@ describe('factory vnModuleLoader', () => { beforeEach(ngModule('vnCore')); - beforeEach(angular.mock.inject((_vnModuleLoader_, $rootScope) => { + beforeEach(angular.mock.inject((_vnModuleLoader_, $rootScope, $window) => { vnModuleLoader = _vnModuleLoader_; $scope = $rootScope; + $window.routes = [{module: 'myModule'}]; })); describe('load()', () => { diff --git a/front/core/lib/spliting-register.js b/front/core/lib/spliting-register.js deleted file mode 100644 index d6af3de64..000000000 --- a/front/core/lib/spliting-register.js +++ /dev/null @@ -1,12 +0,0 @@ - -class SplitingRegister { - constructor() { - this.graph = null; - this.modules = {}; - } - getDependencies(moduleName) { - return this.graph[moduleName]; - } -} - -export default new SplitingRegister(); diff --git a/front/module-import.js b/front/module-import.js new file mode 100755 index 000000000..8929b5fda --- /dev/null +++ b/front/module-import.js @@ -0,0 +1,7 @@ + +export default function moduleImport(moduleName) { + return import( + /* webpackInclude: /modules\/[a-z0-9-]+\/front\/index.js$/ */ + `../modules/${moduleName}/front/index.js` + ); +} \ No newline at end of file diff --git a/front/salix/routes.js b/front/salix/routes.js index 20c2f098d..c435c5e2f 100644 --- a/front/salix/routes.js +++ b/front/salix/routes.js @@ -1,7 +1,4 @@ import ngModule from './module'; -import deps from '../../modules.yml'; -import modules from 'spliting'; -import splitingRegister from 'core/lib/spliting-register'; import getMainRoute from 'core/lib/get-main-route'; function loader(moduleName, validations) { @@ -14,9 +11,6 @@ function loader(moduleName, validations) { config.$inject = ['$stateProvider', '$urlRouterProvider']; function config($stateProvider, $urlRouterProvider) { - splitingRegister.graph = deps; - splitingRegister.modules = modules; - $urlRouterProvider.otherwise('/'); $stateProvider.state('home', { diff --git a/front/spliting.js b/front/spliting.js deleted file mode 100644 index 7242f97c0..000000000 --- a/front/spliting.js +++ /dev/null @@ -1,18 +0,0 @@ -export default { - client: - cb => require.ensure([], () => cb(require('client/front'))), - route: - cb => require.ensure([], () => cb(require('route/front'))), - item: - cb => require.ensure([], () => cb(require('item/front'))), - ticket: - cb => require.ensure([], () => cb(require('ticket/front'))), - order: - cb => require.ensure([], () => cb(require('order/front'))), - claim: - cb => require.ensure([], () => cb(require('claim/front'))), - agency: - cb => require.ensure([], () => cb(require('agency/front'))), - travel: - cb => require.ensure([], () => cb(require('travel/front'))) -}; diff --git a/gulpfile.js b/gulpfile.js index 38355f71b..21f13e637 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -15,15 +15,15 @@ if (argv.NODE_ENV) let langs = ['es', 'en']; let srcDir = './front'; let modulesDir = './modules'; -let modules = require('./modules.yml'); let buildDir = 'dist'; + // Development const localesRoutes = gulp.parallel(locales, routes); localesRoutes.description = `Builds locales and routes`; -const front = gulp.series(buildClean, gulp.parallel(localesRoutes, watch, webpackDevServer)); +const front = gulp.series(clean, gulp.parallel(localesRoutes, watch, webpackDevServer)); front.description = `Starts frontend service`; const back = gulp.series(dockerStart, backOnly); @@ -110,9 +110,6 @@ smokesOnly.description = `Runs the smokes tests only`; smokes = gulp.series(docker, smokesOnly); smokes.description = `Restarts database and runs the smokes tests`; -const clean = gulp.parallel(buildClean); -clean.description = 'Cleans all generated project files'; - function install() { const install = require('gulp-install'); const print = require('gulp-print'); @@ -136,14 +133,14 @@ i.description = `Alias for the 'install' task`; const build = gulp.series(clean, gulp.parallel(localesRoutes, webpack)); build.description = `Generates binaries and configuration files`; -function buildClean() { +function clean() { const del = require('del'); const files = [ `${buildDir}/*` ]; return del(files, {force: true}); } -buildClean.description = `Cleans all files generated by the 'build' task`; +clean.description = `Cleans all files generated by the 'build' task`; // Webpack @@ -216,7 +213,8 @@ function locales() { let streams = []; let localePaths = []; - for (let mod in modules) + let modules = fs.readdirSync(modulesDir); + for (let mod of modules) localePaths[mod] = `${modulesDir}/${mod}`; let baseMods = ['core', 'salix']; @@ -394,11 +392,10 @@ module.exports = { e2e, smokesOnly, smokes, - clean, install, i, build, - buildClean, + clean, webpack, webpackDevServer, locales, diff --git a/loopback/server/middleware.json b/loopback/server/middleware.json index 43fc631d5..5945458c7 100644 --- a/loopback/server/middleware.json +++ b/loopback/server/middleware.json @@ -2,13 +2,6 @@ "initial:before": {}, "initial": { "compression": {}, - "cors": { - "params": { - "origin": "*", - "credentials": true, - "maxAge": 86400 - } - }, "helmet#xssFilter": {}, "helmet#frameguard": { "params": [ diff --git a/loopback/server/middleware/cors.js b/loopback/server/middleware/cors.js deleted file mode 100644 index 835f96350..000000000 --- a/loopback/server/middleware/cors.js +++ /dev/null @@ -1,5 +0,0 @@ -let cors = require('cors'); - -module.exports = function() { - return cors({origin: true}); -}; diff --git a/loopback/server/server.js b/loopback/server/server.js index 37ebddf81..3d8413671 100644 --- a/loopback/server/server.js +++ b/loopback/server/server.js @@ -1,5 +1,4 @@ require('require-yaml'); -const cookieParser = require('cookie-parser'); const loopback = require('loopback'); const boot = require('loopback-boot'); const DataSource = require('loopback-datasource-juggler').DataSource; @@ -32,9 +31,6 @@ let lbDir = path.resolve(`${rootDir}/..`); let appDir = path.resolve(`${__dirname}/../..`); let localeDir = `${lbDir}/locale`; let modulesDir = `${appDir}/modules`; -let modulesPath = `${appDir}/modules.yml`; - -app.use(cookieParser()); // Internationalization @@ -92,8 +88,8 @@ let bootDirs = [ addPath(`${appDir}/back`); -let modules = require(modulesPath); -for (let mod in modules) +let modules = fs.readdirSync(modulesDir); +for (let mod of modules) addPath(`${modulesDir}/${mod}/back`); function addPath(path) { diff --git a/modules.yml b/modules.yml deleted file mode 100644 index d05a4a10a..000000000 --- a/modules.yml +++ /dev/null @@ -1,8 +0,0 @@ -client: [] -item: [client] -ticket: [item, client] -order: [item, ticket] -claim: [item, client] -route: [] -agency: [] -travel: [] diff --git a/modules/claim/front/routes.json b/modules/claim/front/routes.json index 3b7bbef3e..01685228e 100644 --- a/modules/claim/front/routes.json +++ b/modules/claim/front/routes.json @@ -3,6 +3,7 @@ "name": "Claims", "icon": "icon-claims", "validations": true, + "dependencies": ["item", "client"], "routes": [ { "url": "/claim", diff --git a/modules/item/front/routes.json b/modules/item/front/routes.json index d2514107d..3e5518171 100644 --- a/modules/item/front/routes.json +++ b/modules/item/front/routes.json @@ -3,6 +3,7 @@ "name": "Items", "icon": "inbox", "validations" : true, + "dependencies": ["client"], "routes": [ { "url": "/item", diff --git a/modules/order/front/routes.json b/modules/order/front/routes.json index 00cd3e18b..b4b330f94 100644 --- a/modules/order/front/routes.json +++ b/modules/order/front/routes.json @@ -3,6 +3,7 @@ "name": "Orders", "icon": "shopping_cart", "validations": true, + "dependencies": ["item", "ticket"], "routes": [ { "url": "/order", diff --git a/modules/ticket/front/routes.json b/modules/ticket/front/routes.json index dcfe21c90..145653c14 100644 --- a/modules/ticket/front/routes.json +++ b/modules/ticket/front/routes.json @@ -3,6 +3,7 @@ "name": "Tickets", "icon": "icon-ticket", "validations": true, + "dependencies": ["item", "client"], "routes": [ { "url": "/ticket", diff --git a/package-lock.json b/package-lock.json index 73f88aede..eaa5ad342 100644 --- a/package-lock.json +++ b/package-lock.json @@ -384,6 +384,15 @@ "@babel/helper-plugin-utils": "^7.0.0" } }, + "@babel/plugin-syntax-dynamic-import": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-dynamic-import/-/plugin-syntax-dynamic-import-7.2.0.tgz", + "integrity": "sha512-mVxuJ0YroI/h/tbFTPGZR8cv6ai+STMKNBq0f8hFxsxWjl94qqhsb+wXbpNMDPU3cfR1TIsVFzU3nXyZMqyK4w==", + "dev": true, + "requires": { + "@babel/helper-plugin-utils": "^7.0.0" + } + }, "@babel/plugin-syntax-json-strings": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-json-strings/-/plugin-syntax-json-strings-7.2.0.tgz", @@ -3200,15 +3209,6 @@ "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz", "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=" }, - "cookie-parser": { - "version": "1.4.3", - "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.3.tgz", - "integrity": "sha1-D+MfoZ0AC5X0qt8fU/3CuKIDuqU=", - "requires": { - "cookie": "0.3.1", - "cookie-signature": "1.0.6" - } - }, "cookie-signature": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", @@ -3254,15 +3254,6 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, - "cors": { - "version": "2.8.5", - "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", - "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", - "requires": { - "object-assign": "^4", - "vary": "^1" - } - }, "create-ecdh": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/create-ecdh/-/create-ecdh-4.0.3.tgz", @@ -7072,35 +7063,6 @@ "requires-port": "^1.0.0" } }, - "http-proxy-middleware": { - "version": "0.19.1", - "resolved": "https://registry.npmjs.org/http-proxy-middleware/-/http-proxy-middleware-0.19.1.tgz", - "integrity": "sha512-yHYTgWMQO8VvwNS22eLLloAkvungsKdKTLO8AJlftYIKNfJr3GK3zK0ZCfzDDGUBttdGc8xFy1mCitvNKQtC3Q==", - "dev": true, - "requires": { - "http-proxy": "^1.17.0", - "is-glob": "^4.0.0", - "lodash": "^4.17.11", - "micromatch": "^3.1.10" - }, - "dependencies": { - "is-glob": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.0.tgz", - "integrity": "sha1-lSHHaEXMJhCoUgPd8ICpWML/q8A=", - "dev": true, - "requires": { - "is-extglob": "^2.1.1" - } - }, - "lodash": { - "version": "4.17.11", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==", - "dev": true - } - } - }, "http-signature": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz", @@ -8993,6 +8955,11 @@ } } } + }, + "swagger-ui": { + "version": "2.2.10", + "resolved": "https://registry.npmjs.org/swagger-ui/-/swagger-ui-2.2.10.tgz", + "integrity": "sha1-sl56IWZOXZC/OR2zDbCN5B6FLXs=" } } }, @@ -11187,7 +11154,8 @@ "object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", - "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", + "dev": true }, "object-component": { "version": "0.0.3", @@ -14909,11 +14877,6 @@ } } }, - "swagger-ui": { - "version": "2.2.10", - "resolved": "http://registry.npmjs.org/swagger-ui/-/swagger-ui-2.2.10.tgz", - "integrity": "sha1-sl56IWZOXZC/OR2zDbCN5B6FLXs=" - }, "table": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/table/-/table-5.1.1.tgz", diff --git a/package.json b/package.json index 7397bc204..118a93b50 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,6 @@ "license": "GPL-3.0", "dependencies": { "compression": "^1.7.3", - "cookie-parser": "^1.4.3", - "cors": "^2.8.5", "fs-extra": "^5.0.0", "helmet": "^3.15.0", "i18n": "^0.8.3", @@ -28,6 +26,7 @@ }, "devDependencies": { "@babel/core": "^7.2.2", + "@babel/plugin-syntax-dynamic-import": "^7.2.0", "@babel/polyfill": "^7.2.5", "@babel/preset-env": "^7.2.0", "@babel/register": "^7.0.0", @@ -52,7 +51,6 @@ "gulp-yaml": "^1.0.1", "html-loader": "^0.4.5", "html-webpack-plugin": "^4.0.0-beta.5", - "http-proxy-middleware": "^0.19.1", "jasmine": "^2.99.0", "jasmine-reporters": "^2.3.2", "jasmine-spec-reporter": "^4.2.1", diff --git a/webpack.config.js b/webpack.config.js index 8cf02c553..513aa28fc 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -21,7 +21,8 @@ let baseConfig = { loader: 'babel-loader', exclude: /node_modules/, query: { - presets: ['@babel/preset-env'] + presets: ['@babel/preset-env'], + plugins: ['@babel/plugin-syntax-dynamic-import'] } }, { test: /\.yml$/,