I'm trying to get the following setup to work:
- Standard Aurelia "Hello World" Typescript app (created by CLI, build by Webpack)
- Served by an ExpressJS app using webpack-dev-middleware
- Everything combined in one project in order to be able to share code between the Aurelia front-end app and the ExpressJS back-end app
I stared out with the "Hello World" Aurelia app, moved the code into a src/frontend
folder, renamed the webpack.config.js
to webpack-frontend.config.js
and the npm scripts also to start-frontend
and so on. After these modifications, the Aurelia app still works great.
Then I added a basic Express JS back-end application with its own webpack.config.js, tsconfig.js and of course npm start script, which also works fine.
Finally I tried to get the Express app to launch the front-end app through a webpack-dev-middleware
like so:
private applyWebpackDevMiddleware(server: Express) {
if (Environment.isLocal()) {
const config = require('../../webpack-frontend.config.js');
const compiler = require('webpack')(config);
const webpackDevMiddleware = require('webpack-dev-middleware');
server.use(
webpackDevMiddleware(compiler, {
hot: true,
publicPath: config.output.publicPath,
compress: true,
host: 'localhost',
port: Environment.getPort()
})
);
const webpackHotMiddleware = require('webpack-hot-middleware');
server.use(webpackHotMiddleware(compiler));
}
}
This is exactly the same webpack config file that works just fine when used directly with the webpack command. However like so, I'm getting the following error message:
(node:15111) UnhandledPromiseRejectionWarning: WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration should be an object.
at webpack (/Users/will/Desktop/test/aurelia-express/node_modules/webpack/lib/webpack.js:31:9)
at ExpressServer.applyWebpackDevMiddleware (/Users/will/Desktop/test/aurelia-express/src/backend/ExpressServer.ts:141:48)
at ExpressServer.setup (/Users/will/Desktop/test/aurelia-express/src/backend/ExpressServer.ts:41:14)
at Function.createApplication (/Users/will/Desktop/test/aurelia-express/src/backend/Application.ts:18:29)
at Object.<anonymous> (/Users/will/Desktop/test/aurelia-express/src/backend/index.ts:12:13)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Module.m._compile (/Users/will/Desktop/test/aurelia-express/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/will/Desktop/test/aurelia-express/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
at Object.<anonymous> (/Users/will/Desktop/test/aurelia-express/node_modules/ts-node/src/bin.ts:157:12)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
(node:15111) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
Can anyone either give me some pointers, why the same config file works fine when used directly in webpack, but fails when used through the webpack-dev-middleware? After all, it's in the same project with the same node_modules, and therefore with the same webpack version, right?
Or does anyone have a working setup as described above that they could share? Thanks!!
While winwiz1 did provide some very valuable points (e.g. TypeScript path mapping) I would sill like to present the solution to the problem, which is actually quite simple:
au new
creates a "Hello World" project with awebpack.config.js
which exports a function. However when you want to use webpack with Express webapck-dev-middleware, an object is required. This object is returned by the function in thewebpack.config.js
file. So all you need to do is to require the file and then call that function: