Skip to content

How to solve unwanted dynamic requires #87

Closed

Description

@jhnns

I want to use moment.js for date formatting. Unfortunately they have a dynamic require in their code for language files - so webpack includes all of them.

What do you suggest, oh mighty sokra? 😄

Activity

  1. sokra commented on May 13, 2013

    @sokra
    Member

    ContextReplacementPlugin

    var ContextReplacementPlugin = require("webpack/lib/ContextReplacementPlugin");
    module.exports = {
      plugins: [
        new ContextReplacementPlugin(/moment\.js[\/\\]lang$/, /^\.\/(de|pl)$/)
      ]
    }

    It's not tested, but should work (hopefully)... 😄

  2. jhnns commented on May 13, 2013

    @jhnns
    ContributorAuthor

    Can this somehow be combined with the i18n plugin?

  3. sokra commented on May 13, 2013

    @sokra
    Member

    With a (to be developed) tool that starts webpack for multiple languages... (to generate multiple bundles)

    var lang = process.env.LANG || "en";
    var langConfig = require("./i18n/" + lang + ".js")
    
    var ContextReplacementPlugin = require("webpack/lib/ContextReplacementPlugin");
    var I18nPlugin = require("i18n-webpack-plugin");
    module.exports = {
      output: {
        filename: "[hash]." + lang + ".bundle.js"
      }
      plugins: [
        new ContextReplacementPlugin(
          /moment\.js[\/\\]lang$/,
          new RegExp("^\\./" + langConfig.dateLang + "$"),
        new I18nPlugin(langConfig)
      ]
    }
  4. jhnns commented on May 13, 2013

    @jhnns
    ContributorAuthor

    K I'll try my luck with the ContextReplacementPlugin 👍

  5. masonicboom commented on Oct 4, 2014

    @masonicboom

    in the latest version of moment, the directory structure changed and you now need to target "locale" rather than "lang" in your regexes, e.g.

    plugins: [ webpack.ContextReplacementPlugin(/moment[\\\/]locale$/, /^\.\/(de|pl)$/) ]

  6. jhnns commented on Oct 6, 2014

    @jhnns
    ContributorAuthor

    Thx @masonicboom 👍

  7. rafaeleyng commented on May 31, 2016

    @rafaeleyng

    Just in case somebody else come here and follow the link @jhnns provided, the actual line with the dynamic require is this one.

  8. binki commented on Sep 10, 2016

    @binki

    Shouldn’t webpack just create a chunk for each dynamically loadable language? The script already supports dynamic loading, probably to solve the exact problem webpack’s mishandling of it causes (it solves the problem of throwing irrelevant JavaScript that the browser will never call at the browser).

  9. liqiang372 commented on May 3, 2017

    @liqiang372

    I came across similar dynamic require cases in amd

    var deps = [];
    if( some condition )
        deps = [dep1, dep2, ...]
    else
        deps = [other1, other2, ...]
    define(deps, function(arg1, arg2, ....){
    
    });

    My confiuration for ContextReplacementPlugin is

    new webpack.ContextReplacementPlugin(
          /directory of caller file/, 
          "the directory of dependecies",
          {
            "deps": [dep1, dep2, other1, other2]
          } 
        )

    It seems ContextReplacementPlugin does not support dependency array. Is any better way to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions