Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to set a different <base> for dev/staging/production

Tags:

webpack

<base href="">

for dev and production are different(for production it's subfolder). What is the good way to build different base with webpack?

like image 560
Stepan Suvorov Avatar asked Feb 04 '16 15:02

Stepan Suvorov


People also ask

Why is it important to separate development and production environments?

If you have separate development and production environments, it prevents developers from accidentally messing with or deleting production data. It also prevents sensitive information (e.g. passwords and credit card information) from being made available to people who shouldn't have access to it.

What is the difference between staging and Dev?

The development server is where you work. You should be the only one working on the machine. Code is constantly in flux and the site may or may not be up, depending on how much coffee you have in. The staging server is where you deploy your work for folks to look at - before it goes to production.


4 Answers

the best what I found so far was to put this property in config(HtmlWebpackPlugin option):

new HtmlWebpackPlugin({
  ...
  baseUrl: process.env.NODE_ENV == 'development'?'/':'/app/'
})

and then output it in index.html:

<base href="<%= htmlWebpackPlugin.options.baseUrl %>" />
like image 166
Stepan Suvorov Avatar answered Oct 19 '22 08:10

Stepan Suvorov


If you have a template option set to an HTML file then Version 2.x of the plug-in will not perform any substitution.

In this case, you will need to modify @stever's answer as follows:

new HtmlWebpackPlugin({
  ...
  template: './src/index.ejs',
  baseUrl: process.env.NODE_ENV === 'development'?'/':'/app/'
})

and rename your index.html file to index.ejs

like image 31
pards Avatar answered Oct 19 '22 10:10

pards


Now it is easy to do it.

Install the base-href-webpack-plugin in your project:

npm install --save-dev base-href-webpack-plugin

and import this code in webpack file:

// Import package
const { BaseHrefWebpackPlugin } = require('base-href-webpack-plugin'); // Or `import 'base-href-webpack-plugin';` if using typescript

// Add to plugins
plugins: [
  new BaseHrefWebpackPlugin({ baseHref: '/' })
]

Reference: https://github.com/dzonatan/base-href-webpack-plugin

like image 25
Alexandre N. Avatar answered Oct 19 '22 10:10

Alexandre N.


In Webpack 4 I have tried the baseUrl in HtmlWebpackPlugin , but it's never gets parsed in the html. So you need a new plugin called BaseHrefWebpackPlugin along with HtmlWebpackPlugin

Webpack.config

 new HtmlWebpackPlugin(), //this will create default template
        new HtmlWebpackPlugin({
            title: 'MyApp' //replace title
  
        }),
        new BaseHrefWebpackPlugin({
            baseHref: process.env.NODE_ENV == 'development' ? '/' : '/MyApp/'
        })

Html

    <base href="<%= htmlWebpackPlugin.options.baseUrl %>">
    <title><%= htmlWebpackPlugin.options.title %></title>
like image 21
Jameel Moideen Avatar answered Oct 19 '22 09:10

Jameel Moideen