search

Home  >  Q&A  >  body text

javascript - After converting es6 to es5 using webpack, how can I call the previously packaged code in es5?

es6 code test.js

class test {
  constructor() {
    console.log("init");
  }
}

module.exports = test;

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>drawBoard</title>
</head>
<body>
</body>
<script type="text/javascript" src="../build/test.js"></script>
<script type="text/javascript">
  console.log(typeof test)        // undefined
</script>
</html>

Such a direct call will definitely fail.

I looked at the packaged code of webpack, and my understanding is that an anonymous function is packaged, and then the class of module.exports is returned in the anonymous functiontest.
You can use import to write with es6, but how to call it with es5?

Please explain, thank you.

大家讲道理大家讲道理2810 days ago529

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-05-19 10:16:10

    Inside the configuration of

    webpack

    output.libraryTarget set to 'umd'

    reply
    0
  • Cancelreply