Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

[WIP] Perform development environment cleanup #944

Open
wants to merge 6 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions bin/dev-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
if (process.env.NODE_ENV === 'production') {
process.exit(1);
}

process.env.NODE_ENV = 'development';

// Error.stackTraceLimit = Infinity;
require('trace');
require('clarify');

const bb = require('bluebird');
require('babel-runtime/core-js/promise').default = bb;
global.Promise = bb;

require('./utils/hook');
const attachChangeCallback = require('./utils/watch')();

require('../server')(attachChangeCallback),
require('../tasks')();
18 changes: 1 addition & 17 deletions bin/start-server.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
if (process.env.NODE_ENV === 'development') {
// Error.stackTraceLimit = Infinity;
require('trace');
require('clarify');
}

const bb = require('bluebird');

require('babel-runtime/core-js/promise').default = bb;
global.Promise = bb;

require("babel-register")({
ignore: /\/(public|node_modules)\//
});

const { server } = require('universal-webpack');

const settings = require('../server-uwsettings');
const configuration = require('../webpack.config.server.babel').default;

server(configuration, settings);
require('../public/server/server')();
18 changes: 1 addition & 17 deletions bin/start-tasks.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
if (process.env.NODE_ENV === 'development') {
Error.stackTraceLimit = Infinity;
require('trace');
require('clarify');
}

const bb = require('bluebird');

require('babel-runtime/core-js/promise').default = bb;
global.Promise = bb;

require("babel-register")({
ignore: /\/(public|node_modules)\//
});

const { server } = require('universal-webpack');

const settings = require('../tasks-uwsettings');
const configuration = require('../webpack.config.server.babel').default;

server(configuration, settings);
require('../public/server/tasks')();
31 changes: 31 additions & 0 deletions bin/utils/hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require('fs');
const addHook = require('asset-require-hook/lib/hook');
const assetRequireHook = require('asset-require-hook');
const babelRegisterHook = require("babel-register");

function rawLoader(resourcePath) {
const content = fs.readFileSync(resourcePath, 'utf-8');
return content;
}

// attachHook(rawLoader, '.ejs');
addHook('.ejs', rawLoader);

assetRequireHook({
extensions: ['.png', 'jpg', '.gif', '.svg'],
name: 'assets/images/[name]-[hash].[ext]',
publicPath: '/'
});

assetRequireHook({
extensions: ['.ico'],
name: '[name]-[hash].[ext]',
publicPath: '/'
});

babelRegisterHook({
ignore: /\/(public|node_modules)\//,
plugins: [
'react-hot-loader/babel'
]
});
36 changes: 36 additions & 0 deletions bin/utils/watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const path = require('path');

const watcher = require('chokidar')
.watch([
path.join(__dirname, '../../src/api'),
path.join(__dirname, '../../src/utils')
]);

module.exports = function () {
const callbacks = [];

watcher.on('ready', () => {
watcher.on('all', () => {
// eslint-disable-next-line no-console
console.log('Clearing server module cache from server');
Object.keys(require.cache).forEach(id => {
if (/[/\\]src[/\\](api|utils)/.test(id)) {
delete require.cache[id];
}
});

callbacks.forEach(c => c());
});
});

return callbacks.push.bind(callbacks);
};

