This demo illustrates basic Webpack configuration.
Checkout the start branch
and npm install
all dependencies.
- Explain that:
- All required modules are already installed in order to not waste time installing them. Show
package.json
. - We are going to write the Webpack configuration in TypeScript in order to avoid silly mistakes. That's the reason why we have ts-node installed to eliminate the need for compiling the Wepback configuration files.
- Add the following code to the Webpack configuration file explaining why we are adding each key:
entry: join(sourcePath, 'index.tsx'),
output: {
path: outputPath,
filename: 'index.js',
},
resolve: {
extensions: ['', '.js', '.ts', '.tsx'],
root: sourcePath,
},
module: {
loaders: [
{ test: /\.tsx?$/, loader: `awesome-typescript-loader?tsconfig=${join(sourcePath, 'tsconfig.json')}` },
{ test: /\.css$/, loader: 'style!css' },
{ test: /\.svg$/, loader: 'url' },
],
},
plugins: [
new HtmlWebpackPlugin({
template: join(sourcePath, 'index.html'),
}),
],
- Show how to debug the generated code. Add
devtool: 'source-map'
to the configuration. - Add CSS to "beatify" the app.
- Include the loaders:
Explain how they are chained and what{ test: /\.css$/, loader: 'style!css' }
css
andstyle
loaders actually do.import
CSS files:index.css
inindex.tsx
andApp.css
inApp.tsx
- Show the generated HTML via Chrome's Elements tab.
- Add the app's logo.
- Include the loader:
{ test: /\.svg$/, loader: 'url' }
import
the logo:
Explain why we are using require (to please TypeScript).const logo = require('./logo.svg');
- Use the logo. Insert an
img
after the<h2>
inApp.tsx
:
<img src={logo} className="App-logo" alt="logo" />