Skip to content

Commit

Permalink
remove babel-polyfill + add breaking change to entry file (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Donmclean committed Oct 19, 2017
1 parent 1269f18 commit 2194f5b
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 231 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,16 @@ SERVER_PORT: 3000;

##### WEBPACK REQUIRED SETTINGS
```javascript
entry: {
index: [ './src/js/index.js' ]
}
setEntry: (entryObject, mainEntryList, immutable) => {
//mainEntryList contains all hot-reloading paths already
//simply push your main entry on this list.

mainEntryList.push('./src/js/index.js');

entryObject.set('index', mainEntryList);

return entryObject;
},

output: {
path: path.resolve(cwd, 'dist')
Expand Down
8 changes: 6 additions & 2 deletions bin/_setup/react/src/rikoconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ const cwd = process.cwd();
const config = {
SERVER_PORT: 3000,

entry: {
index: [ './src/js/index.js' ]
setEntry: (entryObject, mainEntryList, immutable) => {
mainEntryList.push('./src/js/index.js');

entryObject.set('index', mainEntryList);

return entryObject;
},

output: {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"babel-plugin-react-transform": "^2.0.2",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-runtime": "^6.12.0",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015": "^6.14.0",
"babel-preset-react": "^6.11.1",
"babel-register": "^6.11.6",
Expand Down
281 changes: 136 additions & 145 deletions src/actions/run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { processExitHandler, assignEnvironmentVariablesBasedOnRunCommand, doRunCommandValidations, logElectronRunServerError,
removeDir, genericLog, getStats, onDevBuildActions, checkForNewPackageVersion } from '../utils/functions';
removeDir, genericLog, getStats, onDevBuildActions, checkForNewPackageVersion, setEntryHelper } from '../utils/functions';
import { cwd, baseDir } from '../utils/variables';
import { getElectronPackagerOptions } from '../utils/webpackConfigUtils';
import qfs from 'q-io/fs';
Expand All @@ -10,7 +10,7 @@ import { runCommands } from '../constants/index';
import spawn from 'cross-spawn';
const spawnSync = spawn.sync;

export default (runCommand) => {
export default async (runCommand) => {
//add exit handler
processExitHandler();

Expand All @@ -29,168 +29,159 @@ export default (runCommand) => {
modulesToImport.push(import('../webpack.config.babel').then((m) => m.default));
}

Promise.all(modulesToImport).then((modules) => {
const [ customConfig, config = {} ] = modules;
const [ customConfig, config = {} ] = await Promise.all(modulesToImport).catch((err) => console.error('err > ', err));

//TODO: validate customConfig Here
//TODO: extract these dependencies
//TODO: validate customConfig Here
//TODO: extract these dependencies

switch (runCommand) {
case 'electron-server': {
qfs.list(customConfig.output.path).then((files) => {
switch (os.platform()) {
case 'darwin': {
//handles open electron app on MAC
const macFile = find(files, (file) => file.match(/\b(darwin)\b/i));
switch (runCommand) {
case 'electron-server': {
qfs.list(customConfig.output.path).then((files) => {
switch (os.platform()) {
case 'darwin': {
//handles open electron app on MAC
const macFile = find(files, (file) => file.match(/\b(darwin)\b/i));

if(macFile) {
spawn('open', [`-a`, `${cwd}/${path.basename(customConfig.output.path)}/${macFile}/${customConfig.electronPackagerOptions.name}.app`], {stdio: 'inherit'});
} else {
logElectronRunServerError();
}

break;
}
default: {
break;
if(macFile) {
spawn('open', [`-a`, `${cwd}/${path.basename(customConfig.output.path)}/${macFile}/${customConfig.electronPackagerOptions.name}.app`], {stdio: 'inherit'});
} else {
logElectronRunServerError();
}

break;
}
})
.catch((err) => {
logElectronRunServerError();
console.error('ERROR >', err);
});
break;
}
case 'react-prod': {
config.entry['index'].unshift('babel-polyfill');
default: {
break;
}
}
})
.catch((err) => {
logElectronRunServerError();
console.error('ERROR >', err);
});
break;
}
case 'react-prod': {
//TODO: validate command below
return spawn(`${customConfig.baseDir}/node_modules/.bin/webpack`, [`--config`, `${path.resolve(__dirname, '../webpack.config.babel.js')}`], {stdio: 'inherit'});
}
case 'electron-prod': {
genericLog('Compiling electron app..');

//TODO: validate command below
return spawn(`${customConfig.baseDir}/node_modules/.bin/webpack`, [`--config`, `${path.resolve(__dirname, '../webpack.config.babel.js')}`], {stdio: 'inherit'});
}
case 'electron-prod': {
genericLog('Compiling electron app..');

if(JSON.parse(process.env.isElectron)) {
// const electronPackager = require('electron-packager'); //TODO: use dynamic import here
return import('electron-packager')
.then((electronPackager) => {
const spawned = spawn(`${customConfig.baseDir}/node_modules/.bin/webpack`, [`--config`, `${path.resolve(__dirname, '../webpack.config.babel.js')}`], {stdio: 'inherit'});

spawned.on('close', () => {
//Compile The Electron Application
const electronPackagerOptions = getElectronPackagerOptions();
electronPackager(electronPackagerOptions, (err) => {
if(err) {
console.error('ERROR > in electron build', err);
throw err;
}

removeDir(customConfig.tempDir).then(() => {
genericLog(`${electronPackagerOptions.name} built successfully!`);
});
});
});

return spawn;
if(JSON.parse(process.env.isElectron)) {
const electronPackager = await import('electron-packager');

const spawned = spawn(`${customConfig.baseDir}/node_modules/.bin/webpack`, [`--config`, `${path.resolve(__dirname, '../webpack.config.babel.js')}`], {stdio: 'inherit'});

spawned.on('close', () => {
//Compile The Electron Application
const electronPackagerOptions = getElectronPackagerOptions();
electronPackager(electronPackagerOptions, (err) => {
if(err) {
console.error('ERROR > in electron build', err);
throw err;
}

removeDir(customConfig.tempDir).then(() => {
genericLog(`${electronPackagerOptions.name} built successfully!`);
});
}
break;
}
case 'react-native-ios': {
return spawn('react-native', ['run-ios'], {stdio: 'inherit'});
}
case 'react-native-launch-android': {
genericLog('launching android emulator...', 'blue');
return spawn(`sh`, [`${cwd}/launch-android-Emulator.sh`, '&'], {stdio: 'ignore'});
}
case 'react-native-android': {
return spawnSync('react-native', ['run-android'], {stdio: 'inherit'});
}
case 'electron-dev':
case 'react-dev': {
const stats = getStats('development');
const webpack = require('webpack');
const webpackDevServer = require('webpack-dev-server');

//*******************************************************************
//HOT RELOADING WITH WEBPACK DEV SERVER
//*******************************************************************

config.entry['index'].unshift('webpack/hot/dev-server');
config.entry['index'].unshift(`webpack-dev-server/client?http://localhost:${customConfig.SERVER_PORT}`);
config.entry['index'].unshift('react-hot-loader/patch');
config.entry['index'].unshift('babel-polyfill');

const { overlay } = customConfig.hotReloadingOptions;

new webpackDevServer(webpack(config), {
contentBase: config.output.path,
publicPath: config.output.publicPath,
historyApiFallback: true,
hot: true,
overlay,
inline: true,
headers: { 'Access-Control-Allow-Origin': '*' },
stats
}).listen(customConfig.SERVER_PORT, 'localhost', (err) => {
if (err) { console.error(err); }
genericLog(`Listening at localhost: ${customConfig.SERVER_PORT}`);
onDevBuildActions(customConfig);
});
});

