hard-source-webpack-plugin icon indicating copy to clipboard operation
hard-source-webpack-plugin copied to clipboard

`Cannot read property 'split' of undefined`

Open toriningen opened this issue 6 years ago • 18 comments

Version 0.6.4. Initial build has been successful, however on rebuild I've got this exception:

TypeError: Cannot read property 'split' of undefined
    at /opt/app/node_modules/hard-source-webpack-plugin/index.js:923:22
    at Array.forEach (<anonymous>)
    at /opt/app/node_modules/hard-source-webpack-plugin/index.js:914:31
    at Array.forEach (<anonymous>)
    at /opt/app/node_modules/hard-source-webpack-plugin/index.js:886:39
    at <anonymous>

I've altered hard-source-webpack-plugin/index.js to see what's wrong:

     resolveItem.loaders.forEach(function(loader) {
+      console.log(">>>>", loader);
       if (typeof loader === 'object') {
         loader = loader.loader;
       }
       // Loaders specified in a dependency are searched for from the
       // context of the module containing that dependency.
       var loaderMissing = missingCache.loader[JSON.stringify([
         resolveKey[1],
         loader.split('?')[0]  //// <<<< exception is here
       ])];

It seems that for some reason, some loader objects are improperly serialized strings:

Valid loaders printed by the debugging statement above:

>>>> { loader: '/opt/app/node_modules/html-webpack-plugin/lib/loader.js' }
>>>> { loader: '/opt/app/node_modules/offline-plugin/lib/misc/sw-loader.js',
  options: 'json=%7B%22data_var_name%22%3A%22__wpo%22%2C%22loaders%22%3A%5B%5D%2C%22cacheMaps%22%3A%5B%5D%7D' }
>>>> { options: 
   { cacheDirectory: true,
     plugins: [ 'transform-runtime' ],
     presets: [ 'es2015', 'react', 'stage-0' ],
     env: { local: [Object], production: [Object] } },
  ident: 'ref--0-0',
  loader: '/opt/app/node_modules/babel-loader/lib/index.js' }
>>>> { options: 
   { cacheDirectory: true,
     plugins: [ 'transform-runtime' ],
     presets: [ 'es2015', 'react', 'stage-0' ],
     env: { local: [Object], production: [Object] } },
  ident: 'ref--0-0',
  loader: '/opt/app/node_modules/babel-loader/lib/index.js' }

Problematic loader:

>>>> { '0': '/',
  '1': 'o',
  '2': 'p',
  '3': 't',
  '4': '/',
  '5': 'a',
  '6': 'p',
  '7': 'p',
  '8': '/',
  '9': 'n',
  '10': 'o',
  '11': 'd',
  '12': 'e',
  '13': '_',
  '14': 'm',
  '15': 'o',
  '16': 'd',
  '17': 'u',
  '18': 'l',
  '19': 'e',
  '20': 's',
  '21': '/',
  '22': 'o',
  '23': 'f',
  '24': 'f',
  '25': 'l',
  '26': 'i',
  '27': 'n',
  '28': 'e',
  '29': '-',
  '30': 'p',
  '31': 'l',
  '32': 'u',
  '33': 'g',
  '34': 'i',
  '35': 'n',
  '36': '/',
  '37': 'l',
  '38': 'i',
  '39': 'b',
  '40': '/',
  '41': 'm',
  '42': 'i',
  '43': 's',
  '44': 'c',
  '45': '/',
  '46': 'r',
  '47': 'u',
  '48': 'n',
  '49': 't',
  '50': 'i',
  '51': 'm',
  '52': 'e',
  '53': '-',
  '54': 'l',
  '55': 'o',
  '56': 'a',
  '57': 'd',
  '58': 'e',
  '59': 'r',
  '60': '.',
  '61': 'j',
  '62': 's',
  '63': '?',
  '64': '{',
  '65': '"',
  '66': 'S',
  '67': 'e',
  '68': 'r',
  '69': 'v',
  '70': 'i',
  '71': 'c',
  '72': 'e',
  '73': 'W',
  '74': 'o',
  '75': 'r',
  '76': 'k',
  '77': 'e',
  '78': 'r',
  '79': '"',
  '80': ':',
  '81': '{',
  '82': '"',
  '83': 'l',
  '84': 'o',
  '85': 'c',
  '86': 'a',
  '87': 't',
  '88': 'i',
  '89': 'o',
  '90': 'n',
  '91': '"',
  '92': ':',
  '93': '"',
  '94': '/',
  '95': 's',
  '96': 'w',
  '97': '.',
  '98': 'j',
  '99': 's',
  '100': '"',
  '101': ',',
  '102': '"',
  '103': 'e',
  '104': 'v',
  '105': 'e',
  '106': 'n',
  '107': 't',
  '108': 's',
  '109': '"',
  '110': ':',
  '111': 't',
  '112': 'r',
  '113': 'u',
  '114': 'e',
  '115': '}',
  '116': ',',
  '117': '"',
  '118': 'A',
  '119': 'p',
  '120': 'p',
  '121': 'C',
  '122': 'a',
  '123': 'c',
  '124': 'h',
  '125': 'e',
  '126': '"',
  '127': ':',
  '128': '{',
  '129': '"',
  '130': 'l',
  '131': 'o',
  '132': 'c',
  '133': 'a',
  '134': 't',
  '135': 'i',
  '136': 'o',
  '137': 'n',
  '138': '"',
  '139': ':',
  '140': '"',
  '141': '/',
  '142': 'a',
  '143': 'p',
  '144': 'p',
  '145': 'c',
  '146': 'a',
  '147': 'c',
  '148': 'h',
  '149': 'e',
  '150': '/',
  '151': '"',
  '152': ',',
  '153': '"',
  '154': 'n',
  '155': 'a',
  '156': 'm',
  '157': 'e',
  '158': '"',
  '159': ':',
  '160': '"',
  '161': 'm',
  '162': 'a',
  '163': 'n',
  '164': 'i',
  '165': 'f',
  '166': 'e',
  '167': 's',
  '168': 't',
  '169': '"',
  '170': ',',
  '171': '"',
  '172': 'e',
  '173': 'v',
  '174': 'e',
  '175': 'n',
  '176': 't',
  '177': 's',
  '178': '"',
  '179': ':',
  '180': 't',
  '181': 'r',
  '182': 'u',
  '183': 'e',
  '184': ',',
  '185': '"',
  '186': 'd',
  '187': 'i',
  '188': 's',
  '189': 'a',
  '190': 'b',
  '191': 'l',
  '192': 'e',
  '193': 'I',
  '194': 'n',
  '195': 's',
  '196': 't',
  '197': 'a',
  '198': 'l',
  '199': 'l',
  '200': '"',
  '201': ':',
  '202': 'f',
  '203': 'a',
  '204': 'l',
  '205': 's',
  '206': 'e',
  '207': '}',
  '208': '}',
  loader: undefined }

I suppose that hotfix would be to add check like this (until real cause is discovered):

if (typeof loader === 'object') && (loader.loader == null)) {
    const _a = [];
    for (const [k, v] of Object.entries(loader)) {
        _a[k] = v;
    }
    loader = _a.join('');
}

Please let me know if I can provide any additional info on this.

toriningen avatar Mar 07 '18 02:03 toriningen

I've submitted PR https://github.com/mzgoddard/hard-source-webpack-plugin/pull/290 with fix.

toriningen avatar Mar 07 '18 10:03 toriningen

Having the same problem, with the same problematic plugin (OfflinePlugin)

williamoliveira avatar Mar 18 '18 06:03 williamoliveira

Same issue here! /cc @NeKr

mxstbr avatar Apr 27 '18 08:04 mxstbr

Totally not sure why offline-plugin is problematic here. Are there info what hard-source-plugin does and what should be avoided in other plugins where used together?

NekR avatar Apr 27 '18 10:04 NekR

I am guessing offiline-plugin injects a loader as a string and hard-source-plugin expects an object, or something like that, not sure how webpack works internally

williamoliveira avatar Apr 27 '18 16:04 williamoliveira

Hmm.. so it might be a webpack 4 thing 🤔Not sure what webpack accepted there previously. Well, if object works in 2-4 versions, then I can change that, no problem.

@mxstbr what version of webpack and offline-plugin are you using?

NekR avatar Apr 27 '18 17:04 NekR

Webpack v2 I think, and the latest offline plugin?

mxstbr avatar Apr 27 '18 17:04 mxstbr

@mxstbr there were some changed is that part of the code, but mainly related to webpack 4. Have you just started using hard-source-plugin and got the error or you were using it, now updated to latest offline-plugin and got the error?

NekR avatar Apr 27 '18 17:04 NekR

Just started using this plugin

mxstbr avatar Apr 27 '18 17:04 mxstbr

@mxstbr okay, I see. Sounds like a long standing error. I'll check today what I can do, maybe I can fix it on my side. Thanks for ccing!

NekR avatar Apr 27 '18 17:04 NekR

I've submitted PR #290 with fix.

Thanks. This issue went away after installing the fixed version from your PR: npm install --save-dev toriningen/hard-source-webpack-plugin#feature/fix-289

motin avatar Apr 28 '18 10:04 motin

Unfortunately it cannot fix it on my side.

Changing:

result.loaders.push(
  path.join(__dirname, 'misc/runtime-loader.js') +
    '?' + JSON.stringify(data)
);

to

result.loaders.push({
  loader: path.join(__dirname, 'misc/runtime-loader.js') +
    '?' + JSON.stringify(data)
});

Results into errors.

And there is no docs about it, you know, because it's webpack. https://twitter.com/nekrtemplar/status/990215317879173122?s=20

NekR avatar Apr 28 '18 13:04 NekR

The only docs I can find about it are here: https://github.com/webpack/docs/wiki/plugins#normal-module-factorynmf-normalmodulefactory

And it shows string usage in that case.

NekR avatar Apr 28 '18 13:04 NekR

Well, here it seems to be transformed: https://github.com/webpack/loader-runner/blob/master/lib/LoaderRunner.js#L37

I do send it as:

result.loaders.push({
  loader: path.join(__dirname, 'misc/runtime-loader.js'),
  options: JSON.stringify(data)
});

Seems to be working. It won't be working in webpack 1 though, so the change is done only as 5.0.2 (no 4.9.2). If you need this feature please update to v5+.

https://github.com/NekR/offline-plugin/releases/tag/v5.0.2

https://github.com/NekR/offline-plugin/commit/7b29577c40a578ea595ba94a9ea4b49ed016cf7d

NekR avatar Apr 28 '18 13:04 NekR

Working fine, thank you @NekR!

williamoliveira avatar Apr 28 '18 16:04 williamoliveira

So there is a fix-289 and last version 0.6.4 is released 2 month ago. It is time for release, isn't it?

yaitskov avatar May 09 '18 14:05 yaitskov

Try to set "css-loader": "^3.2.0", webpack-contrib/css-loader#1002

KirillYoYo avatar Aug 31 '20 21:08 KirillYoYo

This happened to me on a project after updating a bunch of dependencies and css-loader was one of them. Downgrading to "css-loader": "^3.2.0", worked.

Also another change worked which was to continue with the version of "css-loaders": "^4.3.0" and I deleted the yarn.lock file and re-ran yarn and then the build started working. So not sure if there was some cached values going with the yarn.lock file but now it's working with the latest dependencies.

jaiew avatar Oct 01 '20 07:10 jaiew