latus/packages/core/src/build.js

163 lines
4.5 KiB
JavaScript
Raw Normal View History

2021-03-22 14:20:11 -05:00
import {
basename,
dirname,
extname,
join,
resolve,
} from 'path';
2021-03-22 08:05:36 -05:00
import Latus from './latus';
2021-03-23 02:20:52 -05:00
import R from './require';
2021-03-23 11:59:22 -05:00
import root from './root';
2021-03-22 02:53:54 -05:00
const {
2021-03-22 14:20:11 -05:00
LATUS_BABEL_CONFIG = require.resolve('@latus/build/build/.babelrc.js'),
2021-03-22 02:53:54 -05:00
LATUS_ESLINT_DEFAULTS = require.resolve('@latus/build/build/.eslint.defaults.js'),
} = process.env;
export default (latus) => (neutrino) => {
2021-03-23 02:20:52 -05:00
const airbnb = R('@neutrinojs/airbnb');
2021-03-22 02:53:54 -05:00
airbnb({
eslint: {
cache: false,
2021-03-23 02:20:52 -05:00
baseConfig: R(LATUS_ESLINT_DEFAULTS),
2021-03-22 02:53:54 -05:00
},
})(neutrino);
2021-03-23 02:20:52 -05:00
const clean = R('@neutrinojs/clean');
2021-03-22 02:53:54 -05:00
clean({
cleanOnceBeforeBuildPatterns: ['**/*.hot-update.*'],
})(neutrino);
2021-03-23 02:20:52 -05:00
const {EnvironmentPlugin} = R('webpack');
2021-03-22 02:53:54 -05:00
neutrino.config
.plugin('environment')
.use(EnvironmentPlugin, [{
SIDE: 'server',
}]);
2021-03-23 02:20:52 -05:00
const node = R('@neutrinojs/node');
2021-03-22 14:20:11 -05:00
node({
babel: {
configFile: LATUS_BABEL_CONFIG,
},
})(neutrino);
2021-03-22 02:53:54 -05:00
const defaults = Object.keys(latus.originalConfig)
.map((plugin) => {
const config = {};
[plugin, `${plugin}/server`].forEach((path) => {
try {
2021-03-23 02:20:52 -05:00
R.resolve(path);
2021-03-22 02:53:54 -05:00
config[path] = {};
}
// eslint-disable-next-line no-empty
catch (error) {}
});
return config;
})
.filter((config) => !!config)
.reduce((r, o) => ({...r, ...o}), {});
const config = Object.fromEntries(
Object.keys(defaults)
.map((path) => [
path,
latus.get(path),
]),
);
2021-03-23 16:05:39 -05:00
const babelOptions = neutrino.config.module
.rules.store.get('compile')
.uses.store.get('babel')
.store.get('options');
R('@babel/register')({
plugins: babelOptions.plugins,
presets: babelOptions.presets,
});
2021-03-22 08:05:36 -05:00
const paths = Object.keys(config)
.map((path) => Latus.runtimePath(path))
.filter((path) => !!path);
const pathMap = paths.map((path) => `'${path}': require('${path}')`).join(', ');
const source = [
'global.$$latus = {',
` config: ${JSON.stringify(config)},`,
` modules: {${pathMap}},`,
'};',
'if (module.hot) {',
];
paths.forEach((key) => {
2021-03-22 14:20:11 -05:00
source.push(` module.hot.accept('${key}', () => {`);
source.push(` global.latus.invoke('@latus/core/hmr', '${key}');`);
source.push(' });');
2021-03-22 08:05:36 -05:00
});
source.push('}');
2021-03-22 14:20:11 -05:00
// Everything's a simulation...
2021-03-23 02:20:52 -05:00
const virtual = R.resolve(`${__dirname}/virtual`);
2021-03-22 14:20:11 -05:00
neutrino.config
.entry('index')
.add(virtual);
2021-03-22 08:05:36 -05:00
neutrino.config.module
2021-03-22 14:20:11 -05:00
.rule(virtual)
.test(virtual)
2021-03-22 08:05:36 -05:00
.use('virtual')
2021-03-22 14:20:11 -05:00
.loader(virtual)
2021-03-22 08:05:36 -05:00
.options({
source: source.join('\n'),
});
2021-03-23 02:20:52 -05:00
const mocha = R('@neutrinojs/mocha');
2021-03-22 02:53:54 -05:00
mocha()(neutrino);
if (process.env.LATUS_LINTING) {
return;
}
2021-03-23 02:20:52 -05:00
const nodeExternals = R('webpack-node-externals');
2021-03-22 14:20:11 -05:00
const allowlist = [
/^@latus\/core\/virtual$/,
];
2021-03-22 02:53:54 -05:00
if ('production' !== neutrino.config.get('mode')) {
neutrino.config
.entry('index')
.prepend('dotenv/config');
2021-03-22 14:20:11 -05:00
allowlist.push(/^webpack/);
paths.forEach((path) => {
2021-03-23 02:20:52 -05:00
const resolved = resolve(R.resolve(path));
2021-03-22 14:20:11 -05:00
// Cheating for now...
const parts = resolved.split('/');
parts.splice(parts.indexOf('packages') + 2, 0, 'src');
let source = parts.join('/');
source = join(dirname(source), basename(source, extname(source)));
allowlist.push(new RegExp(`^${path}$`));
neutrino.config.resolve.alias
.set(`${path}$`, source);
neutrino.config.module
.rule('compile').include.add(dirname(source));
});
}
neutrino.config.externals(nodeExternals({
allowlist,
}));
2021-03-23 11:59:22 -05:00
const entries = neutrino.config.entry('index');
2021-03-23 02:20:52 -05:00
if (entries.has(`${R.resolve('webpack/hot/poll')}?1000`)) {
entries.delete(`${R.resolve('webpack/hot/poll')}?1000`);
2021-03-23 11:59:22 -05:00
entries.add('webpack/hot/signal');
2021-03-22 02:53:54 -05:00
}
2021-03-23 11:59:22 -05:00
entries.delete(join(root, 'src', 'index'));
entries.add('@latus/core/start');
2021-03-22 02:53:54 -05:00
if (process.argv.find((arg) => '--start-server' === arg)) {
neutrino.config
.plugin('start-server')
.tap((args) => {
const options = args[0];
2021-03-22 14:20:11 -05:00
options.signal = true;
2021-03-22 02:53:54 -05:00
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);
}
options.nodeArgs.push('--experimental-repl-await');
options.nodeArgs.push('--unhandled-rejections=strict');
return args;
});
}
else {
neutrino.config.plugins.delete('start-server');
}
};