uglifyjs javascript inside php files - javascript

My project has a lot of JS code inside PHP files. I want to minify the JS code in these files and I like the uglifyjs2 program. Is it possible for that (or any) JS minifier to act on JS code INSIDE a php file?
Someone is going to suggest removing all JS code from the PHP files and placing it in .js files - but that isn't always practical.
Keep in mind that I do NOT wish to minify the JS on the fly (acting on the output of the PHP interpreter). I am delivering PHP code to customers containing JS, and I wish to minify the JS inside the PHP files that I am delivering.

What you're looking for does not exist.
The only real reason to have inline JavaScript inside your PHP files is because you need to interpolate PHP and JavaScript in order to dynamically generate some or all of the script. You cannot reliably minify such JavaScript before the PHP is actually processed, so you'd be looking at minifying it after the PHP is run, on every request. There should be very little value to this, as the vast bulk of your JavaScript should not be written inside your PHP files.

Whatever the php script generates as output is sent to the browser. If you sent the "actual" output of the script instead of the output stream to another application and fetch and redirect that output - yes, you can do just anything.
Another question is how feasible and reasonable that approach is. uglifyjs2 is a node.js appliation, and therefore this step would require some kind of interprocess communication, i.e. extra-complexity and extra time/memory/points-of-failure and all that good stuff.
Maybe it's worth the effort, maybe not. Maybe something like https://github.com/tedious/JShrink will suffice...

Related

Is it good way to write all js code in html page and include that html page in another html page instead of .js file?

I just want to know that is it a good idea to write all javascript code in an HTML(instead of writing in .js) file and that the HTML file will be included in another HTML file.
I'm writing javascript code in an HTML file because I can access Django templates in an HTML file. Django templates are not accessible in pure JS files.
It'll really depend on your project. If the script is as simple as to show an alert box or something then by all means keep it in the same html file.
But if you need much more complex things then it's always better to have a separate file for each of the requirements. Because when the project gets bigger you'll have a hard time going through some thousand line of code. But if those are in separate files readability becomes much easier

Web Project Structure - Can I nest server file in HTML file?

I'm fairly new to web development, but I've take a couple hands-on introductory courses where the basic structure of a website is you have an HTML file (or several ejs template files), a CSS file, and then you have something called a "server" file which is a JavaScript file, typically called app.js
I know that if I wanted to, I could include all my CSS inside the HTML file in <style> tags. My question is, should I have the need to do so, would it be possible for me to include the server code in <script> tags within a single HTML file?
Debating on whether or not I should put forth the effort to attempt to do so, but if it's generally problematic (note: I don't car about "best practice") for some reason such as it prevents you from working with certain packages/modules like EJS or Node.js then I will likely conclude that it doesn't make sense to try. As a novice web developer, I fear I am not aware of certain restrictions that I would likely run into if any (besides lack of module support).
would it be possible for me to include the server code in <script> tags within a single HTML file?
No.
Web browsers, which execute the scripts in webpages, do not provide JavaScript programs with the APIs required to run a server.
If you want to write a web server in JavaScript then you'll need to run it using a tool like Node.js and not a web browser.
No, you cannot put server-side code in HTML or in JS files that are sent to the client. Server-side code is code that is run on a server that you host, while client-side code is code that is run on the computer of the person who is visiting your webpage.
You can put client-side code in HTML <script> tags., and you can also include client-side code in separate JS files. The drawbacks are immense though, as:
Code highlighters won't be able to highlight your code and point out things like syntax errors as easily.
You won't be able to use tools like Babel and TypeScript, which input and output JS files.
You can't use NPM modules without a bundler like Webpack, which itself will output JS files.
Overall, there isn't really a good reason to use the <script> tag. It takes 2 extra seconds to just create another file for your JS, and it's more organized, more modularized and easier to use with external tools.

Create JavaScript with Umbraco

Do you know if there is a way to create a javascript or PHP file in Umbraco?
By this I mean that I would like to have the user populate fields in the same was as creating a page, but for it to output as .js or .php rather than the usual .aspx
Many thanks!
I don't see any reason why you couldn't; you could use a controller to output as pretty much anything by setting the mime-type.
I'd be wondering though what your use-case for generating PHP files would be though; the server would still need to process the PHP script somehow; which may not be a trivial task (unless you just want to generate the PHP file for download/display the code) as it isn't client-side script at all.
JavaScript on the other hand is entirely a different matter - as it's processed client-side you could just point the browser at a url that happens to deliver the generated JavaScript. You could even render the JavaScript out from a WebApi end-point and the browser client would quite happily consume it.

How to use Javascript to analyze arbitrary html files

I want to use javascript to analyze an html file (I want to find the sum of a column in a table). I have the code that I think will do this, but I am unsure of how to tell the javascript where to find the html file.
Most of the things I've done with javascript have involved linking the html file to the javascript by including a tag in the html file. However, in this case I want to be able to run this analysis on arbitrary html files. Is there an argument I can pass in to the javascript somewhere?
Edit: to those asking to see my code, I am using something a lot like the script in the top answer of this stack overflow question: Sum total for column in jQuery
I am trying to make a tool that can be used on various different html files with tables, although all of them should be hosted in the same directory. It is looking like I would need to use AJAX to do this. Can the script be run without being attached to an html file?
Edit 2: I was originally intending to call this javascript from inside a perl script. However, I am now discovering that Perl might already have some support for analyzing html documents with the HTML::TreeBuilder module. Depending on how hard it is to use, I might just implement the same function with that in a perl script instead of messing around with javascript.

How should I structure my Javascript code in files?

On an average I have 30-50 lines of javascript specific to the page (it is not common). How should I structure my Javascript code so that it gets downloaded in an optimal manner on the client.
If I put that in a common Javascript file then the initial page download will take time because it will even download Javascript code that is not for that page.
If I put it embedded within the page itself then disadvantage is that it is never cached.
How should I structure it in optimal manner? I have round about 30-50 pages in my application.
When developing, use a logic folder structure so you can put all files in a folder and you can find them easier. When in production, concat and minify your code/files and use one reference to that minified file.
If you want to seperate even in production, think of a way to concat files as much as possible and minify that files. To load files when needed, you can think of a library as RequireJS.
I have the same directory/file structure under js as I have for the html (php) files, so when I'm on any url (/example1/example2) I include /js/example1/example2.js

Categories

Resources