Context
For a long time I was using Bootstrap and other libraries by sources. I mean I was including it as below:
- static
- vendor
- jquery
- popper
- bootstrap
- ...
But nowadays, I need to improve myself in managing my web dependencies. For some reasons :
Less sources in repositories and a more efficient team project
Greater support to choose version or test migrations
Saving time for later projects with same dependencies
I need to include a library which is only provided through NPM which is codemirror
So I am currently trying to manage dependencies via NPM, in a django project. But I'm stuck at a very basic step I guess. I don't know where to continue after npm install --save jquery popper.js bootstrap. I'm disturbed by all examples which are for node.js application...
Where I'm stuck
I have an index.js at the same level of my main package.json. I thought I had to import my scripts in this file, then include this script in my page. So I tried require Jquery then bootstrap, but I get errors on bootstrap required because jquery is undefined.
I don't have much code to show, the more I need is to understand how it works step by step rather than having it done to me.
Questions
My primary goal is to manage my main javascripts, I mean scripts that must appear through the whole site. Wrap them into one script, then include this script in my base.html assuming I'm in a django project.
I know that bootstrap < v5 depends on Jquery and popper.js, but it comes with its own package.json, isn't that enough ? Do we have to npm install jquery popper.js them anyway, as some people suggest in some SO thread or else ? Even after running npm install inside the node_module/bootstrap ? Additionally, the bootstrap.bundle.js contains popper.js, and node_modules inside bootstrap contains jquery. Why not use those ? See Bootstrap import with webpack Otherwise why are they there ?
How to bundle my javascript dependencies ? Via webpack ? For example, Jquery from bootstrap/node_modules/jquery/dist/jquery.js with bootstrap/dist/js/bootstrap.bundle.js ? Then I will have same versions used by bootstrap developers.
Then how to compile the wanted scripts into one script, and minify it for example ? Then I could get those scripts by collectstatic function into the folder where I group all my desired statics.
Perhaps I'm not clear, because all of this blurred my mind.
Webography
Use Sass/css within django, NPM way
Add react in django project, via NPM
Bootstrap package manager
Bootstrap import with webpack
Npm install jquery, poper, bootstrap
React with webpack
Editions with my progress
package.json
{
"name": "static_src",
"version": "1.0.0",
"description": "Static files from NPM.",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "npx webpack -c webpack-conf.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"bootstrap": "^4.5.3",
"codemirror": "^5.58.2",
"jquery": "^3.5.1",
"popper.js": "^1.16.1"
},
"devDependencies": {
"webpack": "^5.6.0",
"webpack-cli": "^4.2.0"
}
}
webpack-conf.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './index.js',
output: { path: __dirname, filename: 'dist/bootstrap-bundle.js' }
};
index.js
import $ from 'jquery';
import {} from 'popper.js';
import 'bootstrap'
npm run build
> npx webpack -c webpack-conf.js
[webpack-cli] Compilation finished
asset dist/bootstrap-bundle.js 169 KiB [emitted] [minimized] (name: main) 1 related asset
runtime modules 1.13 KiB 5 modules
cacheable modules 508 KiB
./index.js 71 bytes [built] [code generated]
./node_modules/jquery/dist/jquery.js 281 KiB [built] [code generated]
./node_modules/bootstrap/dist/js/bootstrap.js 140 KiB [built] [code generated]
./node_modules/popper.js/dist/esm/popper.js 86.4 KiB [built] [code generated]
webpack 5.6.0 compiled successfully in 4541 ms
Result
Well for now I manage to include this bundle into my site, but an error occured, and it seems not including Jquery : ReferenceError: $ is not defined. Notably it occurs on this snippet for example :
$(function () {
$('[data-toggle="popover"]').popover()
$('[data-toggle="tooltip"]').tooltip()
})
Any idea ?
Answer to Q1, assuming you have run npm install bootstrap and you can find 'popper' and 'jquery' in the node_modules folder:
Add import 'bootstrap'; in your index.js (https://getbootstrap.com/docs/4.0/getting-started/webpack/)
NOTE:
IF you cannot find 'popper' and 'jquery' in the node_modules folder, THEN It's worthwhile to install them via npm using npm install #popperjs/core & npm install jquery
IF you had to install popper and jQuery manually for some reason, THEN Add import $ from 'jquery'; & import {} from '#popperjs/core'; in your index.js
I came to a great solution !
webpack-conf.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './index.js',
output: {path: __dirname, filename: 'dist/bootstrap-bundle.js'},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
]
};
index.js
import $ from 'jquery';
window.jQuery = $;
window.$ = $;
import {} from 'popper.js';
import 'bootstrap'
Then importing this script works well. Thanks to #Karanveer that unblurred my mind to put me on the right way. And this thread about webpack issues
Related
I'm making an Api that returns a pdf by puppeteer. I just instaled puppeteer with npm install chrome-Aws-lambda, install puppeteer --save-dev but when I run the Api, I get this exception.
I tried runing npm install but it doesn't work, how can I install chromium or make puppeteer works???
this is my code and package.json
let browser = await chromium.puppeteer.launch({ headless: true });
let page = await browser.newPage();
await page.goto("https://www.google.com");
const pdf = await page.pdf({
format: "A4",
printBackground: false,
preferCSSPageSize: true,
displayHeaderFooter: false,
headerTemplate: `<div class="header" style="font-size:20px; padding-left:15px;"><h1>Main Heading</h1></div> `,
footerTemplate: '<footer><h5>Page <span class="pageNumber"></span> of <span class="totalPages"></span></h5></footer>',
margin: { top: "200px", bottom: "150px", right: "20px", left: "20px" },
height: "200px",
width: "200px",
});
return {
statusCode: 200,
// Uncomment below to enable CORS requests
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: pdf
};
Pakage:
{
"name": "amplifysandboxpdf",
"version": "2.0.0",
"description": "Lambda function generated by Amplify",
"main": "index.js",
"license": "Apache-2.0",
"devDependencies": {
"#types/aws-lambda": "^8.10.92",
"#types/puppeteer": "^5.4.6",
"puppeteer": "^17.1.2"
},
"dependencies": {
"chrome-aws-lambda": "^10.1.0",
"puppeteer-core": "^10.0.0"
}
}
Usually, when you upload a zip to lambda, you need to provide both - your source code and node_modules (modules can also be added as a lambda layer). In your case error is because of missing package, so the first place I'd look at is the zip you provide to lambda and does it contain all the necessary packages
I see in your package.json you have both puppeteer and puppeteer-core. Puppeteer downloads chromium on installation (that's the bit that errors for you). First you need to decide if puppeteer is even necessary, maybe core package is enough, but if it is necessary - once again, it should be in your zip that you provide.
I'm not sure about how puppeteer does this, but if it downloads chromium into node_modules it should work once your zip file is correctly packaged. Otherwise, you might need to create your own docker container by using puppeteer base image Puppeteer base image.
If this is the case, these are the steps you need to take:
Create a dockerfile with puppeteer base image, build your own image and make sure your application is installed in there. Example of what this file could look like:
FROM ghcr.io/puppeteer/puppeteer:16.1.0
WORKDIR /home/pptruser/app
ADD ./package-lock.json /home/pptruser/app/package-lock.json
ADD ./package.json /home/pptruser/app/package.json
RUN npm ci
# Customize the line below - copy files that your application requires
ADD ./src/. /home/pptruser/app/src/
# Remove development dependencies (in your case puppeteer is not a devDependency)
RUN npm prune --production
CMD [ "index.js" ]
Test locally if it works in the container and later publish this image to ECR (AWS registry for docker containers)
Launch lambda by using the image instead of using NodeJS environment
I am using ionic2 build .
I did ionic plugin add cordova-plugin-file and used following code.
import {File} from 'ionic-native';
#Injectable()
export class GlobalVars {
constructor(platform:Platform) {
platform.ready().then(() => {
this.appRootFolder = cordova.file.documentsDirectory;
}
}
}
then I did ionic build android and I got this error
Cannot find name 'cordova'
After 2 hour of struggling , I resolved the issues by following commands
npm install -g typings
typings install dt~cordova --save --global
typings install dt~cordova/plugins/filesystem --save --global
This helped in building android , but still fails for iOS. when I run this ionic build ios I still get
Cannot find name 'cordova'
I wrote this line(declare var cordova:any;) at the top of file
import {File} from 'ionic-native';
declare var cordova:any;
And the problem is solved for me.
As of lately, you can do this:
In CLI, from your project folder:
ionic plugin add cordova-plugin-file
Then, in your component/class file:
import { File } from 'ionic-native';
declare var cordova: any;
const fs:string = cordova.file.dataDirectory;
File.checkDir(this.fs, 'mydir')
.then(_ => console.log('yay'))
.catch(err => console.log('boooh'));
Many native plugins are now well implemented and documented by the Ionic Team :)
Source: Ionic Native docs
Try copying cordova.d.ts file and plugins folder from here - https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/cordova230
And paste this folder and file in your project/typings directory.
I am looking for way to modify output of build function from
ng build --prod
would like to add some bootstrap css files to head section of index.html, change name index.html => index.php etc.
how to do it ?
You can customise the implementation from the source code. This section explains how you can proceed.
In particular, you can change the following lines from addon/ng2/models/webpack-build-common.ts.
new HtmlWebpackPlugin({
template: path.resolve(projectRoot, `./${sourceDir}/index.php`),
chunksSortMode: 'dependency'
})
For the CSS resources, they can be packaged directly if you use Webpack Angular CLI.
EDIT
The key index in the apps nodes can be modified in the angular-cli.json file. The line should be changed to
"index": "index.php",
I find work around this process. In package.json modify build script to:
"build": "ng build .......... && mv dist/index.html dist/index.php"
I want to have a manifest in my ember app, because I want to use it on mobile devices and have some offline capabilities.
I have found the plugin ember-cli-deploy-manifest:
https://github.com/ember-cli-deploy/ember-cli-deploy-manifest
It is well documented in the readme and looked promising.
But how do I use the plugin?
I have installed it using this command:
ember install ember-cli-deploy-manifest
I build my app using this command:
ember build -prod
No manifest is created. I am probably missing some simple step, but can't figure it out. How do I tell ember build to use the plugin?
UPDATE
I followed the suggestion in the comment of the answer below. I dropped using the manifest-plugin and created a manifest file manually. Then the challenge is to get proper fingerprinted filenames in the manifest file.
In my ember-cli-build.js file I have:
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
fingerprint: {
exclude: [],
extensions: ['js', 'css', 'png', 'jpg', 'gif', 'map'],
replaceExtensions: ['html','css','js', 'appcache']
}
});
...
My manifest file is called eea.appcache and is located in the /public folder. It is copied to the dist-folder during build.
However the content of the file (the list of filenames) are not fingerprinted. Filenames in the other files (html, css, js) are fingerprinted correctly.
Here is my public/eea.appcache:
CACHE MANIFEST
# 2016-03-15
# V 1.0
CACHE:
index.html
assets/vendor.css
assets/eea.css
assets/vendor.js
assets/eea.js
assets/img/Icon120x120.png
My buildstep is still:
ember build -prod
How to get the filenames in the manifets files updated with the MD5 fingerprint?
The ember-cli-deploy-manifest is for use with ember-cli-deploy.
So you need to run ember deploy -production once you have installed ember deploy.
Install ember-cli-deploy
ember install ember-cli-deploy
Then
ember deploy -production
I finally got it to work, but this solution is really hack'ish.
I figured this out by trial and error.
The broccoli-assets-rev has some tricky rules when it parses a file to find the filenames. The format that are normally used in a manifest file the filenames are not found by the parser.
But if the files are listed in the comments of the file with quotes and correct path is used, it will work.
If the manifest file is located in the root of the public folder it will end up in the root of the webpage. Then the paths will be as below.
My file looks like this:
CACHE MANIFEST
# 2016-03-15
# V 1.1
# '
# 'assets/vendor.css'
# 'assets/eea.css'
# 'assets/vendor.js'
# 'assets/eea.js'
# 'assets/img/Icon120x120.png'
CACHE:
index.html
assets/vendor.css
assets/eea.css
assets/vendor.js
assets/eea.js
assets/img/Icon120x120.png
NETWORK
*
Then the resulting built file looks like this:
CACHE MANIFEST
# 2016-03-15
# V 1.1
#
# 'assets/vendor-d41d8cd98f00b204e9800998ecf8427e.css'
# 'assets/eea-ddacde3bdf32d3f94c5a01a2054c6f72.css'
# 'assets/vendor-3229c2c849c3d52c0b362d9fee2106ad.js'
# 'assets/eea-4c760118f51f7402db2f0b6074b6960b.js'
# 'assets/img/Icon120x120-40b31b55211fb293dedf556a648aa47e.png'
CACHE:
index.html
assets/vendor-d41d8cd98f00b204e9800998ecf8427e.css
assets/eea-ddacde3bdf32d3f94c5a01a2054c6f72.css
assets/vendor-3229c2c849c3d52c0b362d9fee2106ad.js
assets/eea-4c760118f51f7402db2f0b6074b6960b.js
assets/img/Icon120x120-40b31b55211fb293dedf556a648aa47e.png
NETWORK
*
I try to learn composer, now I want to include (zurb) foundation, so I added
"require": {"zurb/foundation": "v5.2.2"} to the composer.json file.
After running composer.phar update, I can see that there are some files added to the folder /vendor/zurb/foundation.
But I have no clue how to continue, could anybody please advise how I can start building my web-app now? How do I get it to use the css and js files that are needed for foundation?
I already included the file vendor/autoload.php to my index.php, but that doesn't seem to be enough.
I already built multiple web-sites and apps using foundation, but always "manual", then I just include the right css and js files to the header and footer of the page. Now I just don't know where to start.
thanks for your help.
Check this question first to get the basics: NPM/Bower/Composer - differences?.
Then, if you decide to go with Composer for PHP and Bower for front-end libraries, follow this:
Install Bower using sh $ npm install -g bower (you'll need Node.js and npm first)
Configure Bower for you front-end packages (visit Bower docs for more information)
{
"name": "MyProject",
"dependencies": {
"foundation": "*"
}
}
Hook Bower to Composer adding this to your composer.json
"scripts": {
"post-install-cmd": [
"bower install"
],
"post-update-cmd": [
"bower install"
],
}
Now every time you hit composer update (or install), bower components get updated as well!