I am creating a custom laravel packages for inside company usage and I am trying to figure out how to work with the assets that this package requires.
I have src/resources/js where I have multiple JS and CSS files. I'm want to run npm run watch while developing the package so I could see the changes. After the patch/update is done I want to run npm run production that would save these files in src/dist where user can publish these to their public directory (if wanted) with this command in my serviceProvider:
protected function registerAssets() {
$this->publishes([
__DIR__ . '/dist/' => public_path('/public/vendor/myPackage'),
], 'public');
}
Now my question is how do I structure my webpack and my blade views to work with my logic so while running npm run watch the mix() function in my blade will load the assets from one directory and if I publish my package it will the assets from another directory?
Do I understand the logic correctly for package development with the assets?
When you do npm run watch the view will get them from the same path, but it will just change their hash, so the cache can be removed. I think after you prepare them for production/dist use there won't be a need to use mix() anymore, because your package or the project using the package won't be recompiling them. For example when you add bootstrap.js to your project you just load the asset whenever is needed and you don't compile it.
When you publish or release you package, I think your views should be using the dist folder assets and while you develop it you should use mix. If the project using your package needs some asset in their own view, it should figure out the path for the dist published directory. You can also make your package configurable for where the assets are published and from where your views can load them. Another option would be to make a wrapper of the mix() function and also load assets depending on specific configuration. I hope this gives you some ideas on how to proceed depending on how the package will be used by others.
Related
I am using 'animate.css' as a npm package. to use it I specify import 'animate.css' during development. But in production it says to include 'animate.min.css'. How to specify to parceljs to pick the animate.min.css during production build.
or would import animate.css would be able to resolve to right animate.css in development build and animate.min.css in production build. how does the resolver know to pick right file as the main file is specified as animate.css in the package.json
But the there is "Files" property in package.json ...not sure if it can help.
The high-level answer is that parcel automatically minifies and optimizes css files, so you don't need to do anything special to make sure that your production build is as small as possible.
If you want to understand in detail how parcel handles this particular package, read on.
Parcel generally follows node resolution conventions to figure out which files to actually import when you reference a package name.
In this case, the import "animate.css" will tell parcel to go looking in the nearest node_modules folder for a for a subfolder called animate.css. It finds it, and then looks for the package.json file at the root. In this case, the package.json has a main field that points to a file called animate.css (e.g. the un-minified version).
Parcel will use that file as the basis for development and production builds. When you run parcel in development, it leaves this file untouched. When you run parcel build it will process this (unminified) file through cssnano so the build output is minified.
So the interesting fact is that even though there is another file called node_modules/animate/animate.min.css living there, Parcel is able to achieve the desired behavior without touching it. The publishers of animate.css included it for other folks that aren't using a bundler that's as awesome as parcel - you can safely ignore it.
I am building an an app inside a Laravel project using Vue components. The blade layout reads off of the bundled app.js so I was wondering when using frameworks like Vue and React, are the files even necessary other than just being there for the bundle process? Like what I had actually removed the .vue files I have (components) -- would it still work since it's reading off the bundled source file?
.vue files are source files, and should therefore be in source control all the time. When it is compiled (using vue-cli for instance), a .js file will come out of it and that is what the browser needs.
So yes for production environments you can remove the .vue files since you only need the bundled and compiled code.
I just install laravel-horizon and already tested using it and everything is okay , however I don't know how to edit laravel horizon view ,
Above is the overview of my laravel-horizon , anyone can help me how to edit this ?
because it seems that laravel using vue js for this and i'm not really familiar
You can find the files that are used to build the frontend vue app in the /vendor/laravel/horizon/resources folder.
The page templates are part of the vue components (.vue files) that are found within the assets/js/pages folder. The css is built from sass files found in assets/sass.
You may be able to make superficial changes to the above files without any vue knowledge, however doing some reading up on how vue components are structured and other vue basics will definitely help.
After you make changes you will need to build the app.
There are build scripts included in the horizon package (check the package.json file).
These scripts will update the files that are included in public/vendor/horizon with the built files.
It would be up to you how you choose to track the changes you make, but I would avoid editing the files within /vendor/laravel/horizon directly, but rather fork it and import it in as your own package, or edit and build the frontend files separately to include within your project.
So, I am setting up a new site and my project's folder structure looks like this now.
foo.com/
index.php
assets/
css/
img/
js/
vendor/
I have added vendor/ for js/css libraries that I must install to keep them separate, since I want anyone who installs my project to install those in vendor from package.json - most libraries contain too many files 99% which I don't want to push to production.
Now once the project is finished, I would like to push the code to production with only the necessary js/css files.
This is where the problem comes. For example, if I install bulma css using:
yarn add bulma --modules-folder ./assets/vendor
It will dump all bulma-related files which are almost 70 into /vendor/bulma/ but I will only be needing one or two css files afterwards, since I will compiles the sass file to css as:
sass vendors/bulmna/style.scss assets/css/style.css
So my questions is: I am assuming this is how every developer does it and there are no documentations I can find that suggest how to do it. Is it safe to ignore the /vendor directory? What if I install vue, font-awesome, bootstrap .. how can I only fetch the files I need but not everything in /vendors folder?
Your question is actually quite broad - however, I'll try to list as much as possible.
Lets say you're building a project from scratch and needed to include vuejs, jquery, fontawesome but only need to include a couple of files.
The issue you're hitting here is module dependency with respect to npm modules. (and there are many different tools that you can use to manage versions on your library dependencies as well as ensuring they are included into your project, i.e. package managers).
Ok - now from here, you may say to yourself
but I only need say, one icon from fontawesome in your final build (dist) and I don't want to commit all my modules into source control
Again, this is where you omit node_modules and other dependent libraries from source control (i.e. include node_modules your .gitignore)
To reiterate
You can install the required library,
add node_modules to .gitignore ,
bundle those libraries into a vendor single file to be consumed by your users (can be via browserify/webpack/rollup/gulp/grunt/yarn etc).
generate bundle within npm script
Now you may ask further -
Why would I use any of those tools? - they're distracting me from simply copy/pasting vendor libaries into my source control.
Build tools were created to
streamline the developer pipeline so that you DONT have to copy/paste vendor libaries into a vendor folder.
ensures that all files are bundled to the client automatically
allows you to keep track/restrict library version updates/ when required via package.json
allows you to add other build steps (such as minification, md5hash versioning, compression, code splitting, asset management to name a few).
Now lets break down the original question here:
How to ensure other developers get everything they need when cloning the repository
how do I ensure I can provide only the necessary files to the end user (if I only use specific parts of vendor libaries?)
1. How to ensure developers get what they need
Again, to reiterate above, adding devDependancies and .gitignoring allows you to only add the necessary files to your project.
2. How can I ensure clients get what they need without bloating request files?
This is where build tools such as webpack, browserify, gulp, grunt, rollup, attempt to achieve. This is because with some libraries that exceed in file size of 200kb minified, you may need to separate these files into different client requests (and as such, not demand the user to request one large file, which was symtomatic of browserify projects).
The second technique you will need to be aware of, is with specific libraries, you can use import mdn link where you can require one function/class from a dependant library (which further reduces file size).
Another technique is using less import statements (which can extract less functions/styles similar to above, but this isn't currently supported in SCSS). for SCSS, you're basically left with copy/pasting the necessary styles into your base scss and that'll save you space as well.
EDIT
How to create a bundle from npm install libaries
From the comments you've mentioned above (about not wanting to include a tool into your workflow, there's no point outlining any one particular strategy - you can find answers/tutorials online about how to setup gulp/browserify/webpack for your particular needs).
However, As you are currently using yarn - I'll go into details about that.
Firstly, yarn is a package manager (like npm). All it does with the --modules-folder is install the package into the specified folder, that's all. So we don't really care about that (since it's doing the same thing as npm). (i.e. your vendor folder is the same as node_modules in many respects).
We could use
webpack
gulp
grunt
rollup
browserify
brunch
(All build tools that essentially allow you to bundle all those packages into a single entry point to be packaged to the client).
I won't go into how, because that is a process you can find online, and from the above comments, I can tell you don't particularly care either.
What you are looking for is a zero-config javascript build tool. (Extremely out of the scope of your original question, and i'll only answer that in a separate Q&A).
I'd try Googling on "tree shaking CSS" to see if that gives you something.
Maybe something like: https://github.com/jacobp100/es-css-modules
Rollup plugin may be useful. It works for js, with postcss, the link says it works with css also.
https://code.lengstorf.com/learn-rollup-css
Have a look at Pancake. It has been built specifically for the purpose of moving only those files out of the node_modules folder that you need. I wrote an article about it here: https://medium.com/dailyjs/npm-and-the-front-end-950c79fc22ce
(probably not an answer but a good tip)
PS: I am the author of both, the article and the tool so with clear bias :)
recently I added somethings into my composer.json file and did a "composer dump autoload". All work fine on development side.
When I put my updates in production, I had this error :
[2016-02-17 08:17:47] local.ERROR: exception
'Symfony\Component\Debug\Exception\FatalErrorException' with message
'Call to undefined function App\Http\Controllers\f_array_classe()' in
/home/dominiquir/ecole/app/Http/Controllers/MatieresEnseigneesController.php:67
when I put my app in production, I put only the "app" folder. But certainly when this file is modified , I must also put in production some other files, but which ones ?
An application which uses Composer has to put online
All files needed to run the application itself - you should know the location of the files because I suppose you created them
all files in the vendor folder.
If you don't know exactly point 1, the globally correct answer is: Your entire project folder after you ran composer install.
To optimize upload times, you should omit the development dependencies: Run composer install --no-dev to remove anything that is not being used for running the application. Development dependencies usually includes test frameworks like PHPUnit or any libraries that are only being used to help developing, like some command line interfaces for the framework being used etc. If your composer.json has entries under require-dev, that's what gets omitted.
The next step to reduce upload size would be to not upload anything of the application itself that is not relevant, like it's tests. But this depends entirely on how you constructed the application.
I would recommend to write a script that does all the preparations to create an optimized upload. It can be a simple shell script that creates a new, empty folder, then copies all needed files and folders into it, then runs composer install --no-dev there, and maybe then again deletes files from the dependency packages that are not needed (like test folders).
You may also run optimizing software for images, CSS and Javascript then. Most of them can be compressed in size without any loss of quality or function.
In the end you get a folder full of files you know have to go online. Just upload the whole folder. Done.
More optimization would be to try using rsync, which will compare the files already present at the target with the ones at the source, and only transfer the necessary minimum, but this requires SSH access of some kind, not just FTP.