We will do an installation from the beginning where we will include Webpack Encore, along with React, Sass, TypeScript and Bootstrap.
We will take as a starting point 03 Webpack Encore with React and SASS:
- Continue Project
- Installation and Configuration of TypeScript
- Installation and configuration of Bootstrap
- We will create the project through the command of the console:
composer create-project symfony/skeleton 04_Webpack_Encore_with_React_SASS_TypeScript_and_Bootstrap
- Enabling TypeScript (ts-loader),
npm install --save-dev typescript ts-loader
Enabling TypeScript (ts-loader) - JQuery Component,
npm install --save jquery
- Bootstrap Componente,
npm install --save bootstrap
- Installs Composer and its dependencies from the previous phase:
composer install
-
- Installs Webpack and its dependencies from the previous phase:
npm install
Enabling TypeScript (ts-loader)
- First, install the dependencies:
npm install --save-dev typescript ts-loader
- Next we will configure the file webpack.config.js with the following directives.
var Encore = require('@symfony/webpack-encore');
Encore
// the project directory where compiled assets will be stored
.setOutputPath('public/build/')
// the public path used by the web server to access the previous directory
.setPublicPath('/build')
.cleanupOutputBeforeBuild()
.enableSourceMaps(!Encore.isProduction())
// uncomment to create hashed filenames (e.g. app.abc123.css)
.enableVersioning(Encore.isProduction())
// uncomment to define the assets of the project
-- .addEntry('js/app', './assets/js/app.js')
++ .addEntry('js/app', './assets/js/app.jsx')
.addStyleEntry('css/app', './assets/scss/app.scss')
// uncomment if you use Sass/SCSS files
.enableSassLoader()
// uncomment for legacy applications that require $/jQuery as a global variable
// .autoProvidejQuery()
++ .enableTypeScriptLoader()
.enableReactPreset()
;
module.exports = Encore.getWebpackConfig();
- This plugin requires that you have a tsconfig.json file that is setup correctly.
{
"compilerOptions": {
"target": "es6",
"module": "es6",
"moduleResolution": "node",
"declaration": false,
"noImplicitAny": false,
"jsx": "react",
"sourceMap": true,
"noLib": false,
"suppressImplicitAnyIndexErrors": true
},
"compileOnSave": false,
"exclude": [
"node_modules"
]
}
- Now, we can modify de extension of files.
- ./assets/js/app.js will be ./assets/js/app.jsx
- ./assets/js/Components/app.js will be ./assets/js/Components/app.jsx
Inside the files.
./assets/js/Components/app.jsx
import '../scss/app.scss';
import React from 'react';
import ReactDOM from 'react-dom';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
-- import ItemCard from './Components/ItemCard';
++ import ItemCard from './Components/ItemCard.jsx';
class App extends React.Component {
constructor() {
super();
this.state = {
entries: []
};
}
//...
- To import Bootstrap in our project we will only have to execute.
npm install --save jquery
to install Jquery, and
npm install --save bootstrap
to install Bootstrap in its version 4.
- Additionally in this demo we will install the library popper.js using the command:
npm install popper.js
with it we will generate the windows with floating information.
- Next we will configure the file webpack.config.js with the following directives. And we will uncomment the following line of the file webpack.config.js to facilitate jQuery to Bootstrap.
var Encore = require('@symfony/webpack-encore');
Encore
// the project directory where compiled assets will be stored
.setOutputPath('public/build/')
// the public path used by the web server to access the previous directory
.setPublicPath('/build')
.cleanupOutputBeforeBuild()
.enableSourceMaps(!Encore.isProduction())
// uncomment to create hashed filenames (e.g. app.abc123.css)
// .enableVersioning(Encore.isProduction())
// uncomment to define the assets of the project
.addEntry('js/app', './assets/js/app.jsx')
.addStyleEntry('css/app', './assets/scss/app.scss')
// uncomment if you use Sass/SCSS files
.enableSassLoader()
// uncomment for legacy applications that require $/jQuery as a global variable
-- // .autoProvidejQuery()
++ .autoProvidejQuery()
.enableTypeScriptLoader()
.enableReactPreset()
;
module.exports = Encore.getWebpackConfig();
- And now, we can add bootstrap our style sheet file in SASS./assets/scss/app.scss.
++ @import '~bootstrap/scss/bootstrap';
html {
font-family: Arial, Helvetica, sans-serif;
}
.footer {
display: flex;
justify-content: center;
}
- Finally, we launch loader via
npm run watch
, and the serverphp bin/console server:run
so we can see the results by clicking http://127.0.0.1:8000.
The final result is three React cards that fill the available space on the page.