feat: allow deferring js to specific pages

pull/1329/head
zachdaniel 6 years ago
parent 02a1b46e26
commit 76ea93f735
  1. 1
      apps/block_scout_web/assets/js/app.js
  2. 0
      apps/block_scout_web/assets/js/view_specific/address_contract/code_highlighting.js
  3. 123
      apps/block_scout_web/assets/webpack.config.js
  4. 1
      apps/block_scout_web/lib/block_scout_web.ex
  5. 1
      apps/block_scout_web/lib/block_scout_web/templates/layout/app.html.eex
  6. 4
      apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex
  7. 28
      apps/block_scout_web/lib/block_scout_web/views/script_helpers.ex

@ -40,7 +40,6 @@ import './lib/market_history_chart'
import './lib/pending_transactions_toggle' import './lib/pending_transactions_toggle'
import './lib/pretty_json' import './lib/pretty_json'
import './lib/reload_button' import './lib/reload_button'
import './lib/smart_contract/code_highlighting'
import './lib/smart_contract/read_only_functions' import './lib/smart_contract/read_only_functions'
import './lib/smart_contract/wei_ether_converter' import './lib/smart_contract/wei_ether_converter'
import './lib/stop_propagation' import './lib/stop_propagation'

@ -1,56 +1,85 @@
const path = require('path'); const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin'); const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin'); const CopyWebpackPlugin = require('copy-webpack-plugin');
const glob = require("glob");
module.exports = { const viewScripts = glob.sync('./js/view_specific/**/*.js');
entry: './js/app.js',
output: { function transpileViewScript(file) {
filename: 'app.js', return {
path: path.resolve(__dirname, '../priv/static/js') entry: file,
}, output: {
module: { filename: file.replace('./js/view_specific/', ''),
rules: [ path: path.resolve(__dirname, '../priv/static/js')
{ },
test: /\.js$/, module: {
exclude: /node_modules/, rules: [
use: { {
loader: 'babel-loader' 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 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: { options: {
precision: 8, name: '[name].[ext]',
includePaths: [ outputPath: '../fonts/',
'node_modules/bootstrap/scss', publicPath: '../fonts/'
'node_modules/@fortawesome/fontawesome-free/scss'
]
} }
}],
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'), module.exports = viewScripts.map(transpileViewScript).concat(appJs);
new CopyWebpackPlugin([{ from: 'static/', to: '../' }])
]
};

@ -50,6 +50,7 @@ defmodule BlockScoutWeb do
Router.Helpers, Router.Helpers,
TabHelpers, TabHelpers,
Tokens.Helpers, Tokens.Helpers,
Views.ScriptHelpers,
WeiHelpers WeiHelpers
} }
end end

@ -52,5 +52,6 @@
} }
</script> </script>
<script src="<%= static_path(@conn, "/js/app.js") %>"></script> <script src="<%= static_path(@conn, "/js/app.js") %>"></script>
<%= render_existing(@view_module, "scripts.html", assigns) %>
</body> </body>
</html> </html>

@ -1,6 +1,10 @@
defmodule BlockScoutWeb.AddressContractView do defmodule BlockScoutWeb.AddressContractView do
use BlockScoutWeb, :view 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) def format_smart_contract_abi(abi), do: Poison.encode!(abi, pretty: false)
@doc """ @doc """

@ -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
Loading…
Cancel
Save