Skip to content

Commit

Permalink
Add: useNodeModulesChunk property to voltran config
Browse files Browse the repository at this point in the history
  • Loading branch information
Cihan SELİM committed Sep 28, 2023
1 parent 079b253 commit c7bf21f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
54 changes: 29 additions & 25 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import arg from 'arg';
import fs from 'fs';
import path from 'path';
import clc from "cli-color";
import clc from 'cli-color';
import { spawn } from 'child_process';

import normalizeUrl from './os';
Expand All @@ -19,10 +19,10 @@ function parseArgumentsIntoOptions(rawArgs) {
'--no-bundle': Boolean,
'--analyze': Boolean,
'--port': Number,
'--ssr': Boolean,
'--ssr': Boolean
},
{
argv: rawArgs.slice(2),
argv: rawArgs.slice(2)
}
);
const argsList = removeUnneccesaryValueInObject({
Expand All @@ -32,7 +32,7 @@ function parseArgumentsIntoOptions(rawArgs) {
noBundle: args['--no-bundle'],
analyze: args['--analyze'],
configFile: args['--config'],
ssr: args['--ssr'],
ssr: args['--ssr']
});

return argsList;
Expand Down Expand Up @@ -65,35 +65,40 @@ function runDevelopmentMode() {
function runProductionMode(voltranConfigs, onlyBundle) {
const bundle = require('../src/tools/bundle');

bundle()
.then((res) => {
console.log(clc.green('Bundle is completed.\n',`File: ${voltranConfigs.distFolder}/server/server.js`));
bundle().then(res => {
console.log(
clc.green('Bundle is completed.\n', `File: ${voltranConfigs.distFolder}/server/server.js`)
);

if (!onlyBundle) {
serve(voltranConfigs);
}
});
if (!onlyBundle) {
serve(voltranConfigs);
}
});
}

function serve(voltranConfigs) {
console.log(clc.green('Project Serve is starting...'));

const out = spawn('node', [
'-r',
'source-map-support/register',
'--max-http-header-size=20480',
`${voltranConfigs.distFolder}/server/server.js`
], {env: {'NODE_ENV': 'production', ...process.env}});
const out = spawn(
'node',
[
'-r',
'source-map-support/register',
'--max-http-header-size=20480',
`${voltranConfigs.distFolder}/server/server.js`
],
{ env: { NODE_ENV: 'production', ...process.env } }
);

out.stdout.on('data', (data) => {
out.stdout.on('data', data => {
console.log(data.toString());
});

out.stderr.on('data', (data) => {
out.stderr.on('data', data => {
console.error(data.toString());
});

out.on('close', (code) => {
out.on('close', code => {
console.log(`child process exited with code ${code}`);
});
}
Expand All @@ -110,7 +115,6 @@ function checkRequiredVariables(mergeConfigs) {
}

export function cli(args) {
console.log('SELAM');
const argumentList = parseArgumentsIntoOptions(args);
console.log(clc.blue(JSON.stringify(argumentList)));
const voltranConfigs = argumentList.configFile ? getVoltranConfigs(argumentList.configFile) : {};
Expand All @@ -121,17 +125,17 @@ export function cli(args) {
if (isValid) {
const createdConfig = `module.exports = ${JSON.stringify(mergeAllConfigs)}`;

fs.writeFile(path.resolve(__dirname, '../voltran.config.js'), createdConfig, function (err) {
fs.writeFile(path.resolve(__dirname, '../voltran.config.js'), createdConfig, function(err) {
if (err) throw err;

console.log('File is created successfully.', mergeAllConfigs.dev);

if (mergeAllConfigs.dev) {
runDevelopmentMode();
} else {
argumentList.noBundle ?
serve(voltranConfigs) :
runProductionMode(mergeAllConfigs, argumentList.bundle);
argumentList.noBundle
? serve(voltranConfigs)
: runProductionMode(mergeAllConfigs, argumentList.bundle);
}
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "voltranjs",
"version": "1.2.0-beta.7",
"version": "1.2.0-beta.10",
"main": "src/index.js",
"author": "Hepsiburada Technology Team",
"bin": {
Expand Down
11 changes: 10 additions & 1 deletion src/universal/utils/baseRenderHtml.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { QUERY_PARAMS } from './constants';

const appConfig = require('__APP_CONFIG__');
const assets = require('__ASSETS_FILE_PATH__');
const voltranConfig = require('../../../voltran.config');
import { QUERY_PARAMS } from './constants';

const assetsBaseUrl = !appConfig.mediaUrl ? appConfig.baseUrl : '';
const assetsPrefix = appConfig.mediaUrl.length ? appConfig.mediaUrl : appConfig.baseUrl;
Expand Down Expand Up @@ -56,6 +57,14 @@ const getScripts = (name, subComponentFiles) => {
const mergedScripts =
subComponentFilesScripts?.length > 0 ? scripts.concat(subComponentFiles.scripts) : scripts;

const hasNodeModulesVendor = Boolean(assets?.nodeModulesVendor?.js);
if (hasNodeModulesVendor) {
scripts.push({
src: `${assetsBaseUrl}${assets?.nodeModulesVendor?.js}`,
isAsync: false
});
}

return mergedScripts;
};

Expand Down
15 changes: 14 additions & 1 deletion webpack.client.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ if (isDebug) {

const outputPath = voltranConfig.output.client.path;

const splitChunkOptimization = voltranConfig?.useNodeModulesChunk ? {
splitChunks: {
cacheGroups: {
nodeModulesVendor: {
test: /[\\/]node_modules[\\/]/,
name: 'nodeModulesVendor',
chunks: 'all'
}
}
}
} : {}

const clientConfig = merge(commonConfig, voltranClientConfig, {
name: 'client',

Expand Down Expand Up @@ -197,7 +209,8 @@ const clientConfig = merge(commonConfig, voltranClientConfig, {
}
}),
new CssMinimizerPlugin({})
]
],
...splitChunkOptimization
},

resolve: {
Expand Down

0 comments on commit c7bf21f

Please sign in to comment.