Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace files at compile time using webpack?

I have a project with miltiple configuration. The first config is config.dev.js file that contains some development configiration. I using it in development mode. The second config is config.js file. I using it in production mode.

In the code I using imports:

import * as config from './config.js';

I want to use the first config file in development and the second to production whithout rewriting all of the imports. How can I replace this config depending on the build mode?

like image 386
5tarter Avatar asked Jul 14 '18 12:07

5tarter


People also ask

How we can make the webpack to watch for changes?

Using Watch Mode You can instruct webpack to "watch" all files within your dependency graph for changes. If one of these files is updated, the code will be recompiled so you don't have to run the full build manually. Now run npm run watch from the command line and see how webpack compiles your code.

How do I bundle a JavaScript file using webpack?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script.

How does Webpack know what files are being generated?

You might be wondering how webpack and its plugins seem to "know" what files are being generated. The answer is in the manifest that webpack keeps to track how all the modules map to the output bundles. If you're interested in managing webpack's output in other ways, the manifest would be a good place to start.

How do I use Webpack plugins?

Webpack can be configured by a config file. Plugins modify and extend the webpack build process. Loaders instruct webpack how to handle different file types. The clean-webpack-plugin can be used to remove old build artifacts from the dist directory.

How do I export an object in Webpack?

To do so, open the webpack.config.js file and add this line: When we start webpack, it will look for its configuration file (webpack.config.js). This file will export an object that has all configurations. The first property we need to add to that object is the entry point: module.exports = { entry: "./app/assets/scripts/index.js", };

Why can’t I use Webpack to bundle and serve at 8080?

The server won't stop until you say so and webpack will try to bundle and serve the contents at localhost:8080, by default. Since our configuration is incomplete, if you visit that address you should see this message: “Cannot GET /”. Webpack is warning that we haven’t set up the mode option. To fix that, set the mode to be “development”:


3 Answers

I realize this is an old post, but this is one of the first results on Google, so I thought a better answer would be good.

Webpack has a built in "Normal Module Replacement Plugin".

plugins: [
  new webpack.NormalModuleReplacementPlugin(
    /some\/path\/config\.development\.js/,
    './config.production.js'
  )
]

For my use, I put the env file in a variable Here is an example:

let envFilePath = './environment.dev.js';
if (env === 'production') {
  envFilePath = './environment.prod.js';
} else if (env === 'staging') {
  envFilePath = './environment.stg.js';
}

module.exports = {
    // other webpack stuff
    ....
    plugins:[ 
        new webpack.NormalModuleReplacementPlugin(
            /src\/environment\/environment\.js/,
            envFilePath
        ),
        ...
    ]
}
like image 188
Jacques ジャック Avatar answered Oct 10 '22 18:10

Jacques ジャック


This is an old question but I've recently stumbled across the same issue and webpack.NormalModuleReplacementPlugin doesn't seem to work anymore (or at least not in my case, where I used JSON files as config). Instead I found another solution using alias:

const path = require("path");

modules.export = {
    ...
    resolve: {
        ...
        alias: {
            [path.resolve(__dirname, "src/conf/config.json")]:
                path.resolve(__dirname, "src/conf/config.prod.json")
        }
    }
    ...
}
like image 16
Zer0 Avatar answered Oct 10 '22 18:10

Zer0


You can use webpack file-replace-loader

https://www.npmjs.com/package/file-replace-loader

Example:

//webpack.config.js

const resolve = require('path').resolve;

module.exports = {
  //...
  module: {
    rules: [{
      test: /\.config\.js$/,
      loader: 'file-replace-loader',
      options: {
        condition: process.env.NODE_ENV === 'development',
        replacement: resolve('./config.dev.js'),
        async: true,
      }
    }]
  }
}
like image 10
S. Owner Avatar answered Oct 10 '22 19:10

S. Owner