I have an AngularJS project and I want Webpack to handle the build process for me, I need Webpack to output JS files dynamically from the src folder into my HTML template, and output them like this:
<script src="source1.js"></script>
<script src="source2.js"></script>
<script src="source3.js"></script>
...
Is it possible to load files in specific order rather than getting them all in one big file?
Related
After I build my angular app, I have to perform one last manual step to get my program to run: The platform it runs on has a requirement that its javascript file is in the <head> of the html file that is running. Their .js is on a CDN. So basically, post-build, I have to open up index.html and add the following:
<script src="https://cdn.fragilecorp.com/lib/js/platform.js" type="text/javascript"></script>
Is there a way I can accomplish this automatically using angular configs or a different way?
Yes, in .angular-cli.json (.angular.json in older versions), I believe you can add what's inside of src to the scripts array.
Check this out:
How to include external JS file to angular5?
I am new to Nodejs and having trouble when trying to export my js code which exists in HTML files to more js files for less redundancy in code (same js code in two HTML files):
I exported the js code from both HTML files to js a file (js_code_file.js for example) and tried using <script src="js_code_file.js" type="text/javascript"></script> in the header of the HTML file in order to import the file, and it did not work.
Is it because of Nodejs? and if it is - is there a way to "require" these files somehow?
<script src="js_code_file.js" type="text/javascript"></script>
Looking at the src attribute, js_code_file.js has to be in the same path as the .html file referencing it; that is, it has to literally be in the same folder. So make sure that is the case, otherwise, update your question with any errors you might be receiving.
I meet a problem when using pdf.js to view pdf. The problem comes from the
PDFJS.workerSrc setting. Is that possible to include the pdf.worker.js in
header, like
<script type="text/javascript" src="./../jsfiles/pdf.worker.js"></script>
and not using
PDFJS.workerSrc = './../jsfiles/pdf.worker.js';
is that possible for that? Thanks a lot.
From the documentation:
In order to bundle all src/ files into two production scripts and build the generic viewer, run:
$ gulp generic
This will generate pdf.js and pdf.worker.js in the build/generic/build/ directory. Both scripts are needed but only pdf.js needs to be included since pdf.worker.js will be loaded by pdf.js. The PDF.js files are large and should be minified for production.
This means that you only need to add the following code:
<script type="text/javascript" src="pdf.js"></script>
Remember that all files generated should located in the same directory as pdf.js
Let's say I have folder scripts with 10 javascript files and instead of doing this:
<script src="scripts/js1.js"></script>
<script src="scripts/js2.js"></script>
<script src="scripts/js3.js"></script>
<script src="scripts/js4.js"></script>
<script src="scripts/js5.js"></script>
//and so on...
I want to do this:
<script src="scripts/*"></script>
which load ALL files in scripts
How would I go about doing this?
Won't be as easy as that, here is what you are looking for:
How can I include all JavaScript files in a directory via JavaScript file?
What you try to achieve is cumbersome from the client side. You can achieve the same through server-side scripting before you load your html file. You can use a task runner tool like grunt, gulp etc. (or write a script) that will traverse your target directory, retrieve all the *.js file paths and append their script tags in your html file.
Another solution is to use a tool to concatenate all your js files in one bundle file and only load that file from your html file. There are plenty of tools out there to do that
I am using react starter kit for client side programming. It uses react and webpack. No index.html or any html to edit, all js files. My question is if I want to load a vendor js lib from cloud, how to do I do that?
It would be easy to do that in a html file. <script src="https://forio.com/tools/js-libs/1.5.0/epicenter.min.js"></script>
However, in js file, it only uses npm installed packages. How can I import the above lib with no html file? I tried import and require, they only work for local files.
update 10/21/15
So far I tried two directions, neither is ideal.
#minheq yes there is a html file sort of for react start kit. It is html.js under src/components/Html. I can put cloud lib and all its dependencies there like this:
<div id="app" dangerouslySetInnerHTML={{__html: this.props.body}} />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://forio.com/tools/js-libs/1.5.0/epicenter.min.js"></script>
<script src="/app.js"></script>
<script dangerouslySetInnerHTML={this.trackingCode()} />
</body>
Good news is it works, I don't need do anything else in js file, no import or require. However, now I have two jquery libs loaded in different ways. One in here, the other through npm and webpack. I wonder it will give me trouble later. The react-routing I use give me 'undefined variable' error if I type a none home path in browser window due to the server side loading I guess. So this solution is not very good.
Use webpack externals feature. This is documented as: link. "You can use the externals options for applications too, when you want to import an existing API into the bundle. I.e. you want to use jquery from CDN (separate tag) and still want to require("jquery") in your bundle. Just specify it as external: { externals: { jquery: "jQuery" } }."
However, the documentation I found a few places are all fussy about how to do this exactly. So far I have no idea how to use it to replace <script src="https://forio.com/tools/js-libs/1.5.0/epicenter.min.js"></script> in html.
externals is not intended to let you do this. It means "don't compile this resource into the final bundle because I will include it myself"
What you need is a script loader implementation such as script.js. I also wrote a simple app to compare different script loader implementations: link.
var $script = require("scriptjs");
$script("//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js", function() {
$('body').html('It works!')
});
You can create a script tag in your JS as
$("body").append($("<script src="https://forio.com/tools/js-libs/1.5.0/epicenter.min.js"></script>"))
There is one html file that is definitely being used to serve to users with your js bundle attached. Probably you could attach the script tag into that html file
Use webpack's externals:
externals allows you to specify dependencies for your library that are
not resolved by webpack, but become dependencies of the output. This
means they are imported from the environment during runtime.
I have looked around for a solution and most of all proposals were based on externals, which is not valid in my case.
In this other post, I have posted my solution: https://stackoverflow.com/a/62603539/8650621
In other words, I finished using a separate JS file which is responsible for downloading the desired file into a local directory. Then WebPack scans this directory and bundles the downloaded files together with the application.