balena-supervisor/webpack.config.js
Pablo Carranza Velez b003f48d7b Switch to using knex migrations to set up the database, and change the database format to use integers for ids instead of strings.
Also includes various improvements and bugfixes to services and the migration from legacy /data to volumes.

The switch ti migrations involves a dirty hack for webpack to properly resolve the paths to the migrations js files - it uses an expression
that webpack can't resolve, so we hardcode it to a value and use the ContextReplacementPlugin to make that value resolve to the migrations folder.

The downsides to this approach are:
- a change in knex code would break this
- the migration code is added twice to the supervisor image: once in the migrations folder (because knex needs to loop through the directory to find the files),
and once inside app.js (because I can't make webpack treat them as external)

Signed-off-by: Pablo Carranza Velez <pablo@resin.io>
2018-03-06 10:32:28 -08:00

115 lines
2.8 KiB
JavaScript

var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var path = require('path');
var UglifyPlugin = require("uglifyjs-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
var externalModules = [
'mkfifo',
'sqlite3',
'mysql2',
'pg',
'mariasql',
'mssql',
'mysql',
'strong-oracle',
'oracle',
'oracledb',
'pg-query-stream'
]
var requiredModules = []
var maybeOptionalModules = []
lookForOptionalDeps = function (sourceDir) {
// We iterate over the node modules and mark all optional dependencies as external
var dirs = fs.readdirSync(sourceDir)
for (let dir of dirs) {
let packageJson = {};
let internalNodeModules = path.join(sourceDir, dir, 'node_modules');
if (fs.existsSync(internalNodeModules)) {
lookForOptionalDeps(internalNodeModules);
}
try {
packageJson = JSON.parse(fs.readFileSync(path.join(sourceDir, dir, '/package.json')));
}
catch (e) {
continue;
}
if (packageJson.optionalDependencies != null){
maybeOptionalModules = maybeOptionalModules.concat(_.keys(packageJson.optionalDependencies))
}
if (packageJson.dependencies != null){
requiredModules = requiredModules.concat(_.keys(packageJson.dependencies))
}
}
}
lookForOptionalDeps('./node_modules')
externalModules.push(new RegExp('^(' + _.reject(maybeOptionalModules, requiredModules).map(_.escapeRegExp).join('|') + ')(/.*)?$'));
console.log('Using the following dependencies as external:', externalModules);
module.exports = function (env) {
let plugins = [
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"',
}),
new CopyWebpackPlugin([
{
from: './src/migrations',
to: 'migrations'
}
]),
new webpack.ContextReplacementPlugin(
/\.\/migrations/,
path.resolve(__dirname, 'src/migrations')
)
]
if (env == null || !env.noOptimize) {
plugins.push(new UglifyPlugin())
}
return {
entry: './src/app.coffee',
output: {
filename: 'app.js',
path: path.resolve(__dirname, 'dist')
},
resolve: {
extensions: [".js", ".json", ".coffee"]
},
target: 'node',
node: {
__dirname: false
},
module: {
rules: [
{
test: /knex\/lib\/migrate\/index\.js$/,
use: require.resolve('./hardcode-migrations')
},
{
test: /JSONStream\/index\.js$/,
use: require.resolve('./fix-jsonstream')
},
{
test: /\.coffee$/,
use: require.resolve('coffee-loader')
}
]
},
externals: (context, request, callback) => {
for (let m of externalModules) {
if ((typeof m === 'string' && m === request) || (m instanceof RegExp && m.test(request))) {
return callback(null, 'commonjs ' + request);
} else if (typeof m != 'string' && !(m instanceof RegExp)) {
throw new Error('Invalid entry in external modules: ' + m);
}
}
return callback()
},
plugins: plugins
};
}