I am new to webpack and angular-cli. My problem is that when I create an Angular 4 project using angular-cli, everything works fine with ng-serve but everything get bundled by default. Web pack bundling info:
I am not able to see the component.ts files in browser to debug. Is there any way to disable the bundling? angular-cli version details:
When you do ng serve with the CLI, it will create sourcemap files by default. That means, that although the source files are bundled together, you can view the original source files in the debugger and step through them.
You find them in the DevTools under the tab Sources, the folder webpack://
If you want to view your prod build like this, you can add the flag -sm for sourcemaps. In the prod build, there won't be sourcemaps by default.
ng serve --prod -sm
Yes also you can enable and disable this from the developer tool option
Go to setting (press F12 then F1 ). Under the source you can enable and disable to source mapping. In deploy time you not going to put the map file so this will not get downloaded.
Developer tool settings
Use following build command to hide your source code under Sources Tab
ng build --no-sourcemap ( Development environment )
ng build --env=prod --no-sourcemap (Production environment)
Related
I tried so but I have a 'require is not defined' error. I can't find information about that, can someone enlighten the noob in me please?
It's possible, but you have to be careful. Trying to require() a package means that node will try to locate its files in your file system. A chrome extension only has access to the files you declare in the manifest, not your filesystem.
To get around this, use a module bundler like Webpack, which will generate a single javascript file containing all code for all packages included through require(). You will have to generate a separate module for each component of your chrome extension (e.g. one for the background page, one for content scripts, one for the popup) and declare each generated module in your manifest.
To avoid trying to setup your build system to make using require() possible, I suggest starting with a boilerplate project. You can check out my extension to see how I do it.
An updated answer for 2022
Short answer: yes, you can require/import packages. Rather than going through the tedious work of setting up & configuring a bundler like Webpack on your own (especially if you have no experience with them), there are now build tools you can use to create the boilerplate "scaffolding" for a Chrome extension:
Extension CLI -- this one is well-documented and you can also reference the source code of some Chrome extensions that have used this tool (READ: learn how others have set up their code).
Chrome Extension CLI
Benefits of using them:
New projects are initiated with a default project file structure. Super helpful.
They support modern Javascript (ES6, ES2021), so modules work fine.
They already have bundlers integrated and pre-configured (Webpack in both above cases I think). You therefore don't need to install and configure any on your own.
You can use npm as normal to install any packages/dependencies you need.
Then of course, let the official documentation for Chrome Extensions guide you through the rest.
It's not possible to require node modules directly within a chrome extension. However, it is possible to bundle node applications and packages into the browser for use with your extensions. See here for more: Is it possible to develop Google Chrome extensions using node.js?
Yes, It is possible with esm npm packages.
require is commonjs module loader.
Browser doesn't support commonjs modules system
so that this error showed.
Method 1:
Run npm init -y and add "type" :"module" in your package.json.
create path.js file
add this line in path.js
const fullPath = await import.meta.resolve("npm-pkg-name");
const path = fullPath?.match(/(/node_modules.*)/)[0];
console.log(path);
add this line inside package.json
"path": "node --experimental-import-meta-resolve path.js",
Copy console output text. Replace package name with this copied path.
Method 2:
Install other npm package to find and replace
npm packages' virtual path to real path so that chrome browser will find it.
Install Path-fixxer
Add this line in path.js
import setAllPkgPath from "path-fixxer";
setAllPkgPath();
then run command : npm run path.
Now open browser to test it.
I tried so but I have a 'require is not defined' error. I can't find information about that, can someone enlighten the noob in me please?
It's possible, but you have to be careful. Trying to require() a package means that node will try to locate its files in your file system. A chrome extension only has access to the files you declare in the manifest, not your filesystem.
To get around this, use a module bundler like Webpack, which will generate a single javascript file containing all code for all packages included through require(). You will have to generate a separate module for each component of your chrome extension (e.g. one for the background page, one for content scripts, one for the popup) and declare each generated module in your manifest.
To avoid trying to setup your build system to make using require() possible, I suggest starting with a boilerplate project. You can check out my extension to see how I do it.
An updated answer for 2022
Short answer: yes, you can require/import packages. Rather than going through the tedious work of setting up & configuring a bundler like Webpack on your own (especially if you have no experience with them), there are now build tools you can use to create the boilerplate "scaffolding" for a Chrome extension:
Extension CLI -- this one is well-documented and you can also reference the source code of some Chrome extensions that have used this tool (READ: learn how others have set up their code).
Chrome Extension CLI
Benefits of using them:
New projects are initiated with a default project file structure. Super helpful.
They support modern Javascript (ES6, ES2021), so modules work fine.
They already have bundlers integrated and pre-configured (Webpack in both above cases I think). You therefore don't need to install and configure any on your own.
You can use npm as normal to install any packages/dependencies you need.
Then of course, let the official documentation for Chrome Extensions guide you through the rest.
It's not possible to require node modules directly within a chrome extension. However, it is possible to bundle node applications and packages into the browser for use with your extensions. See here for more: Is it possible to develop Google Chrome extensions using node.js?
Yes, It is possible with esm npm packages.
require is commonjs module loader.
Browser doesn't support commonjs modules system
so that this error showed.
Method 1:
Run npm init -y and add "type" :"module" in your package.json.
create path.js file
add this line in path.js
const fullPath = await import.meta.resolve("npm-pkg-name");
const path = fullPath?.match(/(/node_modules.*)/)[0];
console.log(path);
add this line inside package.json
"path": "node --experimental-import-meta-resolve path.js",
Copy console output text. Replace package name with this copied path.
Method 2:
Install other npm package to find and replace
npm packages' virtual path to real path so that chrome browser will find it.
Install Path-fixxer
Add this line in path.js
import setAllPkgPath from "path-fixxer";
setAllPkgPath();
then run command : npm run path.
Now open browser to test it.
The tutor of my video tutorial compiles javascript/react on save with his VS Code editor. Mine dont.
How can I config VS Code to do this?
Remark:
I find solution for typescript "Visual Studio Code - compile on save", but suggested solution does not work for me.
#added information:
the project is created with create.react-app. npm start opens chrome, and compilation errors are then shown in Chrome's DEV-Console, as noted by Shishani . My tutor has the code compiled on VS Code Terminal console. This offers a quick check of the code compilation before I go to webbrowser, to check for errors there, which seems to me much more intimidating with it's long callstacks.
If you create your React project with create-react-app, and then start it with npm start, it compiles automatically on save. Also, if it's not a React project, but just JS/HTML/CSS, you can open your project with the "Live Server" extension in VSCode, and it will update your page every time you save a file in your VSCode workspace.
You can even do like I do, and enable constant autosave, with the AutoSave: afterDelay setting in VSCode (if you dare), and then set the save delay to a super small number (mine is 2ms) so you don't even need to hit save anymore (go to File>Preferences>Settings>[Search "Auto Save"]).
If you have created react project using there official cli tool (create-react-app) then on saving js files it gets re-compile. This is because they are using webpack bundler behind the scene.
If you have created normal html or js files without any such cli tools then it won't work directly, You have to use some kind of bundler with dev-server support to do it.
Parcel! is a great bundler to use without configuration
I have an Angular application which is build with the Angular-CLI. I want to deploy this project to a Spring-Microservice. When i deploy the build, which generated with the cli-command ng build --prod the application works fine.
But: I want to deploy the Original files including my typescript files to the Microservice so i can see where my console-logs come from (which line in which file, just the same as when i run the application locally with "ng serve").
A few months ago, i had a Angular-project which i started with the Angular Quickstart. I ran this project with NPM (commands: npm install and npm start). This project transpiled the typescript-files to javascript, and for this reason, this project is deployable and works fine.
But my CLI-project dont do that, so it is not deployable.
How can I deploy my application as described?
In this presentation, I generated an Angular project with CLI, generated a REST server using Spring Boot, and showed how to deploy this Angular app under the Spring server https://youtu.be/k8r76d8QzXs?t=870.
To be able to debug your Typescript code with prod bundles, use add the --sourcemaps option:
ng build -prod --sourcemaps true
When you build an Angular project with the production flag the Angular CLI creates and the sourcemaps (you can identify them from their extension .map.js) of the minified Javascript files.
The easiest way to go is to deploy all the generated files.
When you open the developer tools of the browser to debug them the browser recognize them, and generates the unminified Javascript files. You can find them on Sources Panel. (e.g. Chrome)
So you don't need the original Typescript files
I am not sure, if you can see on your console when you log something, the exact file and line where the original code is, from a minified Javascript file.
I am working on a web application using angular 2 with typescript and angular-cli. I wanted to make a test on the server but I didn't want to upload the source code then I tried using the command "ng build --prod". I see that the .map files was on the folder yet and I deleted all .map files and I uploaded the folder on the server, When I opened the dev tool on chrome I saw the source code with TypeScript. The files that I didn't delete were the .gz files. My question is What is the correct way to build a production project without the typescript code with webpack?.
Sorry but I don't speak english well. And I don't know if I am describing in the corret way my dude.
What I want is not to show my TypeScript code for anyone that activate developer mode.
I'm not sure what you mean by, "build a production project without the typescript code".
When you build a project with ng build it compiles all the typescript into js and minifies it. For example, here is the output of ng build for me (note there are no typescript files here):
As you noted you can also use the -prod which sets the environment production value to true and gives you a slightly smaller, more optimized build. If you just want to test you app in the browser you can use ng serve or ng serve -prod.