From 9cceaa6d3a2c282fb6cc8ac8940bad6b50bc999a Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 5 Mar 2019 19:21:09 -0600 Subject: [PATCH] flow: renaming, cjs, rollup, etc. --- comm/cjs/index.js | 430 ++++++++++++++++++++++++++++++++++++++ comm/debug.js | 2 +- comm/dispatcher.js | 25 ++- comm/index.js | 24 ++- comm/package.json | 5 +- comm/socket.ipc.js | 6 +- comm/socket.net.js | 16 +- core/cjs/index.js | 9 + core/package.json | 3 +- docker/cjs/index.js | 74 +++++++ docker/package.json | 3 +- emitters/cjs/index.js | 35 ++++ emitters/http.js | 85 ++++++++ emitters/index.js | 33 +++ emitters/package.json | 16 ++ rollup.sh | 9 + webpack/cjs/index.js | 2 + webpack/webpack.config.js | 8 +- 18 files changed, 744 insertions(+), 41 deletions(-) create mode 100644 comm/cjs/index.js create mode 100644 core/cjs/index.js create mode 100644 docker/cjs/index.js create mode 100644 emitters/cjs/index.js create mode 100644 emitters/http.js create mode 100644 emitters/index.js create mode 100644 emitters/package.json create mode 100755 rollup.sh create mode 100644 webpack/cjs/index.js diff --git a/comm/cjs/index.js b/comm/cjs/index.js new file mode 100644 index 0000000..e8f9c06 --- /dev/null +++ b/comm/cjs/index.js @@ -0,0 +1,430 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); + +function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } + +var events = require('events'); +var net = require('net'); +var D = _interopDefault(require('debug')); + +const debugActionIgnore = (process.env.DEBUG_ACTION_IGNORE || '').split(','); + +function createDebugAction(debug, action) { + if (!debug.enabled || -1 !== debugActionIgnore.indexOf(action.type)) { + return undefined; + } + const debugAction = {type: action.type, payload: {...action.payload}}; + if (!process.env.SILLY_DEBUG) { + for (const i in debugAction.payload) { + // Exceptions... + if ( + // Invocation hook. + -1 !== ['@truss/invoke', '@truss/invoke-flat'] && i !== 'hook' + ) { + debugAction.payload[i] = '[...]'; + } + } + } + return debugAction; +} + +function pack(action) { return JSON.stringify(action); } +function unpack (serial) { return JSON.parse(serial); } + +let clientId = 0; + +class IpcClient extends events.EventEmitter { + + constructor(service) { + super(); + + this.id = clientId; + + process.send({ + type: 'connect', + payload: { + clientId: this.id, + service, + }, + }); + + const messageListener = ({type, payload}) => { + + switch (type) { + + case 'connection_to': + + if (payload.clientId !== this.id) { return; } + + this.connection = payload.connection; + this.to = payload.to; + + setTimeout(() => { + this.emit('ready'); + }, 0); + + break; + + case 'connection_error': + + if (payload.clientId !== this.id) { return; } + + process.off('message', messageListener); + + setTimeout(() => { + this.emit('error', {code: 'ENOTFOUND'}); + }, 0); + + break; + + case 'client_recv': + + if (payload.clientId !== this.id) { return; } + + process.off('message', messageListener); + + setTimeout(() => { + this.emit('data', payload.data); + this.emit('end'); + }, 0); + + break; + + } + }; + + process.on('message', messageListener); + + clientId++; + } + + end() {} + + setEncoding() {} + + write(data) { + + process.send({ + type: 'client_send', + payload: { + clientId: this.id, + data, + to: this.to, + }, + }); + } +} + +function client(address) { + return new IpcClient(address); +} + +class IpcSocket extends events.EventEmitter { + + constructor(clientId, to) { + super(); + + return { + + emit: this.emit, + + end() { + this.emit('end'); + }, + + on: this.on, + + setEncoding() {}, + + write(data) { + + process.send({ + type: 'server_send', + payload: { + clientId, + data, + to, + }, + }); + }, + }; + } +} + +class IpcServer extends events.EventEmitter { + + constructor() { + super(); + + const connections = {}; + + return { + + emit: this.emit, + + listen() { + + process.send({ + type: 'listening', + }); + + setTimeout(() => { + this.emit('listening'); + }, 0); + + process.on('message', ({type, payload}) => { + + switch (type) { + + case 'connection_from': + + const socket = new IpcSocket(payload.clientId, payload.from); + connections[payload.clientId] = socket; + this.emit('connection', socket); + + socket.on('end', () => { + delete connections[payload.clientId]; + }); + + break; + + case 'server_recv': + + if (connections[payload.clientId]) { + connections[payload.clientId].emit('data', payload.data); + } + + break; + + } + }); + }, + + on: this.on, + + setEncoding() {}, + }; + } +} + +function server() { + return new IpcServer(); +} + +const port = process.env.DISPATCHER_PORT || 43170; + +function client$1(address) { + return net.createConnection(port, address); +} + +function server$1() { + const server = net.createServer(); + const originalListen = server.listen; + server.listen = () => { + return originalListen.call(server, port); + }; + return server; +} + +const debug = D('truss:comm:dispatcher'); + +class Dispatcher { + + constructor(actions = (() => ({})), hooks = (() => ({}))) { + this.args = []; + this.lookup = {actions, hooks}; + } + + connect() { + + const server$$1 = process.env.TRUSS_IPC ? server() : server$1(); + + server$$1.on('connection', (socket) => { + socket.on('error', (error) => { this.handleError(error); }); + + socket.setEncoding('utf8'); + socket.on('data', (chunk) => { + + let action = undefined; + try { action = unpack(chunk); } + catch (error) { return this.handleError(error); } + + const actions = this.createActions(); + if (!(action.type in actions)) { + debug(`discarding ${JSON.stringify(action, null, ' ')}!`); + return; + } + + const debugAction = createDebugAction(debug, action); + if (debugAction) { + debug(`dispatching ${JSON.stringify(debugAction, null, ' ')}`); + } + const actionPromise = actions[action.type](action); + Promise.resolve(actionPromise).then((result) => { + if ('undefined' === typeof result) { + debug(`undefined result from ${action.type} not forwarded!`); + } + else { + socket.write(pack(result)); + } + socket.end(); + }).catch((error) => { this.handleError(error); }); + }); + }); + + server$$1.on('listening', () => { + debug(`dispatcher listening...`); + }); + + server$$1.listen(); + + return server$$1; + } + + createActions() { + + const actions = this.lookup.actions(...this.args); + const hooks = this.lookup.hooks(...this.args); + + // builtin hook plumbing + actions['@truss/hook'] = (action) => { + if (!(action.payload.hook in hooks)) { return undefined; } + return hooks[action.payload.hook](...action.payload.args); + }; + + // builtin schema + const originalSchema = actions['@truss/schema'] || (() => ({})); + actions['@truss/schema'] = (action) => { + return {...originalSchema(action), hooks: Object.keys(hooks)}; + }; + + return actions; + } + + handleError(error) { + console.error(error); + } + + lookupActions(lookup) { this.lookup.actions = lookup; } + + lookupHooks(lookup) { this.lookup.hooks = lookup; } + + setArgs(...args) { this.args = args; } +} + +const debug$1 = D('truss:comm'); + +function createDispatcher(...args) { + + if (0 === args.length) { + return new Dispatcher(); + } + + if ('function' !== typeof args[0]) { + const value = args[0]; + args[0] = () => value; + } + + if (1 === args.length) { + return new Dispatcher(args[0]); + } + + if ('function' !== typeof args[1]) { + const value = args[1]; + args[1] = () => value; + } + + if (2 === args.length) { + return new Dispatcher(args[0], args[1]); + } +} +function invokeHook(hook, ...args) { + return sendActionToService({ + type: '@truss/invoke', payload: {hook, args}, + }, 'gateway'); +} +function invokeHookFlat(hook, ...args) { + return sendActionToService({ + type: '@truss/invoke-flat', payload: {hook, args}, + }, 'gateway'); +} +function invokeHookReduce(hook, rhs, ...args) { + return servicesImplementingHook(hook).then((services) => { + return services.reduce((promise, service) => { + return promise.then((rhs) => { + return invokeServiceHook(service, hook, rhs, ...args); + }); + }, Promise.resolve(rhs)); + }); +} +function invokeServiceHook(service, hook, ...args) { + return sendActionToService({ + type: '@truss/hook', payload: {hook, args}, + }, service); +} +function sendActionToService(action, service) { + + const createClient = process.env.TRUSS_IPC ? client : client$1; + const client$$1 = createClient(service); + client$$1.setEncoding('utf8'); + + client$$1.on('ready', writeClientPacket); + + let response = ''; + client$$1.on('data', (chunk) => { + response += chunk; + }); + + return new Promise((resolve, reject) => { + client$$1.on('error', (error) => { + // try try again... + if (-1 !== ['ECONNREFUSED', 'ENOTFOUND'].indexOf(error.code)) { + debug$1(`Can't connect to ${service}, retrying in 1 second...`); + return resolve(tryAgain()); + } + reject(error); + }); + client$$1.on('end', () => { + try { + resolve(response ? unpack(response) : undefined); + } + catch (error) { + reject(error); + } + }); + }); + + function tryAgain() { + return new Promise((resolve) => { + setTimeout(() => { + resolve(sendActionToService(action, service)); + }, 1000); + }); + } + + function writeClientPacket() { + const debugAction = createDebugAction(debug$1, action); + if (debugAction) { + debug$1(`sendActionToService(${ + JSON.stringify(debugAction, null, ' ') + }, '${service}')`); + } + client$$1.write(pack(action)); + } +} + +function servicesImplementingHook(hook) { + return sendActionToService({ + type: '@truss/hook-services', payload: {hook}, + }, 'gateway'); +} + +exports.createDispatcher = createDispatcher; +exports.invokeHook = invokeHook; +exports.invokeHookFlat = invokeHookFlat; +exports.invokeHookReduce = invokeHookReduce; +exports.invokeServiceHook = invokeServiceHook; +exports.sendActionToService = sendActionToService; +exports.servicesImplementingHook = servicesImplementingHook; +exports.createDebugAction = createDebugAction; diff --git a/comm/debug.js b/comm/debug.js index f643e68..4934235 100644 --- a/comm/debug.js +++ b/comm/debug.js @@ -10,7 +10,7 @@ export function createDebugAction(debug, action) { // Exceptions... if ( // Invocation hook. - -1 !== ['truss/invoke', 'truss/invoke-flat'] && i !== 'hook' + -1 !== ['@truss/invoke', '@truss/invoke-flat'] && i !== 'hook' ) { debugAction.payload[i] = '[...]'; } diff --git a/comm/dispatcher.js b/comm/dispatcher.js index c07db6d..dbbb6a5 100644 --- a/comm/dispatcher.js +++ b/comm/dispatcher.js @@ -1,7 +1,10 @@ -const {createDebugAction} = require('./debug'); -const {pack, unpack} = require('./packer'); +import {createDebugAction} from './debug'; +import {pack, unpack} from './packer'; +import {server as createIpcServer} from './socket.ipc'; +import {server as createNetServer} from './socket.net'; -const debug = require('debug')('truss:comm:dispatcher'); +import D from 'debug'; +const debug = D('truss:comm:dispatcher'); export class Dispatcher { @@ -12,9 +15,9 @@ export class Dispatcher { connect() { - const netServer = require('./socket.ipc').server(); + const server = process.env.TRUSS_IPC ? createIpcServer() : createNetServer(); - netServer.on('connection', (socket) => { + server.on('connection', (socket) => { socket.on('error', (error) => { this.handleError(error); }); socket.setEncoding('utf8'); @@ -47,13 +50,13 @@ export class Dispatcher { }); }); - netServer.on('listening', () => { + server.on('listening', () => { debug(`dispatcher listening...`); }); - netServer.listen(); + server.listen(); - return netServer; + return server; } createActions() { @@ -62,14 +65,14 @@ export class Dispatcher { const hooks = this.lookup.hooks(...this.args); // builtin hook plumbing - actions['truss/hook'] = (action) => { + actions['@truss/hook'] = (action) => { if (!(action.payload.hook in hooks)) { return undefined; } return hooks[action.payload.hook](...action.payload.args); } // builtin schema - const originalSchema = actions['truss/schema'] || (() => ({})); - actions['truss/schema'] = (action) => { + const originalSchema = actions['@truss/schema'] || (() => ({})); + actions['@truss/schema'] = (action) => { return {...originalSchema(action), hooks: Object.keys(hooks)}; } diff --git a/comm/index.js b/comm/index.js index c3f830f..e737545 100644 --- a/comm/index.js +++ b/comm/index.js @@ -1,8 +1,11 @@ -const {createDebugAction} = require('./debug'); -const {pack, unpack} = require('./packer'); -const {Dispatcher} = require('./dispatcher'); +import {createDebugAction} from './debug'; +import {pack, unpack} from './packer'; +import {Dispatcher} from './dispatcher'; +import {client as createIpcClient} from './socket.ipc'; +import {client as createNetClient} from './socket.net'; -const debug = require('debug')('truss:comm'); +import D from 'debug'; +const debug = D('truss:comm'); export function createDispatcher(...args) { @@ -31,17 +34,17 @@ export function createDispatcher(...args) { export function invokeHook(hook, ...args) { return sendActionToService({ - type: 'truss/invoke', payload: {hook, args}, + type: '@truss/invoke', payload: {hook, args}, }, 'gateway'); }; export function invokeHookFlat(hook, ...args) { return sendActionToService({ - type: 'truss/invoke-flat', payload: {hook, args}, + type: '@truss/invoke-flat', payload: {hook, args}, }, 'gateway'); }; -export function invokeHookSerial(hook, rhs, ...args) { +export function invokeHookReduce(hook, rhs, ...args) { return servicesImplementingHook(hook).then((services) => { return services.reduce((promise, service) => { return promise.then((rhs) => { @@ -53,13 +56,14 @@ export function invokeHookSerial(hook, rhs, ...args) { export function invokeServiceHook(service, hook, ...args) { return sendActionToService({ - type: 'truss/hook', payload: {hook, args}, + type: '@truss/hook', payload: {hook, args}, }, service); }; export function sendActionToService(action, service) { - const client = require('./socket.ipc').client(service); + const createClient = process.env.TRUSS_IPC ? createIpcClient : createNetClient; + const client = createClient(service); client.setEncoding('utf8'); client.on('ready', writeClientPacket); @@ -109,7 +113,7 @@ export function sendActionToService(action, service) { export function servicesImplementingHook(hook) { return sendActionToService({ - type: 'truss/hook-services', payload: {hook}, + type: '@truss/hook-services', payload: {hook}, }, 'gateway'); }; diff --git a/comm/package.json b/comm/package.json index a3892c3..70efb11 100644 --- a/comm/package.json +++ b/comm/package.json @@ -1,8 +1,9 @@ { "name": "@truss/comm", - "version": "1.0.0", + "version": "1.0.1", "description": "", - "main": "index.js", + "main": "./cjs", + "module": "./index", "author": "cha0s", "license": "MIT", "publishConfig": { diff --git a/comm/socket.ipc.js b/comm/socket.ipc.js index 59fd285..64cfb6c 100644 --- a/comm/socket.ipc.js +++ b/comm/socket.ipc.js @@ -1,4 +1,4 @@ -const {EventEmitter} = require('events'); +import {EventEmitter} from 'events'; let clientId = 0; @@ -84,7 +84,7 @@ class IpcClient extends EventEmitter { } } -exports.client = function(address) { +export function client(address) { return new IpcClient(address); } @@ -176,6 +176,6 @@ class IpcServer extends EventEmitter { } } -exports.server = function() { +export function server() { return new IpcServer(); } diff --git a/comm/socket.net.js b/comm/socket.net.js index 545f443..581cc56 100644 --- a/comm/socket.net.js +++ b/comm/socket.net.js @@ -1,18 +1,16 @@ -const net = require('net'); +import {createConnection, createServer} from 'net'; const port = process.env.DISPATCHER_PORT || 43170; -exports.client = (address) => { - return net.createConnection(port, address); -}; - -exports.server = () => { - const server = net.createServer(); +export function client(address) { + return createConnection(port, address); +} +export function server() { + const server = createServer(); const originalListen = server.listen; server.listen = () => { return originalListen.call(server, port); } - return server; -}; +} diff --git a/core/cjs/index.js b/core/cjs/index.js new file mode 100644 index 0000000..8fdb2cb --- /dev/null +++ b/core/cjs/index.js @@ -0,0 +1,9 @@ +'use strict'; + +exports.servicesList = () => { + const services = process.env.SERVICES.split(','); + if (-1 === services.indexOf('gateway')) { + services.unshift('gateway'); + } + return services; +}; diff --git a/core/package.json b/core/package.json index 42270b6..1ff1e17 100644 --- a/core/package.json +++ b/core/package.json @@ -2,7 +2,8 @@ "name": "@truss/core", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "./cjs", + "module": "./index", "author": "cha0s", "license": "MIT", "publishConfig": { diff --git a/docker/cjs/index.js b/docker/cjs/index.js new file mode 100644 index 0000000..b18f9ab --- /dev/null +++ b/docker/cjs/index.js @@ -0,0 +1,74 @@ +'use strict'; + +// Core. +const fs = require('fs'); +// 3rd-party. +const yaml = require('js-yaml'); + +exports.emitComposeFile = function(services) { + return emitString(emitObject(services)); +}; + +function emitString(object) { + return yaml.safeDump(object); +} + +function emitObject(services) { + const composeFile = { + version: '3', + services: {}, + }; + const cwd = process.cwd(); + for (const service of services) { + composeFile.services[service] = emitService(service); + const modulePath = `${cwd}/services/${service}/compose`; + try { + require(modulePath)(composeFile.services[service], composeFile); + } + catch (error) { + if (`Cannot find module '${modulePath}'` !== error.message) { + console.error(error); + process.exit(1); + } + } + } + return composeFile; +} + +function emitService(service) { + const definition = {}; + if ('production' === process.env.NODE_ENV) { + definition.image = 'docker.i12e.cha0s.io/cha0s6983/truss-production'; + } + else { + definition.image = 'docker.i12e.cha0s.io/cha0s6983/truss-dev'; + } + if ('production' === process.env.NODE_ENV) { + definition.env_file = [ + '.env', + ]; + } + else { + definition.env_file = [ + '.common.env', `.dev.env`, '.env', + ]; + } + if ('production' === process.env.NODE_ENV) { + definition.volumes = [ + `./${service}:/var/node/dist`, + ]; + } + else { + definition.volumes = [ + `./services/${service}:/var/node/src`, + `./dist/dev/${service}:/var/node/dist`, + ]; + // Add individual lib entries. + const cwd = process.cwd(); + const entries = fs.readdirSync(`${cwd}/lib`); + for (const entry of entries) { + definition.volumes.push(`./lib/${entry}:/var/node/lib/${entry}`); + } + } + return definition; +} diff --git a/docker/package.json b/docker/package.json index 261c117..7eb339b 100644 --- a/docker/package.json +++ b/docker/package.json @@ -2,7 +2,8 @@ "name": "@truss/docker", "version": "1.0.1", "description": "", - "main": "index.js", + "main": "./cjs", + "module": "./index", "author": "cha0s", "license": "MIT", "publishConfig": { diff --git a/emitters/cjs/index.js b/emitters/cjs/index.js new file mode 100644 index 0000000..949d7a4 --- /dev/null +++ b/emitters/cjs/index.js @@ -0,0 +1,35 @@ +'use strict'; + +// 2nd. +const {sendActionToService} = require('@truss/comm'); +const debug = require('debug')('truss:emitters'); +exports.ActionEmitter = class ActionEmitter { + + constructor(executors, listeners) { + this.executors = executors; + this.listeners = listeners; + } + onExecutorResponse(res, eres) { + } + + onNoExecutor(res) { + } + + processAction(action, res) { + debug(`Processing ${action.type}...`); + // listeners... + for (service of this.listeners[action.type] || []) { + sendActionToService(action, service).done(); + } + // executor + if (!(action.type in this.executors)) { + debug(`No executor for ${action.type}!`); + this.onNoExecutor(action, res); + return; + } + sendActionToService(action, this.executors[action.type]).then((eres) => { + this.onExecutorResponse(action, res, eres); + }).catch(console.error); + } + +}; diff --git a/emitters/http.js b/emitters/http.js new file mode 100644 index 0000000..0233fa8 --- /dev/null +++ b/emitters/http.js @@ -0,0 +1,85 @@ +const {createServer} = require('http'); +const bodyParser = require('body-parser'); +const {invokeHookReduce} = require('@truss/comm'); +const {ActionEmitter} = require('./index'); +const debug = require('debug')('truss:emitters:http'); +const parser = bodyParser.json(); + +module.exports = class HttpActionEmitter extends ActionEmitter { + + constructor(executors, listeners) { + super(executors, listeners); + this.server = createServer(); + } + + listen() { + const port = process.env.GATEWAY_PORT || 8000; + this.server.listen(port); + this.server.on('listening', () => { + debug(`listening on port ${port}...`); + }) + this.server.on('request', (req, res) => { parser(req, res, () => { + const method = req.method.toLowerCase(); + const url = req.url; + debug(`${method} ${url}`); + // map to action + const headers = { + ...req.headers, + ...{ + connection: undefined, + host: undefined, + } + }; + const action = { + type: undefined, + payload: { + headers: headers, + method: method, + url: url, + } + }; + switch (method) { + case 'post': + // truss action + if (url.startsWith('/truss/action/')) { + action.type = url.substr('/truss/action/'.length); + } + // @truss/http-post + else { + action.type = '@truss/http-post'; + } + action.payload = req.body; + break; + default: + // @truss/http-(get|delete|...) + action.type = `@truss/http-${method}`; + break; + } + this.processAction(action, res); + })}); + } + + onExecutorResponse(action, res, eres) { + const {payload: {headers: actionHeaders}} = action; + const {status, headers, response} = eres; + const reduced$ = invokeHookReduce( + '@truss/emitters/http', eres, actionHeaders + ); + reduced$.then(({status, headers, response}) => { + res.writeHead(status || 200, headers); + res.end(response); + }); + } + + onNoExecutor(action, res) { + res.writeHead(501); + res.end( + '

501 Not Implemented

' + ); + } + + unlisten() { + this.server.close(); + } +} + diff --git a/emitters/index.js b/emitters/index.js new file mode 100644 index 0000000..ede8bd6 --- /dev/null +++ b/emitters/index.js @@ -0,0 +1,33 @@ +// 2nd. +const {sendActionToService} = require('@truss/comm'); +const debug = require('debug')('truss:emitters'); +exports.ActionEmitter = class ActionEmitter { + + constructor(executors, listeners) { + this.executors = executors; + this.listeners = listeners; + } + onExecutorResponse(res, eres) { + } + + onNoExecutor(res) { + } + + processAction(action, res) { + debug(`Processing ${action.type}...`); + // listeners... + for (service of this.listeners[action.type] || []) { + sendActionToService(action, service).done(); + } + // executor + if (!(action.type in this.executors)) { + debug(`No executor for ${action.type}!`); + this.onNoExecutor(action, res); + return; + } + sendActionToService(action, this.executors[action.type]).then((eres) => { + this.onExecutorResponse(action, res, eres); + }).catch(console.error); + } + +} diff --git a/emitters/package.json b/emitters/package.json new file mode 100644 index 0000000..2b83962 --- /dev/null +++ b/emitters/package.json @@ -0,0 +1,16 @@ +{ + "name": "@truss/emitters", + "version": "1.0.0", + "description": "", + "main": "./cjs", + "module": "./index", + "author": "cha0s", + "license": "MIT", + "publishConfig": { + "registry": "https://npm.i12e.cha0s.io" + }, + "dependencies": { + "debug": "3.1.0", + "supports-color": "5.4.0" + } +} diff --git a/rollup.sh b/rollup.sh new file mode 100755 index 0000000..d8756c6 --- /dev/null +++ b/rollup.sh @@ -0,0 +1,9 @@ +cwd=$(pwd) +for path in *; do + if [ -d "$path" ]; then + echo "rollup $path..." + cd $path + rollup index.js -o cjs/index.js -f cjs + cd "$cwd" + fi +done diff --git a/webpack/cjs/index.js b/webpack/cjs/index.js new file mode 100644 index 0000000..eb109ab --- /dev/null +++ b/webpack/cjs/index.js @@ -0,0 +1,2 @@ +'use strict'; + diff --git a/webpack/webpack.config.js b/webpack/webpack.config.js index b7a2049..fde4661 100644 --- a/webpack/webpack.config.js +++ b/webpack/webpack.config.js @@ -17,7 +17,10 @@ function defaultConfig() { const config = { mode: 'production' !== process.env.NODE_ENV ? 'development' : 'production', entry: { - index: path.join(SOURCE_PATH, 'index.js'), + index: [ + '@babel/polyfill', + path.join(SOURCE_PATH, 'index.js'), + ] }, target: 'node', output: { @@ -85,6 +88,7 @@ function defaultConfig() { ], resolve: { alias: {}, + mainFields: ['module', 'main'], modules: [path.join(OUTPUT_PATH, 'node_modules')], }, resolveLoader: { @@ -109,8 +113,6 @@ function defaultConfig() { config.externals = []; } - config.resolve.alias['./socket.ipc'] = './socket.net'; - return config }