Skip to content

Commit 0286b98

Browse files
committed
replace brunch with webpack
1 parent 32342b9 commit 0286b98

File tree

6 files changed

+68
-30
lines changed

6 files changed

+68
-30
lines changed

config/dev.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ config :todo, Todo.Endpoint,
1111
debug_errors: true,
1212
code_reloader: true,
1313
check_origin: false,
14-
watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
14+
watchers: [npm: ["run", "watch",
1515
cd: Path.expand("../", __DIR__)], mix: ["elixirscript.watch"]]
1616

1717

lib/todo/store.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
defmodule Todo.Store do
22

33
def start_link() do
4-
Agent.start_link(fn -> HashDict.new end, name: __MODULE__)
4+
Agent.start_link(fn -> Map.new end, name: __MODULE__)
55
end
66

77
def list() do
88
Agent.get(__MODULE__, fn(state) ->
9-
Enum.map(Dict.to_list(state), fn({key, value}) ->
9+
Enum.map(Map.to_list(state), fn({key, value}) ->
1010
value
1111
end)
1212
end)
1313
end
1414

1515
def add(title) do
1616
Agent.update(__MODULE__, fn(state) ->
17-
Dict.put(state, Set.size(state), %Todo.Models.Todo{ title: title, completed: false, id: Set.size(state) })
17+
Map.put(state, Set.size(state), %Todo.Models.Todo{ title: title, completed: false, id: Set.size(state) })
1818
end)
1919
end
2020

2121
def update(id, completed) do
2222
Agent.update(__MODULE__, fn(state) ->
23-
if Dict.has_key?(state, id) do
24-
updated_todo = %{ Dict.get(state, id) | completed: completed }
25-
Dict.put(state, id, updated_todo)
23+
if Map.has_key?(state, id) do
24+
updated_todo = %{ Map.get(state, id) | completed: completed }
25+
Map.put(state, id, updated_todo)
2626
else
2727
state
2828
end
@@ -31,8 +31,8 @@ defmodule Todo.Store do
3131

3232
def remove(id) do
3333
Agent.update(__MODULE__, fn(state) ->
34-
if Dict.has_key?(state, id) do
35-
Dict.delete(state, id)
34+
if Map.has_key?(state, id) do
35+
Map.delete(state, id)
3636
else
3737
state
3838
end

package.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
"repository": {},
33
"license": "MIT",
44
"scripts": {
5-
"deploy": "brunch build --production",
6-
"watch": "brunch watch --stdin"
5+
"deploy": "webpack -p",
6+
"watch": "webpack --watch-stdin --color"
77
},
88
"dependencies": {
99
"phoenix": "file:deps/phoenix",
1010
"phoenix_html": "file:deps/phoenix_html"
1111
},
1212
"devDependencies": {
13-
"babel-brunch": "~6.0.0",
14-
"brunch": "2.10",
15-
"clean-css-brunch": "~2.0.0",
16-
"css-brunch": "~2.0.0",
17-
"javascript-brunch": "~2.0.0",
18-
"uglify-js-brunch": "~2.0.1",
13+
"babel-core": "6.21.0 ",
1914
"babel-preset-env": "1.1.4",
2015
"react": "^15.4.0",
21-
"react-dom": "^15.4.0"
16+
"react-dom": "^15.4.0",
17+
"babel-loader": "^6.2.10",
18+
"css-loader": "^0.26.1",
19+
"raw-loader": "^0.5.1",
20+
"style-loader": "^0.13.1",
21+
"webpack": "^2.2.0",
22+
"copy-webpack-plugin": "^4.0.1",
23+
"extract-text-webpack-plugin": "^2.0.0-beta.4"
2224
}
2325
}

web/static/exjs/data.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ defmodule Todo.Data do
22
def list() do
33
:window.fetch("/api/todo").then(fn(response) ->
44
response.json().then(fn(todos) ->
5-
Main.update(todos)
5+
todos
6+
|> Enum.map(fn(x) -> %Todo.Models.Todo{ id: x.id, completed: x.completed, title: x.title } end)
7+
|> Main.update()
68
end)
79
end).catch(fn(err) ->
810
:console.debug(err)

web/static/exjs/main.ex

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ defmodule Main do
22
require JS
33
use ReactUI
44

5-
defp process_event(%{"which" => 13} = event) do
6-
Todo.Data.add(event.target.value)
7-
JS.update(event.target, object(value: ""))
8-
end
9-
105
defp process_event(%{} = event) do
11-
:console.debug(event)
6+
if event.which == 13 do
7+
Todo.Data.add(event.target.value)
8+
JS.update(event.target, object(value: ""))
9+
else
10+
:console.debug(event)
11+
end
1212
end
1313

1414
def initial_state() do
@@ -34,7 +34,7 @@ defmodule Main do
3434
id: "new-todo",
3535
placeholder: "What needs to be done?",
3636
autoFocus: true,
37-
onKeyPress: fn(event) -> process_event(event) end
37+
onKeyPress: fn(event, _) -> process_event(event) end
3838
]
3939
end
4040
section id: "main" do
@@ -46,21 +46,21 @@ defmodule Main do
4646
{"", ""}
4747
end
4848

49-
li data_id: todo.id, className: the_completed do
49+
li key: todo.id, className: the_completed do
5050
ReactUI.div className: "view" do
5151
input [
5252
className: "toggle",
5353
type: "checkbox",
5454
checked: todo.completed,
55-
onChange: fn(event) -> Todo.Data.update(event.target.parentElement.parentElement.data_id, event.target.checked) end
55+
onChange: fn(event) -> Todo.Data.update(todo.id, event.target.checked) end
5656
]
5757
label do
5858
todo.title
5959
end
6060
button [
6161
className: "destroy",
62-
onClick: fn(event) ->
63-
Todo.Data.remove(event.target.parentElement.parentElement.data_id)
62+
onClick: fn(event, _) ->
63+
Todo.Data.remove(todo.id)
6464
end
6565
]
6666
end

webpack.config.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const path = require('path');
2+
const webpack = require('webpack');
3+
const CopyWebpackPlugin = require('copy-webpack-plugin');
4+
const ExtractTextPlugin = require('extract-text-webpack-plugin');
5+
6+
module.exports = {
7+
devtool: 'source-map',
8+
cache: true,
9+
entry: [ './web/static/js/app.js', './web/static/css/app.css' ],
10+
output: {
11+
path: path.join(__dirname, 'priv', 'static'),
12+
filename: 'js/app.js',
13+
},
14+
module: {
15+
rules: [
16+
{
17+
test: /\.jsx?$/,
18+
exclude: /(node_modules|bower_components)/,
19+
use: 'babel-loader',
20+
},
21+
{
22+
test: [ /\.css$/ ],
23+
loader: ExtractTextPlugin.extract({
24+
fallbackLoader: 'style-loader',
25+
loader: 'css-loader',
26+
}),
27+
},
28+
],
29+
},
30+
plugins: [
31+
new CopyWebpackPlugin([ { from: './web/static/assets' } ]),
32+
new ExtractTextPlugin('css/app.css'),
33+
],
34+
};

0 commit comments

Comments
 (0)