feat: dynamic webpack :o

This commit is contained in:
cha0s 2019-11-22 02:14:00 -06:00
parent 6d5cb6cf74
commit 02fcc9968c
5 changed files with 7 additions and 182 deletions

View File

@ -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;
}

View File

@ -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();

View File

@ -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;

View File

@ -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'),

View File

@ -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;