refactor: build

This commit is contained in:
cha0s 2020-12-24 23:12:26 -06:00
parent c30d4c5a2f
commit e02619edfc
2 changed files with 48 additions and 63 deletions

View File

@ -1,14 +1,12 @@
const airbnbBase = require('@neutrinojs/airbnb-base'); const airbnbBase = require('@neutrinojs/airbnb-base');
const library = require('@neutrinojs/library'); const library = require('@neutrinojs/library');
const mocha = require('@neutrinojs/mocha'); const mocha = require('@neutrinojs/mocha');
const react = require('@neutrinojs/react');
const nodeExternals = require('webpack-node-externals'); const nodeExternals = require('webpack-node-externals');
module.exports = () => ({ module.exports = {
options: {}, options: {},
use: [ use: [
(neutrino) => {
neutrino.options.output = '.';
},
airbnbBase({ airbnbBase({
eslint: { eslint: {
cache: false, cache: false,
@ -16,14 +14,53 @@ module.exports = () => ({
}, },
}), }),
(neutrino) => { (neutrino) => {
const {name} = neutrino.options.packageJson; const {files, name} = neutrino.options.packageJson;
library({ files
.filter((file) => {
try {
require.resolve(`${neutrino.options.source}/${file}`);
return true;
}
catch (error) {
if (file.match(/\.js$/)) {
try {
require.resolve(`${neutrino.options.source}/${file.slice(0, -3)}`);
return true;
}
catch (error) {
return false;
}
}
return false;
}
})
.map((file) => file.match(/\.js$/) ? file.slice(0, -3) : file)
.forEach((file) => {
neutrino.options.mains[file] = {entry: 'index' === file ? file : `./src/${file}`};
});
neutrino.options.output = '.';
react({
clean: false, clean: false,
name,
target: 'node',
})(neutrino); })(neutrino);
}, Object.keys(neutrino.options.mains).forEach((main) => {
(neutrino) => { neutrino.config.plugins.delete(`html-${main}`);
});
neutrino.config
.devtool('source-map')
.target('node')
.optimization
.splitChunks(false)
.runtimeChunk(false)
.end()
.output
.filename('[name].js')
.library(name)
.libraryTarget('umd')
.umdNamedDefine(true)
.end()
.node
.set('__dirname', false)
.set('__filename', false);
const options = neutrino.config.module const options = neutrino.config.module
.rule('compile') .rule('compile')
.use('babel') .use('babel')
@ -33,4 +70,4 @@ module.exports = () => ({
}, },
mocha(), mocha(),
], ],
}); };

View File

@ -1,52 +0,0 @@
const react = require('@neutrinojs/react');
const nodeExternals = require('webpack-node-externals');
const config = require('./.neutrinorc');
module.exports = ({name, files}, clientMains) => {
const mains = files
.filter((file) => file.match(/\.js$/))
.map((file) => file.slice(0, -3))
.reduce((r, file) => ({...r, [file]: file}), {});
if (clientMains.length > 0) {
const serverMains = Object
.entries(mains)
.filter(([key]) => -1 === clientMains.indexOf(key))
.reduce((r, [k, v]) => ({...r, [k]: v}), {});
const serverConfig = config();
serverConfig.options.mains = serverMains;
const clientConfig = config();
clientConfig.options.mains = clientMains.reduce((r, file) => ({...r, [file]: file}), {});
clientConfig.use[2] = (neutrino) => {
react({
clean: false,
})(neutrino);
Object.keys(clientConfig.options.mains).forEach((main) => {
neutrino.config.plugins.delete(`html-${main}`);
});
neutrino.config
.target('web')
.optimization
.splitChunks(false)
.runtimeChunk(false)
.end()
.output
.filename('[name].js')
.library(name)
.libraryTarget('umd')
.umdNamedDefine(true);
};
clientConfig.use.push((neutrino) => {
neutrino.config.node.set('Buffer', true);
});
return [serverConfig, clientConfig];
}
else {
const serverConfig = config();
serverConfig.options = {
mains,
};
return [serverConfig];
}
};