Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Webpack config (React/Redux) seems inherently fragile #507

Closed

Description

@jonorogers

When using the provided webpack config from the React/Redux template it seems to be fragile - breaking as soon as extra modules are installed.

The worst example I've run into is using openlayers and firebase. They break in different ways.

Adding firebase (npm install --save firebase) and a couple of statements to load up firebase will start generating webpack errors which aren't present if including this from a basic react-webpack template.

import * as firebase from 'firebase';

        let firebaseConfig = {
            apiKey: "",
            authDomain: "",
            databaseURL: "",
            storageBucket: "",
            messagingSenderId: ""
        };

        // Initialize Firebase - once for the app
        firebase.initializeApp(firebaseConfig);

Then adding openlayers (npm install --save openlayers) immediately breaks webpack - generating a huge amount of errors reading json files - like there's no json loader present.

ERROR in ./~/tough-cookie/package.json Module parse failed: C:\code\yo-asp-net-core-spa-react-redux\node_modules\tough-cookie\package.json Unexpected token (2:9) You may need an appropriate loader to handle this file type. SyntaxError: Unexpected token (2:9)

There's lots more like that; all about peer packages.

I've seen this symptom (lots of errors about .json files) before, but I can't remember the exact modules which caused the issue.

Have tested this combination with react-webpack yo template with no problem.

