frontend-app
Start using latest ES syntax and features, Livereload and Hot Module Replacement.
This includes babel-polyfill and whatwg-fetch polyfill and transpiles code using env and stage-0 Babel presets, so you may use many new features of the language out-of-the-box.
JSX and AngularJS dependency injection annotations (babel-plugin-ng-annotate, ng-annotate) are also supported.
Contents
- Getting Started
- Commands
- Scripts
- Options
- React Intl helper Components
- List of Preconfigured Loaders
- Livereload
- Dev Server
- Single Entry
- Multiple Entries
- Examples
- Webpack Configuration
- Cookbook
Getting Started
Make sure you have at least Node.js 6.13.1 and npm 5.2 installed:
node -vnpm -v
Create package.json
and install frontend-app:
mkdir my-appcd my-appnpm init -ynpm install --save-dev frontend-app
Install one of the examples, have your app entry point(s) in src
directory and webpack.config.js
in place. Then build using:
npm run build
Commands
init
Initialize example project.
npx frontend-app init <example-name>
Scripts
build
Files are transpiled, bundled and minified to the dist
directory and are ready to be served. Source maps are also generated.
You may pass additional arguments to Webpack using --
. For example, don't show progress information:
npm run build -- --progress false
dev
Bundle files to disk in watch mode (see Livereload).
dev-server
Bundle files in watch mode (see Dev Server).
Options
You may use package.json
config
field to set options. Example:
type
react
: Adds JSX support.angular
: Adds ng-annotate support.all
: Support JSX and ng-annotate.ng-strict-di
is not supported in livereload mode.slim
(default): Just ES2015. No React or Angular specific transformations.portlet
: Setsoutput.publicPath
in Spring MVC Liferay plugin project built with Maven.publicPath
is determined byartifactId
inpom.xml
.
Multiple values are also supported. package.json
config.frontendApp
property example:
intl
Set "intl": "true"
to use default list of available locales.
intl.locales
Available locales for Moment.js, React Intl and Intl. Default: ["fi", "sv", "en"]
(When "intl": "true"
).
polyfills
Included polyfills. Default: ["babel-polyfill", "whatwg-fetch"]
. Set "false"
to not include any polyfills.
React Intl helper Components
Moved to @visma/react-intl-helpers.
List of Preconfigured Loaders
Extension | Loader |
---|---|
JS | |
.js |
(ng-annotate-loader,) babel-loader |
HTML | |
.html |
raw-loader |
.hbs |
handlebars-template-loader |
Styles | |
.css |
css-loader |
.scss |
sass-loader |
.less |
less-loader |
Fonts & Media | |
.woff , .woff2 |
url-loader |
.ttf |
url-loader |
.eot |
file-loader |
.svg |
url-loader |
.png |
url-loader |
.gif |
file-loader |
.jpg |
file-loader |
Other | |
.txt |
raw-loader |
.properties |
@visma/react-intl-helpers/cjs/loader |
Livereload
Start build process, watch changes and refresh browser automatically on changes.
npm run dev
If you wish to serve files from different directory while developing (i.e. server content base is elsewhere):
npm run dev -- --output-path /some/other/content/base/dist
Dev Server
Use this for Hot Module Replacement with React or as an alternative to Livereload.
npm run dev-server
src/index.js
)
Single Entry (my-app/
dist/ // generated
bundle.aa4915533a5b5f53c072.css
bundle.aa4915533a5b5f53c072.css.map
bundle.aa4915533a5b5f53c072.js
bundle.aa4915533a5b5f53c072.js.map
src/
index.js // entry point
other.js
package.json
src/entries
)
Multiple Entries (my-app/
dist/ // generated
commons.ee0ce0879c8b5aaefae4.css
commons.ee0ce0879c8b5aaefae4.css.map
commons.ee0ce0879c8b5aaefae4.js
commons.ee0ce0879c8b5aaefae4.js.map
entries/pageA/bundle.8036db3af1be1831e295.css
entries/pageA/bundle.8036db3af1be1831e295.css.map
entries/pageA/bundle.8036db3af1be1831e295.js
entries/pageA/bundle.8036db3af1be1831e295.js.map
entries/pageB/bundle.19596c286128bce14cf9.css
entries/pageB/bundle.19596c286128bce14cf9.css.map
entries/pageB/bundle.19596c286128bce14cf9.js
entries/pageB/bundle.19596c286128bce14cf9.js.map
src/
entries/ // entry points e.g. for each page
pageA.js
pageB.js
other.js
package.json
You must load commons bundle before the entry point. pageA.html
:
Examples
React
Install example project with React Router, React-Bootstrap, Bootstrap 3 custom build, React Intl and mock API:
npx frontend-app init react
Start Webpack Dev Server:
npm run dev-server
Go to http://localhost:8080/
.
React (multiple entries)
Install React example project for multi-portlet use, with Bootstrap 3 custom build, React Intl and mock API:
npx frontend-app init react-multiple-entries
Start Webpack Dev Server:
npm run dev-server
Go to http://localhost:8080/
.
Angular
Install example project with AngularUI Router and Bootstrap:
npx frontend-app init angular
Start Webpack Dev Server:
npm run dev-server
Go to http://localhost:8080/
.
Webpack Configuration
Provide the built-in Webpack configuration. webpack.config.js
:
moduleexports = default;
Optionally merge own configuration. It will be merged into the built-in configuration using webpack-merge in smartStrategy
mode:
moduleexports = default;
And/or customize the final configuration:
moduleexports = default ;
Cookbook
History API Fallback with HTML Webpack Plugin
Suppose there is no backend or it's on another machine, and you just need to test some frontend code with Webpack Dev Server.
Install dependencies:
npm install --save-dev html-webpack-plugin
Edit webpack.config.js
:
const HtmlWebpackPlugin = ; moduleexports = default;
/libs/ckeditor
CKEditor from Install dependencies:
npm install --save-dev html-webpack-plugin html-webpack-template
Edit webpack.config.js
:
const HtmlWebpackPlugin = ; moduleexports = default;
Use CKEDITOR
in your app:
// CKEDITOR is available.windowCKEDITOR;
node_modules
CKEditor from Install dependencies:
npm install --save-dev html-webpack-plugin html-webpack-template ckeditor
Edit webpack.config.js
:
const HtmlWebpackPlugin = ; moduleexports = default;
Use CKEDITOR
in your app:
; // CKEDITOR is available.windowCKEDITOR;
output.publicPath
in production
Different process.env.NODE_ENV
is set correctly. You may use NODE_ENV
, and for example process.env.npm_lifecycle_event
to customize your configuration. webpack.config.js
:
const production = processenvNODE_ENV === "production";// process.env.npm_lifecycle_event => "build" | "dev" | "dev-server" moduleexports = default;
Maven
Install in webapp
directory:
cd .../src/main/webappnpm initnpm install --save-dev frontend-app
Add build step to pom.xml
:
com.github.eirslett frontend-maven-plugin 1.0 ::install node and npm:: install-node-and-npm ::npm ci:: npm ci ::npm run build:: npm run build v8.12.0 6.4.1 src/main/webapp
Add exclude rules to pom.xml
:
org.apache.maven.plugins maven-war-plugin src/,node/,node_modules/,package.json src/main/webapp false src/** node/** node_modules/** package.json
.gitignore
/dist/
/node_modules/