/* Optional: "hot-reloading" of client related modules on the server

const compiler = require('webpack')(require('../../res/webpack/client.js'));
compiler.plugin('done', () => {
// Need to separate client and server code to prevent unnecessary reloads
});

*/
54 changes: 25 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@
},
"main": "index.js",
"scripts": {
"start": "run-s build:pre dev:all",
"start:prod": "run-s build:all:prod start:_all:prod",
"build:all:prod": "run-s build:pre build:_all:prod",
"test": "NODE_ENV=development DB_ENV=test run-s reset-db:test mocha",
"coverage": "NODE_ENV=development DB_ENV=test npm run coverage:run",
"travis": "NODE_ENV=development DB_ENV=travis run-s reset-db:travis build:client:dev travis:test lint travis:flow",
"start": "run-s build:client:vendor start-dev",
"start-dev": "node bin/dev-server.js 2>&1 | bunyan",
"prod": "run-s build:all:prod start:all:prod",
"start:all:prod": "NODE_ENV=production run-p start:server start:tasks",
"start:server": "node bin/start-server.js 2>&1 | bunyan",
"start:tasks": "node bin/start-tasks.js",
"build": "run-s cleanup build:client:app build:server build:tasks",
"build:all:prod": "NODE_ENV=production npm run build",
"build:client": "run-s build:client:vendor build:client:app",
"build:client:app": "webpack --config './res/webpack/client.js'",
"build:client:vendor": "webpack --config './res/webpack/client-vendor.js'",
"build:server": "webpack --config './res/webpack/server.js'",
"build:tasks": "webpack --config './res/webpack/tasks.js'",
"test": "DB_ENV=test run-s reset-db:test cleanup build:client mocha",
"coverage": "DB_ENV=test npm run coverage:run",
"travis": "DB_ENV=travis run-s reset-db:travis cleanup build:client travis:test lint travis:flow",
"travis:flow": "run-s flow:install-types flow",
"travis:test": "run-s coverage:run coverage:coveralls coverage:clean",
"reset-db:test": "echo \"Preparing test database...\" && babel-node test-helpers/dropDatabase.js && knex --env test migrate:latest",
Expand All @@ -37,28 +47,7 @@
"update-post-counters": "babel-node bin/postCounters.js",
"gulp:build": "gulp build",
"gulp:watch": "gulp watch",
"build:pre": "run-s cleanup prepare-server-build build:server:dummy-chunks",
"cleanup": "rm -rf public/*",
"prepare-server-build": "universal-webpack --settings ./server-uwsettings.js prepare",
"build:server:dummy-chunks": "ln -s ../webpack-chunks.json public/server/webpack-chunks.json",
"dev:all": "run-p build:_all:dev:watch start:_all:dev",
"start:_all:dev": "run-p start:server:dev start:tasks:dev",
"start:_all:prod": "run-p start:server:prod start:tasks:prod",
"start:server:prod": "DEV=0 NODE_ENV=production node bin/start-server.js 2>&1 | bunyan",
"start:tasks:prod": "DEV=0 NODE_ENV=production node bin/start-tasks.js",
"start:server:dev": "DEV=1 NODE_ENV=development nodemon ./bin/start-server.js --watch ./public/server 2>&1 | bunyan",
"start:tasks:dev": "DEV=1 NODE_ENV=development nodemon ./bin/start-tasks.js --watch ./public/server",
"build:_all:prod": "run-p build:client:prod build:server:prod build:tasks:prod",
"build:_all:dev:watch": "run-p build:client:dev:watch build:server:dev:watch build:tasks:dev:watch",
"build:client:dev": "DEV=1 NODE_ENV=development webpack --config './webpack.config.client.babel.js' --colors --hide-modules --display-error-details",
"build:client:dev:watch": "DEV=1 NODE_ENV=development webpack --config './webpack.config.client.babel.js' --colors --hide-modules --watch",
"build:client:prod": "DEV=0 NODE_ENV=production webpack --config './webpack.config.client.babel.js' --colors --display-error-details",
"build:server:dev": "DEV=1 NODE_ENV=development webpack --config './webpack.config.server.babel.js' --colors --hide-modules --display-error-details",
"build:server:dev:watch": "DEV=1 NODE_ENV=development webpack --config './webpack.config.server.babel.js' --colors --hide-modules --display-error-details --watch",
"build:server:prod": "DEV=0 NODE_ENV=production webpack --config './webpack.config.server.babel.js' --colors --display-error-details",
"build:tasks:dev": "DEV=1 NODE_ENV=development webpack --config './webpack.config.tasks.babel.js' --colors --hide-modules --display-error-details",
"build:tasks:dev:watch": "DEV=1 NODE_ENV=development webpack --config './webpack.config.tasks.babel.js' --colors --hide-modules --display-error-details --watch",
"build:tasks:prod": "DEV=0 NODE_ENV=production webpack --config './webpack.config.tasks.babel.js' --colors --display-error-details"
"cleanup": "rm -rf public/*"
},
"author": {
"name": "Loki Education (Social Enterprise)",
Expand Down Expand Up @@ -162,6 +151,7 @@
},
"devDependencies": {
"adm-zip": "^0.4.7",
"asset-require-hook": "^1.2.0",
"autoprefixer": "~7.1.4",
"babel-core": "~6.26.0",
"babel-eslint": "~8.0.0",
Expand All @@ -184,6 +174,7 @@
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-1": "~6.24.1",
"brfs": "^1.4.3",
"chokidar": "^1.7.0",
"clarify": "^2.0.0",
"cookie": "~0.3.1",
"coveralls": "^2.11.9",
Expand Down Expand Up @@ -211,6 +202,7 @@
"istanbul": "~1.1.0-alpha.1",
"jsdom": "~11.2.0",
"json-loader": "~0.5.7",
"koa-webpack-dev-middleware": "^2.0.2",
"less": "^2.6.0",
"less-loader": "~4.0.3",
"loader-utils": "^1.1.0",
Expand All @@ -225,6 +217,7 @@
"postcss-loader": "~2.0.6",
"raf": "^3.4.0",
"raw-loader": "^0.5.1",
"react-hot-loader": "^3.1.1",
"react-svg-inline-loader": "^0.2.2",
"react-test-renderer": "^16.2.0",
"react-transform-hmr": "^1.0.1",
Expand All @@ -240,9 +233,12 @@
"unexpected-immutable": "~0.2.6",
"unexpected-react": "^5.0.1",
"unexpected-sinon": "~10.8.2",
"universal-webpack": "~0.4.0",
"url-loader": "^0.5.7",
"webpack": "~3.6.0",
"webpack-bundle-analyzer": "^2.9.0",
"webpack-koa-hot-middleware": "^0.1.2",
"webpack-manifest-plugin": "^1.3.2",
"webpack-node-externals": "^1.6.0",
"wikidata-sdk": "~5.2.7",
"zopfli-webpack-plugin": "^0.1.0"
},
Expand Down
90 changes: 90 additions & 0 deletions res/webpack/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
This file is a part of libertysoil.org website
Copyright (C) 2017 Loki Education (Social Enterprise)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const path = require('path');
const merge = require('lodash/merge');
const webpack = require('webpack');

