From 02fcc9968c7fb8f63a319d889db0ffed1bb5e000 Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 22 Nov 2019 02:14:00 -0600 Subject: [PATCH] feat: dynamic webpack :o --- defgen.js | 95 ---------------------------------------- server/index.js | 3 -- webpack.client.config.js | 17 ++----- webpack.common.config.js | 57 +----------------------- webpack.server.config.js | 17 ++----- 5 files changed, 7 insertions(+), 182 deletions(-) delete mode 100644 defgen.js diff --git a/defgen.js b/defgen.js deleted file mode 100644 index 0979971..0000000 --- a/defgen.js +++ /dev/null @@ -1,95 +0,0 @@ -const glob = require('glob'); -const {getOptions} = require('loader-utils'); -const path = require('path'); -const validateOptions = require('schema-utils'); - -const schema = { - type: 'object', - properties: { - paths: { - items: { - type: 'string', - }, - type: 'array', - }, - registrar: { - type: 'object', - properties: { - function: { - type: 'string', - }, - module: { - type: 'string', - }, - }, - required: [ - 'function', - 'module', - ], - }, - type: { - type: 'string', - }, - }, - required: [ - 'registrar', - 'type', - ], -}; - -// Dynamically require all traits. -module.exports = function(source) { - const options = getOptions(this); - // Validate schema. - validateOptions(schema, options, 'Avocado defgen'); - // Extract options. - const {classTransformer: transformer, paths, registrar, type} = options; - // Search avocado. - paths.push(path.resolve( - __dirname, 'node_modules', '@avocado', - )); - // Get all trait source paths. - const sourcePaths = []; - paths.forEach((path) => { - const files = glob.sync(`${path}/**/*.${type}.js`, { - follow: true, - }); - sourcePaths.push(...files); - }); - // Build import definitions. - const importDefinitions = sourcePaths.map((sourcePath) => { - const relativePath = path.relative(__dirname, sourcePath); - const basename = path.basename(relativePath, `.${type}.js`); - const parts = relativePath.split('/'); - // Chop off basename. - parts.pop(); - // Module or local? - let importDirectory; - if ('node_modules' === parts[0]) { - importDirectory = `${parts.slice(1).join('/')}`; - } - else { - importDirectory = `./${parts.join('/')}` - } - const importPath = `${importDirectory}/${basename}.${type}`; - // Extract class name. - const baseparts = basename.split('-'); - const className = baseparts.reduce((className, part) => { - const firstLetter = part.charAt(0).toUpperCase(); - const rest = part.substr(1).toLowerCase(); - return className + firstLetter + rest; - }, ''); - const transformed = transformer ? transformer(className) : className; - // Import and register. - return [ - `${registrar.function}(require('${importPath}').${transformed});`, - ].join('\n'); - }); - // Import trait registry first. - const output = [ - `import {${registrar.function}} from '${registrar.module}'`, - '', - ...importDefinitions - ].join('\n'); - return output; -} diff --git a/server/index.js b/server/index.js index db66f07..9105738 100644 --- a/server/index.js +++ b/server/index.js @@ -2,8 +2,6 @@ import http from 'http'; // 2nd party. import {SocketServer} from '@avocado/net/server/socket'; -// Import directly for HMR hierarchy. -import 'register-traits'; // Create fixtures. import './create-fixtures'; // Start game server. @@ -46,7 +44,6 @@ createGame(); if (module.hot) { module.hot.accept([ './game', - 'register-traits' ], () => { destroyGame(() => { createGame(); diff --git a/webpack.client.config.js b/webpack.client.config.js index 4bf1cd3..ca4d23d 100644 --- a/webpack.client.config.js +++ b/webpack.client.config.js @@ -1,5 +1,6 @@ const path = require('path'); +const {AvocadoPlugin} = require('@avocado/core/webpack/plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const webpack = require('webpack'); @@ -8,11 +9,7 @@ const isProduction = !!process.argv.find((arg) => '--production' === arg); config.entry = { client: [ - '@babel/polyfill', - 'register-packets', - 'register-synchronizeds', - 'register-traits', - '@avocado/behavior/item/initialize', + '@avocado/core/webpack/entry', path.join(__dirname, 'client', 'index.scss'), path.join(__dirname, 'client', 'index.js'), ], @@ -42,15 +39,6 @@ else { config.module.rules[0].use.options.configFile = path.resolve( __dirname, 'babel.client.config.js' ); -config.module.rules[1].use.options.paths.push( - path.resolve(__dirname, 'client'), -); -config.module.rules[2].use.options.paths.push( - path.resolve(__dirname, 'client'), -); -config.module.rules[3].use.options.paths.push( - path.resolve(__dirname, 'client'), -); config.node = { fs: 'empty', path: 'empty', @@ -65,5 +53,6 @@ config.plugins.push(new webpack.DefinePlugin({ AVOCADO_CLIENT: true, AVOCADO_SERVER: false, })); +config.plugins.push(new AvocadoPlugin(__dirname, 'client')); module.exports = config; diff --git a/webpack.common.config.js b/webpack.common.config.js index 7239347..50b7c1f 100644 --- a/webpack.common.config.js +++ b/webpack.common.config.js @@ -20,57 +20,6 @@ const config = { options: {}, }, }, - { - test: /register-packets\.js$/, - use: { - loader: './defgen', - options: { - classTransformer: (Packet) => { - return `${Packet}Packet`; - }, - paths: [ - path.resolve(__dirname, 'common'), - ], - registrar: { - function: 'registerPacket', - module: '@avocado/net', - }, - type: 'packet', - }, - }, - }, - { - test: /register-synchronizeds\.js$/, - use: { - loader: './defgen', - options: { - paths: [ - path.resolve(__dirname, 'common'), - ], - registrar: { - function: 'registerSynchronized', - module: '@avocado/net', - }, - type: 'synchronized', - }, - }, - }, - { - test: /register-traits\.js$/, - use: { - loader: './defgen', - options: { - paths: [ - path.resolve(__dirname, 'common'), - ], - registrar: { - function: 'registerTrait', - module: '@avocado/entity', - }, - type: 'trait', - }, - }, - }, // Styles. { test: /\.(?:sa|sc|c)ss$/, @@ -132,11 +81,7 @@ const config = { }), ], resolve: { - alias: { - 'register-packets': path.join(__dirname, 'register-packets'), - 'register-synchronizeds': path.join(__dirname, 'register-synchronizeds'), - 'register-traits': path.join(__dirname, 'register-traits'), - }, + alias: {}, modules: [ path.join(__dirname, 'node_modules'), path.join(__dirname, 'scss'), diff --git a/webpack.server.config.js b/webpack.server.config.js index 5280275..ef67dd0 100644 --- a/webpack.server.config.js +++ b/webpack.server.config.js @@ -1,5 +1,6 @@ const path = require('path'); +const {AvocadoPlugin} = require('@avocado/core/webpack/plugin'); const webpack = require('webpack'); const nodeExternals = require('webpack-node-externals'); const StartServerPlugin = require('start-server-webpack-plugin'); @@ -13,10 +14,7 @@ if (!isProduction) { config.entry = { server: [ 'source-map-support/register', - '@babel/polyfill', - path.join(__dirname, 'register-packets.js'), - path.join(__dirname, 'register-synchronizeds.js'), - '@avocado/behavior/item/initialize', + '@avocado/core/webpack/entry', path.join(__dirname, 'server', 'index.js'), ], }; @@ -48,16 +46,6 @@ pixiPackages.forEach((pixiPackage) => { config.module.rules[0].use.options.configFile = path.resolve( __dirname, 'babel.server.config.js' ); -// Include server files when scanning. -config.module.rules[1].use.options.paths.push( - path.resolve(__dirname, 'server'), -); -config.module.rules[2].use.options.paths.push( - path.resolve(__dirname, 'server'), -); -config.module.rules[3].use.options.paths.push( - path.resolve(__dirname, 'server'), -); const nodeArgs = ['--preserve-symlinks']; if (process.argv.find((arg) => '--inspect' === arg)) { nodeArgs.push('--inspect'); @@ -76,6 +64,7 @@ config.plugins.push(new webpack.DefinePlugin({ AVOCADO_CLIENT: false, AVOCADO_SERVER: true, })); +config.plugins.push(new AvocadoPlugin(__dirname, 'server')); config.target = 'node'; module.exports = config;