You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Configure Webpack to compile html & bundle javascript files
Create a local file named webpack.config.js with the following code:
require('webpack');// PluginsvarHtmlWebpackPlugin=require('html-webpack-plugin');varglobalPlugins=[newHtmlWebpackPlugin({template: "./src/index.html"})];varproductionPlugins=[newHtmlWebpackPlugin({template: "./src/index.html",minify: {collapseWhitespace: true},// Removes whitespace from htmlhash:true// Appends unique hashes to included scripts and CSS files})];// Webpack configurationvarnodeEnv=process.env.NODE_ENV;// Run `set NODE_ENV=production` locally in the command line to set nodeEnv to production.varplugins=nodeEnv==="production" ? productionPlugins : globalPlugins;vardevtool=nodeEnv==="production" ? false : "inline-sourcemap";// Use SourceMaps to map error message locations to modules. Slows compilation, increases file size & not recommended for production.module.exports={context: __dirname,entry: "./src/app.js",output: {path: __dirname+"/dist",filename: "app.min.js"},plugins: plugins,devtool: devtool};
Create the dist folder by running webpack locally in the command line.
In the dist folder you should see the app.js file automatically included in the index.html file.
If nodeEnv is set to production you should see the html minified and a hash appended to the app.js include.
Optional You can set webpack to watch for changes in your webpack.config.js file by running webpack -d --watch
Add a 3rd party javascript package
Add Angularjs as a dependency to the package.json file by running npm install angular -S
Add require('angular') to the src/app.js file.
Update the webpack.config.js file with the following code:
varwebpack=require("webpack");// PluginsvarHtmlWebpackPlugin=require("html-webpack-plugin");varglobalPlugins=[newwebpack.optimize.OccurrenceOrderPlugin(),// Optimizes order of plugins, reduces total file size & is recommended by official documentation.newwebpack.optimize.UglifyJsPlugin({// Minifies the javascriptsourcemap: false,// Use SourceMaps to map error message locations to modules. Not recommended for production.mangle: false,// Renames complex filenames. Not recommended for production.}),newHtmlWebpackPlugin({template: "./src/index.html"}),];varproductionPlugins=[newwebpack.optimize.OccurrenceOrderPlugin(),newHtmlWebpackPlugin({template: "./src/index.html",minify: {collapseWhitespace: true},// Removes whitespace from htmlhash:true// Appends unique hashes to included scripts and CSS files}),];// Webpack configurationvarnodeEnv=process.env.NODE_ENV;// Run `set NODE_ENV=production` locally in the command line to set nodeEnv to production.varplugins=nodeEnv==="production" ? productionPlugins : globalPlugins;vardevtool=nodeEnv==="production" ? false : "inline-sourcemap";// Use SourceMaps to map error message locations to modules. Slows compilation, increases file size & not recommended for production.module.exports={context: __dirname,entry: "./src/app.js",output: {path: __dirname+"/dist",filename: "app.min.js"},plugins: plugins,devtool: devtool};
Create the dist folder by running webpack locally in the command line.
Open the dist/index.html file in a browser. Confirm Angularjs was installed by running angular in the browser console.
Configure Webpack to compile scss & bundle css files
Run the following commands in the command prompt to install the needed plugins:
npm install -S node-sass
npm install -S css-loader
npm install -S style-loader
npm install -S sass-loader
npm install -S extract-text-webpack-plugin
Inside the src folder create a file named index.scss with the following code:
$primary-color: pink;
body {
color: $primary-color;
}
Update the webpack.config.js file with the following code:
varwebpack=require("webpack");// PluginsvarHtmlWebpackPlugin=require("html-webpack-plugin");varExtractTextPlugin=require("extract-text-webpack-plugin");varglobalPlugins=[newwebpack.optimize.OccurrenceOrderPlugin(),// Optimizes order of plugins, reduces total file size & is recommended by official documentation.newwebpack.optimize.UglifyJsPlugin({// Minifies the javascriptsourcemap: false,// Use SourceMaps to map error message locations to modules. Not recommended for production.mangle: false,// Renames complex filenames. Not recommended for production.}),newHtmlWebpackPlugin({template: "./src/index.html"}),newExtractTextPlugin({filename: 'index.css',disable: false,allChunks: true})];varproductionPlugins=[newwebpack.optimize.OccurrenceOrderPlugin(),newHtmlWebpackPlugin({template: "./src/index.html",minify: {collapseWhitespace: true},// Removes whitespace from htmlhash:true// Appends unique hashes to included scripts and CSS files}),newExtractTextPlugin({filename: 'index.css',disable: false,allChunks: true})];// Webpack configurationvarnodeEnv=process.env.NODE_ENV;// Run `set NODE_ENV=production` locally in the command line to set nodeEnv to production.varplugins=nodeEnv==="production" ? productionPlugins : globalPlugins;vardevtool=nodeEnv==="production" ? false : "inline-sourcemap";// Use SourceMaps to map error message locations to modules. Slows compilation, increases file size & not recommended for production.varpath=require("path");module.exports={context: __dirname,entry: "./src/app.js",output: {path: path.resolve(__dirname,"dist"),filename: "app.min.js"},plugins: plugins,devtool: devtool,module: {rules: [{test: /\.scss$/,use: ExtractTextPlugin.extract({fallback: 'style-loader',use: ['css-loader','sass-loader'],publicPath: __dirname+"/dist",})}]},};
Update the src/app.js file with the following code:
require('angular')require('./index.scss');
Create the dist folder by running webpack locally in the command line.
Open the dist/index.html file in a browser. Confirm the sass file was converted to css & injected into the index.html file by checking the color of the body tag.
Configure Webpack to load html on a webserver
Run npm install -S webpack-dev-server and npm install -g webpack-dev-server to install needed plugin.
Update the webpack.config.js file with the following code:
varwebpack=require("webpack");// PluginsvarHtmlWebpackPlugin=require("html-webpack-plugin");varExtractTextPlugin=require("extract-text-webpack-plugin");varglobalPlugins=[newwebpack.optimize.OccurrenceOrderPlugin(),// Optimizes order of plugins, reduces total file size & is recommended by official documentation.newwebpack.optimize.UglifyJsPlugin({// Minifies the javascriptsourcemap: false,// Use SourceMaps to map error message locations to modules. Not recommended for production.mangle: false,// Renames complex filenames. Not recommended for production.}),newHtmlWebpackPlugin({template: "./src/index.html"}),newExtractTextPlugin({filename: 'index.css',disable: false,allChunks: true})];varproductionPlugins=[newwebpack.optimize.OccurrenceOrderPlugin(),newHtmlWebpackPlugin({template: "./src/index.html",minify: {collapseWhitespace: true},// Removes whitespace from htmlhash:true// Appends unique hashes to included scripts and CSS files}),newExtractTextPlugin({filename: 'index.css',disable: false,allChunks: true})];// Webpack configurationvarnodeEnv=process.env.NODE_ENV;// Run `set NODE_ENV=production` locally in the command line to set nodeEnv to production.varplugins=nodeEnv==="production" ? productionPlugins : globalPlugins;vardevtool=nodeEnv==="production" ? false : "inline-sourcemap";// Use SourceMaps to map error message locations to modules. Slows compilation, increases file size & not recommended for production.varpath=require("path");module.exports={context: __dirname,entry: "./src/app.js",output: {path: path.resolve(__dirname,"dist"),filename: "app.min.js"},plugins: plugins,devtool: devtool,module: {rules: [{test: /\.scss$/,use: ExtractTextPlugin.extract({fallback: 'style-loader',use: ['css-loader','sass-loader'],publicPath: __dirname+"/dist",})}]},devServer: {contentBase: path.join(__dirname,"dist"),compress: true,port: 9000,stats: "errors-only",open: true},};
Run webpack-dev-server in the command line. Your default browser should open at localhost:9000 with the code compiled.