refactor: build

This commit is contained in:
cha0s 2021-03-24 23:25:32 -05:00
parent 86d416737c
commit fc0287e050
7 changed files with 126 additions and 95 deletions

View File

@ -6,7 +6,6 @@ const neutrino = require('neutrino');
const {
LATUS_ONLY_BUILD = '',
LATUS_BABEL_CONFIG = require.resolve('./.babelrc.js'),
LATUS_NEUTRINO_CONFIG = require.resolve('./.neutrinorc.js'),
} = process.env;
const onlyBuilds = LATUS_ONLY_BUILD.split(',').map((name) => name.trim());
@ -14,42 +13,38 @@ R('@babel/register')({
configFile: LATUS_BABEL_CONFIG,
});
module.exports = process.env.LATUS_LINTING
? neutrino(server).webpack()
: new Promise((resolve, reject) => {
try {
const gatherConfigs = async () => {
const latus = Latus.create();
const configs = {
server: require(LATUS_NEUTRINO_CONFIG),
};
latus.invokeFlat('@latus/core/build', configs);
Promise.all(Object.entries(configs).map(async ([k, v]) => [k, await v]))
.then(Object.fromEntries)
.then((configs) => {
const webpackConfigs = Object.values(
let configs = latus.invokeReduce('@latus/core/build');
configs = Object.fromEntries(
await Promise.all(Object.entries(configs).map(async ([k, v]) => [k, await v])),
);
latus.invokeFlat('@latus/core/build/alter', configs);
if (0 === Object.keys(onlyBuilds).length) {
return;
}
configs = Object.fromEntries(
await Promise.all(Object.entries(configs).map(async ([k, v]) => [k, await v])),
);
if (onlyBuilds.length > 0) {
configs = Object.fromEntries(
Object.entries(configs)
.reduce(
(r, [name, config]) => ({
...r,
...(
(0 === onlyBuilds.length || -1 !== onlyBuilds.indexOf(name))
? {[name]: config}
: {}
),
}),
{},
)
)
.map((config) => neutrino(config).webpack());
if (webpackConfigs.length > 1) {
resolve(webpackConfigs);
.map(([k, v]) => (-1 !== onlyBuilds.indexOf(k) ? [k, v] : undefined))
.filter((e) => !!e)
);
}
if (1 === webpackConfigs.length) {
resolve(webpackConfigs[0]);
return configs;
};
const buildConfigs = async () => {
const configs = await gatherConfigs();
const webpackConfigs = Object.values(configs).map((config) => neutrino(config).webpack());
if (0 === webpackConfigs.length) {
console.error('Latus: nothing to build');
await new Promise(() => {});
}
});
}
catch (error) {
reject(error);
}
});
return webpackConfigs;
};
module.exports = buildConfigs();

View File

@ -27,8 +27,10 @@
"virtual.js"
],
"dependencies": {
"@neutrinojs/banner": "^9.5.0",
"@neutrinojs/node": "^9.1.0",
"debug": "4.3.1",
"deepmerge": "^4.2.2",
"js-yaml": "3.14.0",
"lodash.flatten": "^4.4.0",
"lodash.get": "^4.4.2",

View File

@ -101,12 +101,10 @@ export default (latus) => (neutrino) => {
});
const mocha = R('@neutrinojs/mocha');
mocha()(neutrino);
if (process.env.LATUS_LINTING) {
return;
}
const nodeExternals = R('webpack-node-externals');
const allowlist = [
/^@latus\/core\/virtual$/,
/^@babel\/runtime\/helpers\/esm/,
];
if ('production' !== neutrino.config.get('mode')) {
neutrino.config

View File

@ -2,6 +2,7 @@ import fs from 'fs';
import {join} from 'path';
import D from 'debug';
import merge from 'deepmerge';
import yaml from 'js-yaml';
import flatten from 'lodash.flatten';
import get from 'lodash.get';
@ -67,6 +68,17 @@ export default class Latus {
this.runtimePath(`${path}/server`),
]),
).filter((path) => !!path);
const dependencies = this.dependencies(paths);
if (dependencies.dom) {
const regex = new RegExp(dependencies.dom.join('|'));
R('pirates').addHook(
() => '',
{
ignoreNodeModules: false,
matcher: (filename) => !!filename.match(regex),
},
);
}
const pathMap = Object.fromEntries(paths.map((path) => [path, R(path)]));
return new Latus({
config,
@ -74,6 +86,25 @@ export default class Latus {
});
}
static dependencies(paths) {
return paths
.reduce((r, path) => {
const rc = this.runtimePath(join(path, '.latusrc.js'));
if (!rc) {
return r;
}
const config = R(rc)(this);
if (!config) {
return r;
}
return merge(r, config.dependencies);
}, {});
}
dependencies() {
return this.constructor.dependencies(Object.keys(this.config));
}
get(path, defaultValue) {
return get(this.config, path, defaultValue);
}

View File

