How to use rtlcss node package with twitter-bootstrap on Laravel? - php

It may sound stupid but I've recently started working with laravel mix for compiling scss and js files. But I can't understand something.
I want to use rtlcss npm to make the twitter-bootstrap rtl.
This is the default app.scss asset of Laravel
// Fonts
#import url("https://fonts.googleapis.com/css?family=Raleway:300,400,600");
// Variables
#import "variables";
// Bootstrap
#import "~bootstrap-sass/assets/stylesheets/bootstrap";
And this is the default app.js asset:
window._ = require('lodash');
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap-sass');
} catch (e) {}
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
I've installed the rtlCSS through node package, now I what I want to happen is when I run npm run dev or the watcher, it compiles the bootstrap scss file and make it RTL and put it in the public css directory like it does with the default LTR that is.

Your can easily generate RTL css with Laravel Mix:
First, to use Laravel mix you should install the necessary libraries from npm:
npm install
Then, install webpack-rtl-plugin: npm install webpack-rtl-plugin --save-dev
Then, in webpack.mix.js (located at the root of your laravel app) import the pulgin at the top:
let WebpackRTLPlugin = require('webpack-rtl-plugin');
Then, replace the:
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
with the following:
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css')
.webpackConfig({
plugins: [
new WebpackRTLPlugin()
]
});
Finally, run: npm run dev (for develpoment) or npm run prod (for production)
As a result, Larave Mix will generate:
public/css/app.css (the original css from sass)
public/css/app.rtl.css (the RTL css version)
and public/css/app.js

I tried multiple solutions with no luck after multiple tries the below code works fine and it covers all mix features like versioning, source-maps, etc.
Don't forget to install rtlcss package
mix.sass('resources/sass/app.scss', 'public/css');
mix.postCss('public/css/app.css', 'public/css/app.rtl.css', [
require('rtlcss'),
]);

Related

Live changes Wordpress

I'm looking for something like npm live-server, that refreshes my webbrowser every time I make changes to a document. Npm live-server doesn't work with PHP files.
Currently using XAMPP Apache/MySQL to run WP locally.
Any ideas?
For the one's that are interested: here is the final solution.
(Big thanks to #Kintamasis )
Install Gulp / Gulp BrowserSync
Create a gulpfile.js in your themes' folder.
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
gulp.task('browser-sync', function() {
browserSync.init({
proxy: "YOUR PROXY HERE"
});
gulp.watch("*").on('change', browserSync.reload);
});
Run gulp browser-sync in your WP theme folder.

Symfony 4, include assets from vendor directory

I would like to load vendor assets, downloaded with composer inside vendor directory, from my twig template.
Using a relative path is one solution (in this example I'm going to include bootstrap css, but the same problem is for any other libs required from composer, as jQuery, jQueryUI etc. )
<link rel="stylesheet" href="{{ asset('../vendor/twbs/bootstrap/dist/css/bootstrap.min.css') }}" >
Symfony docs suggest to use asset:install in order to generate a symlink from vendor directory to public, but I was unable to understand how it works.
assets:install -h wasn't so clear to let me understand how to link a specific vendor path to the public directory.
Creating a simlink with
ln -s /path/of/vendor/lib /path/public/dir
works fine but, symlinks created will be deleted every time I look for an update with composer.
Any idea about "a best practice" to include assets from vendor directory?
Thank you
In terms of 'Best Practice', I generally use npm with gulp or something to that effect, which generates distribution css and js files that are output to a designated file in public/.
Here's an example from a recent project package.json file
{
"devDependencies": {
"bootstrap": "^4.1.3",
"gulp": "^4.0.0",
"gulp-concat": "^2.6.1",
"gulp-sass": "^3.1.0"
},
"scripts": {
"compile:styles": "gulp styles"
}
}
Rather run npm install --save-dev bootstrap gulp gulp-concat gulp-sass to get the latest versions etc.
And you'll need this gulpfile.js too
var gulp = require('gulp');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
gulp.task('styles', function(){
return gulp
.src([
'app/Resources/sass/main.scss',
])
.pipe(sass({outputStyle:'compressed', includePaths: ['node_modules']}).on('error', sass.logError))
.pipe(concat('styles.min.css'))
.pipe(gulp.dest('public/css'));
});
Once setup, you can run npm run compile:styles from the console and the app/Resources/sass/main.scss SASS file will be pre-processed, minified and output to public/css/styles.min.css.
Note that the gulp file includes the node_modules folder so you can import bootstrap inside the main.scss file, e.g.
$primary: #55a367;
#import 'bootstrap/scss/bootstrap';
From a twig template:
<link rel="stylesheet" type="text/css" href="{{ asset('css/styles.min.css') }}">
I generally commit both the main.scss and styles.min.css
The reason why you can't tell the assets:install to link and arbitrary vendor directory is that the command is designed to loop through the list of installed bundles and link a well-known directory (Resources/public) directory if it exists. It relies on both the short bundle name and the directory existing, so it can only work with symfony bundles, there's no support for other libraries like bootstrap or jquery. (Link to current command source).
The recommended way to handle frontend libraries nowadays is encore.
In a situation where that's not possible, you could use composer scripts. I wouldn't call this "best practice", might end up being more trouble than it's worth but is an option you can consider.
You would create a shell, php script or console command where you basically replicate the functionality of assets:install to link your library assets. You will still need to manually update the script when you install a new library, but you can configure it to automatically run after installing or updating packages.
Copy this simple sample bash script into you project directory, name it install_vendors.sh:
#!/bin/bash
BASE_PATH=`pwd`
PUBLIC="public"
if [ "$1" != "" ]; then
PUBLIC=$1
fi;
PUBLIC=${BASE_PATH}/${PUBLIC}/vendor
VENDOR=${BASE_PATH}/vendor
rm $PUBLIC -rf
mkdir $PUBLIC
function link_asset
{
SOURCE_DIR=${VENDOR}/$1
TARGET_DIR=${PUBLIC}/$2
ln -s $SOURCE_DIR $TARGET_DIR
}
link_asset twbs/bootstrap/dist bootstrap
Add it to the scripts section of composer.json and the auto-scripts:
"scripts": {
"vendors:install": "bash install_vendors.sh",
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
"#vendors:install"
},
// ...
}
You can also execute it at any time with composer run vendors:install.
Then include them in your twig files: {{ asset('vendor/bootstrap/css/bootstrap.min.css') }}.

Use PrimerCSS.io in Laravel 5.5

I am building an app with Laravel & I want to build it with PrimerCSS.io and remove Bootstrap completely.
I followed the docs, but I could not complete it.
I replaced #import "~bootstrap-sass/assets/stylesheets/bootstrap";
with
#import "primer-css/index.scss"; in
resources/assets/sass/app.scss and ran npm run watch.
But encountered problem
Module build failed:
#import "primer-css/index.scss;
File to import not found or unreadable:
Here's what I got to work with a fresh install of Laravel 5.5 (this Laracasts post led me on the right track).
First, we need to use the includePaths option for node-sass:
// webpack.mix.js
let mix = require('laravel-mix');
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css', {
includePaths: ["node_modules"]
});
Then, we can just include the primer-css index.scss file:
// resources/assets/sass/app.scss
#import "~primer-css/index";
(Note that the above ~ is equivalent to ../../../node_modules/)

