<base href="">
for dev and production are different(for production it's subfolder). What is the good way to build different base with webpack?
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.
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.
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 %>" />
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
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With