this transformed to undefined in webpack 4

Bug report

What is the current behavior?
module this is being replace to undefined when switching to webpack 4 from webpack 3.

If the current behavior is a bug, please provide the steps to reproduce.

thisTest.js:

this.hello = 'helloworld';
console.log(this.hello);

index.js

import a from './thisTest';

package.json

"build:dev": "webpack --config ./build/webpack.dev.js --mode development"

webpack.dev.js

const PATH = require('path');
//-------------------
// Modes
//-------------------
const ENV_MODES = {
    DEV: 'development',
    PROD: 'production'
};
const ENV_MODE = process.env.NODE_ENV;

const ROOT = PATH.resolve(__dirname, '../');
const SRC_FOLDER = PATH.resolve(__dirname, ROOT, 'src/');
const BUILD_FOLDER = PATH.resolve(__dirname, ROOT, 'dist/');

const publicPath = (ENV_MODE === ENV_MODES.DEV) ? '/' : '/retail/';

module.exports = {
    mode: 'development',
    cache: true,
    context: ROOT,
    entry: {
        bundle: PATH.resolve(SRC_FOLDER, 'index.js'),
    },
    output: {
        filename: '[name].js',
        publicPath,
        path: BUILD_FOLDER,
        chunkFilename: '[name].chunk.js'
    },
    module: {
        rules: [
            {
                test: /(\.jsx|\.js)$/,
                include: [SRC_FOLDER],
                use: {
                    loader: 'babel-loader',
                },
            },
        ]
    },
};

npm run build:dev

output: bundle.js

actual:
undefined.hello = ‘helloworld’

expected:
this.hello = ‘helloworld’

try to run node bundle.js

Cannot set property ‘hello’ of undefined

What is the expected behavior?
this.hello = ‘helloworld’

Other relevant information:
webpack version: 4.6.15 (tried with every webpack version on 4)
Node.js version: 8.11.3 (same issue on node 6)
“webpack”: “~4.16.5”,
“webpack-cli”: “~3.1.0”,
“babel-cli”: “~6.26.0”,
“babel-loader”: “~7.1.4”,
“babel-preset-env”: “~1.7.0”,

Operating System:
Windows 10

Additional tools:

Author: Fantashit

1 thought on “this transformed to undefined in webpack 4

Comments are closed.