Activity

  1. SteveSandersonMS commented on Dec 13, 2016

    @SteveSandersonMS
    Member

    Can you give exact repro steps? I did what you said but didn't get errors:

    1. In a newly-generated React-Redux app, run npm install --save firebase. Run webpack to check it still builds without errors.
    2. In ClientApp/boot-client.tsx, add the code snippet you posted (from import * as firebase ... to the initializeApp call). Run webpack again; it still builds without errors.
    3. Add openlayers via npm install --save openlayers @types/openlayers. Run webpack again; it still builds without errors.
    4. Add some code to exercise openlayers in boot-client.tsx: import * as openlayers from 'openlayers'; and then console.log(openlayers.color.asArray('red'));. Now the webpack build will report This seems to be a pre-built javascript file warnings: it's a general issue with openlayers and isn't specific to this project structure. Find the fix on stackoverflow; now it builds and runs without warnings or errors.

    So, I'm sure you are right about the build errors you got, but for us to look at any solutions we'd have to be able to reproduce them. Can you provide exact steps?

    In particular, I'm aware that json-loader isn't in webpack.config.js (whereas it is in the vendor config). We could certainly add that, but I'd like to see what specific problem we're fixing. Hope that's OK!

    There's lots more like that; all about peer packages.

    Please also specify how to make these issues occur. Thanks!

  2. jonorogers commented on Dec 13, 2016

    @jonorogers
    Author

    Hi @SteveSandersonMS , here's some repro steps:

    1. In a newly-generated React-Redux app, run npm install --save firebase and npm install --save @types/firebase. Run webpack, check that it still builds without errors.
    2. In ClientApp/components/Counter.tsx, add the code snippet I listed: to the imports, import * as firebase from 'firebase'; and in the render() method,
    const firebaseConfig = {
                apiKey: "",
                authDomain: "",
                databaseURL: "",
                storageBucket: "",
                messagingSenderId: ""
            };
    
            // Initialize Firebase - once for the app
            firebase.initializeApp(firebaseConfig);
    
    1. Run webpack - two errors are generated, as here:
    WARNING in ./~/firebase/~/rsvp/dist/rsvp.js
        Module not found: Error: Cannot resolve module 'vertx' in C:\code sandbox\test2\node_modules\firebase\node_modules\rsvp\dist
         @ ./~/firebase/~/rsvp/dist/rsvp.js 1210:20-30
    
        ERROR in ./~/firebase/database-node.js
        Module not found: Error: Cannot resolve module 'request' in C:\code sandbox\test2\node_modules\firebase
         @ ./~/firebase/database-node.js 104:25-43
    
    1. Install openlayers with npm install openlayers --save

    2. Run webpack, tonnes of errors generated, first one pasted below:

    ERROR in ./~/tough-cookie/package.json
    Module parse failed: C:\code sandbox\test2\node_modules\tough-cookie\package.json Unexpected token (2:9)
    You may need an appropriate loader to handle this file type.
    SyntaxError: Unexpected token (2:9)
        at Parser.pp$4.raise (C:\code sandbox\test2\node_modules\acorn\dist\acorn.js:2221:15)
        at Parser.pp.unexpected (C:\code sandbox\test2\node_modules\acorn\dist\acorn.js:603:10)
        at Parser.pp.semicolon (C:\code sandbox\test2\node_modules\acorn\dist\acorn.js:581:61)
        at Parser.pp$1.parseExpressionStatement (C:\code sandbox\test2\node_modules\acorn\dist\acorn.js:966:10)
        at Parser.pp$1.parseStatement (C:\code sandbox\test2\node_modules\acorn\dist\acorn.js:730:24)
        at Parser.pp$1.parseBlock (C:\code sandbox\test2\node_modules\acorn\dist\acorn.js:981:25)
        at Parser.pp$1.parseStatement (C:\code sandbox\test2\node_modules\acorn\dist\acorn.js:709:33)
        at Parser.pp$1.parseTopLevel (C:\code sandbox\test2\node_modules\acorn\dist\acorn.js:638:25)
        at Parser.parse (C:\code sandbox\test2\node_modules\acorn\dist\acorn.js:516:17)
        at Object.parse (C:\code sandbox\test2\node_modules\acorn\dist\acorn.js:3098:39)
        at Parser.parse (C:\code sandbox\test2\node_modules\webpack\lib\Parser.js:902:15)
        at DependenciesBlock.<anonymous> (C:\code sandbox\test2\node_modules\webpack\lib\NormalModule.js:104:16)
        at DependenciesBlock.onModuleBuild (C:\code sandbox\test2\node_modules\webpack-core\lib\NormalModuleMixin.js:310:10)
        at nextLoader (C:\code sandbox\test2\node_modules\webpack-core\lib\NormalModuleMixin.js:275:25)
        at C:\code sandbox\test2\node_modules\webpack-core\lib\NormalModuleMixin.js:259:5
        at Storage.provide (C:\code sandbox\test2\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:52:20)
        at CachedInputFileSystem.readFile (C:\code sandbox\test2\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:140:24)
        at DependenciesBlock.onLoadPitchDone (C:\code sandbox\test2\node_modules\webpack-core\lib\NormalModuleMixin.js:255:7)
        at DependenciesBlock.loadPitch (C:\code sandbox\test2\node_modules\webpack-core\lib\NormalModuleMixin.js:182:27)
        at DependenciesBlock.doBuild (C:\code sandbox\test2\node_modules\webpack-core\lib\NormalModuleMixin.js:241:4)
        at DependenciesBlock.build (C:\code sandbox\test2\node_modules\webpack\lib\NormalModule.js:84:14)
        at Compilation.buildModule (C:\code sandbox\test2\node_modules\webpack\lib\Compilation.js:126:9)
        at C:\code sandbox\test2\node_modules\webpack\lib\Compilation.js:309:10
        at C:\code sandbox\test2\node_modules\webpack\lib\NormalModuleFactory.js:58:13
        at NormalModuleFactory.applyPluginsAsyncWaterfall (C:\code sandbox\test2\node_modules\tapable\lib\Tapable.js:75:69)
        at onDoneResolving (C:\code sandbox\test2\node_modules\webpack\lib\NormalModuleFactory.js:38:11)
        at onDoneResolving (C:\code sandbox\test2\node_modules\webpack\lib\NormalModuleFactory.js:121:6)
        at C:\code sandbox\test2\node_modules\webpack\lib\NormalModuleFactory.js:116:7
        at C:\code sandbox\test2\node_modules\async\lib\async.js:726:13
        at C:\code sandbox\test2\node_modules\async\lib\async.js:52:16
     @ ./~/tough-cookie/lib/cookie.js 38:14-40
    

    Thanks for looking into this! Not sure why the earlier re-pro steps didn't work, might have been the code being in boot-client.tsx instead of Counter.tsx..

  3. SteveSandersonMS commented on Dec 13, 2016

    @SteveSandersonMS
    Member

    Thanks for clarifying! Yes, you're right - the difference is with putting it in boot-client.tsx versus somewhere else. Code in boot-client.tsx is only included in the client-side bundle. Code elsewhere, such as in Counter.tsx, is built for both server and client bundles.

    In general, running your application both server-side and client-side is more complicated than running it only client-side. The react-webpack yo template might only do client-side execution, so it's not surprising if it's simpler. If you want to use generator-aspnetcore-spa for client-side execution only, that's very easy: just remove the asp-prerender-module tag help from your Index.cshtml, and optionally also stop building the server-side bundle by removing the serverBundleConfig array entries from the last lines of webpack.config.js and webpack.config.vendor.js.

    However, it's quite straightforward to fix the two build issues you've encountered:

    • For the Unexpected token (2:9) ... You may need an appropriate loader to handle this file type ones, you are correct to point out that you need json-loader in your Webpack config to load .json files. To save others the trouble of adding this, I've now added that by default to all the templates: 4cc61d7. You could add the same line to your webpack.config.js.
    • For the Cannot resolve module 'vertx' issue, the issue is that firebase depends on rsvp which uses a non-Webpack-compatible technique for detecting whether the vertx module is present (it supresses the vertx dependency only for client-side builds, not server-side ones). Here's how another NPM module handled this. Third-party incompatibility issues like this are always possible and we can't control them all. In this specific case, there is a known fix that can be found by searching for Cannot resolve module 'vertx', but I totally appreciate it's annoying to have to track these kinds of things down. The specific fix is to add the following to the plugins section of your webpack.config.js: new webpack.IgnorePlugin(/^vertx$/). That just tells Webpack not try to include that module in the bundle even though rsvp does reference it. I suppose a better, longer-term fix would be to report this issue to the rsvp maintainers and emphasise the need to work with Webpack without manual config (if you do report it, be sure to emphasise that their existing solution of using a browser field in package.json is not enough, because that doesn't cover server-side builds).

    Hope that makes sense and is reasonable!

  4. jonorogers commented on Dec 13, 2016

    @jonorogers
    Author

    Hey @SteveSandersonMS , thanks very much for looking into this! Sorry that it ended up just being third-party stuff and nothing to do with JavaScriptServices!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions