diff --git a/server/build/node-loader.js b/server/build/node-loader.js index 196d0ec..273c109 100644 --- a/server/build/node-loader.js +++ b/server/build/node-loader.js @@ -10,4 +10,4 @@ module.exports = function nodeLoader(m, q) { throw new Error('dlopen: Cannot open ' + modulePath + ': ' + e); } `); -} \ No newline at end of file +} diff --git a/server/build/webpack.614.prod.conf.js b/server/build/webpack.614.prod.conf.js index 7f3c49a..8710f3e 100644 --- a/server/build/webpack.614.prod.conf.js +++ b/server/build/webpack.614.prod.conf.js @@ -18,6 +18,10 @@ module.exports = { test: /\.js$/, use: [ { loader: 'babel-loader' } + ], + test: /\.node$/, + use: [ + { loader: './build/node-loader' } ] } ] diff --git a/server/build/webpack.prod.conf.js b/server/build/webpack.prod.conf.js index 5af7d63..821580c 100644 --- a/server/build/webpack.prod.conf.js +++ b/server/build/webpack.prod.conf.js @@ -1,15 +1,61 @@ +const webpack = require('webpack'); +const path = require('path'); +const fs = require('fs'); + var base = require('./webpack.base.conf'); +const nodeVersion = 'node-' + /[0-9]+/.exec(process.versions.node)[0]; +const PLATFORM = `${process.platform}-${process.arch}-${nodeVersion}`; + +const DEASYNC_NODE_MODULES_PATH = path.resolve( + 'node_modules', + 'deasync', + 'bin', + PLATFORM +); + +const DEASYNC_DIST_PATH = path.resolve('dist', 'bin', PLATFORM); + +if(!fs.existsSync(DEASYNC_NODE_MODULES_PATH)) { + throw new Error(`deasync doesn't support this platform: ${PLATFORM}`); +} base.mode = 'production'; -base.module.rules.push({ - test: /\.node$/, - use: [ - { loader: './build/node-loader' }, - { loader: 'file-loader', options: { name: '[name].[ext]' } }, - ] -}); +base.externals = base.externals ? base.externals : []; +base.externals.push( + function (context, request, callback) { + if(request.indexOf('bindings') === 0) { + callback(null, `() => require('./deasync.node')`) + } else { + callback(); + } + } +); + +const prodRules = [ + { + test: /\.node$/, + use: [ + { loader: './build/node-loader' }, + { loader: 'file-loader', options: { name: '[name].[ext]' } } + ] + } +]; + +let contextPathMapping = {}; +contextPathMapping[path.resolve(DEASYNC_DIST_PATH, 'deasync')] = './deasync.node'; + +const prodPlugins = [ + new webpack.ContextReplacementPlugin( + /deasync/, + DEASYNC_NODE_MODULES_PATH, + contextPathMapping + ) +]; + +base.module.rules = [...base.module.rules, ...prodRules]; +base.plugins = [...base.plugins, ...prodPlugins]; module.exports = base;