You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
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.
Can you give exact repro steps? I did what you said but didn't get errors:
In a newly-generated React-Redux app, run npm install --save firebase. Run webpack to check it still builds without errors.
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.
Add openlayers via npm install --save openlayers @types/openlayers. Run webpack again; it still builds without errors.
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!
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.
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);
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
Install openlayers with npm install openlayers --save
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..
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).
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!
Activity
SteveSandersonMS commentedon Dec 13, 2016
Can you give exact repro steps? I did what you said but didn't get errors:
npm install --save firebase. Runwebpackto check it still builds without errors.ClientApp/boot-client.tsx, add the code snippet you posted (fromimport * as firebase ...to theinitializeAppcall). Runwebpackagain; it still builds without errors.npm install --save openlayers @types/openlayers. Runwebpackagain; it still builds without errors.boot-client.tsx:import * as openlayers from 'openlayers';and thenconsole.log(openlayers.color.asArray('red'));. Now the webpack build will reportThis seems to be a pre-built javascript filewarnings: 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-loaderisn't inwebpack.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!Please also specify how to make these issues occur. Thanks!
jonorogers commentedon Dec 13, 2016
Hi @SteveSandersonMS , here's some repro steps:
npm install --save firebaseandnpm install --save @types/firebase. Runwebpack, check that it still builds without errors.ClientApp/components/Counter.tsx, add the code snippet I listed: to the imports,import * as firebase from 'firebase';and in therender()method,webpack- two errors are generated, as here:Install openlayers with
npm install openlayers --saveRun
webpack, tonnes of errors generated, first one pasted below: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.tsxinstead ofCounter.tsx..Add json-loader to add webpack.config.js files. Helps with #507.
SteveSandersonMS commentedon Dec 13, 2016
Thanks for clarifying! Yes, you're right - the difference is with putting it in
boot-client.tsxversus somewhere else. Code inboot-client.tsxis only included in the client-side bundle. Code elsewhere, such as inCounter.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-spafor client-side execution only, that's very easy: just remove theasp-prerender-moduletag help from yourIndex.cshtml, and optionally also stop building the server-side bundle by removing theserverBundleConfigarray entries from the last lines ofwebpack.config.jsandwebpack.config.vendor.js.However, it's quite straightforward to fix the two build issues you've encountered:
Unexpected token (2:9) ... You may need an appropriate loader to handle this file typeones, you are correct to point out that you needjson-loaderin your Webpack config to load.jsonfiles. 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 yourwebpack.config.js.Cannot resolve module 'vertx'issue, the issue is thatfirebasedepends onrsvpwhich uses a non-Webpack-compatible technique for detecting whether thevertxmodule is present (it supresses thevertxdependency 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 forCannot 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 thepluginssection of yourwebpack.config.js:new webpack.IgnorePlugin(/^vertx$/). That just tells Webpack not try to include that module in the bundle even thoughrsvpdoes reference it. I suppose a better, longer-term fix would be to report this issue to thersvpmaintainers 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 abrowserfield inpackage.jsonis not enough, because that doesn't cover server-side builds).Hope that makes sense and is reasonable!
jonorogers commentedon Dec 13, 2016
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!