如何在Phoenix 1.4 + Webpack上设置Zurb Foundation CSS框架?不幸的是我无法在任何地方找到任何例子...
mix phx.server可以正常运行且没有运行Webpack的错误,但我的app.js / app.scss中未加载zurb基础CSS / JS ...
Package.json
{
"repository": {},
"license": "MIT",
"scripts": {
"deploy": "webpack --mode production",
"watch": "webpack --mode development --watch"
},
"dependencies": {
"formdata-polyfill": "^3.0.18",
"mdn-polyfills": "^5.17.0",
"phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html",
"phoenix_live_view": "file:../deps/phoenix_live_view",
"url-search-params-polyfill": "^6.0.0",
"foundation-sites": "^6.5.3",
"what-input": "^5.2.3",
"jquery": "^3.3.1",
"sass-loader": "^7.1.0",
"node-sass": "^4.12.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"babel-loader": "^8.0.0",
"copy-webpack-plugin": "^4.5.0",
"css-loader": "^2.1.1",
"mini-css-extract-plugin": "^0.4.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"uglifyjs-webpack-plugin": "^1.2.4",
"webpack": "4.4.0",
"webpack-cli": "^3.3.4"
}
}
Webpack.config.js
const webpack = require('webpack');
const path = require('path');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
//const foundation = require('foundation-sites/dist/js/foundation.min.js');
module.exports = (env, options) => ({
optimization: {
minimizer: [
new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),
new OptimizeCSSAssetsPlugin({})
]
},
entry: {
'./js/app.js': glob.sync('./vendor/**/*.js').concat(['./js/app.js']),
},
output: {
filename: 'app.js',
path: path.resolve(__dirname, '../priv/static/js')
},
externals: {
jquery: 'jQuery',
foundation: 'Foundation'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.scss$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
}
]
},
resolve: {
modules: ['node_modules', path.resolve(__dirname, 'js')],
extensions: ['.js'],
alias: {
foundation: 'foundation-sites/js/foundation.core'
}
},
plugins: [
new MiniCssExtractPlugin({ filename: '../css/app.css' }),
new CopyWebpackPlugin([{ from: 'static/', to: '../' }]),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
]
});
app.js
import LiveSocket from "phoenix_live_view"
let liveSocket = new LiveSocket("/live")
liveSocket.connect()
import css from "../css/app.scss"
// webpack automatically bundles all modules in your
// entry points. Those entry points can be configured
// in "webpack.config.js".
//
// Import dependencies
//
import "phoenix_html"
这是对我有用的解决方案。
安装:
node-sass": "^4.12.0"
sass-loader": "^8.0.0"
"autoprefixer": "^9.6.4"
"foundation-sites": "^6.5.3"
"jquery": "^3.4.1"
"what-input": "^5.2.6"
[使用npm
或yarn
(我用过的纱线)。
检查以使用"webpack": "4.41.1"
版本,我看到人们对先前版本有一些问题。
将assets/css/app.css
重命名为assets/css/app.scss
并在其中添加以下几行:
@import "~foundation-sites/scss/foundation";
@include foundation-everything();
assets/app.js
:import $ from 'jquery'
require ('what-input');
window.$ = $;
import Foundation from 'foundation-sites';
$(document).foundation();
import css from "../css/app.scss"
import "phoenix_html"
webpack.cpnfig.js
如下:const webpack = require('webpack');
:modules/rules
部分中,修改有关CSS和装载程序的行:...
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.s?css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
}
]
},
...
pligins section, add the following line to use
jQuery`:]中>plugins: [ require('autoprefixer'), new MiniCssExtractPlugin({ filename: '../css/app.css' }), new CopyWebpackPlugin([{ from: 'static/', to: '../' }]), new webpack.ProvidePlugin({ // inject modules as global vars $: 'jquery', jQuery: 'jquery', 'window.jQuery': 'jquery', }) ]
- 如下修改主布局
../templates/layout/app.html.eex
以使用Foundation
样式:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>Storex · Phoenix Framework</title> <link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/> </head> <body> <header> <div class="top-bar"> <div class="top-bar-left"> <ul class="dropdown menu" data-dropdown-menu> <li class="menu-text">Site Title</li> <li class="has-submenu"> <a href="#0">One</a> <ul class="submenu menu vertical" data-submenu> <li><a href="#0">One</a></li> <li><a href="#0">Two</a></li> <li><a href="#0">Three</a></li> </ul> </li> <li><a href="#0">Two</a></li> <li><a href="#0">Three</a></li> </ul> </div> <div class="top-bar-right"> <ul class="menu"> <li><input type="search" placeholder="Search"></li> <li><button type="button" class="button">Search</button></li> </ul> </div> </div> </header> <div class="grid-container fluid"> <%= unless is_nil(get_flash(@conn, :info)) do %> <p class="success label" role="alert"><%= get_flash(@conn, :info) %></p> <% end %> <%= unless is_nil(get_flash(@conn, :error)) do %> <p class="alert label" role="alert"><%= get_flash(@conn, :error) %></p> <% end %> <%= render @view_module, @view_template, assigns %> </div> <script type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script> </body> </html>
- 使用
mix phx.server
启动服务器并检查浏览器控制台,-您将收到一些警告,并且页面应具有Foundation样式。
您在出租车上找到源代码here.
希望这会有所帮助。