Skip to content

Commit

Permalink
feat(maleo webpack): add rimraf before run and cache server (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinkl committed Feb 15, 2019
1 parent 24a835b commit 3b45e4e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 97 deletions.
4 changes: 2 additions & 2 deletions example/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "",
"main": "index.js",
"scripts": {
"dev": "maleo run",
"dev": "maleo dev",
"build:prod": "export NODE_ENV=production && maleo build",
"start": "export NODE_ENV=production && node .maleo/server.js"
"start": "export NODE_ENV=production && maleo run"
},
"keywords": [],
"author": "",
Expand Down
11 changes: 6 additions & 5 deletions packages/Maleo.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"clean-webpack-plugin": "^1.0.1",
"es6-promise": "4.2.5",
"express": "4.16.4",
"figlet": "^1.2.1",
"friendly-errors-webpack-plugin": "^1.7.0",
"hard-source-webpack-plugin": "0.12.0",
"figlet": "1.2.1",
"friendly-errors-webpack-plugin": "1.7.0",
"hard-source-webpack-plugin": "0.13.1",
"helmet": "3.14.0",
"isomorphic-fetch": "2.2.1",
"loader-utils": "1.1.0",
Expand All @@ -44,14 +44,15 @@
"react-loadable": "5.5.0",
"react-router": "4.4.0-beta.1",
"react-router-config": "4.4.0-beta.6",
"rimraf": "2.6.3",
"source-map-support": "0.5.9",
"terser-webpack-plugin": "1.1.0",
"webpack": "4.29.0",
"webpack-bundle-analyzer": "^3.0.3",
"webpack-bundle-analyzer": "3.0.3",
"webpack-cli": "3.1.2",
"webpack-dev-middleware": "3.4.0",
"webpack-hot-middleware": "2.24.3",
"webpack-stats-plugin": "^0.2.1",
"webpack-stats-plugin": "0.2.1",
"webpackbar": "2.6.3"
},
"peerDependencies": {
Expand Down
62 changes: 36 additions & 26 deletions packages/Maleo.js/src/bin/maleo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ const isDev = env === 'development';

const binaryPath = path.resolve(__dirname);
const projectPath = path.resolve(process.cwd());
const buildDirectory = path.join(projectPath, '.maleo');

// bin codes starts here
// Importing node's own dependencies
import path from 'path';
import figlet from 'figlet';
import rimraf from 'rimraf';
import { spawn } from 'child_process';

// Importing required bin dependencies
import { build } from '../build/index';
import { build } from '@build/index';
import { loadUserConfig } from '@build/webpack/webpack';
import { BUILD_DIR } from '@constants/index';

console.log(
figlet.textSync('Maleo.js', {
Expand All @@ -33,33 +35,41 @@ console.log(
}),
);

console.log('==> Current Working Directory: ', projectPath);
console.log('==> Environment (isDevelopment): ', env, '(' + isDev + ')');
console.log('==> Running Command: ', type);
console.log('==> Command Args: ', args);
const userConfig = loadUserConfig(projectPath, true);
const buildDirectory = userConfig.buildDir || BUILD_DIR;

if (type === 'build') {
console.log('==> Running build');
build({
env,
buildType,
});
} else if (type === 'run' || !type) {
// Generating server execution
const serverPath = path.join(buildDirectory, 'server.js');
const exec = spawn.bind(null, 'node', [serverPath], {
stdio: 'inherit',
});

if (type === 'run') {
console.log('[MALEO] Running Application');
exec();
} else {
// Clean up the folder
rimraf(path.join(projectPath, buildDirectory), {}, () => {
console.log('==> Current Working Directory: ', projectPath);
console.log('==> Current Build Directory: ', buildDirectory);
console.log('==> Environment (isDevelopment): ', env, '(' + isDev + ')');
console.log('==> Running Command: ', type);
console.log('==> Command Args: ', args);

const serverPath = path.join(buildDirectory, 'server.js');
if (type === 'build') {
console.log('==> Running build');
build({
env,
buildType,
});
} else if (type === 'dev') {
console.log('[MALEO] Running Development');

const exec = spawn.bind(null, 'node', [serverPath], {
stdio: 'inherit',
build({
env,
buildType: 'server',
callback: exec,
});
}
});

if (isDev) {
build({
env,
buildType: 'server',
callback: exec,
});
} else {
exec();
}
}
39 changes: 34 additions & 5 deletions packages/Maleo.js/src/build/webpack/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
// Webpack Optimizations Plugin
import TerserPlugin from 'terser-webpack-plugin';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
// import HardSourcePlugin from 'hard-source-webpack-plugin';
import HardSourcePlugin from 'hard-source-webpack-plugin';

// Webpack required plugins
import { StatsWriterPlugin } from 'webpack-stats-plugin';
Expand Down Expand Up @@ -443,6 +443,34 @@ export const getDefaultPlugins = (
raw: true,
entryOnly: false,
}),

isDev &&
new HardSourcePlugin({
cacheDirectory: path.join(
projectDir,
`node_modules/.cache/hard-source/server/[confighash]`,
),
cachePrune: {
// Caches younger than `maxAge` are not considered for deletion. They must
// be at least this (default: 2 days) old in milliseconds.
maxAge: 2 * 24 * 60 * 60 * 1000,
// All caches together must be larger than `sizeThreshold` before any
// caches will be deleted. Together they must be at least this
// (default: 50 MB) big in bytes.
sizeThreshold: 50 * 1024 * 1024,
},

environmentHash: {
root: projectDir,
directories: [],
files: ['package-lock.json', 'yarn.lock'],
},

info: {
mode: env,
level: 'log',
},
}),
].filter(Boolean) as Configuration['plugins']) || [];

return [...commonPlugins, ...serverPlugins];
Expand Down Expand Up @@ -495,15 +523,15 @@ export const getDefaultOutput = (
/**
* Load User Config with file name USER_CUSTOM_CONFIG (maleo.config.js)
*/
export const loadUserConfig = (dir: string): CustomConfig => {
export const loadUserConfig = (dir: string, quiet?: boolean): CustomConfig => {
const cwd: string = path.resolve(dir);
const userConfigPath: string = path.resolve(cwd, USER_CUSTOM_CONFIG);
try {
const userConfig = requireRuntime(userConfigPath);

if (userConfig !== undefined) {
// tslint:disable-next-line:quotemark
console.log("[Webpack] Using user's config");
// tslint:disable-next-line:no-unused-expression quotemark
!quiet && console.log("[Webpack] Using user's config");
return {
...defaultUserConfig,
...userConfig,
Expand All @@ -513,7 +541,8 @@ export const loadUserConfig = (dir: string): CustomConfig => {
return defaultUserConfig;
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
console.log('[Webpack] Using Default Config');
// tslint:disable-next-line:no-unused-expression
!quiet && console.log('[Webpack] Using Default Config');
}
return defaultUserConfig;
}
Expand Down
77 changes: 18 additions & 59 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1920,11 +1920,6 @@ JSONStream@^1.0.4, JSONStream@^1.3.4:
jsonparse "^1.2.0"
through ">=2.2.7 <3"

JSV@^4.0.x:
version "4.0.2"
resolved "https://registry.yarnpkg.com/JSV/-/JSV-4.0.2.tgz#d077f6825571f82132f9dffaed587b4029feff57"
integrity sha1-0Hf2glVx+CEy+d/67Vh7QCn+/1c=

abab@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.0.tgz#aba0ab4c5eee2d4c79d3487d85450fb2376ebb0f"
Expand Down Expand Up @@ -2087,11 +2082,6 @@ ansi-styles@^3.2.0, ansi-styles@^3.2.1:
dependencies:
color-convert "^1.9.0"

ansi-styles@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-1.0.0.tgz#cb102df1c56f5123eab8b67cd7b98027a0279178"
integrity sha1-yxAt8cVvUSPquLZ817mAJ6AnkXg=

ansi-wrap@0.1.0, ansi-wrap@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/ansi-wrap/-/ansi-wrap-0.1.0.tgz#a82250ddb0015e9a27ca82e82ea603bbfa45efaf"
Expand Down Expand Up @@ -3038,15 +3028,6 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.0, chalk@^2.3.1, chalk@^2.3
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@~0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-0.4.0.tgz#5199a3ddcd0c1efe23bc08c1b027b06176e0c64f"
integrity sha1-UZmj3c0MHv4jvAjBsCewYXbgxk8=
dependencies:
ansi-styles "~1.0.0"
has-color "~0.1.0"
strip-ansi "~0.1.0"

chardet@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
Expand Down Expand Up @@ -4807,7 +4788,7 @@ figgy-pudding@^3.1.0, figgy-pudding@^3.4.1, figgy-pudding@^3.5.1:
resolved "https://registry.yarnpkg.com/figgy-pudding/-/figgy-pudding-3.5.1.tgz#862470112901c727a0e495a80744bd5baa1d6790"
integrity sha512-vNKxJHTEKNThjfrdJwHc7brvM6eVevuO5nTj6ez8ZQ1qbXTvGthucRF7S4vf2cr71QVnT70V34v0S1DyQsti0w==

figlet@^1.2.1:
figlet@1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/figlet/-/figlet-1.2.1.tgz#48d35df9d9b10b1b3888302e6e57904a0b00509c"
integrity sha512-qc8gycfnnfOmfvPl7Fi3JeTbcvdmbZkckyUVGGAM02je7Ookvu+bBfKy1I4FKqTsQHCs3ARJ76ip/k98r+OQuQ==
Expand Down Expand Up @@ -5057,7 +5038,7 @@ fresh@0.5.2:
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7"
integrity sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=

friendly-errors-webpack-plugin@^1.7.0:
friendly-errors-webpack-plugin@1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/friendly-errors-webpack-plugin/-/friendly-errors-webpack-plugin-1.7.0.tgz#efc86cbb816224565861a1be7a9d84d0aafea136"
integrity sha512-K27M3VK30wVoOarP651zDmb93R9zF28usW4ocaK3mfQeIEI5BPht/EzZs5E8QLLwbLRJQMwscAjDxYPb1FuNiw==
Expand Down Expand Up @@ -5632,19 +5613,21 @@ har-validator@~5.1.0:
ajv "^6.5.5"
har-schema "^2.0.0"

hard-source-webpack-plugin@0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/hard-source-webpack-plugin/-/hard-source-webpack-plugin-0.12.0.tgz#ea21f0407538fcb62f6995371541baab0a5f679e"
integrity sha512-xELLABhB3WtdVxDZ4iku8W5Xi0Tq5w+1fOXrSWjeML90uWikykkiQmTV6Lk0MKC5uFEKUZcSuTY5BZS+8ZXr8A==
hard-source-webpack-plugin@0.13.1:
version "0.13.1"
resolved "https://registry.yarnpkg.com/hard-source-webpack-plugin/-/hard-source-webpack-plugin-0.13.1.tgz#a99071e25b232f1438a5bc3c99f10a3869e4428e"
integrity sha512-r9zf5Wq7IqJHdVAQsZ4OP+dcUSvoHqDMxJlIzaE2J0TZWn3UjMMrHqwDHR8Jr/pzPfG7XxSe36E7Y8QGNdtuAw==
dependencies:
chalk "^2.4.1"
find-cache-dir "^2.0.0"
jsonlint "^1.6.3"
graceful-fs "^4.1.11"
lodash "^4.15.0"
mkdirp "^0.5.1"
node-object-hash "^1.2.0"
parse-json "^4.0.0"
pkg-dir "^3.0.0"
rimraf "^2.6.2"
semver "^5.6.0"
tapable "^1.0.0-beta.5"
webpack-sources "^1.0.1"
write-json-file "^2.3.0"
Expand All @@ -5656,11 +5639,6 @@ has-ansi@^2.0.0:
dependencies:
ansi-regex "^2.0.0"

has-color@~0.1.0:
version "0.1.7"
resolved "https://registry.yarnpkg.com/has-color/-/has-color-0.1.7.tgz#67144a5260c34fc3cca677d041daf52fe7b78b2f"
integrity sha1-ZxRKUmDDT8PMpnfQQdr1L+e3iy8=

has-flag@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-1.0.0.tgz#9d9e793165ce017a00f00418c43f942a7b1d11fa"
Expand Down Expand Up @@ -7104,14 +7082,6 @@ jsonfile@^4.0.0:
optionalDependencies:
graceful-fs "^4.1.6"

jsonlint@^1.6.3:
version "1.6.3"
resolved "https://registry.yarnpkg.com/jsonlint/-/jsonlint-1.6.3.tgz#cb5e31efc0b78291d0d862fbef05900adf212988"
integrity sha512-jMVTMzP+7gU/IyC6hvKyWpUU8tmTkK5b3BPNuMI9U8Sit+YAWLlZwB6Y6YrdCxfg2kNz05p3XY3Bmm4m26Nv3A==
dependencies:
JSV "^4.0.x"
nomnom "^1.5.x"

jsonparse@^1.2.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
Expand Down Expand Up @@ -8212,14 +8182,6 @@ nodemon@^1.18.4:
undefsafe "^2.0.2"
update-notifier "^2.5.0"

nomnom@^1.5.x:
version "1.8.1"
resolved "https://registry.yarnpkg.com/nomnom/-/nomnom-1.8.1.tgz#2151f722472ba79e50a76fc125bb8c8f2e4dc2a7"
integrity sha1-IVH3Ikcrp55Qp2/BJbuMjy5Nwqc=
dependencies:
chalk "~0.4.0"
underscore "~1.6.0"

"nopt@2 || 3":
version "3.0.6"
resolved "https://registry.yarnpkg.com/nopt/-/nopt-3.0.6.tgz#c6465dbf08abcd4db359317f79ac68a646b28ff9"
Expand Down Expand Up @@ -9959,6 +9921,13 @@ rimraf@2, rimraf@^2.5.2, rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2:
dependencies:
glob "^7.0.5"

rimraf@2.6.3:
version "2.6.3"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"
integrity sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==
dependencies:
glob "^7.1.3"

ripemd160@^2.0.0, ripemd160@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-2.0.2.tgz#a1c1a6f624751577ba5d07914cbc92850585890c"
Expand Down Expand Up @@ -10585,11 +10554,6 @@ strip-ansi@^5.0.0:
dependencies:
ansi-regex "^4.0.0"

strip-ansi@~0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-0.1.1.tgz#39e8a98d044d150660abe4a6808acf70bb7bc991"
integrity sha1-OeipjQRNFQZgq+SmgIrPcLt7yZE=

strip-bom-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/strip-bom-stream/-/strip-bom-stream-2.0.0.tgz#f87db5ef2613f6968aa545abfe1ec728b6a829ca"
Expand Down Expand Up @@ -11154,11 +11118,6 @@ undefsafe@^2.0.2:
dependencies:
debug "^2.2.0"

underscore@~1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.6.0.tgz#8b38b10cacdef63337b8b24e4ff86d45aea529a8"
integrity sha1-izixDKze9jM3uLJOT/htRa6lKag=

undertaker-registry@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/undertaker-registry/-/undertaker-registry-1.0.1.tgz#5e4bda308e4a8a2ae584f9b9a4359a499825cc50"
Expand Down Expand Up @@ -11545,7 +11504,7 @@ webidl-conversions@^4.0.2:
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"
integrity sha512-YQ+BmxuTgd6UXZW3+ICGfyqRyHXVlD5GtQr5+qjiNW7bF0cqrzX500HVXPBOvgXb5YnzDd+h0zqyv61KUD7+Sg==

webpack-bundle-analyzer@^3.0.3:
webpack-bundle-analyzer@3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/webpack-bundle-analyzer/-/webpack-bundle-analyzer-3.0.3.tgz#dbc7fff8f52058b6714a20fddf309d0790e3e0a0"
integrity sha512-naLWiRfmtH4UJgtUktRTLw6FdoZJ2RvCR9ePbwM9aRMsS/KjFerkPZG9epEvXRAw5d5oPdrs9+3p+afNjxW8Xw==
Expand Down Expand Up @@ -11615,7 +11574,7 @@ webpack-sources@^1.0.1, webpack-sources@^1.1.0, webpack-sources@^1.3.0:
source-list-map "^2.0.0"
source-map "~0.6.1"

webpack-stats-plugin@^0.2.1:
webpack-stats-plugin@0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/webpack-stats-plugin/-/webpack-stats-plugin-0.2.1.tgz#1f5bac13fc25d62cbb5fd0ff646757dc802b8595"
integrity sha512-OYMZLpZrK/qLA79NE4kC4DCt85h/5ipvWJcsefKe9MMw0qU4/ck/IJg+4OmWA+5EfrZZpHXDq92IptfYDWVfkw==
Expand Down

0 comments on commit 3b45e4e

Please sign in to comment.