Skip to content

Commit

Permalink
feat: allow deferring js to specific pages
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jan 9, 2019
1 parent 02a1b46 commit 76ea93f
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 48 deletions.
1 change: 0 additions & 1 deletion apps/block_scout_web/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import './lib/market_history_chart'
import './lib/pending_transactions_toggle'
import './lib/pretty_json'
import './lib/reload_button'
import './lib/smart_contract/code_highlighting'
import './lib/smart_contract/read_only_functions'
import './lib/smart_contract/wei_ether_converter'
import './lib/stop_propagation'
Expand Down
123 changes: 76 additions & 47 deletions apps/block_scout_web/assets/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,85 @@
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const glob = require("glob");

module.exports = {
entry: './js/app.js',
output: {
filename: 'app.js',
path: path.resolve(__dirname, '../priv/static/js')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: [{
loader: "css-loader"
}, {
loader: "postcss-loader"
}, {
loader: "sass-loader",
const viewScripts = glob.sync('./js/view_specific/**/*.js');

function transpileViewScript(file) {
return {
entry: file,
output: {
filename: file.replace('./js/view_specific/', ''),
path: path.resolve(__dirname, '../priv/static/js')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
]
}
}
};



const appJs =
{
entry: './js/app.js',
output: {
filename: 'app.js',
path: path.resolve(__dirname, '../priv/static/js')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: [{
loader: "css-loader"
}, {
loader: "postcss-loader"
}, {
loader: "sass-loader",
options: {
precision: 8,
includePaths: [
'node_modules/bootstrap/scss',
'node_modules/@fortawesome/fontawesome-free/scss'
]
}
}],
fallback: 'style-loader'
})
}, {
test: /\.(svg|ttf|eot|woff|woff2)$/,
use: {
loader: 'file-loader',
options: {
precision: 8,
includePaths: [
'node_modules/bootstrap/scss',
'node_modules/@fortawesome/fontawesome-free/scss'
]
name: '[name].[ext]',
outputPath: '../fonts/',
publicPath: '../fonts/'
}
}],
fallback: 'style-loader'
})
}, {
test: /\.(svg|ttf|eot|woff|woff2)$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: '../fonts/',
publicPath: '../fonts/'
}
}
}
]
},
plugins: [
new ExtractTextPlugin('../css/app.css'),
new CopyWebpackPlugin([{ from: 'static/', to: '../' }])
]
},
plugins: [
new ExtractTextPlugin('../css/app.css'),
new CopyWebpackPlugin([{ from: 'static/', to: '../' }])
]
};
}

module.exports = viewScripts.map(transpileViewScript).concat(appJs);
1 change: 1 addition & 0 deletions apps/block_scout_web/lib/block_scout_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ defmodule BlockScoutWeb do
Router.Helpers,
TabHelpers,
Tokens.Helpers,
Views.ScriptHelpers,
WeiHelpers
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@
}
</script>
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
<%= render_existing(@view_module, "scripts.html", assigns) %>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
defmodule BlockScoutWeb.AddressContractView do
use BlockScoutWeb, :view

def render("scripts.html", %{conn: conn}) do
render_scripts(conn, "address_contract/code_highlighting.js")
end

def format_smart_contract_abi(abi), do: Poison.encode!(abi, pretty: false)

@doc """
Expand Down
28 changes: 28 additions & 0 deletions apps/block_scout_web/lib/block_scout_web/views/script_helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule BlockScoutWeb.Views.ScriptHelpers do
@moduledoc """
Helpers for rendering view specific script tags.
"""

import Phoenix.HTML, only: [sigil_E: 2]
import BlockScoutWeb.Router.Helpers, only: [static_path: 2]

def render_scripts(conn, file_names) do
conn
|> files(file_names)
|> Enum.map(fn file ->
~E"""
<script src="<%= file %>"> </script>
"""
end)
end

defp files(conn, file_names) do
file_names
|> List.wrap()
|> Enum.map(fn file ->
path = "/" <> Path.join("js", file)

static_path(conn, path)
end)
end
end

0 comments on commit 76ea93f

Please sign in to comment.