Skip to content
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

Using @ngtools/webpack with ContextReplacementPlugin plugin does not create lazy routes chunks #4431

Closed
artaommahe opened this issue Feb 4, 2017 · 21 comments
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix

Comments

@artaommahe
Copy link

artaommahe commented Feb 4, 2017

OS?

Ubuntu 16.04

Versions.

@ngtools/webpack - 1.2.7

Repro steps.

plugins: [
  new webpack.ContextReplacementPlugin(
    /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
    __dirname
  ),
  • run build npm run build
  • no chunks for lazy routes in dist folder
  • comment out ContextReplacementPlugin in plugins, build, chunks created

Mention any other details that might be useful.

this check never passes with ContextReplacementPlugin usage

if (!result.resource.endsWith(path.join('@angular/core/src/linker'))) {

@hansl

@artaommahe artaommahe changed the title @ngtools/webpack with using ContextReplacementPlugin in webpack config does not create lazy routes chunks Using @ngtools/webpack withContextReplacementPlugin plugin does not create lazy routes chunks Feb 4, 2017
@artaommahe
Copy link
Author

artaommahe commented Feb 4, 2017

I checked this in my current project - comment out ContextReplacementPlugin - chunks created successfully, but each chunk includes all imported code despite they are included in non-lazy main bundle.

As i understand it's due to this webpack.optimize.CommonsChunkPlugin usage

        new webpack.optimize.CommonsChunkPlugin({
            name: [ 'main', 'vendor' ]
        }),

For repo above main is ./app/main.aot.ts entry and vendor - added ./app/vendor.ts with contents like this

import 'core-js/es7/reflect';

import '@angular/core';
import '@angular/platform-browser';
import '@angular/router';

With this configuration any shared (between AppModule and FeatureModule) modules will be added both to main and chunk bundles.
If i leave it like so

        new webpack.optimize.CommonsChunkPlugin({
            name: [ 'main' ]
        }),

then SharedModule includes properly, only to main bundle.

I'l add final test repo link today.

@artaommahe
Copy link
Author

Added repo where both issues can be reproduced
https://github.com/artaommahe/webpack-aot

App contains 3 modules

  • main AppModule that imports SharedModule
  • lazy loaded FeatureModule that also imports SharedModule
  • SharedModule with commonly used component

For proper work there should be 3 bundles in ./dist folder on npm run build command

  • vendor.js with all common libs (angular, corejs)
  • main.js with AppModule and SharedModule
  • 0.js chunk with only FeatureModule

ContextReplacementPlugin issue

Uncomment this https://github.com/artaommahe/webpack-aot/blob/master/webpack.config.js#L21 and after npm run build there will be no chunks generated in dist folder

CommonsChunkPlugin issue

Run build as it is and see that 0.js chunk also contains SharedModule.
Change plugin options like so

        new webpack.optimize.CommonsChunkPlugin({
            name: [ 'main' ]
        }),

and 0.js chunk propely contains only FeatureModule, but main.js holds all app libs that should be in vendor.js

@artaommahe artaommahe changed the title Using @ngtools/webpack withContextReplacementPlugin plugin does not create lazy routes chunks Using @ngtools/webpack with ContextReplacementPlugin plugin does not create lazy routes chunks Feb 4, 2017
@nikolasleblanc
Copy link

nikolasleblanc commented Feb 22, 2017

https://github.com/AngularClass/resolve-angular-routes may help. On second look, it seems ancient and unmaintained.

@antonybudianto
Copy link
Contributor

the problem is still present on v1.2.11, but the comment trick still works

@danielRadiceski
Copy link

experiencing the same problem. Would be nice if we could have the app and vendor chuncks work together with lazy loading.

@danielRadiceski
Copy link

@artaommahe I believe I found something that could help. In v2 of webpack there are other options for the CommonsChunks plugin and splitting the vendor stuff out.

This config seemed to split out the vendor dependencies out and the extra chunck worked properly containing only the lazy modules. Here it is:

new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks: function (module) {
                // this assumes your vendor imports exist in the node_modules directory
                return module.context && module.context.indexOf('node_modules') !== -1;
            }
        }),

taken from: https://webpack.js.org/guides/code-splitting-libraries/#implicit-common-vendor-chunk

@artaommahe
Copy link
Author

artaommahe commented Feb 26, 2017

// this assumes your vendor imports exist in the node_modules directory

@danielRadiceski it's wrong assumption. In real app my vendor bundle contains common libs from node_modules and some locally saved and patched libs that placed at separate folder ./libs.

@danielRadiceski
Copy link

@artaommahe you should be able to tweak the function to your requirements. (i.e to include also any files that are under ./libs)

@kyranjamie
Copy link