@ -1,11 +1,51 @@
import banner from '@neutrinojs/banner';
import build from './build';
import R from './require';
export {default as appdata} from './appdata';
const {
LATUS_NEUTRINO_CONFIG = R.resolve('@latus/build/build/.neutrinorc.js'),
} = process.env;
export default {
hooks: {
'@latus/core/build': (configs, latus) => {
configs.server.use.unshift(build(latus));
'@latus/core/build': (latus) => {
const config = R(LATUS_NEUTRINO_CONFIG);
config.use.unshift(build(latus));
return {
server: config,
};
},
'@latus/core/build/alter': ({server}, latus) => {
if (!server) {
return;
}
const dependencies = latus.dependencies();
if (!dependencies.dom) {
return;
}
const code = [
`const regex = new RegExp('${dependencies.dom.join('|')}');`,
'eval(\'require\')(\'pirates\').addHook(',
' () => \'\',',
' {',
' ignoreNodeModules: false,',
' matcher: (filename) => !!filename.match(regex),',
' },',
');',
].join('\n');
server.use.push(banner({
banner: code,
pluginId: 'pixi-stub-banner',
}));
server.use.unshift((neutrino) => {
dependencies.dom.forEach((path) => {
neutrino.config.resolve.alias
.set(path, R.resolve('@latus/core'));
});
});
},
'@latus/core/config': () => ({
up: [],

View File

@ -892,7 +892,7 @@
eslint-plugin-react "^7.21.5"
eslint-plugin-react-hooks "^4.2.0"
"@neutrinojs/banner@9.5.0", "@neutrinojs/banner@^9.4.0":
"@neutrinojs/banner@9.5.0", "@neutrinojs/banner@^9.4.0", "@neutrinojs/banner@^9.5.0":
version "9.5.0"
resolved "http://npm.cha0sdev/@neutrinojs%2fbanner/-/banner-9.5.0.tgz#ee8df39db5d76033211a1811428e444a06d7222f"
integrity sha512-SL4nT0V1Wykf+LcRlCp/L8Frt4dk7MZITToC+OeDz2w6V7gg8YfEwDfdEg+aampjyUoxaq+A02ZyZP1TyRDtLA==
@ -1435,19 +1435,6 @@ atob@^2.1.2:
resolved "http://npm.cha0sdev/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==
autoprefixer@^9.8.6:
version "9.8.6"
resolved "http://npm.cha0sdev/autoprefixer/-/autoprefixer-9.8.6.tgz#3b73594ca1bf9266320c5acf1588d74dea74210f"
integrity sha512-XrvP4VVHdRBCdX1S3WXVD8+RyG9qeb1D5Sn1DeLiG2xfSpzellk5k54xbUERJ3M5DggQxes39UGOTP8CFrEGbg==
dependencies:
browserslist "^4.12.0"
caniuse-lite "^1.0.30001109"
colorette "^1.2.1"
normalize-range "^0.1.2"
num2fraction "^1.2.2"
postcss "^7.0.32"
postcss-value-parser "^4.1.0"
axe-core@^4.0.2:
version "4.1.1"
resolved "http://npm.cha0sdev/axe-core/-/axe-core-4.1.1.tgz#70a7855888e287f7add66002211a423937063eaf"
@ -1657,7 +1644,7 @@ browserify-zlib@^0.2.0:
dependencies:
pako "~1.0.5"
browserslist@^4.12.0, browserslist@^4.14.5, browserslist@^4.16.1:
browserslist@^4.14.5, browserslist@^4.16.1:
version "4.16.3"
resolved "http://npm.cha0sdev/browserslist/-/browserslist-4.16.3.tgz#340aa46940d7db878748567c5dea24a48ddf3717"
integrity sha512-vIyhWmIkULaq04Gt93txdh+j02yX/JzlyhLYbV3YQCn/zvES3JnY7TifHHvvr1w5hTDluNKMkV05cs4vy8Q7sw==
@ -1751,7 +1738,7 @@ camelcase@^6.0.0:
resolved "http://npm.cha0sdev/camelcase/-/camelcase-6.2.0.tgz#924af881c9d525ac9d87f40d964e5cea982a1809"
integrity sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==
caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001181:
caniuse-lite@^1.0.30001181:
version "1.0.30001187"
resolved "http://npm.cha0sdev/caniuse-lite/-/caniuse-lite-1.0.30001187.tgz#5706942631f83baa5a0218b7dfa6ced29f845438"
integrity sha512-w7/EP1JRZ9552CyrThUnay2RkZ1DXxKe/Q2swTC4+LElLh9RRYrL1Z+27LlakB8kzY0fSmHw9mc7XYDUKAKWMA==
@ -2161,6 +2148,11 @@ deepmerge@^2.2.1:
resolved "http://npm.cha0sdev/deepmerge/-/deepmerge-2.2.1.tgz#5d3ff22a01c00f645405a2fbc17d0778a1801170"
integrity sha512-R9hc1Xa/NOBi9WRVUWg19rl1UB7Tt4kuPd+thNJgFZoxXsTz7ncaPaeIm+40oSGuP33DfMb4sZt1QIGiJzC4EA==
deepmerge@^4.2.2:
version "4.2.2"
resolved "http://npm.cha0sdev/deepmerge/-/deepmerge-4.2.2.tgz#44d2ea3679b8f4d4ffba33f03d865fc1e7bf4955"
integrity sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==
define-properties@^1.1.3:
version "1.1.3"
resolved "http://npm.cha0sdev/define-properties/-/define-properties-1.1.3.tgz#cf88da6cbee26fe6db7094f61d870cbd84cee9f1"
@ -4056,16 +4048,6 @@ normalize-path@^3.0.0, normalize-path@~3.0.0:
resolved "http://npm.cha0sdev/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==
normalize-range@^0.1.2:
version "0.1.2"
resolved "http://npm.cha0sdev/normalize-range/-/normalize-range-0.1.2.tgz#2d10c06bdfd312ea9777695a4d28439456b75942"
integrity sha1-LRDAa9/TEuqXd2laTShDlFa3WUI=
num2fraction@^1.2.2:
version "1.2.2"
resolved "http://npm.cha0sdev/num2fraction/-/num2fraction-1.2.2.tgz#6f682b6a027a4e9ddfa4564cd2589d1d4e669ede"
integrity sha1-b2gragJ6Tp3fpFZM0lidHU5mnt4=
object-assign@^4.0.1, object-assign@^4.1.1:
version "4.1.1"
resolved "http://npm.cha0sdev/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
@ -4433,20 +4415,6 @@ posix-character-classes@^0.1.0:
resolved "http://npm.cha0sdev/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
integrity sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=
postcss-value-parser@^4.1.0:
version "4.1.0"
resolved "http://npm.cha0sdev/postcss-value-parser/-/postcss-value-parser-4.1.0.tgz#443f6a20ced6481a2bda4fa8532a6e55d789a2cb"
integrity sha512-97DXOFbQJhk71ne5/Mt6cOu6yxsSfM0QGQyl0L25Gca4yGWEGJaig7l7gbCX623VqTBNGLRLaVUCnNkcedlRSQ==
postcss@^7.0.32:
version "7.0.35"
resolved "http://npm.cha0sdev/postcss/-/postcss-7.0.35.tgz#d2be00b998f7f211d8a276974079f2e92b970e24"
integrity sha512-3QT8bBJeX/S5zKTTjTCIjRF3If4avAT6kqxcASlTWEtAFCb9NH0OUxNDfgZSWdP5fJnBYCMEWkIFfWeugjzYMg==
dependencies:
chalk "^2.4.2"
source-map "^0.6.1"
supports-color "^6.1.0"
prelude-ls@^1.2.1:
version "1.2.1"
resolved "http://npm.cha0sdev/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
@ -5586,11 +5554,6 @@ webpack-sources@^1.4.0, webpack-sources@^1.4.1:
source-list-map "^2.0.0"
source-map "~0.6.1"
webpack-virtual-modules@^0.4.1:
version "0.4.2"
resolved "http://npm.cha0sdev/webpack-virtual-modules/-/webpack-virtual-modules-0.4.2.tgz#68ce4479df7334a491b7a3f3bead47fe382947d9"
integrity sha512-OUsT1VZhArN8nY7g6mMlw91HWnXcNXsIQjsQ83WteF4ViZ6YXqF2sWKOTDIZ0H+PPiApQdszLdZIrD7NNlU0Yw==
webpack@^4:
version "4.46.0"
resolved "http://npm.cha0sdev/webpack/-/webpack-4.46.0.tgz#bf9b4404ea20a073605e0a011d188d77cb6ad542"

View File

@ -12,7 +12,7 @@ const onlyBuilds = LATUS_ONLY_BUILD.split(',').map((name) => name.trim());
export default {
hooks: {
'@latus/core/build': (configs, latus) => {
'@latus/core/build': (latus) => {
const {
config: {
'@latus/http/server': {
@ -26,12 +26,13 @@ export default {
!process.argv.find((arg) => '--watch' === arg)
|| process.argv.find((arg) => 'production' === arg)
) {
// eslint-disable-next-line no-param-reassign
configs.http = R(join(__dirname, 'build/.neutrinorc.js'));
return {
http: R(join(__dirname, 'build/.neutrinorc.js')),
};
}
else if (0 === onlyBuilds.length || -1 !== onlyBuilds.indexOf('http')) {
if (0 === onlyBuilds.length || -1 !== onlyBuilds.indexOf('http')) {
const binary = `$(npm --prefix ${root} bin)/webpack-dev-server`;
const config = `${__dirname}/build/webpack.config.js`;
const config = join(__dirname, 'build', 'webpack.config.js');
const options = {
env: {
...process.env,
@ -44,6 +45,7 @@ export default {
};
spawn(`${binary} --hot --inline --mode development --config ${config}`, options);
}
return undefined;
},
'@latus/core/config': () => ({
build: join('build', 'http'),