diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 50978dd..eebd0d6 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -68,6 +68,12 @@ Object { exports[`loader should work for nested preporties for a global object: warnings 1`] = `Array []`; +exports[`loader should work with inline sourceMap: errors 1`] = `Array []`; + +exports[`loader should work with inline sourceMap: sourceMap 1`] = `"data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9zaW1wbGUtY29tbW9uanMyLXNpbmdsZS1leHBvcnQuanMuanMiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly9FeHBvc2VMb2FkZXIvLi9zaW1wbGUtY29tbW9uanMyLXNpbmdsZS1leHBvcnQuanM/ZmM0YyJdLCJzb3VyY2VzQ29udGVudCI6WyJjb25zdCBteUV4cG9ydHMgPSByZXF1aXJlKCcuL2dsb2JhbC1jb21tb25qczItc2luZ2xlLWV4cG9ydCcpO1xuXG5tb2R1bGUuZXhwb3J0cyA9IG15RXhwb3J0cztcbiJdLCJtYXBwaW5ncyI6IkFBQUE7QUFDQTtBQUNBOyIsInNvdXJjZVJvb3QiOiIifQ==\\\\n//#"`; + +exports[`loader should work with inline sourceMap: warnings 1`] = `Array []`; + exports[`loader should work with multiple exposes: errors 1`] = `Array []`; exports[`loader should work with multiple exposes: module 1`] = ` @@ -95,6 +101,12 @@ Object { exports[`loader should work with multiple exposes: warnings 1`] = `Array []`; +exports[`loader should work with sourceMap: errors 1`] = `Array []`; + +exports[`loader should work with sourceMap: sourceMap 1`] = `"{\\"version\\":3,\\"sources\\":[\\"webpack://ExposeLoader/webpack/bootstrap\\",\\"webpack://ExposeLoader/(webpack)/buildin/global.js\\",\\"webpack://ExposeLoader//media/veracrypt1/OS/expose-loader/src/runtime/getGlobalThis.js\\",\\"webpack://ExposeLoader/./global-commonjs2-single-export.js\\",\\"webpack://ExposeLoader/./global-commonjs2-single-export.js-exposed\\",\\"webpack://ExposeLoader/./simple-commonjs2-single-export.js\\"],\\"names\\":[],\\"mappings\\":\\";;QAAA;QACA;;QAEA;QACA;;QAEA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;;QAEA;QACA;;QAEA;QACA;;QAEA;QACA;QACA;;;QAGA;QACA;;QAEA;QACA;;QAEA;QACA;QACA;QACA,0CAA0C,gCAAgC;QAC1E;QACA;;QAEA;QACA;QACA;QACA,wDAAwD,kBAAkB;QAC1E;QACA,iDAAiD,cAAc;QAC/D;;QAEA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA,yCAAyC,iCAAiC;QAC1E,gHAAgH,mBAAmB,EAAE;QACrI;QACA;;QAEA;QACA;QACA;QACA,2BAA2B,0BAA0B,EAAE;QACvD,iCAAiC,eAAe;QAChD;QACA;QACA;;QAEA;QACA,sDAAsD,+DAA+D;;QAErH;QACA;;;QAGA;QACA;;;;;;;;;;;;AClFA;;AAEA;AACA;AACA;AACA,CAAC;;AAED;AACA;AACA;AACA,CAAC;AACD;AACA;AACA;;AAEA;AACA;AACA,4CAA4C;;AAE5C;;;;;;;;;;;;ACnBA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;;;;;;;;;;;;ACrBA,uBAAuB;;AAEvB;;;;;;;;;;;;ACFA,iCAAiC,mBAAO,CAAC,kFAAuC;AAChF,0CAA0C,mBAAO,CAAC,8EAAoC;AACtF;AACA;AACA;AACA,4C;;;;;;;;;;;ACLA,kBAAkB,mBAAO,CAAC,qFAAkC;;AAE5D\\",\\"file\\":\\"main.bundle.js\\",\\"sourcesContent\\":[\\" \\\\t// The module cache\\\\n \\\\tvar installedModules = {};\\\\n\\\\n \\\\t// The require function\\\\n \\\\tfunction __webpack_require__(moduleId) {\\\\n\\\\n \\\\t\\\\t// Check if module is in cache\\\\n \\\\t\\\\tif(installedModules[moduleId]) {\\\\n \\\\t\\\\t\\\\treturn installedModules[moduleId].exports;\\\\n \\\\t\\\\t}\\\\n \\\\t\\\\t// Create a new module (and put it into the cache)\\\\n \\\\t\\\\tvar module = installedModules[moduleId] = {\\\\n \\\\t\\\\t\\\\ti: moduleId,\\\\n \\\\t\\\\t\\\\tl: false,\\\\n \\\\t\\\\t\\\\texports: {}\\\\n \\\\t\\\\t};\\\\n\\\\n \\\\t\\\\t// Execute the module function\\\\n \\\\t\\\\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\\\\n\\\\n \\\\t\\\\t// Flag the module as loaded\\\\n \\\\t\\\\tmodule.l = true;\\\\n\\\\n \\\\t\\\\t// Return the exports of the module\\\\n \\\\t\\\\treturn module.exports;\\\\n \\\\t}\\\\n\\\\n\\\\n \\\\t// expose the modules object (__webpack_modules__)\\\\n \\\\t__webpack_require__.m = modules;\\\\n\\\\n \\\\t// expose the module cache\\\\n \\\\t__webpack_require__.c = installedModules;\\\\n\\\\n \\\\t// define getter function for harmony exports\\\\n \\\\t__webpack_require__.d = function(exports, name, getter) {\\\\n \\\\t\\\\tif(!__webpack_require__.o(exports, name)) {\\\\n \\\\t\\\\t\\\\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\\\\n \\\\t\\\\t}\\\\n \\\\t};\\\\n\\\\n \\\\t// define __esModule on exports\\\\n \\\\t__webpack_require__.r = function(exports) {\\\\n \\\\t\\\\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\\\\n \\\\t\\\\t\\\\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\\\\n \\\\t\\\\t}\\\\n \\\\t\\\\tObject.defineProperty(exports, '__esModule', { value: true });\\\\n \\\\t};\\\\n\\\\n \\\\t// create a fake namespace object\\\\n \\\\t// mode & 1: value is a module id, require it\\\\n \\\\t// mode & 2: merge all properties of value into the ns\\\\n \\\\t// mode & 4: return value when already ns object\\\\n \\\\t// mode & 8|1: behave like require\\\\n \\\\t__webpack_require__.t = function(value, mode) {\\\\n \\\\t\\\\tif(mode & 1) value = __webpack_require__(value);\\\\n \\\\t\\\\tif(mode & 8) return value;\\\\n \\\\t\\\\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\\\\n \\\\t\\\\tvar ns = Object.create(null);\\\\n \\\\t\\\\t__webpack_require__.r(ns);\\\\n \\\\t\\\\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\\\\n \\\\t\\\\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\\\\n \\\\t\\\\treturn ns;\\\\n \\\\t};\\\\n\\\\n \\\\t// getDefaultExport function for compatibility with non-harmony modules\\\\n \\\\t__webpack_require__.n = function(module) {\\\\n \\\\t\\\\tvar getter = module && module.__esModule ?\\\\n \\\\t\\\\t\\\\tfunction getDefault() { return module['default']; } :\\\\n \\\\t\\\\t\\\\tfunction getModuleExports() { return module; };\\\\n \\\\t\\\\t__webpack_require__.d(getter, 'a', getter);\\\\n \\\\t\\\\treturn getter;\\\\n \\\\t};\\\\n\\\\n \\\\t// Object.prototype.hasOwnProperty.call\\\\n \\\\t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\\\\n\\\\n \\\\t// __webpack_public_path__\\\\n \\\\t__webpack_require__.p = \\\\\\"\\\\\\";\\\\n\\\\n\\\\n \\\\t// Load entry module and return exports\\\\n \\\\treturn __webpack_require__(__webpack_require__.s = \\\\\\"./simple-commonjs2-single-export.js\\\\\\");\\\\n\\",\\"var g;\\\\n\\\\n// This works in non-strict mode\\\\ng = (function() {\\\\n\\\\treturn this;\\\\n})();\\\\n\\\\ntry {\\\\n\\\\t// This works if eval is allowed (see CSP)\\\\n\\\\tg = g || new Function(\\\\\\"return this\\\\\\")();\\\\n} catch (e) {\\\\n\\\\t// This works if the window reference is available\\\\n\\\\tif (typeof window === \\\\\\"object\\\\\\") g = window;\\\\n}\\\\n\\\\n// g can still be undefined, but nothing to do about it...\\\\n// We return undefined, instead of nothing here, so it's\\\\n// easier to handle this case. if(!global) { ...}\\\\n\\\\nmodule.exports = g;\\\\n\\",\\"module.exports = function getGlobalThis() {\\\\n if (typeof globalThis !== 'undefined') {\\\\n // eslint-disable-next-line no-undef\\\\n return globalThis;\\\\n }\\\\n\\\\n if (typeof self !== 'undefined') {\\\\n // eslint-disable-next-line no-undef\\\\n return self;\\\\n }\\\\n\\\\n if (typeof window !== 'undefined') {\\\\n // eslint-disable-next-line no-undef\\\\n return window;\\\\n }\\\\n\\\\n if (typeof global !== 'undefined') {\\\\n return global;\\\\n }\\\\n\\\\n throw new Error('unable to locate global object');\\\\n};\\\\n\\",\\"const globalObject4 = { foo: 'bar' };\\\\n\\\\nmodule.exports = globalObject4;\\\\n\\",\\"var ___EXPOSE_LOADER_IMPORT___ = require(\\\\\\"-!./global-commonjs2-single-export.js\\\\\\");\\\\nvar ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\\\\\"../../src/runtime/getGlobalThis.js\\\\\\");\\\\nvar ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___();\\\\nif (!___EXPOSE_LOADER_GLOBAL_THIS___[\\\\\\"globalObject1\\\\\\"]) ___EXPOSE_LOADER_GLOBAL_THIS___[\\\\\\"globalObject1\\\\\\"] = {};\\\\n___EXPOSE_LOADER_GLOBAL_THIS___[\\\\\\"globalObject1\\\\\\"][\\\\\\"foo\\\\\\"] = ___EXPOSE_LOADER_IMPORT___;\\\\nmodule.exports = ___EXPOSE_LOADER_IMPORT___;\\",\\"const myExports = require('./global-commonjs2-single-export');\\\\n\\\\nmodule.exports = myExports;\\\\n\\"],\\"sourceRoot\\":\\"\\"}"`; + +exports[`loader should work with sourceMap: warnings 1`] = `Array []`; + exports[`loader should work: errors 1`] = `Array []`; exports[`loader should work: module 1`] = ` diff --git a/test/helpers/getSourceMap.js b/test/helpers/getSourceMap.js new file mode 100644 index 0000000..bdb3e90 --- /dev/null +++ b/test/helpers/getSourceMap.js @@ -0,0 +1,36 @@ +// Matches only the last occurrence of sourceMappingURL + +const innerRegex = /\s*[#@]\s*sourceMappingURL\s*=\s*([^\s'"]*)\s*/; + +/* eslint-disable prefer-template */ +const sourceMappingURLRegex = RegExp( + '(?:' + + '/\\*' + + '(?:\\s*\r?\n(?://)?)?' + + '(?:' + + innerRegex.source + + ')' + + '\\s*' + + '\\*/' + + '|' + + '//(?:' + + innerRegex.source + + ')' + + ')' + + '\\s*' +); +/* eslint-enable prefer-template */ + +export default (code) => { + const lines = code.split(/^/m); + let match; + + for (let i = lines.length - 1; i >= 0; i--) { + match = lines[i].match(sourceMappingURLRegex); + if (match) { + break; + } + } + + return match ? match[1] || match[2] || '' : null; +}; diff --git a/test/helpers/index.js b/test/helpers/index.js index 0ec62ae..4c51b1e 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -1,5 +1,6 @@ import compile from './compile'; import execute from './execute'; +import getSourceMap from './getSourceMap'; import getCompiler from './getCompiler'; import getErrors from './getErrors'; import getModuleSource from './getModuleSource'; @@ -11,6 +12,7 @@ import readsAssets from './readAssets'; export { compile, execute, + getSourceMap, getCompiler, getErrors, getModuleSource, diff --git a/test/loader.test.js b/test/loader.test.js index 65c2d15..1ced907 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -1,6 +1,7 @@ import { compile, execute, + getSourceMap, getCompiler, getErrors, getModuleSource, @@ -72,4 +73,42 @@ describe('loader', () => { expect(getErrors(stats)).toMatchSnapshot('errors'); expect(getWarnings(stats)).toMatchSnapshot('warnings'); }); + + it('should work with inline sourceMap', async () => { + const compiler = getCompiler( + 'simple-commonjs2-single-export.js', + { + expose: 'globalObject1.foo', + }, + { + devtool: 'eval-cheap-module-source-map', + } + ); + const stats = await compile(compiler); + + expect( + getSourceMap(readAsset('main.bundle.js', compiler, stats)) + ).toMatchSnapshot('sourceMap'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); + + it('should work with sourceMap', async () => { + const compiler = getCompiler( + 'simple-commonjs2-single-export.js', + { + expose: 'globalObject1.foo', + }, + { + devtool: 'source-map', + } + ); + const stats = await compile(compiler); + + expect(readAsset('main.bundle.js.map', compiler, stats)).toMatchSnapshot( + 'sourceMap' + ); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + }); });