Skip to content

Commit

Permalink
Use Phoenix and Phoenix.HTML through npm packages
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Jan 7, 2016
1 parent d9df9b8 commit 5d00300
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 168 deletions.
13 changes: 4 additions & 9 deletions brunch-config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
exports.config = {
// See http://brunch.io/#documentation for docs.
sourceMaps: false,
production: true,

modules: {
// use common js wrapper, but expose global `Phoenix` object for browser
// truncate module path simple to "phoenix"
wrapper: function(path, data){
return(
"require.define({'phoenix': function(exports, require, module){ " + data + " }});\n" +
"if(typeof(window) === 'object' && !window.Phoenix){ window.Phoenix = require('phoenix') };"
)
}
wrapper: false,
definition: false
},

files: {
javascripts: {
joinTo: 'phoenix.js'
Expand Down
42 changes: 16 additions & 26 deletions installer/lib/phoenix_new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Mix.Tasks.Phoenix.New do
@brunch [
{:text, "static/brunch/.gitignore", ".gitignore"},
{:eex, "static/brunch/brunch-config.js", "brunch-config.js"},
{:text, "static/brunch/package.json", "package.json"},
{:eex, "static/brunch/package.json", "package.json"},
{:text, "static/app.css", "web/static/css/app.css"},
{:eex, "static/brunch/app.js", "web/static/js/app.js"},
{:eex, "static/brunch/socket.js", "web/static/js/socket.js"},
Expand Down Expand Up @@ -175,9 +175,7 @@ defmodule Mix.Tasks.Phoenix.New do
{adapter_app, adapter_module, adapter_config} = get_ecto_adapter(db, String.downcase(app), mod)
pubsub_server = get_pubsub_server(mod)
in_umbrella? = in_umbrella?(path)

{brunch_deps_prefix, static_deps_prefix} =
if in_umbrella?, do: {"../../", "../../../"}, else: {"", ""}
brunch_deps_prefix = if in_umbrella?, do: "../../", else: ""

binary_id = Keyword.get(opts, :binary_id, false)
adapter_config = Keyword.put_new(adapter_config, :binary_id, binary_id)
Expand All @@ -193,7 +191,6 @@ defmodule Mix.Tasks.Phoenix.New do
signing_salt: random_string(8),
in_umbrella: in_umbrella?,
brunch_deps_prefix: brunch_deps_prefix,
static_deps_prefix: static_deps_prefix,
brunch: brunch,
ecto: ecto,
html: html,
Expand All @@ -214,26 +211,18 @@ defmodule Mix.Tasks.Phoenix.New do
install? = Mix.shell.yes?("\nFetch and install dependencies?")

File.cd!(path, fn ->
brunch =
case install_brunch(install?) do
{:ok, task} -> task
:not_required -> nil
:not_allowed -> print_brunch_info()
end

{mix, extra} =
case install_mix(install?) do
{:ok, task} -> {task, []}
:not_allowed -> {nil, ["$ mix deps.get"]}
end

brunch && Task.await(brunch, :infinity)
mix && Task.await(mix, :infinity)
mix? = install_mix(install?)
brunch? = install_brunch(install?)
extra = if mix?, do: [], else: ["$ mix deps.get"]

print_mix_info(path, extra)

if binding[:ecto] do
print_ecto_info
print_ecto_info()
end

if not brunch? do
print_brunch_info()
end
end)
end
Expand Down Expand Up @@ -351,23 +340,24 @@ defmodule Mix.Tasks.Phoenix.New do
defp maybe_cmd(cmd, should_run?, can_run?) do
cond do
should_run? && can_run? ->
{:ok, cmd(cmd)}
cmd(cmd)
true
should_run? ->
:not_allowed
false
true ->
:not_required
true
end
end

defp cmd(cmd) do
Mix.shell.info [:green, "* running ", :reset, cmd]
Task.async(fn ->

# We use :os.cmd/1 because there is a bug in OTP
# where we cannot execute .cmd files on Windows.
# We could use Mix.shell.cmd/1 but that automatically
# outputs to the terminal and we don't want that.
:os.cmd(String.to_char_list(cmd))
end)

end

defp check_application_name!(name, from_app_flag) do
Expand Down
2 changes: 1 addition & 1 deletion installer/templates/static/brunch/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//
// If you no longer want to use a dependency, remember
// to also remove its path from "config.paths.watched".
import "<%= static_deps_prefix %>deps/phoenix_html/web/static/js/phoenix_html"
import "phoenix_html"

// Import local files
//
Expand Down
5 changes: 2 additions & 3 deletions installer/templates/static/brunch/brunch-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ exports.config = {
paths: {
// Dependencies and current project directories to watch
watched: [
"<%= brunch_deps_prefix %><%= phoenix_path %>/web/static",
"<%= brunch_deps_prefix %>deps/phoenix_html/web/static",
"web/static",
"test/static"
],
Expand All @@ -64,6 +62,7 @@ exports.config = {
},

npm: {
enabled: true
enabled: true,
whitelist: ["phoenix", "phoenix_html"]
}
};
4 changes: 3 additions & 1 deletion installer/templates/static/brunch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"clean-css-brunch": ">= 1.0 < 1.8",
"css-brunch": ">= 1.0 < 1.8",
"javascript-brunch": ">= 1.0 < 1.8",
"uglify-js-brunch": ">= 1.0 < 1.8"
"uglify-js-brunch": ">= 1.0 < 1.8",
"phoenix": "file:<%= brunch_deps_prefix %><%= phoenix_path %>",
"phoenix_html": "file:<%= brunch_deps_prefix %>deps/phoenix_html"
}
}
2 changes: 1 addition & 1 deletion installer/templates/static/brunch/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// To use Phoenix channels, the first step is to import Socket
// and connect at the socket path in "lib/my_app/endpoint.ex":
import {Socket} from "<%= static_deps_prefix %><%= phoenix_static_path %>/web/static/js/phoenix"
import {Socket} from "phoenix"

let socket = new Socket("/socket", {params: {token: window.userToken}})

Expand Down
21 changes: 10 additions & 11 deletions installer/test/phoenix_new_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,20 @@ defmodule Mix.Tasks.Phoenix.NewTest do

# Brunch
assert_file "photo_blog/.gitignore", "/node_modules"
assert_file "photo_blog/brunch-config.js", ~s["deps/phoenix/web/static"]
assert_file "photo_blog/brunch-config.js", ~s["phoenix", "phoenix_html"]
assert_file "photo_blog/config/dev.exs", "watchers: [node:"
assert_file "photo_blog/web/static/assets/favicon.ico"
assert_file "photo_blog/web/static/assets/images/phoenix.png"
assert_file "photo_blog/web/static/css/app.css"
assert_file "photo_blog/web/static/js/app.js",
~s[import socket from "./socket"]
assert_file "photo_blog/web/static/js/socket.js",
~s[import {Socket} from "deps/phoenix/web/static/js/phoenix"]
~s[import {Socket} from "phoenix"]

assert_file "photo_blog/package.json", fn(file) ->
assert file =~ ~s["file:deps/phoenix"]
assert file =~ ~s["file:deps/phoenix_html"]
end

refute File.exists? "photo_blog/priv/static/css/app.css"
refute File.exists? "photo_blog/priv/static/js/phoenix.js"
Expand Down Expand Up @@ -235,16 +240,10 @@ defmodule Mix.Tasks.Phoenix.NewTest do
assert file =~ "lockfile: \"../../mix.lock\""
end

assert_file "photo_blog/brunch-config.js", fn(file) ->
assert file =~ ~s["../../deps/phoenix/web/static"]
assert file =~ ~s["../../deps/phoenix_html/web/static"]
assert_file "photo_blog/package.json", fn(file) ->
assert file =~ ~s["file:../../deps/phoenix"]
assert file =~ ~s["file:../../deps/phoenix_html"]
end

assert_file "photo_blog/web/static/js/socket.js",
~s["../../../deps/phoenix/web/static/js/phoenix"]

assert_file "photo_blog/web/static/js/app.js",
~s["../../../deps/phoenix_html/web/static/js/phoenix_html"]
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "phoenix",
"version": "1.1.1",
"main": "./priv/static/phoenix.js",
"repository": {
},
"dependencies": {
"devDependencies": {
"brunch": "^2.1.1",
"babel-brunch": "^6.0.0"
"babel-brunch": "^6.0.0",
"uglify-js-brunch": ">= 1.0 < 1.8"
}
}
115 changes: 1 addition & 114 deletions priv/static/phoenix.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,4 @@
(function() {
'use strict';

var globals = typeof window === 'undefined' ? global : window;
if (typeof globals.require === 'function') return;

var modules = {};
var cache = {};
var aliases = {};
var has = ({}).hasOwnProperty;

var endsWith = function(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
};

var _cmp = 'components/';
var unalias = function(alias, loaderPath) {
var start = 0;
if (loaderPath) {
if (loaderPath.indexOf(_cmp) === 0) {
start = _cmp.length;
}
if (loaderPath.indexOf('/', start) > 0) {
loaderPath = loaderPath.substring(start, loaderPath.indexOf('/', start));
}
}
var result = aliases[alias + '/index.js'] || aliases[loaderPath + '/deps/' + alias + '/index.js'];
if (result) {
return _cmp + result.substring(0, result.length - '.js'.length);
}
return alias;
};

var _reg = /^\.\.?(\/|$)/;
var expand = function(root, name) {
var results = [], part;
var parts = (_reg.test(name) ? root + '/' + name : name).split('/');
for (var i = 0, length = parts.length; i < length; i++) {
part = parts[i];
if (part === '..') {
results.pop();
} else if (part !== '.' && part !== '') {
results.push(part);
}
}
return results.join('/');
};

var dirname = function(path) {
return path.split('/').slice(0, -1).join('/');
};

var localRequire = function(path) {
return function expanded(name) {
var absolute = expand(dirname(path), name);
return globals.require(absolute, path);
};
};

var initModule = function(name, definition) {
var module = {id: name, exports: {}};
cache[name] = module;
definition(module.exports, localRequire(name), module);
return module.exports;
};

var require = function(name, loaderPath) {
var path = expand(name, '.');
if (loaderPath == null) loaderPath = '/';
path = unalias(name, loaderPath);

if (has.call(cache, path)) return cache[path].exports;
if (has.call(modules, path)) return initModule(path, modules[path]);

var dirIndex = expand(path, './index');
if (has.call(cache, dirIndex)) return cache[dirIndex].exports;
if (has.call(modules, dirIndex)) return initModule(dirIndex, modules[dirIndex]);

throw new Error('Cannot find module "' + name + '" from '+ '"' + loaderPath + '"');
};

require.alias = function(from, to) {
aliases[to] = from;
};

require.register = require.define = function(bundle, fn) {
if (typeof bundle === 'object') {
for (var key in bundle) {
if (has.call(bundle, key)) {
modules[key] = bundle[key];
}
}
} else {
modules[bundle] = fn;
}
};

require.list = function() {
var result = [];
for (var item in modules) {
if (has.call(modules, item)) {
result.push(item);
}
}
return result;
};

require.brunch = true;
require._cache = cache;
globals.require = require;
})();
require.define({'phoenix': function(exports, require, module){ "use strict";
"use strict";

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

Expand Down Expand Up @@ -1141,5 +1030,3 @@ var Timer = (function () {
return Timer;
})();

}});
if(typeof(window) === 'object' && !window.Phoenix){ window.Phoenix = require('phoenix') };

0 comments on commit 5d00300

Please sign in to comment.