Gulp Command running Error in Laravel 5.3

I am new in Laravel. I am trying to run gulp command in CMD to get CSS and JS (vue.js) file. I am getting following error while I am trying to run gulp command in CMD. I am using Laravel 5.3.
{ [Error: ./resources/assets/js/app.js
Module build failed: ReferenceError: Unknown plugin "add-module-exports" specified in "base" at 0, attempted to resolve relative to "D:\\php7\\htdocs\\addbook\\
resources\\assets\\js" at D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\options\option-manager.js:176:17
at Array.map (native) at Function.normalisePlugins (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\options\option-manager.js:154:20)
at OptionManager.mergeOptions (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\options\option-manager.js:229:36)
at OptionManager.init (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\options\option-manager.js:374:12)
at File.initOptions (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\index.js:216:65)
at new File (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\index.js:139:24)
at Pipeline.transform (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\pipeline.js:46:16)
at transpile (D:\php7\htdocs\addbook\node_modules\babel-loader\index.js:38:20)
at Object.module.exports (D:\php7\htdocs\addbook\node_modules\babel-loader\index.js:131:12)]
message: './resources/assets/js/app.js\nModule build failed: ReferenceError: Unknown plugin "add-module-exports" specified in "base" at 0, attempted to resolv
e relative to "D:\\\\php7\\\\htdocs\\\\addbook\\\\resources\\\\assets\\\\js"\n at D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\transformation\\f
ile\\options\\option-manager.js:176:17\n at Array.map (native)\n at Function.normalisePlugins (D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\t
ransformation\\file\\options\\option-manager.js:154:20)\n at OptionManager.mergeOptions (D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\transforma
tion\\file\\options\\option-manager.js:229:36)\n at OptionManager.init (D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\transformation\\file\\optio
ns\\option-manager.js:374:12)\n at File.initOptions (D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\transformation\\file\\index.js:216:65)\n at
new File (D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\transformation\\file\\index.js:139:24)\n at Pipeline.transform (D:\\php7\\htdocs\\addboo
k\\node_modules\\babel-core\\lib\\transformation\\pipeline.js:46:16)\n at transpile (D:\\php7\\htdocs\\addbook\\node_modules\\babel-loader\\index.js:38:20)\n
at Object.module.exports (D:\\php7\\htdocs\\addbook\\node_modules\\babel-loader\\index.js:131:12)',
showStack: false,
showProperties: true,
plugin: 'webpack-stream',
__safety: { toString: [Function: bound ] } }
Could anyone give me any solution regarding this ?
gulpfile.js
var elixir = require('laravel-elixir');
require('laravel-elixir-vue');
elixir(function(mix) {
mix.sass('app.scss');
});
elixir(function(mix) {
mix.webpack('app.js');
});
elixir(function(mix) {
mix.version(['css/app.css', 'js/app.js']);
});
From the Laravel Docs:
If you are developing on a Windows system or you are running your VM
on a Windows host system, you may need to run the npm install command
with the --no-bin-links switch enabled:
npm install --no-bin-links
Laravel Docs: Elixir Installation and Setup
Remove /node_modules and run npm install with the --no-bin-links switch like mentioned in the docs and quoted above.

