assets.ex (4283B)
1 defmodule Absinthe.Plug.GraphiQL.Assets do 2 @moduledoc """ 3 """ 4 5 @config Application.get_env(:absinthe_plug, Absinthe.Plug.GraphiQL) 6 @default_config [ 7 source: :smart, 8 local_url_path: "/absinthe_graphiql", 9 local_directory: "priv/static/absinthe_graphiql", 10 local_source: ":package/:alias", 11 remote_source: "https://cdn.jsdelivr.net/npm/:package@:version/:file" 12 ] 13 14 @react_version "15.6.1" 15 16 @assets [ 17 {"whatwg-fetch", "2.0.3", 18 [ 19 {"fetch.min.js", "fetch.js"} 20 ]}, 21 {"react", @react_version, 22 [ 23 {"dist/react.min.js", "react.js"} 24 ]}, 25 {"react-dom", @react_version, 26 [ 27 {"dist/react-dom.min.js", "react-dom.js"} 28 ]}, 29 {"bootstrap", "3.3.7", 30 [ 31 {"dist/fonts/glyphicons-halflings-regular.eot", "fonts/glyphicons-halflings-regular.eot"}, 32 {"dist/fonts/glyphicons-halflings-regular.ttf", "fonts/glyphicons-halflings-regular.ttf"}, 33 {"dist/fonts/glyphicons-halflings-regular.woff2", 34 "fonts/glyphicons-halflings-regular.woff2"}, 35 {"dist/fonts/glyphicons-halflings-regular.svg", "fonts/glyphicons-halflings-regular.svg"}, 36 {"dist/css/bootstrap.min.css", "css/bootstrap.css"} 37 ]}, 38 {"graphiql", "0.11.10", 39 [ 40 "graphiql.css", 41 {"graphiql.min.js", "graphiql.js"} 42 ]}, 43 {"graphiql-workspace", "1.1.4", 44 [ 45 "graphiql-workspace.css", 46 {"graphiql-workspace.min.js", "graphiql-workspace.js"} 47 ]}, 48 # Used by graphql-playground 49 {"typeface-source-code-pro", "0.0.44", 50 [ 51 {"index.css", "index.css"}, 52 {"files/source-code-pro-latin-400.woff2", "files/source-code-pro-latin-400.woff2"}, 53 {"files/source-code-pro-latin-700.woff2", "files/source-code-pro-latin-700.woff2"} 54 ]}, 55 # Used by graphql-playground 56 {"typeface-open-sans", "0.0.44", 57 [ 58 {"index.css", "index.css"}, 59 {"files/open-sans-latin-300.woff2", "files/open-sans-latin-300.woff2"}, 60 {"files/open-sans-latin-400.woff2", "files/open-sans-latin-400.woff2"}, 61 {"files/open-sans-latin-600.woff2", "files/open-sans-latin-600.woff2"}, 62 {"files/open-sans-latin-700.woff2", "files/open-sans-latin-700.woff2"} 63 ]}, 64 {"@absinthe/graphql-playground", "1.2.0", 65 [ 66 {"build/static/css/middleware.css", "playground.css"}, 67 {"build/static/js/middleware.js", "playground.js"} 68 ]}, 69 {"@absinthe/socket-graphiql", "0.1.1", 70 [ 71 {"compat/umd/index.js", "socket-graphiql.js"} 72 ]} 73 ] 74 75 def assets_config do 76 case @config do 77 nil -> 78 @default_config 79 80 config -> 81 Keyword.merge(@default_config, Keyword.get(config, :assets, [])) 82 end 83 end 84 85 def get_assets do 86 reduce_assets( 87 %{}, 88 &Map.put( 89 &2, 90 build_asset_path(:local_source, &1), 91 asset_source_url(assets_config()[:source], &1) 92 ) 93 ) 94 end 95 96 def get_remote_asset_mappings do 97 reduce_assets( 98 [], 99 &(&2 ++ 100 [ 101 { 102 local_asset_path(&1), 103 asset_source_url(:remote, &1) 104 } 105 ]) 106 ) 107 end 108 109 defp reduce_assets(initial, reducer) do 110 Enum.reduce(@assets, initial, fn {package, version, files}, acc -> 111 Enum.reduce(files, acc, &reducer.({package, version, &1}, &2)) 112 end) 113 end 114 115 defp local_asset_path(asset) do 116 Path.join(assets_config()[:local_directory], build_asset_path(:local_source, asset)) 117 end 118 119 defp asset_source_url(:smart, asset) do 120 if File.exists?(local_asset_path(asset)) do 121 asset_source_url(:local, asset) 122 else 123 asset_source_url(:remote, asset) 124 end 125 end 126 127 defp asset_source_url(:local, asset) do 128 Path.join(assets_config()[:local_url_path], build_asset_path(:local_source, asset)) 129 end 130 131 defp asset_source_url(:remote, asset) do 132 build_asset_path(:remote_source, asset) 133 end 134 135 defp build_asset_path(source, {package, version, {real_path, aliased_path}}) do 136 assets_config()[source] 137 |> String.replace(":package", package) 138 |> String.replace(":version", version) 139 |> String.replace(":file", real_path) 140 |> String.replace(":alias", aliased_path) 141 end 142 143 defp build_asset_path(source, {package, version, path}) do 144 build_asset_path(source, {package, version, {path, path}}) 145 end 146 end