Perhaps this should be a separate issue, will leave here for now. The plugin ("@ngtools/webpack": "^1.2.11") also silently fails to generate lazyload routes when using the webpack html-loader.
Also facing same issues with ContextReplacement & CommonsChunk plugin.

@filipesilva filipesilva added package4: @angular-sdk/webpack P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix labels Mar 9, 2017
@hansl hansl self-assigned this Mar 16, 2017
@photostu
Copy link

watching...

@brunopalaoro
Copy link

brunopalaoro commented Apr 26, 2017

I have the same problem with version 1.3.1. Any news on this?

@GeorgeKnap
Copy link

Having the same issue with version 1.3.1
DEV build uses [ 'ts-loader', 'angular2-template-loader', 'angular2-router-loader' ]
PROD build uses @ngtools/webpack

now, during dev builds I have to use this plugin, otherwise getting Critical dependency: the request of a dependency is an expression warnings:

        new webpack.ContextReplacementPlugin(
            /angular(\\|\/)core(\\|\/)@angular/,
            helpers.root('app')
        )

If I use this plugin also for PROD build, the @ngtools/webpack does not generate lazy loaded chunks. If the ContextReplacementPlugin is commented out, the @ngtools/webpack generates chunks normally.

So far I do not include ContextReplacementPlugin into my PROD build and it seems to work.

@GiuseppePiscopo
Copy link

Importing from now-closed duplicate #6518, please consider it happens also with these specs:

  • Output from: ng --version. ng is not installed in this project. @ngtools/webpack is.
  • output from: node --version: v6.2.2
  • npm --version: 3.9.5
  • OS: Windows 10 version 1703, as well as Mac OS Sierra
  • angular 4.1.3,
  • @ngtools/webpack 1.3.3,
  • webpack 2.5.1,
  • typescript 2.3.2,
  • tslib 1.7.1

on this demo project

@hheexx
Copy link

hheexx commented May 31, 2017

@GeorgeKnap Thanks for workaround but if I remove ContextReplacementPlugin I receive this error:

ERROR in ./ClientApp/aot lazy
Module not found: Error: Can't resolve 'C:\Users\Boris\Documents\Visual Studio 2015\Projects\cx_public\ClientApp\aot\app\modules\lazy.module.ngfactory.ts' in 'C:\Users\Boris\Documents\Visual Studio 2015\Projects\cx_public\ClientApp\aot'
@ ./ClientApp/aot lazy
@ ./~/@angular/core/@angular/core.es5.js
@ ./ClientApp/boot-client.ts

It does not output chunks.

Exact ContextReplacementPlugin line is:
new webpack.ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)@angular/, path.resolve(__dirname, 'notexist/'))

ngtools 1.4.0-rc.2 (tried also with 131 and 133)

@GiuseppePiscopo
Copy link

For completeness, here are current ContextReplacementPlugin config:

    new webpack.ContextReplacementPlugin(
      /angular(\\|\/)core(\\|\/)@angular/,,
      'D:\path\to\my\source\root',
      {}
    ),

and CommonsChunkPlugin config:

    new webpack.optimize.CommonsChunkPlugin({
      name: ['main', 'vendor'],
      'js/[name]-bundle.js',
      minChunks: mod => /node_modules/.test(mod.resource),
    }),

@GiuseppePiscopo
Copy link

@sumitarora all in all, I must say I'm not sure the two issues are really duplicate. Others here are reporting that they cannot build when commenting out that replacement plugin, while I can. Also, we don't even know if they share the same steps (e.g. does build fail? Does opening page fail? Does browsing to lazy module fail?) and the same stacktrace.

Just my 2c

@GiuseppePiscopo
Copy link

GiuseppePiscopo commented Jun 1, 2017

@artaommahe maybe I missed that in the thread, could you please state whether the error shows up at compile time, or runtime and doing what? Also, could you please show the error or stacktrace you see? This is to better understand if present issue and #6518 are really duplicate or not. Tnx

@artaommahe
Copy link
Author

@GiuseppePiscopo what error? my issues #4431 (comment) about completely missed lazy loaded chunks on build and duplicated shared modules in different chunks. It's about build time, not runtime.

@GiuseppePiscopo
Copy link

GiuseppePiscopo commented Jun 1, 2017

I see. So I guess you can browse to the app at runtime, then there should be some error in developer console when you try to access a lazy module URL, or maybe right away when app shows up, don't know. Could you please post the stacktrace in that case? Just to compare to what happens in #6518 . Thank you

@clydin
Copy link
Member

clydin commented Aug 17, 2018

Closing as the provided configuration code show below is both unnecessary when used with the ngtools plugin and directly interferes with its functionality; and therefore should not be used with the plugin.

plugins: [
  new webpack.ContextReplacementPlugin(
    /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
    __dirname
  ),

@clydin clydin closed this as completed Aug 17, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix
Projects
None yet
Development

No branches or pull requests