How to use Webpack in PrestaShop 1.7 Classic theme
PrestaShop 1.7 is using Webpack Module Bundler to manage assets in PrestaShop front. Webpack takes modules with dependencies and generates static assets representing those modules.
Before installing Webpack you have to install node.js .
Now, Using npm package manager you can install Webpack.
Install Webpack in PrestaShop/themes/classic/_dev folder.
After successful installation of Webpack, you need to install some modules of Webpack.
$ npm install --save-dev style-loader $ npm install --save-dev css-loader $ npm install --save-dev babel-loader $ npm install --save-dev postcss-loader
After installing these modules you can see the folder ‘node_modules’ in your _dev folder.
In _dev folder there is a file ‘webpack.config.js’ which is having the configuration for webpack.
var webpack = require('webpack'); var path = require('path'); var ExtractTextPlugin = require("extract-text-webpack-plugin"); var plugins = []; var production = false; if (production) { plugins.push( new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }) ); } plugins.push( new ExtractTextPlugin( path.join( '..', 'css', 'theme.css' ) ) ); module.exports = { entry: [ './js/theme.js' ], output: { path: '../assets/js', filename: 'theme.js' }, module: { loaders: [{ test: /\.js$/, exclude: /node_modules/, loaders: ['babel-loader'] }, { test: /\.scss$/, loader: ExtractTextPlugin.extract( "style", "css?sourceMap!postcss!sass?sourceMap" ) }, { test: /.(png|woff(2)?|eot|ttf|svg)(\?[a-z0-9=\.]+)?$/, loader: 'file-loader?name=../css/[hash].[ext]' }, { test: /\.css$/, loader: "style-loader!css-loader!postcss-loader" }] }, postcss: function() { return [require('postcss-flexibility')]; }, externals: { prestashop: 'prestashop' }, devtool: 'source-map', plugins: plugins, resolve: { extensions: ['', '.js', '.scss'] } };
Now create your js or css file in _dev/css or _dev/js file,
Now, run these command to create compiled file in assets/css and assets/js file,
In the directory, _dev run these commands,
To build your assets once
$ npm run build
To rebuild your assets every time you change a file in the _dev folder
$ npm run watch
You can see now the compiled file in your assets/css or js folder.
Troubleshoot while run these commands,
Error :: Module build failed: ReferenceError: Promise is not defined
- Check your node version, Try to install, 7.2.0
-
$ npm install es6-promise --save
and then added require(‘es6-promise’).polyfill() at the top of webpack.config.js.
Reference :: http://developers.prestashop.com/themes/assets/index.html#about-webpack
0 info it worked if it ends with ok
1 verbose cli [ ‘C:\Program Files\nodejs\node.exe’,
1 verbose cli ‘C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js’,
1 verbose cli ‘run’,
1 verbose cli ‘build’ ]
2 info using [email protected]
3 info using [email protected]
4 verbose stack Error: ENOENT: no such file or directory, open ‘C:xampphtdocsmypresta-comthemesmypresta_devpackage.json’
4 verbose stack at Error (native)
5 verbose cwd C:xampphtdocsmypresta-comthemesmypresta_dev
6 error Windows_NT 10.0.14393
7 error argv “C:\Program Files\nodejs\node.exe” “C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js” “run” “build”
8 error node v6.10.2
9 error npm v3.10.10
10 error path C:xampphtdocsmypresta-comthemesmypresta_devpackage.json
11 error code ENOENT
12 error errno -4058
13 error syscall open
14 error enoent ENOENT: no such file or directory, open ‘C:xampphtdocsmypresta-comthemesmypresta_devpackage.json’
15 error enoent ENOENT: no such file or directory, open ‘C:xampphtdocsmypresta-comthemesmypresta_devpackage.json’
15 error enoent This is most likely not a problem with npm itself
15 error enoent and is related to npm not being able to find a file.
16 verbose exit [ -4058, true ]