roman01la / webpack-closure-compiler

[DEPRECATED] Google Closure Compiler plugin for Webpack
MIT License
465 stars 25 forks source link

'Uncaught Error: No element indexed by 1' when running java compiler with sourcemaps #29

Closed chuckdumont closed 6 years ago

chuckdumont commented 7 years ago

Here's my config:

      new ClosureCompilerPlugin({
        compiler: {
          language_in: 'ECMASCRIPT6',
          language_out: 'ECMASCRIPT5',
          compilation_level: 'SIMPLE',
          warning_level: 'QUIET',
          create_source_map: true
        },
        concurrency: 3
      })

Following is the full stack trace:

Error: No element indexed by 1
    at ArraySet_at [as at] (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack-core\node_modules\source-map\lib\source-map\array-set.js:93:11)
    at SourceMapConsumer.<anonymous> (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack-core\node_modules\source-map\lib\source-map\source-map-consumer.js:147:69)
    at Array.map (native)
    at SourceMapConsumer_eachMapping [as eachMapping] (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack-core\node_modules\source-map\lib\source-map\source-map-consumer.js:146:16)
    at Function.SourceMapGenerator_fromSourceMap [as fromSourceMap] (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack-core\node_modules\source-map\lib\source-map\source-map-generator.js:52:26)
    at SourceMapSource.node (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack-core\lib\SourceMapSource.js:36:34)
    at SourceMapSource.proto.sourceAndMap (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack-core\lib\SourceAndMapMixin.js:22:18)
    at Compilation.<anonymous> (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack\lib\SourceMapDevToolPlugin.js:55:32)
    at Array.map (native)
    at Compilation.<anonymous> (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack\lib\SourceMapDevToolPlugin.js:43:84)
    at Array.forEach (native)
    at Compilation.<anonymous> (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack\lib\SourceMapDevToolPlugin.js:42:11)
    at Compilation.applyPlugins (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\tapable\lib\Tapable.js:26:37)
    at Compilation.<anonymous> (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack\lib\Compilation.js:571:10)
    at Compilation.next (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\tapable\lib\Tapable.js:67:11)
    at done (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack-closure-compiler\index.js:105:17)
    at Object.callback (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack-closure-compiler\index.js:113:17)
    at C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack-closure-compiler\index.js:79:16
    at ChildProcess.<anonymous> (C:\Users\Chuck\git\sequoia\sequoia-webpack\node_modules\webpack-closure-compiler\lib\runner.js:70:12)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:877:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)

And the version of closure.

C:\dev\node_modules\google-closure-compiler>java -jar compiler.jar --version
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20170124
Built on: 2017-01-27 11:44

The problem seems to be with the following code in index.js:

        if (compilerOptions['create_source_map']) {
          var outputSourceMap = JSON.parse(fs.readFileSync(outputSourceMapFile.path));
          fs.unlinkSync(outputSourceMapFile.path);
          outputSourceMap.sources = [];
          outputSourceMap.sources.push(task.file);

The plugin is replacing the sources array produced by closure with a single element array containing the input filename. The problem is that the original sources array contains more than one element (10 in my case) and these sources elements are referenced by the mappings, so when SourceMapGenerator.fromSourceMap() is subsequently called to generate a source map file from this source map, then references to the removed sources array elements cause the exception.

Following is the contents of the sources array generated by the compiler before it is overwritten by the above code:

0:" [synthetic:base] "
1:" [synthetic:util/defineproperty] "
2:" [synthetic:util/global] "
3:" [synthetic:util/polyfill] "
4:" [synthetic:es6/number/isfinite] "
5:" [synthetic:es6/number/isinteger] "
6:" [synthetic:es6/symbol] "
7:" [synthetic:es6/util/iteratorfromarray] "
8:" [synthetic:es6/array/keys] "
9:"stdin"

Are there any compiler options I can try to work around this problem?

roman01la commented 7 years ago

@chuckdumont Hmm, it seems more like a bug. I'm not entirely sure what should be done to solve this. @jscheid was working on source maps support, maybe he can shed some light here.

HeroofOne commented 7 years ago

I also have this issue. Here is my configuration:

new ClosureCompilerPlugin({ compiler: { language_in: 'ECMASCRIPT6', language_out: 'ECMASCRIPT5', compilation_level: 'SIMPLE_OPTIMIZATIONS', create_source_map: true } })

huineng commented 6 years ago

Still problem exists @jscheid , is there any solution for this or do you recommend to move away from closure compiler ?

thanks

jscheid commented 6 years ago

Find these lines:

outputSourceMap.sources = [];
outputSourceMap.sources.push(task.file);

and add debug output before:

console.log("outputSourceMap.sources=", outputSourceMap.sources, "task.file=", task.file);

Post the result here and it might shed some light on what the problem is.

huineng commented 6 years ago

this is the result

outputSourceMap.sources= [ 
  ' [synthetic:base] ',        
  ' [synthetic:util/defines] ',
  ' [synthetic:util/defineproperty] ',
  ' [synthetic:util/global] ',
  ' [synthetic:es6/symbol] ',
  ' [synthetic:es6/util/makeiterator] ',
  ' [synthetic:es6/util/inherits] ',
  ' [synthetic:util/polyfill] ',
  ' [synthetic:es6/promise] ',
  ' [synthetic:util/checkstringargs] ',
  ' [synthetic:es6/string/startswith] ',
  ' [synthetic:es6/object/is] ',
  ' [synthetic:es6/array/includes] ',
  ' [synthetic:es6/string/includes] ',
  ' [synthetic:es6/util/iteratorfromarray] ',
  ' [synthetic:es6/array/keys] ',
  ' [synthetic:es6/array/values] ',
  ' [synthetic:es6/number/isfinite] ',
  ' [synthetic:es6/number/isinteger] ',
  ' [synthetic:util/owns] ',
  ' [synthetic:es6/object/assign] ',
  ' [synthetic:es6/object/getownpropertysymbols] ',
  'stdin',
  ' [synthetic:1] ',
  ' [synthetic:2] ',
  ' [synthetic:3] ',
  ' [synthetic:4] ',
  ' [synthetic:5] ',
  ' [synthetic:7] ',
  ' [synthetic:6] ',
  ' [synthetic:8] ' ] task.file= server/app.js

so i did the following

// outputSourceMap.sources = [];
          outputSourceMap.sources.unshift(task.file);

i commented out the new array and add my task file at the beginning of the array

At least it compiles now without error, but of course my sourcemap have all these elements i don't need and i can also not see my ts files

hope this helps

huineng commented 6 years ago

there's a similar issue some time ago here https://github.com/TypeStrong/ts-loader/issues/81

zeronone commented 6 years ago

I was able to fix the problem with the following:

// outputSourceMap.sources = []
// outputSoruceMap.push(task.file)
outputSourceMap.sources = outputSourceMap.sources.map(source => source === 'stdin' ? task.file : source);
Ninerian commented 6 years ago

@zeronone Are you sure it's not

outputSourceMap.sources = outputSourceMap.sources.map(source => source === 'stdin' ? task.file : source);

As you're code doesn't change the outputSourceMap.sources

zeronone commented 6 years ago

@Ninerian Oops sorry, copy paste mistake. Edited the post.

roman01la commented 6 years ago

Is this issue still present?

bansawbanchee commented 6 years ago

I believe this issue was resolved with the above referenced pull request.

roman01la commented 6 years ago

Cool, closing then. Thanks.