Webpack refactor

- added JSX support (webpack + robofile
- added public.js bundle
- refactored webpack config for clarity
- added settings.jsx form example
- fixed acceptance test failing on login
This commit is contained in:
Jonathan Labreuille
2015-08-19 19:29:11 +02:00
parent b8f0ec4e88
commit bc65836c31
7 changed files with 330 additions and 126 deletions

View File

@@ -9,7 +9,9 @@ class RoboFile extends \Robo\Tasks {
private $js_files = array(
'assets/js/src/*.js',
'assets/js/src/**/*.js'
'assets/js/src/*.jsx',
'assets/js/src/**/*.js',
'assets/js/src/**/*.jsx'
);
function install() {

View File

@@ -1,12 +1,13 @@
define('admin', [
'mailpoet',
'jquery',
'handlebars',
], function(MailPoet, jQuery, Handlebars) {
'jquery'
],
function(MailPoet, jQuery) {
jQuery(function($) {
// dom ready
$(function() {
});
});
});
}
);

View File

@@ -1,6 +1,8 @@
jQuery(function($) {
define('public', ['jquery'], function(jQuery) {
jQuery(function($) {
// dom ready
$(function() {
});
});
});

203
assets/js/src/settings.jsx Normal file
View File

@@ -0,0 +1,203 @@
define('settings', ['react'], function(React) {
var STATES = [
'AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI',
'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS',
'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR',
'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'
]
var Example = React.createClass({
getInitialState: function() {
return {
email: true
, question: true
, submitted: null
}
},
render: function() {
var submitted
if (this.state.submitted !== null) {
submitted = <div className="alert alert-success">
<p>ContactForm data:</p>
<pre><code>{JSON.stringify(this.state.submitted, null, ' ')}</code></pre>
</div>
}
return <div>
<div className="panel panel-default">
<div className="panel-heading clearfix">
<h3 className="panel-title pull-left">Contact Form</h3>
<div className="pull-right">
<label className="checkbox-inline">
<input type="checkbox"
checked={this.state.email}
onChange={this.handleChange.bind(this, 'email')}
/> Email
</label>
<label className="checkbox-inline">
<input type="checkbox"
checked={this.state.question}
onChange={this.handleChange.bind(this, 'question')}
/> Question
</label>
</div>
</div>
<div className="panel-body">
<ContactForm ref="contactForm"
email={this.state.email}
question={this.state.question}
company={this.props.company}
/>
</div>
<div className="panel-footer">
<button type="button" className="btn btn-primary btn-block" onClick={this.handleSubmit}>Submit</button>
</div>
</div>
{submitted}
</div>
},
handleChange: function(field, e) {
var nextState = {}
nextState[field] = e.target.checked
this.setState(nextState)
},
handleSubmit: function() {
if (this.refs.contactForm.isValid()) {
this.setState({submitted: this.refs.contactForm.getFormData()})
}
}
})
/**
* A contact form with certain optional fields.
*/
var ContactForm = React.createClass({
getDefaultProps: function() {
return {
email: true
, question: false
}
},
getInitialState: function() {
return {errors: {}}
},
isValid: function() {
var fields = ['firstName', 'lastName', 'phoneNumber', 'address', 'city', 'state', 'zipCode']
if (this.props.email) fields.push('email')
if (this.props.question) fields.push('question')
var errors = {}
fields.forEach(function(field) {
var value = trim(this.refs[field].getDOMNode().value)
if (!value) {
errors[field] = 'This field is required'
}
}.bind(this))
this.setState({errors: errors})
var isValid = true
for (var error in errors) {
isValid = false
break
}
return isValid
},
getFormData: function() {
var data = {
firstName: this.refs.firstName.getDOMNode().value
, lastName: this.refs.lastName.getDOMNode().value
, phoneNumber: this.refs.phoneNumber.getDOMNode().value
, address: this.refs.address.getDOMNode().value
, city: this.refs.city.getDOMNode().value
, state: this.refs.state.getDOMNode().value
, zipCode: this.refs.zipCode.getDOMNode().value
, currentCustomer: this.refs.currentCustomerYes.getDOMNode().checked
}
if (this.props.email) data.email = this.refs.email.getDOMNode().value
if (this.props.question) data.question = this.refs.question.getDOMNode().value
return data
},
render: function() {
return <div className="form-horizontal">
{this.renderTextInput('firstName', 'First Name')}
{this.renderTextInput('lastName', 'Last Name')}
{this.renderTextInput('phoneNumber', 'Phone number')}
{this.props.email && this.renderTextInput('email', 'Email')}
{this.props.question && this.renderTextarea('question', 'Question')}
{this.renderTextInput('address', 'Address')}
{this.renderTextInput('city', 'City')}
{this.renderSelect('state', 'State', STATES)}
{this.renderTextInput('zipCode', 'Zip Code')}
{this.renderRadioInlines('currentCustomer', 'Are you currently a ' + this.props.company + ' Customer?', {
values: ['Yes', 'No']
, defaultCheckedValue: 'No'
})}
</div>
},
renderTextInput: function(id, label) {
return this.renderField(id, label,
<input type="text" className="form-control" id={id} ref={id}/>
)
},
renderTextarea: function(id, label) {
return this.renderField(id, label,
<textarea className="form-control" id={id} ref={id}/>
)
},
renderSelect: function(id, label, values) {
var options = values.map(function(value) {
return <option value={value}>{value}</option>
})
return this.renderField(id, label,
<select className="form-control" id={id} ref={id}>
{options}
</select>
)
},
renderRadioInlines: function(id, label, kwargs) {
var radios = kwargs.values.map(function(value) {
var defaultChecked = (value == kwargs.defaultCheckedValue)
return <label className="radio-inline">
<input type="radio" ref={id + value} name={id} value={value} defaultChecked={defaultChecked}/>
{value}
</label>
})
return this.renderField(id, label, radios)
},
renderField: function(id, label, field) {
return <div className={$c('form-group', {'has-error': id in this.state.errors})}>
<label htmlFor={id} className="col-sm-4 control-label">{label}</label>
<div className="col-sm-6">
{field}
</div>
</div>
}
})
React.render(<Example company="FakeCo"/>, document.getElementById('mailpoet_settings'))
// Utils
var trim = function() {
var TRIM_RE = /^\s+|\s+$/g
return function trim(string) {
return string.replace(TRIM_RE, '')
}
}()
function $c(staticClassName, conditionalClassNames) {
var classNames = []
if (typeof conditionalClassNames == 'undefined') {
conditionalClassNames = staticClassName
}
else {
classNames.push(staticClassName)
}
for (var className in conditionalClassNames) {
if (!!conditionalClassNames[className]) {
classNames.push(className)
}
}
return classNames.join(' ')
}
});

View File

@@ -5,7 +5,6 @@
},
"napa": {
"sticky-kit": "leafo/sticky-kit.git",
"jquery-validation-engine": "posabsolute/jQuery-Validation-Engine.git",
"interact.js": "taye/interact.js.git"
},
"dependencies": {
@@ -19,14 +18,17 @@
"handlebars": "3.0.3",
"html2canvas": "latest",
"moment": "^2.10.3",
"napa": "^1.2.0",
"papaparse": "4.1.1",
"react": "^0.13.3",
"select2": "3.5.1",
"spectrum-colorpicker": "^1.6.2",
"tinymce": "4.1.10",
"underscore": "1.8.3",
"napa": "^1.2.0"
"underscore": "1.8.3"
},
"devDependencies": {
"babel-core": "^5.8.22",
"babel-loader": "^5.3.2",
"chai": "2.2.0",
"chai-jq": "0.0.8",
"grunt": "^0.4.5",

View File

@@ -7,8 +7,9 @@
class_name: AcceptanceTester
modules:
enabled:
- \Helper\Acceptance
- WebDriver:
url: 'http://localhost'
browser: phantomjs
restart: true
- \Helper\Acceptance
capabilities:
webStorageEnabled: true

View File

@@ -1,65 +1,62 @@
var path = require('path'),
fs = require('fs'),
webpack = require("webpack"),
var webpack = require('webpack'),
_ = require('underscore'),
baseConfig;
baseConfig = {},
config = [];
baseConfig = {
name: 'main',
context: __dirname ,
entry: {
vendor: ['handlebars', 'handlebars_helpers'],
mailpoet: ['mailpoet', 'ajax', 'modal', 'notice'],
admin: 'admin.js',
},
context: __dirname,
output: {
path: './assets/js',
filename: '[name].js',
},
plugins: [
new webpack.optimize.CommonsChunkPlugin(/* chunkName= */"vendor", /* filename= */"vendor.js")
],
loaders: [
{
test: /\.js$/i,
loader: 'js'
},
{
test: /\.css$/i,
loader: 'css'
},
{
test: /\.jpe?g$|\.gif$|\.png$|\.svg$|\.woff$|\.ttf$|\.wav$|\.mp3$/i,
loader: 'file'
}
],
resolve: {
modulesDirectories: [
'node_modules',
'assets/js/src',
'assets/css/lib'
],
fallback: path.join(__dirname, 'node_modules'),
alias: {
'handlebars': 'handlebars/dist/handlebars.js'
}
'assets/js/src'
]
},
resolveLoader: {
fallback: path.join(__dirname, 'node_modules'),
alias: {
'hbs': 'handlebars-loader'
}
node: {
fs: 'empty'
},
externals: {
'jquery': 'jQuery',
module: {
loaders: [
{
test: /\.jsx$/,
loader: 'babel-loader'
}
]
}
};
module.exports = [
baseConfig,
// Admin
config.push(_.extend({}, baseConfig, {
name: 'admin',
entry: {
vendor: ['handlebars', 'handlebars_helpers'],
mailpoet: ['mailpoet', 'ajax', 'modal', 'notice'],
admin: ['settings.jsx']
},
plugins: [
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js'),
],
externals: {
'jquery': 'jQuery'
}
}));
// Configuration specific for testing
_.extend({}, baseConfig, {
// Public
config.push(_.extend({}, baseConfig, {
name: 'public',
entry: {
public: 'public.js'
},
externals: {
'jquery': 'jQuery'
}
}));
// Test
config.push(_.extend({}, baseConfig, {
name: 'test',
entry: {
testAjax: 'testAjax.js',
@@ -73,12 +70,8 @@ module.exports = [
'node_modules',
'assets/js/src',
'tests/javascript/newsletter_editor'
],
fallback: path.join(__dirname, 'node_modules'),
alias: {
'handlebars': 'handlebars/dist/handlebars.js'
]
}
},
plugins: [],
})
];
}));
module.exports = config;