php-node Node Express cannot find module

The error is:
Cannot find module 'php'
Error: Cannot find module 'php'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at new View (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\view.js:50:49)
at EventEmitter.app.render (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\application.js:545:12)
at ServerResponse.res.render (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\response.js:938:7)
at F:\Users\MichaelJacksonIsDead\publisherServer\routes\index.js:12:6
at Layer.handle [as handle_request] (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\router\layer.js:82:5)
at next (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\router\route.js:110:13)
The Route for this page to render as php is as follows:
router.get('/index.php', function(req, res, next) {
var render = require('php-node')({bin:"F:\\xampp\php\\php.exe"});
res.render('index.php');
});
I have run a npm install -g, npm install node-php -g at both project level and level above with no success:
F:\Users\MichaelJacksonIsDead\publisherServer>npm install -g
publisherServer#0.0.0 F:\Users\MichaelJacksonIsDead\AppData\Roaming\npm\node_mod
ules\publisherServer
├── php-node#0.0.2
├── debug#2.2.0 (ms#0.7.1)
├── serve-favicon#2.2.1 (ms#0.7.1, fresh#0.2.4, parseurl#1.3.0, etag#1.6.0)
├── cookie-parser#1.3.5 (cookie#0.1.3, cookie-signature#1.0.6)
├── morgan#1.5.3 (basic-auth#1.0.1, depd#1.0.1, on-finished#2.2.1)
├── body-parser#1.12.4 (bytes#1.0.0, content-type#1.0.1, depd#1.0.1, raw-body#2.
0.2, on-finished#2.2.1, qs#2.4.2, iconv-lite#0.4.8, type-is#1.6.2)
├── express#4.12.4 (merge-descriptors#1.0.0, utils-merge#1.0.0, methods#1.1.1, f
resh#0.2.4, cookie#0.1.2, escape-html#1.0.1, cookie-signature#1.0.6, range-parse
r#1.0.2, content-type#1.0.1, parseurl#1.3.0, finalhandler#0.3.6, vary#1.0.0, ser
ve-static#1.9.3, content-disposition#0.5.0, path-to-regexp#0.1.3, depd#1.0.1, on
-finished#2.2.1, qs#2.4.2, etag#1.6.0, proxy-addr#1.0.8, send#0.12.3, type-is#1.
6.2, accepts#1.2.7)
├── hjs#0.0.6 (hogan.js#3.0.2)
└── less-middleware#1.0.4 (mkdirp#0.3.5, node.extend#1.0.10, less#1.7.5)
F:\Users\MichaelJacksonIsDead\publisherServer>npm start
> publisherServer#0.0.0 start F:\Users\MichaelJacksonIsDead\publisherServer
> node ./bin/www
GET /index.php 500 41.145 ms - 954
Does anyone have any expirence with this issue? As far as I can tell I have correctly installed it... also to make sure I added the package to my json file as follows:
"dependencies": {
"body-parser": "~1.12.4",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.12.4",
"hjs": "~0.0.6",
"less-middleware": "1.0.x",
"morgan": "~1.5.3",
"serve-favicon": "~2.2.1",
"php-node": "0.0.2"
}
Lastly I did not have enough rep to open a tag php-node for the project # https://www.npmjs.com/package/php-node, not a issue but would of been nice to have a more speficic tag :)
I have found the answer, thank you #mscdex and #adeneo
I replaced the default:
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
with
// use PHP as view engine in Express
var phpnode = require('php-node')({bin:"F:\\xampp\\php\\php.exe"});
app.engine('php', phpnode);
app.set('view engine', 'php');
under routes:
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index.hjs', { title: 'Express' });
});
router.get('/index.php', function(req, res, next) {
res.render('index.php');
});
The app still supports php and index.hjs pages.
You should only use -g when you're installing a module from npm that contains a command line utility (e.g. express-generator is an example of this). However in almost all cases you should just simply do npm install <module name> which will install the module locally and allow you to require() it.
So do npm install in your project's root directory and require() should be able to find php-node.
only if the plugin are use you are not 100% happy of it there is a simple method(this is only a prototype so if you are node.js expert you may improve it) to build FROM ONLY ONE SERVER APPS NODE.JS - PHP (and theoretical you have yet sqlite3 and after installing a mariadb or mysql server you can use them as well) :
https://stackoverflow.com/a/68422021/5781320

Categories