- Initialize sails web app by doing
sails new sails-webpackand then Choose 1 for “Web App”. - Uninstall Grunt:
a. Run
npm un grunt sails-hook-grunt. b. Delete/tasksdirectory. - Install packages for our webpack hook:
a. Install dev dependency by running
npm i webpack -D. b. Install lodash as prod dependency (as I'm sure you'll use this in prod later) by runningnpm i lodash. - Create webpack hook:
a. Run
sails generate hook webpack, this creates fileapi/hooks/webpack/index.js. b. Overwrite it with this contents - https://gist.github.com/Noitidart/e1a674f9b6dfcd8219baab1e6bce7b04 - Create webpack config:
a. Install dependencies by running
npm i -D babel-loader css-loader less less-loader style-loader mini-css-extract-plugin copy-webpack-pluginb. Create/config/webpack.jsfile and fill it with this contents - https://gist.github.com/Noitidart/1094a4c3ee60f38714d35752b761fd43 - Create babel config:
a. Install dev dependencies with
npm i -D @babel/core @babel/plugin-transform-runtime @babel/preset-envb. Install prod dependencies withnpm i @babel/runtime. c. Then create file/.babelrcand populate it with - https://gist.github.com/Noitidart/b23684edc4da24a374071f8feacb8b33 - Add our Javascript and CSS to our HTML by opening
views/layouts/layout.ejs: a. Find<!--STYLES-->and<!--STYLES END-->. In between this, paste this - https://gist.github.com/Noitidart/ff5f3d097bffb3bc5f9188b27db51b53 b. Find<!--SCRIPTS-->and<!--SCRIPTS END-->. In between this, paste this - https://gist.github.com/Noitidart/a39744399c73e12404d97ccd839e86af - Create
/assets/js/index.jsand populate it with this - https://gist.github.com/Noitidart/2ea431e34fd6ed947f4ae4049ad732a6
- Whenever you add a new page with
sails generate page foomake sure to add importimport './pages/foo.page.js';at the bottom of this file.
- That's it! Do a
sails liftand enjoy!
Notice how in step 7b we are injecting into the page a lot of dependencies. The order of matters here. As you are now using webpack, you should slowly move away from this. For instance instead of having to put <script src="/dependencies/moment.js"></script> inside layout.ejs, you would import moment from 'moment' in the files you are using it. Once you move to using import statements for these dependencies, you can remove it from the scripts in layout.ejs.