How to include and use DefinePlugin in webpack config?












0















Hi there i am trying to use the define plugin so i can update the version number to make sure my JS refreshes after releasing a new build. I can't seem to get DefinePlugin to work properly though. I see it in the folder webpack and i'm trying to follow the documentation but i get errors that it isn't found. Here is my config:



const path = require('path'),
settings = require('./settings');

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');


module.exports = {
entry: {
'scrollerbundled': [settings.themeLocation + "js/scroller.js"],
'mapbundled': [settings.themeLocation + "js/shopmap.js"],
'sculptor': [settings.themeLocation + "js/sculptor.js"]
},
output: {
path: path.resolve(__dirname, settings.themeLocation + "js-dist"),
filename: "[name].js"
},
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
],
plugins: [new webpack.DefinePlugin({
PRODUCTION: JSON.stringify(true),
VERSION: JSON.stringify('5fa3b9'),
})]
},
optimization: {
minimizer: [new UglifyJsPlugin({
uglifyOptions: {
mangle: true,
output: {
comments: false
}
}
})]
},
mode: 'production'
}









share|improve this question



























    0















    Hi there i am trying to use the define plugin so i can update the version number to make sure my JS refreshes after releasing a new build. I can't seem to get DefinePlugin to work properly though. I see it in the folder webpack and i'm trying to follow the documentation but i get errors that it isn't found. Here is my config:



    const path = require('path'),
    settings = require('./settings');

    const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
    const webpack = require('webpack');


    module.exports = {
    entry: {
    'scrollerbundled': [settings.themeLocation + "js/scroller.js"],
    'mapbundled': [settings.themeLocation + "js/shopmap.js"],
    'sculptor': [settings.themeLocation + "js/sculptor.js"]
    },
    output: {
    path: path.resolve(__dirname, settings.themeLocation + "js-dist"),
    filename: "[name].js"
    },
    module: {
    rules: [
    {
    test: /.js$/,
    exclude: /node_modules/,
    use: {
    loader: 'babel-loader',
    options: {
    presets: ['@babel/preset-env']
    }
    }
    }
    ],
    plugins: [new webpack.DefinePlugin({
    PRODUCTION: JSON.stringify(true),
    VERSION: JSON.stringify('5fa3b9'),
    })]
    },
    optimization: {
    minimizer: [new UglifyJsPlugin({
    uglifyOptions: {
    mangle: true,
    output: {
    comments: false
    }
    }
    })]
    },
    mode: 'production'
    }









    share|improve this question

























      0












      0








      0








      Hi there i am trying to use the define plugin so i can update the version number to make sure my JS refreshes after releasing a new build. I can't seem to get DefinePlugin to work properly though. I see it in the folder webpack and i'm trying to follow the documentation but i get errors that it isn't found. Here is my config:



      const path = require('path'),
      settings = require('./settings');

      const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
      const webpack = require('webpack');


      module.exports = {
      entry: {
      'scrollerbundled': [settings.themeLocation + "js/scroller.js"],
      'mapbundled': [settings.themeLocation + "js/shopmap.js"],
      'sculptor': [settings.themeLocation + "js/sculptor.js"]
      },
      output: {
      path: path.resolve(__dirname, settings.themeLocation + "js-dist"),
      filename: "[name].js"
      },
      module: {
      rules: [
      {
      test: /.js$/,
      exclude: /node_modules/,
      use: {
      loader: 'babel-loader',
      options: {
      presets: ['@babel/preset-env']
      }
      }
      }
      ],
      plugins: [new webpack.DefinePlugin({
      PRODUCTION: JSON.stringify(true),
      VERSION: JSON.stringify('5fa3b9'),
      })]
      },
      optimization: {
      minimizer: [new UglifyJsPlugin({
      uglifyOptions: {
      mangle: true,
      output: {
      comments: false
      }
      }
      })]
      },
      mode: 'production'
      }









      share|improve this question














      Hi there i am trying to use the define plugin so i can update the version number to make sure my JS refreshes after releasing a new build. I can't seem to get DefinePlugin to work properly though. I see it in the folder webpack and i'm trying to follow the documentation but i get errors that it isn't found. Here is my config:



      const path = require('path'),
      settings = require('./settings');

      const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
      const webpack = require('webpack');


      module.exports = {
      entry: {
      'scrollerbundled': [settings.themeLocation + "js/scroller.js"],
      'mapbundled': [settings.themeLocation + "js/shopmap.js"],
      'sculptor': [settings.themeLocation + "js/sculptor.js"]
      },
      output: {
      path: path.resolve(__dirname, settings.themeLocation + "js-dist"),
      filename: "[name].js"
      },
      module: {
      rules: [
      {
      test: /.js$/,
      exclude: /node_modules/,
      use: {
      loader: 'babel-loader',
      options: {
      presets: ['@babel/preset-env']
      }
      }
      }
      ],
      plugins: [new webpack.DefinePlugin({
      PRODUCTION: JSON.stringify(true),
      VERSION: JSON.stringify('5fa3b9'),
      })]
      },
      optimization: {
      minimizer: [new UglifyJsPlugin({
      uglifyOptions: {
      mangle: true,
      output: {
      comments: false
      }
      }
      })]
      },
      mode: 'production'
      }






      javascript npm webpack






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 23:34









      Tintinabulator ZeaTintinabulator Zea

      5772721




      5772721
























          2 Answers
          2






          active

          oldest

          votes


















          1














          {
          "parser": "babel-eslint",
          "extends": [
          "airbnb",
          "plugin:react/recommended",
          "prettier",
          "prettier/react"
          ],
          "plugins": ["react", "import", "prettier"],
          "env": {
          "browser": true
          },
          "settings": {
          "import/resolver": {
          "webpack": {
          "config": "webpack.dev.js"
          }
          }
          }
          }


          That's my eslintrc. This is for use absolute imports created in your webpack config with the modules alias. You need to install eslint-import-resolver-webpack






          share|improve this answer































            0














            I Have "webpack": "^4.28.4" and define in webpack config



            new webpack.DefinePlugin({
            PRODUCTION: JSON.stringify(true),
            });


            if you console that variables, you don't find it. I use in conditional



            if (PRODUCTION) {
            //do stuff
            }


            Another case is to set globals variables in a object and share with webpack.



            here is an example



                    new webpack.ProvidePlugin({
            CONFIG: path.resolve(__dirname, './CONSTS.js')
            }),
            // the path is src/CONST.JS


            In the eslintrc file you can add that variables to avoid import errors.



                "settings": {
            "import/resolver": {
            "webpack": {
            "config": "webpack.dev.js"
            }
            }
            }


            then in any file you can use import {value} from 'CONFIG'






            share|improve this answer
























            • where is it that you are setting "settings": { "import/resolver": { "webpack": { "config": "webpack.dev.js" } } }? Could you show your entire config file so i can see under which sections to put the plugin and config?

              – Tintinabulator Zea
              Jan 19 at 20:01











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54262735%2fhow-to-include-and-use-defineplugin-in-webpack-config%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            {
            "parser": "babel-eslint",
            "extends": [
            "airbnb",
            "plugin:react/recommended",
            "prettier",
            "prettier/react"
            ],
            "plugins": ["react", "import", "prettier"],
            "env": {
            "browser": true
            },
            "settings": {
            "import/resolver": {
            "webpack": {
            "config": "webpack.dev.js"
            }
            }
            }
            }


            That's my eslintrc. This is for use absolute imports created in your webpack config with the modules alias. You need to install eslint-import-resolver-webpack






            share|improve this answer




























              1














              {
              "parser": "babel-eslint",
              "extends": [
              "airbnb",
              "plugin:react/recommended",
              "prettier",
              "prettier/react"
              ],
              "plugins": ["react", "import", "prettier"],
              "env": {
              "browser": true
              },
              "settings": {
              "import/resolver": {
              "webpack": {
              "config": "webpack.dev.js"
              }
              }
              }
              }


              That's my eslintrc. This is for use absolute imports created in your webpack config with the modules alias. You need to install eslint-import-resolver-webpack






              share|improve this answer


























                1












                1








                1







                {
                "parser": "babel-eslint",
                "extends": [
                "airbnb",
                "plugin:react/recommended",
                "prettier",
                "prettier/react"
                ],
                "plugins": ["react", "import", "prettier"],
                "env": {
                "browser": true
                },
                "settings": {
                "import/resolver": {
                "webpack": {
                "config": "webpack.dev.js"
                }
                }
                }
                }


                That's my eslintrc. This is for use absolute imports created in your webpack config with the modules alias. You need to install eslint-import-resolver-webpack






                share|improve this answer













                {
                "parser": "babel-eslint",
                "extends": [
                "airbnb",
                "plugin:react/recommended",
                "prettier",
                "prettier/react"
                ],
                "plugins": ["react", "import", "prettier"],
                "env": {
                "browser": true
                },
                "settings": {
                "import/resolver": {
                "webpack": {
                "config": "webpack.dev.js"
                }
                }
                }
                }


                That's my eslintrc. This is for use absolute imports created in your webpack config with the modules alias. You need to install eslint-import-resolver-webpack







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 20 at 14:22









                Fernando ColomFernando Colom

                1662




                1662

























                    0














                    I Have "webpack": "^4.28.4" and define in webpack config



                    new webpack.DefinePlugin({
                    PRODUCTION: JSON.stringify(true),
                    });


                    if you console that variables, you don't find it. I use in conditional



                    if (PRODUCTION) {
                    //do stuff
                    }


                    Another case is to set globals variables in a object and share with webpack.



                    here is an example



                            new webpack.ProvidePlugin({
                    CONFIG: path.resolve(__dirname, './CONSTS.js')
                    }),
                    // the path is src/CONST.JS


                    In the eslintrc file you can add that variables to avoid import errors.



                        "settings": {
                    "import/resolver": {
                    "webpack": {
                    "config": "webpack.dev.js"
                    }
                    }
                    }


                    then in any file you can use import {value} from 'CONFIG'






                    share|improve this answer
























                    • where is it that you are setting "settings": { "import/resolver": { "webpack": { "config": "webpack.dev.js" } } }? Could you show your entire config file so i can see under which sections to put the plugin and config?

                      – Tintinabulator Zea
                      Jan 19 at 20:01
















                    0














                    I Have "webpack": "^4.28.4" and define in webpack config



                    new webpack.DefinePlugin({
                    PRODUCTION: JSON.stringify(true),
                    });


                    if you console that variables, you don't find it. I use in conditional



                    if (PRODUCTION) {
                    //do stuff
                    }


                    Another case is to set globals variables in a object and share with webpack.



                    here is an example



                            new webpack.ProvidePlugin({
                    CONFIG: path.resolve(__dirname, './CONSTS.js')
                    }),
                    // the path is src/CONST.JS


                    In the eslintrc file you can add that variables to avoid import errors.



                        "settings": {
                    "import/resolver": {
                    "webpack": {
                    "config": "webpack.dev.js"
                    }
                    }
                    }


                    then in any file you can use import {value} from 'CONFIG'






                    share|improve this answer
























                    • where is it that you are setting "settings": { "import/resolver": { "webpack": { "config": "webpack.dev.js" } } }? Could you show your entire config file so i can see under which sections to put the plugin and config?

                      – Tintinabulator Zea
                      Jan 19 at 20:01














                    0












                    0








                    0







                    I Have "webpack": "^4.28.4" and define in webpack config



                    new webpack.DefinePlugin({
                    PRODUCTION: JSON.stringify(true),
                    });


                    if you console that variables, you don't find it. I use in conditional



                    if (PRODUCTION) {
                    //do stuff
                    }


                    Another case is to set globals variables in a object and share with webpack.



                    here is an example



                            new webpack.ProvidePlugin({
                    CONFIG: path.resolve(__dirname, './CONSTS.js')
                    }),
                    // the path is src/CONST.JS


                    In the eslintrc file you can add that variables to avoid import errors.



                        "settings": {
                    "import/resolver": {
                    "webpack": {
                    "config": "webpack.dev.js"
                    }
                    }
                    }


                    then in any file you can use import {value} from 'CONFIG'






                    share|improve this answer













                    I Have "webpack": "^4.28.4" and define in webpack config



                    new webpack.DefinePlugin({
                    PRODUCTION: JSON.stringify(true),
                    });


                    if you console that variables, you don't find it. I use in conditional



                    if (PRODUCTION) {
                    //do stuff
                    }


                    Another case is to set globals variables in a object and share with webpack.



                    here is an example



                            new webpack.ProvidePlugin({
                    CONFIG: path.resolve(__dirname, './CONSTS.js')
                    }),
                    // the path is src/CONST.JS


                    In the eslintrc file you can add that variables to avoid import errors.



                        "settings": {
                    "import/resolver": {
                    "webpack": {
                    "config": "webpack.dev.js"
                    }
                    }
                    }


                    then in any file you can use import {value} from 'CONFIG'







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 19 at 3:01









                    Fernando ColomFernando Colom

                    1662




                    1662













                    • where is it that you are setting "settings": { "import/resolver": { "webpack": { "config": "webpack.dev.js" } } }? Could you show your entire config file so i can see under which sections to put the plugin and config?

                      – Tintinabulator Zea
                      Jan 19 at 20:01



















                    • where is it that you are setting "settings": { "import/resolver": { "webpack": { "config": "webpack.dev.js" } } }? Could you show your entire config file so i can see under which sections to put the plugin and config?

                      – Tintinabulator Zea
                      Jan 19 at 20:01

















                    where is it that you are setting "settings": { "import/resolver": { "webpack": { "config": "webpack.dev.js" } } }? Could you show your entire config file so i can see under which sections to put the plugin and config?

                    – Tintinabulator Zea
                    Jan 19 at 20:01





                    where is it that you are setting "settings": { "import/resolver": { "webpack": { "config": "webpack.dev.js" } } }? Could you show your entire config file so i can see under which sections to put the plugin and config?

                    – Tintinabulator Zea
                    Jan 19 at 20:01


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54262735%2fhow-to-include-and-use-defineplugin-in-webpack-config%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Callistus III

                    Ostreoida

                    Plistias Cous