const
NODE_ENV = process.env.NODE_ENV,
__DEV__ = NODE_ENV !== 'production',
context = path.join(__dirname, '../../');

// console.log(NODE_ENV);

const baseConfiguration = {
context,
module: {
noParse: (path) => {
if (/react.*\.production\.min\.js$/.test(path)) {
return false;
}
return /\.min\.js$/.test(path);
}
},
output: {
filename: '[name].js',
publicPath: '/'
},
performance: {
hints: false
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
'process.env.FACEBOOK_AUTH_ENABLED': !!process.env.FACEBOOK_CLIENT_ID,
'process.env.GOOGLE_AUTH_ENABLED': !!process.env.GOOGLE_CLIENT_ID,
'process.env.TWITTER_AUTH_ENABLED': !!process.env.TWITTER_CONSUMER_KEY,
'process.env.GITHUB_AUTH_ENABLED': !!process.env.GITHUB_CLIENT_ID
}),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.LoaderOptionsPlugin({
debug: __DEV__,
minimize: !__DEV__,
options: {
context
}
})
],
stats: {
assets: false,
cached: false,
children: false,
chunks: true,
chunkOrigins: false,
modules: false,
reasons: false,
source: false,
timings: true
}
};

if (__DEV__) {
merge(baseConfiguration, {
cache: true,
performance: {
hints: 'warning'
},
resolve: {
alias: {
'prop-types$': path.join(context, './src/external/prop-types.js')
}
}
});
}

module.exports = baseConfiguration;
Loading