[bug?] Webpack adds “use strict” to non-ES6 modules.

Do you want to request a feature or report a bug? bug

What is the current behavior?

Webpack adds "use strict"; to non-ES6 modules (for example a CommonJS module).

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

Build an entry point that is CommonJS format, and Webpack will add use strict to it.

What is the expected behavior?

Webpack should not add use strict unless the source code already has it, or if the module is an ES6+ module.

If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

Webpack 2.2.1.

Details:

@sokra mentioned:

v2 doesn’t add it by default. It only adds it to ES6 modules (spec).

I am passing code to Webpack 2.2.1 that looks like the following, which is not ES6 module format (note the require() call):

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Utils = require("@Utils");
function main() {
    // Yay, we can use ES6 classes!
    var App = (function (_super) {
        __extends(App, _super);
        function App() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        App.prototype.initialize = function (options) {
            _super.prototype.initialize.call(this, options);
            this.blah();
        };
        return App;
    }(Utils.BaseApp));
}
main();

However Webpack still adds use strict to it:

/* 1 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Utils = __webpack_require__(0);
function main() {
    // Yay, we can use ES6 classes!
    var App = (function (_super) {
        __extends(App, _super);
        function App() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        App.prototype.initialize = function (options) {
            _super.prototype.initialize.call(this, options);
            this.blah();
        };
        return App;
    }(Utils.BaseApp));
}
main();


/***/ })

Is this a bug?

Author: Fantashit

1 thought on “[bug?] Webpack adds “use strict” to non-ES6 modules.

  1. Could it be that ts-loader is ignoring my tsconfig.json and adding "use strict" automatically?

    The reason I’m wondering is because when I run tsc by hand, no use strict is outputted. Then when I run webpack I see the use strict.

    I would think that ts-loader would obey tsconfig.json.

    @jbrantly @johnnyreilly What’s ts-loader doing?

Comments are closed.