From 41efa76a5b9ba1c9eb7a0dfdcc06ae43501079f8 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 15 Mar 2016 22:42:10 +0000 Subject: [PATCH] API structuring + webpack config --- .babelrc | 2 +- assets/js/src/choices.js | 62 --------------- assets/scripts/dist/bundle.js | 1 + assets/scripts/src/choices.js | 137 ++++++++++++++++++++++++++++++++++ index.html | 2 +- package.json | 44 +++++++++++ server.js | 13 ++++ webpack.config.dev.js | 32 ++++++++ webpack.config.prod.js | 41 ++++++++++ 9 files changed, 270 insertions(+), 64 deletions(-) delete mode 100644 assets/js/src/choices.js create mode 100644 assets/scripts/dist/bundle.js create mode 100644 assets/scripts/src/choices.js create mode 100644 package.json create mode 100644 server.js create mode 100644 webpack.config.dev.js create mode 100644 webpack.config.prod.js diff --git a/.babelrc b/.babelrc index 9b7d435..eaf3238 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,3 @@ { - "presets": ["es2015", "stage-0", "react"] + "presets": ["es2015", "stage-0"] } diff --git a/assets/js/src/choices.js b/assets/js/src/choices.js deleted file mode 100644 index 7f4333c..0000000 --- a/assets/js/src/choices.js +++ /dev/null @@ -1,62 +0,0 @@ -'use strict'; - -class Choices { - constructor() { - const DEFAULT_CONFIG = { - element: '[data-choice]', - disabled: false, - maxItems: 0, - debug: false, - callbackOnInit: function(){}, - callbackOnKeyDown: function(){}, - callbackOnEnter: function(){}, - callbackOnSearch: function(){} - }; - - this.onClick = this.onClick.bind(this); - this.onKeyDown = this.onKeyDown.bind(this); - this.onChange = this.onChange.bind(this); - this.onFocus = this.onFocus.bind(this); - this.onBlur = this.onChange.bind(this); - - this.render(); - } - - onKeyDown() { - console.log('Key down'); - } - - onFocus() { - console.log('Focus'); - } - - onClick() { - console.log('Click'); - } - - onChange() { - console.log('Change'); - } - - addEventListeners() { - document.addEventListener('click', this.onClick); - document.addEventListener('keydown', this.onKeyDown); - document.addEventListener('change', this.onChange); - document.addEventListener('focus', this.onFocus); - document.addEventListener('blur', this.onBlur); - } - - removeEventListeners() { - document.removeEventListener('click', this.onClick); - document.removeEventListener('keydown', this.onKeyDown); - document.removeEventListener('change', this.onChange); - document.removeEventListener('focus', this.onFocus); - document.removeEventListener('blur', this.onBlur); - } - - render(){ - console.log('Render'); - } -} - -new Choices(); \ No newline at end of file diff --git a/assets/scripts/dist/bundle.js b/assets/scripts/dist/bundle.js new file mode 100644 index 0000000..30bc7a3 --- /dev/null +++ b/assets/scripts/dist/bundle.js @@ -0,0 +1 @@ +!function(e){function n(o){if(t[o])return t[o].exports;var i=t[o]={exports:{},id:o,loaded:!1};return e[o].call(i.exports,i,i.exports,n),i.loaded=!0,i.exports}var t={};return n.m=e,n.c=t,n.p="/assets/scripts/dist/",n(0)}([function(e,n,t){e.exports=t(1)},function(e,n,t){function o(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}var i,u=function(){function e(e,n){for(var t=0;t - + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..18f1c7b --- /dev/null +++ b/package.json @@ -0,0 +1,44 @@ +{ + "name": "choices", + "version": "1.0.0", + "description": "A vanilla JS customisable select box plugin", + "main": "index.js", + "scripts": { + "start": "node server.js", + "lint": "eslint ./assets/scripts/src", + "css:watch": "nodemon -e scss -x \"npm run css:build\"", + "css:build": "npm run css:sass -s && npm run css:prefix -s && npm run css:min -s", + "css:sass": "node-sass --include-path scss assets/styles/scss/main.scss assets/styles/css/main.css", + "css:prefix": "postcss --use autoprefixer -b 'last 2 versions' assets/styles/css/*.css -d assets/styles/css/", + "css:min": "csso assets/styles/css/main.css assets/styles/css/main.min.css", + "js:build": "webpack --config webpack.config.prod.js", + "js:test": "./node_modules/karma/bin/karma start --single-run --no-auto-watch tests/karma.config.js", + "js:test_watch": "./node_modules/karma/bin/karma start --auto-watch --no-single-run tests/karma.config.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/jshjohnson/Choices.git" + }, + "author": "Josh Johnson", + "license": "MIT", + "bugs": { + "url": "https://github.com/jshjohnson/Choices/issues" + }, + "homepage": "https://github.com/jshjohnson/Choices#readme", + "devDependencies": { + "autoprefixer": "^6.3.3", + "babel-core": "^6.7.2", + "babel-eslint": "^5.0.0", + "babel-loader": "^6.2.4", + "babel-preset-es2015": "^6.6.0", + "babel-preset-stage-0": "^6.5.0", + "csso": "^1.7.0", + "eslint": "^2.4.0", + "node-sass": "^3.4.2", + "nodemon": "^1.9.1", + "opn-cli": "^3.1.0", + "postcss-cli": "^2.5.1", + "webpack": "^1.12.14", + "webpack-dev-server": "^1.14.1" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..58e0828 --- /dev/null +++ b/server.js @@ -0,0 +1,13 @@ +var webpack = require('webpack'); +var WebpackDevServer = require('webpack-dev-server'); +var config = require('./webpack.config.dev'); +var opn = require('opn'); + +new WebpackDevServer(webpack(config), { + publicPath: config.output.publicPath, + historyApiFallback: true, +}).listen(3000, 'localhost', function (err, result) { + if (err) console.log(err); + opn('http://localhost:3000'); + console.log('Listening at localhost:3000'); +}); diff --git a/webpack.config.dev.js b/webpack.config.dev.js new file mode 100644 index 0000000..9b713a5 --- /dev/null +++ b/webpack.config.dev.js @@ -0,0 +1,32 @@ +var path = require('path'); +var webpack = require('webpack'); + +module.exports = { + devtool: 'eval', + entry: [ + 'webpack-dev-server/client?http://localhost:3000', + './assets/scripts/src/choices' + ], + output: { + path: path.join(__dirname, 'dist'), + filename: 'bundle.js', + publicPath: '/assets/scripts/dist/' + }, + plugins: [ + new webpack.HotModuleReplacementPlugin(), + new webpack.NoErrorsPlugin(), + new webpack.DefinePlugin({ + 'process.env': { + 'NODE_ENV': JSON.stringify('development') + } + }) + ], + module: { + loaders: [{ + test: /\.js$/, + exclude: /(node_modules|bower_components)/, + loaders: ['babel'], + include: path.join(__dirname, 'assets/scripts/src') + }] + } +}; \ No newline at end of file diff --git a/webpack.config.prod.js b/webpack.config.prod.js new file mode 100644 index 0000000..fdb9e19 --- /dev/null +++ b/webpack.config.prod.js @@ -0,0 +1,41 @@ +var path = require('path'); +var webpack = require('webpack'); + +module.exports = { + devtool: 'cheap-module-source-map', + entry: [ + './assets/scripts/src/choices' + ], + output: { + path: path.join(__dirname, '/assets/scripts/dist'), + filename: 'bundle.js', + publicPath: '/assets/scripts/dist/' + }, + plugins: [ + new webpack.optimize.UglifyJsPlugin({ + sourceMap: false, + mangle: true, + output: { + comments: false + }, + compress: { + warnings: false, + screw_ie8: true + } + }), + new webpack.DefinePlugin({ + 'process.env': { + // This has effect on the react lib size + 'NODE_ENV': JSON.stringify('production'), + } + }), + ], + module: { + loaders: [{ + test: /\.js$/, + exclude: /(node_modules|bower_components)/, + loaders: ['babel'], + include: path.join(__dirname, 'assets/scripts/src') + }] + } +}; \ No newline at end of file