break;
}
case 'node-server-dev': {
spawn(`${baseDir}/node_modules/.bin/nodemon`, ['--config', `${customConfig.nodemonJson}`, `${customConfig.entryFile}`], {stdio: 'inherit'});
break;
}
case 'node-server-prod': {
spawn('node', [`${customConfig.entryFile}`], {stdio: 'inherit'});
break;
return new Promise((resolve) => resolve(spawned));
}
case 'react-server':
case 'react-prod-server': {
//TODO: use dynamic import
const browserSync = require('browser-sync');
const morgan = require('morgan');
const fallback = require('express-history-api-fallback');
const express = require('express');
const app = require('express')();

app.use(morgan('dev'));
break;
}
case 'react-native-ios': {
return spawn('react-native', ['run-ios'], {stdio: 'inherit'});
}
case 'react-native-launch-android': {
genericLog('launching android emulator...', 'blue');
return spawn(`sh`, [`${cwd}/launch-android-Emulator.sh`, '&'], {stdio: 'ignore'});
}
case 'react-native-android': {
return spawnSync('react-native', ['run-android'], {stdio: 'inherit'});
}
case 'electron-dev':
case 'react-dev': {
const stats = getStats('development');
const webpack = require('webpack');
const webpackDevServer = require('webpack-dev-server');

//*******************************************************************
//HOT RELOADING WITH WEBPACK DEV SERVER
//*******************************************************************

config.entry = setEntryHelper(customConfig);

const { overlay } = customConfig.hotReloadingOptions;

new webpackDevServer(webpack(config), {
contentBase: config.output.path,
publicPath: config.output.publicPath,
historyApiFallback: true,
hot: true,
overlay,
inline: true,
headers: { 'Access-Control-Allow-Origin': '*' },
stats
}).listen(customConfig.SERVER_PORT, 'localhost', (err) => {
if (err) { console.error(err); }
genericLog(`Listening at localhost: ${customConfig.SERVER_PORT}`);
onDevBuildActions(customConfig);
});

break;
}
case 'node-server-dev': {
spawn(`${baseDir}/node_modules/.bin/nodemon`, ['--config', `${customConfig.nodemonJson}`, `${customConfig.entryFile}`], {stdio: 'inherit'});
break;
}
case 'node-server-prod': {
spawn('node', [`${customConfig.entryFile}`], {stdio: 'inherit'});
break;
}
case 'react-server':
case 'react-prod-server': {
//TODO: use dynamic import
const browserSync = require('browser-sync');
const morgan = require('morgan');
const fallback = require('express-history-api-fallback');
const express = require('express');
const app = require('express')();

const root = config.output.path;
app.use(morgan('dev'));

app.use(express.static(root));
app.use(fallback('index.html', { root }));
const root = config.output.path;

const portToServe = process.argv[4] || customConfig.SERVER_PORT;
app.use(express.static(root));
app.use(fallback('index.html', { root }));

app.listen(portToServe);
const portToServe = process.argv[4] || customConfig.SERVER_PORT;

app.use((err, req, res, next) => {
genericLog(`ERROR --> : ${err.stack}`);
next(err);
});
app.listen(portToServe);

const isProdServer = (runCommand === 'react-prod-server');
app.use((err, req, res, next) => {
genericLog(`ERROR --> : ${err.stack}`);
next(err);
});

if(isProdServer) {
genericLog('Listening on port: ' + portToServe, 'yellow');
} else {
genericLog('Launching Browser Sync proxy of port: ' + portToServe, 'yellow');
const isProdServer = (runCommand === 'react-prod-server');

browserSync.init({
proxy: `localhost:${portToServe}`
});
}
if(isProdServer) {
genericLog('Listening on port: ' + portToServe, 'yellow');
} else {
genericLog('Launching Browser Sync proxy of port: ' + portToServe, 'yellow');

break;
}
default: {
break;
browserSync.init({
proxy: `localhost:${portToServe}`
});
}

break;
}
});
default: {
break;
}
}
};
2 changes: 1 addition & 1 deletion src/utils/coreRikoConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ genericLog('validating rikoconfig.js file..');

const requiredFields = {
SERVER_PORT: 'number',
entry: 'object',
setEntry: 'function',
output: 'object'
};

Expand Down
8 changes: 6 additions & 2 deletions src/utils/defaultConfigs/reactElectronConfig.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export default {
SERVER_PORT: 3000,

entry: {
index: [ './src/js/index.js' ]
setEntry: (newEntryObject, mainEntryList, immutable) => {
mainEntryList.push('./src/js/index.js');

newEntryObject.set('index', mainEntryList);

return newEntryObject;
},

output: {
Expand Down
Loading

0 comments on commit 2194f5b

Please sign in to comment.