Simple Example for Dynamic Imports

244 views
Skip to first unread message

Patrick Kuhlmann

unread,
Jan 10, 2019, 11:02:06 AM1/10/19
to Closure Compiler Discuss
Hey

I'm writing a simple application with rollup and vanilla js utilizing dynamic imports. As last step I try to use closure compiler but this is harder than I imagined.

I tried several flags maybe in wrong combination: 
language_in ECMASCRIPT_NEXT
js_module_root
env BROWSER
module
dependency_mode LOOSE
module_resolution

If there is not support yet would I be able to tell closure compiler to ignore imports and let him do the optimization for everything it understands?

Sincerly
Patrick

Yiting Wang

unread,
Jan 10, 2019, 4:54:24 PM1/10/19
to Closure Compiler Discuss
Hi Patrick,

To confirm, are you using the Rollup plugin for Closure Compiler? https://github.com/ampproject/rollup-plugin-closure-compiler

I have not used Rollup before, but it seems from the documentation that you could pass in the compiler flags by specifying them as key-value pairs in your rollup.config.js
For example,
// rollup.config.js
import compiler from '@ampproject/rollup-plugin-closure-compiler';

export default {
  input
: 'main.js',
  output
: {
    file
: 'bundle.js',
    format
: 'iife',
 
},
  plugins
: [
   
compiler({
      language_in
: 'ECMASCRIPT_NEXT',
      js_module_root: 'some/path/prefix'
   
}),
 
],
}

Have you tried the above? What is not working for you?

Best,
Yiting

Patrick Kuhlmann

unread,
Jan 10, 2019, 4:59:29 PM1/10/19
to Closure Compiler Discuss
I tried it but it looks like it swallows the error message so I just used the cli thats provided by https://github.com/google/closure-compiler
So I got an error too but I cannot say it is the same but I assume it

Yiting Wang

unread,
Jan 10, 2019, 5:36:27 PM1/10/19
to Closure Compiler Discuss
What error message are you getting when using the command line?

Patrick Kuhlmann

unread,
Jan 11, 2019, 5:26:48 AM1/11/19
to Closure Compiler Discuss
google-closure-compiler --js=index.js --js_output_file=out.js --language_in=ECMASCRIPT_NEXT --js_module_root './'
index.js:690: ERROR - Parse error. primary expression expected
      await import('./chunk-c64f0c6a.js');
                  ^

1 error(s), 0 warning(s)

Yiting Wang

unread,
Jan 11, 2019, 6:33:31 PM1/11/19
to Closure Compiler Discuss
Hi Patrick,

The compiler currently does not support dynamic imports, hence the parse error. Here's the tracking issue for this in GitHub. https://github.com/google/closure-compiler/issues/2770 

In the meantime, you can try getting around this by using the standard kind of import. 

Best,
Yiting

Patrick Kuhlmann

unread,
Jan 11, 2019, 6:39:27 PM1/11/19
to Closure Compiler Discuss
Thanks for the input would there be any way to let the compiler ignore specific lines like the imports?

Yiting Wang

unread,
Jan 11, 2019, 6:46:49 PM1/11/19
to Closure Compiler Discuss
AFAIK that is not possible, unfortunately.

Patrick Kuhlmann

unread,
Jan 11, 2019, 7:12:35 PM1/11/19
to Closure Compiler Discuss
I see thanks for the help!
Reply all
Reply to author
Forward
0 new messages