Skip to content

Instantly share code, notes, and snippets.

@iamakulov
Last active September 3, 2020 04:31
Show Gist options
  • Select an option

  • Save iamakulov/59d88d00404259abb83daaf51b70cb07 to your computer and use it in GitHub Desktop.

Select an option

Save iamakulov/59d88d00404259abb83daaf51b70cb07 to your computer and use it in GitHub Desktop.

Revisions

  1. iamakulov revised this gist May 2, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Webpack’s ContextReplacementPlugin examples

    _[The plugin docs](https://webpack.github.io/docs/list-of-plugins.html#contextreplacementplugin)_
    _This is an example to my article [“How webpack’s ContextReplacementPlugin works”](https://iamakulov.com/notes/all/webpack-contextreplacementplugin)_

    You have the _moment.js_ library, and you have a dynamic import:

  2. iamakulov revised this gist Apr 25, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion index.md
    Original file line number Diff line number Diff line change
    @@ -59,4 +59,6 @@ new ContextReplacementPlugin(

    This plugin instance will match all dynamic imports that reference the `/moment[\/\\]locale/` directory.

    It will change the directory in which webpack should look for the files to `./src/locales` – instead of something like `./node_modules/moment/locales`. Also, it will make webpack match the files against the `/(en-gb|ru)\.js/` regular expression – instead of `/^.*\.js/`. Webpack will still look into subdirectories.
    It will change the directory in which webpack should look for the files to `./src/locales` – instead of something like `./node_modules/moment/locales`. Also, it will make webpack match the files against the `/(en-gb|ru)\.js/` regular expression – instead of `/^.*\.js/`. Webpack will still look into subdirectories.

    _Follow me on Twitter: [@iamakulov](https://twitter.com/iamakulov)_
  3. iamakulov revised this gist Apr 25, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Webpack’s ContextReplacementPlugin examples

    _[The plugin docs](https://webpack.github.io/docs/list-of-plugins.html#contextreplacementplugin))_
    _[The plugin docs](https://webpack.github.io/docs/list-of-plugins.html#contextreplacementplugin)_

    You have the _moment.js_ library, and you have a dynamic import:

  4. iamakulov revised this gist Apr 25, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion index.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # ContextReplacementPlugin examples
    # Webpack’s ContextReplacementPlugin examples

    _[The plugin docs](https://webpack.github.io/docs/list-of-plugins.html#contextreplacementplugin))_

    You have the _moment.js_ library, and you have a dynamic import:

  5. iamakulov created this gist Apr 25, 2017.
    60 changes: 60 additions & 0 deletions index.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    # ContextReplacementPlugin examples

    You have the _moment.js_ library, and you have a dynamic import:

    ```js
    require('./locale/' + name + '.js')
    ```

    ## Single parameter: directory

    ```js
    new ContextReplacementPlugin(
    /moment[\/\\]locale/,
    path.resolve(__dirname, './src/locales'),
    )
    ```

    This plugin instance will match all dynamic imports that reference the `/moment[\/\\]locale/` directory.

    It will change the directory in which webpack should look for the files to `./src/locales` – instead of something like `./node_modules/moment/locales`. Webpack will still look into subdirectories and still match the files against `/^.*\.js/` regular expression.

    ## Single parameter: recursive flag

    ```js
    new ContextReplacementPlugin(
    /moment[\/\\]locale/,
    false,
    )
    ```

    This plugin instance will match all dynamic imports that reference the `/moment[\/\\]locale/` directory.

    It will make webpack not look in the subdirectories of the root directory. Webpack will still look into the root `./locales` directory and still match the files against `/^.*\.js/` regular expression.

    ## Single parameter: files regular expression

    ```js
    new ContextReplacementPlugin(
    /moment[\/\\]locale/,
    /(en-gb|ru)\.js/,
    )
    ```

    This plugin instance will match all dynamic imports that reference the `/moment[\/\\]locale/` directory.

    It will make webpack match the files against the `/(en-gb|ru)\.js/` regular expression – instead of `/^.*\.js/`. Webpack will still look into the root `./locales` directory and still recursively traverse its subdirectories.

    ## Multiple parameters

    ```js
    new ContextReplacementPlugin(
    /moment[\/\\]locale/,
    path.resolve(__dirname, './src/locales'),
    /(en-gb|ru)\.js/,
    )
    ```

    This plugin instance will match all dynamic imports that reference the `/moment[\/\\]locale/` directory.

    It will change the directory in which webpack should look for the files to `./src/locales` – instead of something like `./node_modules/moment/locales`. Also, it will make webpack match the files against the `/(en-gb|ru)\.js/` regular expression – instead of `/^.*\.js/`. Webpack will still look into subdirectories.