2020-07-12 03:45:53 -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');
|
2020-07-12 21:10:29 -05:00
|
|
|
const {EnvironmentPlugin} = require('webpack');
|
2020-07-12 03:45:53 -05:00
|
|
|
const nodeExternals = require('webpack-node-externals');
|
|
|
|
|
|
|
|
const side = require('./side');
|
|
|
|
|
2020-07-12 21:10:29 -05:00
|
|
|
// eslint-disable-next-line import/no-dynamic-require
|
2020-07-12 03:45:53 -05:00
|
|
|
const pkg = require(`${__dirname}/package.json`);
|
|
|
|
|
|
|
|
const gatherPackagePaths = (root, packageMatchers) => {
|
|
|
|
return packageMatchers.reduce((r, packageMatcher) => (
|
|
|
|
r.concat([
|
|
|
|
(pkg.dependencies || {}),
|
|
|
|
(pkg.devDependencies || {}),
|
2020-07-12 21:10:29 -05:00
|
|
|
].reduce((r2, deps) => {
|
2020-07-12 03:45:53 -05:00
|
|
|
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))));
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 21:10:29 -05:00
|
|
|
return r2.concat(packages);
|
2020-07-12 03:45:53 -05:00
|
|
|
}, []))
|
|
|
|
), []);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.initial = (options) => (neutrino) => {
|
|
|
|
const {
|
2020-07-12 21:10:29 -05:00
|
|
|
environmentDefines = {},
|
2020-07-12 03:45:53 -05:00
|
|
|
rawScwpPaths = [],
|
|
|
|
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(
|
|
|
|
gatherPackagePaths(neutrino.options.root, scwpPaths.concat(/^scwp/)),
|
|
|
|
).concat(
|
|
|
|
rawScwpPaths,
|
|
|
|
),
|
|
|
|
})(neutrino);
|
|
|
|
neutrino.config
|
2020-07-12 21:10:29 -05:00
|
|
|
.plugin('environment')
|
|
|
|
.use(EnvironmentPlugin, [environmentDefines]);
|
2020-07-12 03:45:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
}));
|
|
|
|
}
|
2020-07-12 21:10:29 -05:00
|
|
|
};
|