persea-old/middleware.js

136 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-06-17 11:01:01 -05:00
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-param-reassign */
const path = require('path');
const airbnb = require('@neutrinojs/airbnb');
const airbnbBase = require('@neutrinojs/airbnb-base');
const mocha = require('@neutrinojs/mocha');
const scwp = require('scwp/neutrino');
const {DefinePlugin} = require('webpack');
const nodeExternals = require('webpack-node-externals');
const side = require('./side');
const avocadoPackages = {};
const pkg = require(`${__dirname}/package.json`);
const gatherPackagePaths = (root, packageMatchers) => {
return packageMatchers.reduce((r, packageMatcher) => (
r.concat([
(pkg.dependencies || {}),
(pkg.devDependencies || {}),
].reduce((r, deps) => {
const packageNames = Object.keys(deps);
const packages = [];
for (let i = 0; i < packageNames.length; i++) {
const packageName = packageNames[i];
if (packageName.match(packageMatcher)) {
packages.push(path.relative(root, path.dirname(require.resolve(packageName))));
}
}
return r.concat(packages);
}, []))
), []);
}
exports.initial = (options) => (neutrino) => {
const {
environmentDefines = [],
2020-06-18 07:52:38 -05:00
rawScwpPaths = [],
2020-06-17 11:01:01 -05:00
scwpPaths = [],
} = options;
neutrino.options.mains.index = `${side}/index`;
neutrino.options.output = `build/${side}`;
neutrino.config.resolve.modules
.add(`${neutrino.options.root}/node_modules`);
neutrino.config.resolveLoader.modules
.add(`${neutrino.options.root}/node_modules`);
neutrino.config.resolve.alias
.set('~', neutrino.options.source);
('server' === side ? airbnbBase : airbnb)({
eslint: {
cache: false,
// eslint-disable-next-line global-require
baseConfig: require('./.eslint.defaults'),
},
})(neutrino);
mocha({
spec: `src/+(${side}|common)/**/*.spec.js`,
})(neutrino);
scwp({
paths: [
'./src/common',
`./src/${side}`,
].concat(
2020-06-18 07:52:38 -05:00
gatherPackagePaths(neutrino.options.root, scwpPaths.concat(/^scwp/)),
).concat(
rawScwpPaths,
2020-06-17 11:01:01 -05:00
),
})(neutrino);
neutrino.config
.plugin('environment-define')
.use(DefinePlugin, [
environmentDefines.reduce((r, k) => ({...r, [k]: JSON.stringify(process.env[k])}), {})
]);
};
exports.afterPlatform = (options) => (neutrino) => {
const {
babelPaths = [],
externalMatcher = [],
} = options;
const allBabelPaths = gatherPackagePaths(neutrino.options.root, babelPaths);
neutrino.config.module
.rule('compile')
.use('babel')
.tap((options) => {
options.only = [
neutrino.options.source,
].concat(allBabelPaths);
options.ignore = [];
return options;
});
allBabelPaths.forEach((babelPath) => {
neutrino.config.module
.rule('compile')
.include
.add(path.resolve(neutrino.options.root, babelPath));
});
neutrino.config.module
.rule('compile')
.use('babel')
.get('options').plugins.push(
[
'babel-plugin-webpack-alias',
{
config: `${__dirname}/webpack.config.js`,
},
],
);
if ('client' === side) {
neutrino.config.node.delete('Buffer');
}
else /* if ('server' === side) */ {
neutrino.config.stats('normal');
if ('production' !== process.env.NODE_ENV) {
neutrino.config
.plugin('start-server')
.tap((args) => {
const options = args[0];
const inspectArg = process.argv.find((arg) => -1 !== arg.indexOf('--inspect'));
if (inspectArg) {
options.nodeArgs.push(inspectArg);
}
const profArg = process.argv.find((arg) => -1 !== arg.indexOf('--prof'));
if (profArg) {
options.nodeArgs.push(profArg);
}
return args;
});
}
neutrino.config
.externals(nodeExternals({
whitelist: externalMatcher,
}));
}
}