From 46bd532aae400f78d87738f3b03a9380d27115ea Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 24 Jun 2020 04:25:03 +0300 Subject: [PATCH] refactor: `override` --- README.md | 76 +++- src/index.js | 18 +- src/options.json | 3 + src/runtime/getGlobalThis.js | 36 +- src/utils.js | 22 +- test/__snapshots__/loader.test.js.snap | 387 ++++++++++++++---- .../validate-options.test.js.snap | 28 +- test/fixtures/override-1.js | 9 + test/helpers/execute.js | 2 + test/loader.test.js | 215 +++++++++- test/runtime/getGlobalThis.test.js | 7 + test/validate-options.test.js | 1 + 12 files changed, 676 insertions(+), 128 deletions(-) create mode 100644 test/fixtures/override-1.js create mode 100644 test/runtime/getGlobalThis.test.js diff --git a/README.md b/README.md index fbbffd7..e085607 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ # expose-loader -The `expose-loader` loader allow you to expose a module (in whole or in part) to `global` scope (`self`, `window` and `global`). +The `expose-loader` loader allows to expose a module (in whole or in part) to global object (`self`, `window` and `global`). For further hints on compatibility issues, check out [Shimming](https://webpack.js.org/guides/shimming/) of the official docs. @@ -30,33 +30,37 @@ Then you can use the `expose-loader` using two approaches. ## Inline -The `|` or `%20` (space) separate command parts. - -> ⚠ `%20` is space in a query string, because you can't use spaces in URLs - **src/index.js** ```js import $ from 'expose-loader?exposes[]=$&exposes[]=jQuery!jquery'; // -// Adds the `jquery` to the `global` object under the names `$` and `jQuery` +// Adds the `jquery` to the global object under the names `$` and `jQuery` ``` +**src/index.js** + ```js import { concat } from 'expose-loader?exposes=_.concat!lodash/concat'; // -// Adds the `lodash/concat` to the `global` object under the name `_.concat` +// Adds the `lodash/concat` to the global object under the name `_.concat` ``` +**src/index.js** + ```js import { map, reduce, } from 'expose-loader?exposes[]=_.map|map&exposes[]=_.reduce|reduce!underscore'; // -// Adds the `map` and `reduce` method from `underscore` to the `global` object under the name `_.map` and `_.reduce` +// Adds the `map` and `reduce` method from `underscore` to the global object under the name `_.map` and `_.reduce` ``` +The `|` or `%20` (space) allow to separate the export name of the module and the name in the global object. + +> ⚠ `%20` is space in a query string, because you can't use spaces in URLs + Description of string values can be found in the documentation below. ## Using Configuration @@ -125,12 +129,13 @@ List of exposes. Allows to use a string to describe an expose. -String syntax - `[[globalName] [moduleLocalName]]` or `[[globalName]|[moduleLocalName]]`, where: +String syntax - `[[globalName] [moduleLocalName] [override]]` or `[[globalName]|[moduleLocalName]|[override]]`, where: -- `globalName` - the name under which the value will be available in the global scope, for example `windows.$` for a browser environment (**required**) -- `moduleLocalName` - the name of method or variable (module should export it) (**may be omitted**) +- `globalName` - the name in the global object, for example `window.$` for a browser environment (**required**) +- `moduleLocalName` - the name of method/variable/etc of the module (the module must export it) (**may be omitted**) +- `override` - allows to override existing value in the global object (**may be omitted**) -If no `moduleLocalName` is specified, it exposes the entire module to global scope, otherwise it exposes only the `moduleLocalName` value. +If `moduleLocalName` is not specified, it exposes the entire module to the global object, otherwise it exposes only the value of `moduleLocalName`. **src/index.js** @@ -166,7 +171,7 @@ Allows to use an object to describe an expose. Type: `String|Array` Default: `undefined` -Name of an exposed value in `global` scope (**required**). +The name in the global object. (**required**). **src/index.js** @@ -201,9 +206,8 @@ module.exports = { Type: `String` Default: `undefined` -Name of method or variable (module should export it). - -If the `moduleLocalName` option is specified, it exposes only the `moduleLocalName` value. +The name of method/variable/etc of the module (the module must export it). +If `moduleLocalName` is specified, it exposes only the value of `moduleLocalName`. **src/index.js** @@ -232,6 +236,44 @@ module.exports = { }; ``` +##### `override` + +Type: `Boolean` +Default: `false` + +By default loader does not override the existing value in the global object, because it is unsafe. +In `development` mode, we throw an error if the value already present in the global object. +But you can configure loader to override the existing value in the global object using this option. + +To force override the value that is already present in the global object you can set the `override` option to the `true` value. + +**src/index.js** + +```js +import $ from 'jquery'; +``` + +**webpack.config.js** + +```js +module.exports = { + module: { + rules: [ + { + test: require.resolve('jquery'), + loader: 'expose-loader', + options: { + exposes: { + globalName: '$', + override: true, + }, + }, + }, + ], + }, +}; +``` + #### `Array` **src/index.js** @@ -268,7 +310,7 @@ module.exports = { }; ``` -It will expose **only** `map`, `filter` and `find` (under `myNameForFind` name) methods in global scope. +It will expose **only** `map`, `filter` and `find` (under `myNameForFind` name) methods to the global object. In a browser these methods will be available under `windows._.map(..args)`, `windows._.filter(...args)` and `windows._.myNameForFind(...args)` methods. diff --git a/src/index.js b/src/index.js index 1e8eded..d6e56ad 100644 --- a/src/index.js +++ b/src/index.js @@ -56,10 +56,10 @@ export default function loader() { this, require.resolve('./runtime/getGlobalThis.js') )});\n`; - code += `var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___();\n`; + code += `var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___;\n`; for (const expose of exposes) { - const { globalName, moduleLocalName } = expose; + const { globalName, moduleLocalName, override } = expose; const globalNameInterpolated = globalName.map((item) => interpolateName(this, item, {}) ); @@ -72,16 +72,28 @@ export default function loader() { for (let i = 0; i < globalName.length; i++) { if (i > 0) { - code += `if (!${propertyString}) ${propertyString} = {};\n`; + code += `if (typeof ${propertyString} === 'undefined') ${propertyString} = {};\n`; } propertyString += `[${JSON.stringify(globalNameInterpolated[i])}]`; } + if (!override) { + code += `if (typeof ${propertyString} === 'undefined') `; + } + code += typeof moduleLocalName !== 'undefined' ? `${propertyString} = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___;\n` : `${propertyString} = ___EXPOSE_LOADER_IMPORT___;\n`; + + if (!override) { + if (this.mode === 'development') { + code += `else throw new Error('[exposes-loader] The "${globalName.join( + '.' + )}" value exists in the global scope, it may not be safe to overwrite it, use the "override" option')\n`; + } + } } code += `module.exports = ___EXPOSE_LOADER_IMPORT___;\n`; diff --git a/src/options.json b/src/options.json index 85648ea..d6c7c66 100644 --- a/src/options.json +++ b/src/options.json @@ -23,6 +23,9 @@ "moduleLocalName": { "type": "string", "minLength": 1 + }, + "override": { + "type": "boolean" } }, "required": ["globalName"] diff --git a/src/runtime/getGlobalThis.js b/src/runtime/getGlobalThis.js index 8695457..4c1b815 100644 --- a/src/runtime/getGlobalThis.js +++ b/src/runtime/getGlobalThis.js @@ -1,19 +1,31 @@ -module.exports = function getGlobalThis() { - if (typeof globalThis !== 'undefined') { +// eslint-disable-next-line func-names +module.exports = (function () { + if (typeof globalThis === 'object') { return globalThis; } - if (typeof self !== 'undefined') { - return self; - } + let g; - if (typeof window !== 'undefined') { - return window; - } + try { + // This works if eval is allowed (see CSP) + // eslint-disable-next-line no-new-func + g = this || new Function('return this')(); + } catch (e) { + // This works if the window reference is available + if (typeof window === 'object') { + return window; + } + + // This works if the self reference is available + if (typeof self === 'object') { + return self; + } - if (typeof global !== 'undefined') { - return global; + // This works if the global reference is available + if (typeof global !== 'undefined') { + return global; + } } - throw new Error('unable to locate global object'); -}; + return g; +})(); diff --git a/src/utils.js b/src/utils.js index 60c2558..589c833 100644 --- a/src/utils.js +++ b/src/utils.js @@ -33,19 +33,39 @@ function splitCommand(command) { return result; } +function parseBoolean(string, defaultValue = null) { + if (typeof string === 'undefined') { + return defaultValue; + } + + switch (string.toLowerCase()) { + case 'true': + return true; + case 'false': + return false; + default: + return defaultValue; + } +} + function resolveExposes(item) { let result; if (typeof item === 'string') { const splittedItem = splitCommand(item.trim()); - if (splittedItem.length > 2) { + if (splittedItem.length > 3) { throw new Error(`Invalid "${item}" for exposes`); } result = { globalName: splittedItem[0], moduleLocalName: splittedItem[1], + override: + typeof splittedItem[2] !== 'undefined' + ? parseBoolean(splittedItem[2], false) + : // eslint-disable-next-line no-undefined + undefined, }; } else { result = item; diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 5cd37c5..fa9605e 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -40,12 +40,7 @@ Error: Invalid command \\"\\" in \\"myGlobal_alias | globalObject6\\" for expo exports[`loader should emit error because of invalid arguments: warnings 1`] = `Array []`; -exports[`loader should emit error because of many arguments: errors 1`] = ` -Array [ - "ModuleBuildError: Module build failed (from \`replaced original path\`): -Error: Invalid \\"myGlobal_alias globalObject6 excessArgument\\" for exposes", -] -`; +exports[`loader should emit error because of many arguments: errors 1`] = `Array []`; exports[`loader should emit error because of many arguments: warnings 1`] = `Array []`; @@ -54,22 +49,223 @@ exports[`loader should match hashes on all operating systems: errors 1`] = `Arra exports[`loader should match hashes on all operating systems: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./simple-commonjs2-single-export.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; exports[`loader should match hashes on all operating systems: warnings 1`] = `Array []`; +exports[`loader should not override existing value in the global object in the "production" mode: errors 1`] = `Array []`; + +exports[`loader should not override existing value in the global object in the "production" mode: module 1`] = ` +"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); +var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +module.exports = ___EXPOSE_LOADER_IMPORT___; +" +`; + +exports[`loader should not override existing value in the global object in the "production" mode: result 1`] = ` +Object { + "ExposeLoader": "no exports", + "global-commonjs2-single-export": Object { + "foo": "bar", + }, + "myGlobal": "not overridden", + "myOtherGlobal": Object { + "foo": "not overridden", + }, +} +`; + +exports[`loader should not override existing value in the global object in the "production" mode: warnings 1`] = `Array []`; + +exports[`loader should throw an error on existing module local value in the global object: errors 1`] = `Array []`; + +exports[`loader should throw an error on existing module local value in the global object: module 1`] = ` +"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); +var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.foo +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') +module.exports = ___EXPOSE_LOADER_IMPORT___; +" +`; + +exports[`loader should throw an error on existing module local value in the global object: runtime error 1`] = `"[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option"`; + +exports[`loader should throw an error on existing module local value in the global object: warnings 1`] = `Array []`; + +exports[`loader should throw an error on existing nested value in the global object: errors 1`] = `Array []`; + +exports[`loader should throw an error on existing nested value in the global object: module 1`] = ` +"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); +var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"][\\"foo\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"][\\"foo\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"][\\"foo\\"][\\"bar\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"][\\"foo\\"][\\"bar\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"][\\"foo\\"][\\"bar\\"][\\"bar\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"][\\"foo\\"][\\"bar\\"][\\"bar\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myOtherGlobal.foo.bar.bar\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') +module.exports = ___EXPOSE_LOADER_IMPORT___; +" +`; + +exports[`loader should throw an error on existing nested value in the global object: runtime error 1`] = `"Cannot read property 'bar' of undefined"`; + +exports[`loader should throw an error on existing nested value in the global object: warnings 1`] = `Array []`; + +exports[`loader should throw an error on existing value in the global object in the "development" mode: errors 1`] = `Array []`; + +exports[`loader should throw an error on existing value in the global object in the "development" mode: module 1`] = ` +"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); +var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') +module.exports = ___EXPOSE_LOADER_IMPORT___; +" +`; + +exports[`loader should throw an error on existing value in the global object in the "development" mode: runtime error 1`] = `"[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option"`; + +exports[`loader should throw an error on existing value in the global object in the "development" mode: warnings 1`] = `Array []`; + +exports[`loader should throw an error on invalid exposed value: errors 1`] = ` +Array [ + "ModuleBuildError: Module build failed (from \`replaced original path\`): +Error: Invalid \\"myGlobal foo bar baz\\" for exposes", +] +`; + +exports[`loader should throw an error on invalid exposed value: warnings 1`] = `Array []`; + +exports[`loader should work and override existing module local value in the global object: errors 1`] = `Array []`; + +exports[`loader should work and override existing module local value in the global object: module 1`] = ` +"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); +var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.foo +___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +module.exports = ___EXPOSE_LOADER_IMPORT___; +" +`; + +exports[`loader should work and override existing module local value in the global object: result 1`] = ` +Object { + "ExposeLoader": "no exports", + "global-commonjs2-single-export": Object { + "foo": "bar", + }, + "myGlobal": "bar", + "myOtherGlobal": Object { + "foo": "not overridden", + }, +} +`; + +exports[`loader should work and override existing module local value in the global object: warnings 1`] = `Array []`; + +exports[`loader should work and override existing nested value in the global object: errors 1`] = `Array []`; + +exports[`loader should work and override existing nested value in the global object: module 1`] = ` +"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); +var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = {}; +___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"][\\"foo\\"] = ___EXPOSE_LOADER_IMPORT___; +module.exports = ___EXPOSE_LOADER_IMPORT___; +" +`; + +exports[`loader should work and override existing nested value in the global object: result 1`] = ` +Object { + "ExposeLoader": "no exports", + "global-commonjs2-single-export": Object { + "foo": "bar", + }, + "myGlobal": "not overridden", + "myOtherGlobal": Object { + "foo": Object { + "foo": "bar", + }, + }, +} +`; + +exports[`loader should work and override existing nested value in the global object: warnings 1`] = `Array []`; + +exports[`loader should work and override existing value in the global object in the "development" mode: errors 1`] = `Array []`; + +exports[`loader should work and override existing value in the global object in the "development" mode: module 1`] = ` +"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); +var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +module.exports = ___EXPOSE_LOADER_IMPORT___; +" +`; + +exports[`loader should work and override existing value in the global object in the "development" mode: result 1`] = ` +Object { + "ExposeLoader": "no exports", + "global-commonjs2-single-export": Object { + "foo": "bar", + }, + "myGlobal": Object { + "foo": "bar", + }, + "myOtherGlobal": Object { + "foo": "not overridden", + }, +} +`; + +exports[`loader should work and override existing value in the global object in the "development" mode: warnings 1`] = `Array []`; + +exports[`loader should work and override existing value in the global object in the "production" mode: errors 1`] = `Array []`; + +exports[`loader should work and override existing value in the global object in the "production" mode: module 1`] = ` +"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); +var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +module.exports = ___EXPOSE_LOADER_IMPORT___; +" +`; + +exports[`loader should work and override existing value in the global object in the "production" mode: result 1`] = ` +Object { + "ExposeLoader": "no exports", + "global-commonjs2-single-export": Object { + "foo": "bar", + }, + "myGlobal": Object { + "foo": "bar", + }, + "myOtherGlobal": Object { + "foo": "not overridden", + }, +} +`; + +exports[`loader should work and override existing value in the global object in the "production" mode: warnings 1`] = `Array []`; + exports[`loader should work for a nested property for a global object: errors 1`] = `Array []`; exports[`loader should work for a nested property for a global object: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"nested\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"nested\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"nested\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal.nested\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -94,12 +290,14 @@ exports[`loader should work for nested properties for a global object: errors 1` exports[`loader should work for nested properties for a global object: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"nested\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"nested\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"nested\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal.nested\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.foo -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"][\\"nested\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"][\\"nested\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"][\\"nested\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myOtherGlobal.nested\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -127,8 +325,9 @@ exports[`loader should work from esModule export: errors 1`] = `Array []`; exports[`loader should work from esModule export: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-module-default-export.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -155,9 +354,10 @@ exports[`loader should work inline 1 without extension: errors 1`] = `Array []`; exports[`loader should work inline 1 without extension: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!../../node_modules/babel-loader/lib/index.js!./custom?foo=bar\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.default -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -178,9 +378,10 @@ exports[`loader should work inline 1: errors 1`] = `Array []`; exports[`loader should work inline 1: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!../../node_modules/babel-loader/lib/index.js!./custom.js?foo=bar\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.default -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -201,8 +402,9 @@ exports[`loader should work inline 2: errors 1`] = `Array []`; exports[`loader should work inline 2: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./simple-commonjs2-single-export.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -243,10 +445,12 @@ exports[`loader should work interpolate: errors 1`] = `Array []`; exports[`loader should work interpolate: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"global-commonjs2-single-export\\"] = ___EXPOSE_LOADER_IMPORT___; -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"global-commonjs2-single-export\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"global-commonjs2-single-export\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"global-commonjs2-single-export\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"[name]\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"global-commonjs2-single-export\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"global-commonjs2-single-export\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal.[name]\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -274,11 +478,13 @@ exports[`loader should work multiple commonjs exports: errors 1`] = `Array []`; exports[`loader should work multiple commonjs exports: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-multiple-exports.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myOtherGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.globalObject2 -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"globalObject2\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"globalObject2\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"][\\"globalObject2\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal.globalObject2\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -316,16 +522,19 @@ exports[`loader should work multiple syntax to array: errors 1`] = `Array []`; exports[`loader should work multiple syntax to array: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-module-named-exports.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.globalObject6 -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject6\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject6\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject6\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal_alias.globalObject6\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.globalObject7 -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject7\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject7\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject7\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal_alias.globalObject7\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.default -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"default\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"default\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"default\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal_alias.default\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -362,10 +571,11 @@ exports[`loader should work object config: errors 1`] = `Array []`; exports[`loader should work object config: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-module-named-exports.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.globalObject6 -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal.alias\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal.alias\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal.alias\\"][\\"globalObject6\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal.alias\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal.alias\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal.alias\\"][\\"globalObject6\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal.alias\\"][\\"globalObject6\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal.alias.globalObject6\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -398,18 +608,23 @@ exports[`loader should work string config 2: errors 1`] = `Array []`; exports[`loader should work string config 2: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-module-named-exports.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.globalObject6 -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject6\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject6\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject6\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal_alias.globalObject6\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.globalObject7 -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject7\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject7\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject7\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal_alias.globalObject7\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.default -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"default\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"default\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"default\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal_alias.default\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myOtherGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -464,18 +679,23 @@ exports[`loader should work string config: errors 1`] = `Array []`; exports[`loader should work string config: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-module-named-exports.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.globalObject6 -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject6\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject6\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject6\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal_alias.globalObject6\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.globalObject7 -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject7\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject7\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"globalObject7\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal_alias.globalObject7\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.default -if (!___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"default\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"] = {}; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"default\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal_alias\\"][\\"default\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"myGlobal_alias.default\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myOtherGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -530,9 +750,10 @@ exports[`loader should work with "moduleLocalName": errors 1`] = `Array []`; exports[`loader should work with "moduleLocalName": module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-multiple-exports.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.myGlobal -___EXPOSE_LOADER_GLOBAL_THIS___[\\"moduleMethod\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"moduleMethod\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"moduleMethod\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___; +else throw new Error('[exposes-loader] The \\"moduleMethod\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -557,8 +778,9 @@ exports[`loader should work with CommonJS format when module in CommonJS format: exports[`loader should work with CommonJS format when module in CommonJS format: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-module-commonjs.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -586,8 +808,9 @@ exports[`loader should work with CommonJS module format when module in ES module exports[`loader should work with CommonJS module format when module in ES module format: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-module-es.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -625,8 +848,9 @@ exports[`loader should work with ES module format when module in CommonJS format exports[`loader should work with ES module format when module in CommonJS format: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-module-commonjs.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -654,8 +878,9 @@ exports[`loader should work with ES module format when module in ES format witho exports[`loader should work with ES module format when module in ES format without default: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-module-es-without-default.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -687,8 +912,9 @@ exports[`loader should work with ES module format when module in ES format: erro exports[`loader should work with ES module format when module in ES format: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-module-es.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -726,9 +952,11 @@ exports[`loader should work with multiple exposes: errors 1`] = `Array []`; exports[`loader should work with multiple exposes: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myOtherGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myOtherGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -754,8 +982,8 @@ exports[`loader should work with side-effects free modules: errors 1`] = `Array exports[`loader should work with side-effects free modules: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./rx.all.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; module.exports = ___EXPOSE_LOADER_IMPORT___; " `; @@ -864,8 +1092,9 @@ exports[`loader should work: errors 1`] = `Array []`; exports[`loader should work: module 1`] = ` "var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./global-commonjs2-single-export.js\\"); var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\"); -var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___(); -___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___; +if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___; +else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option') module.exports = ___EXPOSE_LOADER_IMPORT___; " `; diff --git a/test/__snapshots__/validate-options.test.js.snap b/test/__snapshots__/validate-options.test.js.snap index dddf67e..09ee583 100644 --- a/test/__snapshots__/validate-options.test.js.snap +++ b/test/__snapshots__/validate-options.test.js.snap @@ -30,71 +30,71 @@ exports[`validate options should throw an error on the "exposes" option with "{} exports[`validate options should throw an error on the "exposes" option with "false" value 1`] = ` "Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema. - options.exposes should be one of these: - non-empty string | object { globalName, moduleLocalName? } | [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item) + non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item) Details: * options.exposes should be a non-empty string. * options.exposes should be an object: - object { globalName, moduleLocalName? } + object { globalName, moduleLocalName?, override? } * options.exposes should be an array: - [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item)" + [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)" `; exports[`validate options should throw an error on the "exposes" option with "true" value 1`] = ` "Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema. - options.exposes should be one of these: - non-empty string | object { globalName, moduleLocalName? } | [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item) + non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item) Details: * options.exposes should be a non-empty string. * options.exposes should be an object: - object { globalName, moduleLocalName? } + object { globalName, moduleLocalName?, override? } * options.exposes should be an array: - [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item)" + [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)" `; exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = ` "Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema. - options misses the property 'exposes'. Should be: - non-empty string | object { globalName, moduleLocalName? } | [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item)" + non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)" `; exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = ` "Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema. - options misses the property 'exposes'. Should be: - non-empty string | object { globalName, moduleLocalName? } | [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item)" + non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)" `; exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = ` "Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema. - options misses the property 'exposes'. Should be: - non-empty string | object { globalName, moduleLocalName? } | [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item)" + non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)" `; exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = ` "Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema. - options misses the property 'exposes'. Should be: - non-empty string | object { globalName, moduleLocalName? } | [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item)" + non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)" `; exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = ` "Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema. - options misses the property 'exposes'. Should be: - non-empty string | object { globalName, moduleLocalName? } | [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item)" + non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)" `; exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = ` "Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema. - options misses the property 'exposes'. Should be: - non-empty string | object { globalName, moduleLocalName? } | [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item)" + non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)" `; exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = ` "Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema. - options misses the property 'exposes'. Should be: - non-empty string | object { globalName, moduleLocalName? } | [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item)" + non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)" `; exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = ` "Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema. - options misses the property 'exposes'. Should be: - non-empty string | object { globalName, moduleLocalName? } | [non-empty string | object { globalName, moduleLocalName? }, ...] (should not have fewer than 1 item)" + non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)" `; diff --git a/test/fixtures/override-1.js b/test/fixtures/override-1.js new file mode 100644 index 0000000..fae66c0 --- /dev/null +++ b/test/fixtures/override-1.js @@ -0,0 +1,9 @@ +const myGlobalThis = require('../../src/runtime/getGlobalThis'); + +myGlobalThis.myGlobal = 'not overridden'; + +myGlobalThis.myOtherGlobal = { foo: 'not overridden' }; + +const myExports = require('./global-commonjs2-single-export'); + +module.exports = 'no exports'; diff --git a/test/helpers/execute.js b/test/helpers/execute.js index 2d9bf2d..5f985c3 100644 --- a/test/helpers/execute.js +++ b/test/helpers/execute.js @@ -15,6 +15,8 @@ export default (code) => { // eslint-disable-next-line no-underscore-dangle module._compile( ` +console.log = () => {}; + const result = {}; if (typeof myGlobal !== "undefined") { diff --git a/test/loader.test.js b/test/loader.test.js index c871fa2..93cc4d7 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -348,8 +348,8 @@ describe('loader', () => { ).toMatchSnapshot('module'); expect(module.hash).toBe( isWebpack5 - ? 'b993fe180cfddcb0d654fd862e6f1afe' - : '40beb9b0cb6f070cad500e1179e00e12' + ? '66885468666c44a6f1c2edf107b8893d' + : 'c3e516476bee11406ecca2a29b66c743' ); expect(getErrors(stats)).toMatchSnapshot('errors'); expect(getWarnings(stats)).toMatchSnapshot('warnings'); @@ -391,6 +391,207 @@ describe('loader', () => { expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); + it('should throw an error on existing value in the global object in the "development" mode', async () => { + const compiler = getCompiler( + 'override-1.js', + { + exposes: { + globalName: ['myGlobal'], + override: false, + }, + }, + { + mode: 'development', + } + ); + const stats = await compile(compiler); + + expect( + getModuleSource('./global-commonjs2-single-export-exposed.js', stats) + ).toMatchSnapshot('module'); + expect(() => + execute(readAsset('main.bundle.js', compiler, stats)) + ).toThrowErrorMatchingSnapshot('runtime error'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); + + it('should not override existing value in the global object in the "production" mode', async () => { + const compiler = getCompiler( + 'override-1.js', + { + exposes: { + globalName: ['myGlobal'], + }, + }, + { + mode: 'production', + } + ); + const stats = await compile(compiler); + + expect( + getModuleSource('./global-commonjs2-single-export-exposed.js', stats) + ).toMatchSnapshot('module'); + expect( + execute(readAsset('main.bundle.js', compiler, stats)) + ).toMatchSnapshot('result'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); + + it('should work and override existing value in the global object in the "development" mode', async () => { + const compiler = getCompiler( + 'override-1.js', + { + exposes: { + globalName: ['myGlobal'], + override: true, + }, + }, + { + mode: 'development', + } + ); + const stats = await compile(compiler); + + expect( + getModuleSource('./global-commonjs2-single-export-exposed.js', stats) + ).toMatchSnapshot('module'); + expect( + execute(readAsset('main.bundle.js', compiler, stats)) + ).toMatchSnapshot('result'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); + + it('should work and override existing value in the global object in the "production" mode', async () => { + const compiler = getCompiler( + 'override-1.js', + { + exposes: { + globalName: ['myGlobal'], + override: true, + }, + }, + { + mode: 'production', + } + ); + const stats = await compile(compiler); + + expect( + getModuleSource('./global-commonjs2-single-export-exposed.js', stats) + ).toMatchSnapshot('module'); + expect( + execute(readAsset('main.bundle.js', compiler, stats)) + ).toMatchSnapshot('result'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); + + it('should throw an error on existing module local value in the global object', async () => { + const compiler = getCompiler( + 'override-1.js', + { + exposes: { + moduleLocalName: 'foo', + globalName: ['myGlobal'], + override: false, + }, + }, + { + mode: 'development', + } + ); + const stats = await compile(compiler); + + expect( + getModuleSource('./global-commonjs2-single-export-exposed.js', stats) + ).toMatchSnapshot('module'); + expect(() => + execute(readAsset('main.bundle.js', compiler, stats)) + ).toThrowErrorMatchingSnapshot('runtime error'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); + + it('should work and override existing module local value in the global object', async () => { + const compiler = getCompiler( + 'override-1.js', + { + exposes: { + moduleLocalName: 'foo', + globalName: ['myGlobal'], + override: true, + }, + }, + { + mode: 'development', + } + ); + const stats = await compile(compiler); + + expect( + getModuleSource('./global-commonjs2-single-export-exposed.js', stats) + ).toMatchSnapshot('module'); + expect( + execute(readAsset('main.bundle.js', compiler, stats)) + ).toMatchSnapshot('result'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); + + it('should throw an error on existing nested value in the global object', async () => { + const compiler = getCompiler( + 'override-1.js', + { + exposes: { + globalName: ['myOtherGlobal', 'foo', 'bar', 'bar'], + override: false, + }, + }, + { + mode: 'development', + } + ); + const stats = await compile(compiler); + + expect( + getModuleSource('./global-commonjs2-single-export-exposed.js', stats) + ).toMatchSnapshot('module'); + expect(() => + execute(readAsset('main.bundle.js', compiler, stats)) + ).toThrowErrorMatchingSnapshot('runtime error'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); + + it('should work and override existing nested value in the global object', async () => { + const compiler = getCompiler( + 'override-1.js', + { + exposes: { + globalName: ['myOtherGlobal', 'foo'], + override: true, + }, + }, + { + mode: 'development', + } + ); + const stats = await compile(compiler); + + expect( + getModuleSource('./global-commonjs2-single-export-exposed.js', stats) + ).toMatchSnapshot('module'); + expect( + execute(readAsset('main.bundle.js', compiler, stats)) + ).toMatchSnapshot('result'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); + it('should work with CommonJS format when module in CommonJS format', async () => { const compiler = getCompiler('loader-commonjs-with-commonjs.js', { exposes: 'myGlobal', @@ -492,4 +693,14 @@ describe('loader', () => { expect(getErrors(stats)).toMatchSnapshot('errors'); expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); + + it('should throw an error on invalid exposed value', async () => { + const compiler = getCompiler('simple-commonjs2-single-export.js', { + exposes: 'myGlobal foo bar baz', + }); + const stats = await compile(compiler); + + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); }); diff --git a/test/runtime/getGlobalThis.test.js b/test/runtime/getGlobalThis.test.js new file mode 100644 index 0000000..8782384 --- /dev/null +++ b/test/runtime/getGlobalThis.test.js @@ -0,0 +1,7 @@ +import getGlobalThis from '../../src/runtime/getGlobalThis'; + +describe('getGlobalThis', () => { + it('should work', () => { + expect(getGlobalThis).toEqual(global); + }); +}); diff --git a/test/validate-options.test.js b/test/validate-options.test.js index ba14a61..c6e9e76 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -17,6 +17,7 @@ describe('validate options', () => { }, 'globalObject1', 'globalObject1 myMethodName', + 'globalObject1 myMethodName true', 'globalObject1.foo', 'globalObject1.foo myMethodName', ['globalObject1'],