-
-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom Webpack config for platform-webworker or worker-loader #13
Comments
You should add `module.exports = ` at the beginning of the file. The rest seems all right to me and should work. Doesn't it?
…On Wed, Aug 8, 2018, 22:29 Ajyress ***@***.***> wrote:
Hello,
I have to run some angular code inside a separated thread. With ng eject
being currently disabled, is it possible to use angular-cli-builders in
order modify the webpack configuration to use platform-webworker
<https://blog.angularindepth.com/angular-with-web-workers-step-by-step-dc11d5872135>
or worker-loader <https://github.com/webpack-contrib/worker-loader> ?
Example: webpack config proposed for worker-loader:
// webpack.config.js { module: { rules: [ { test: /\.worker\.js$/, use: {
loader: 'worker-loader' } } ] } }
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#13>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AJXjb62mYjxiNqtaW-0GS69PqppoARVtks5uOzwrgaJpZM4V0hwi>
.
|
Thank you for the help, it compiled as expected but in the end I don't think the worker-loader package is the good way to go for angular. I'm trying to implement platform-webworker following this tutorial. Here is my new extra-webpack.config.js for angular-cli-builders :
When running ng build I get the error "An @ngtools/webpack plugin already exist for this compilation" |
Are you using |
I indeed forgot the option I still have an error I don't know how to resolve on the browser console, but I don't think it's related to angular-cli-builders
|
Have you specified the original config for the replaced plugins like it is mentioned in #14? |
You are right, i didn't specify the original config for the replaced plugins! On the links that you give in #14, I see that some plugin options are given by |
After some research, I managed to get rid of the previous error and some others. I'm now stuck on the exact same error that someone asked of StackOverflow yesterday. |
Could you create a minimal reproduction case so that I would be able to investigate? |
Thanks again for proposing your help. Here is a minimal reproduction of the error, with angular CLI 6.1.3: Change src/app/app.module.ts with the following code:
Change src/main.ts with the following code:
Create src/workerLoader.ts file containing the following code:
Configure angular.json as following:
Create an extra-webpack.config.js file with the following code:
Run |
I finally managed to get it work! I had to add the following option to Not related to the issue, i also had to replace |
Great, happy to hear that! Would you share your final configuration with us, please? |
My final configuration: angular.json:
extra-webpack.config.js:
I'm still new to wepback, I can't tell this configuration is optimal or not but it works |
Hey I tried creating an Angular app running in a web worker following your configuration but I cannot get it working. All I'm getting is an empty page, the workerLoader.ts is never executed. I assume for some reason the worker isn't loaded properly, but I don't know why. Did something change in the default plugin config that is overwritten? |
I assume you're trying to apply the configuration @Ajyress provided? |
@meltedspark Yeah I was trying @Ajyress configuration. No luck with changing |
Would you mind providing a repository with minimal reproduction? |
Hi, The configuration changes quite a lot now that there is plugins merge support. Here is a reproduction with angular CLI 6.2.2: Change src/app/app.module.ts with the following code:
Change src/main.ts with the following code:
Create src/workerLoader.ts file containing the following code:
Configure angular.json as following:
Create an extra-webpack.config.js file with the following code:
|
@Ajyress so you're saying it's not working with the new config? Or does it? |
it works for me with the new config I gave today. It also works when I set In this situation, I think it is better not to use |
@Ajyress great, thank you very much. |
@Ajyress Thanks, I'll give it a try! @meltedspark Yeah will do later! Thanks! |
I can confirm the configuration provided here works, applied it to my project just now. The only thing I had to enhance was absolutizing the path to the
otherwise I kept getting build errors, with complaints that the |
Hmm, now it doesn't work anymore. Strange, as I can't understand the change in my environment that broke it. I can reproduce the error creating an app from scratch (the example from above with |
How it goes? Is this |
Here is all we can do for now. According to just-jeb/angular-builders#13 (comment) then unless they find a way to workaround the "Uncaught ReferenceError: window is not defined" problem then this task stop here
It's not working for me any of this example with angular 7. angular.json config "build-webpack": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./extra-webpack.config.js",
"replaceDuplicatePlugins": true,
"mergeStrategies": {
"entry": "append",
"output": "replace",
"optimization": "replace"
}
},
"outputPath": "dist/ng722",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
}
},
"serve-webpack": {
"builder": "@angular-builders/dev-server:generic",
"options": {
"browserTarget": "ng722:build-webpack"
}
}, webpack-extra.config.js const path = require('path');
const entryPoints = ["inline","polyfills","sw-register","styles","vendor","main"];
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AotPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
module.exports = {
"entry": {
"webworker": [
"./src/workerLoader.ts"
]
},
"output": {
"path": path.join(process.cwd(), "dist"),
"filename": "[name].bundle.js",
"chunkFilename": "[id].chunk.js",
"globalObject": "this"
},
"plugins": [
new HtmlWebpackPlugin({
"template": "./src/index.html",
"filename": "./index.html",
"excludeChunks": [
"webworker"
],
"chunksSortMode": function sort(left, right) {
let leftIndex = entryPoints.indexOf(left.names[0]);
let rightIndex = entryPoints.indexOf(right.names[0]);
if (leftIndex > rightIndex) {
return 1;
}
else if (leftIndex < rightIndex) {
return -1;
}
else {
return 0;
}
}
}),
new AotPlugin({
"mainPath": "main.ts",
"entryModule": 'src/app/app.module#AppModule',
"exclude": [],
"tsConfigPath": "src/tsconfig.app.json",
"skipCodeGeneration": true
})
],
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
name: 'vendor',
test: /\/node_modules\//,
chunks: 'all',
priority: 0,
enforce: true,
},
},
},
},
}; workerLoader.ts console.log('worker');
import './polyfills.ts';
import '@angular/core';
import '@angular/common';
import { platformWorkerAppDynamic } from '@angular/platform-webworker-dynamic';
import { AppModule } from './app/app.module';
platformWorkerAppDynamic().bootstrapModule(AppModule); main.ts import {enableProdMode} from '@angular/core';
import {bootstrapWorkerUi, WORKER_UI_LOCATION_PROVIDERS} from '@angular/platform-webworker';
import {environment} from './environments/environment';
if (environment.production) {
enableProdMode();
}
console.log('main');
bootstrapWorkerUi('webworker.bundle.js', WORKER_UI_LOCATION_PROVIDERS); package.json "bworker": "ng run ng722:build-worker",
"sworker": "ng run ng722:serve-webpack"
Open the browser, connect to localhost:4200, and open the console. The app prints:
expected behavior
If I comment all lines of workerLoader.ts except console.log('worker'), then the app prints this log, but if I add only polyfills, no logs printed. My conclusion: I think this is a webpack problem. Any ideas? |
@Serginho I'd assume it's not even a webpack problem but some configuration issue. Since this problem is quite common I'd like to find a solution and add it to the documentation. Probably someone of Angular CLI team can shed some light on this mystery (how the webworkers should be enabled for Angular 7+). @hansl, @filipesilva, @mgechev, guys, any suggestions? I'm reopening this issue just to track it, labeling as |
@meltedspark Here is the proof of concept: https://github.com/Serginho/angular-platform-webworker I can confirm it's not working for The Thanks for helping. |
Angular CLI has no support for webworkers currently. angular/angular-cli#12575 aims to support bundling workers, angular/angular-cli#2305 tracks using workers in a CLI app, and angular/angular-cli#2305 tracks running Angular in a web worker. |
For anyone who's coming to this thread looking to run part of his application in WebWorker - it is officially supported since Angular 8: https://angular.io/guide/web-worker |
Did you do some tests? @meltedspark angular 8 deprecates platform-worker, we dont have any guarantee if it will works properly and sngular cli 8 autobundle workers but it's not compatible with lazy loading. |
I personally did not do any tests but this guy definitely did. |
@meltedspark This guy bundles stuff in a workers. But it didn't test platform-webworker. I pretty sure angular-cli won't work with lazy loading because you need two webpack targets amd angular-cli only supports one (target=web). To say webpack: hey compiles me this part with target=web and this other part with target=webworker you will have lots of changes in the CLI. That's the problem. More info: https://webpack.js.org/concepts/targets/ |
Hi @meltedspark, I have a question with custom web worker in angular 8, could you provide some advices? |
Hello,
I have to run some angular code inside a separated thread. With ng eject being currently disabled, is it possible to use angular-cli-builders in order modify the webpack configuration to use platform-webworker or worker-loader ?
Example: webpack config proposed for worker-loader:
// webpack.config.js { module: { rules: [ { test: /\.worker\.js$/, use: { loader: 'worker-loader' } } ] } }
The text was updated successfully, but these errors were encountered: