ECMAScript Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. Open your command-line application and navigate to the directory containing the 02-creating-client-bundles package.
  2. Start the Python HTTP server.
  3. Create a file named index.html (copied from the Nesting modules under a single namespace recipe in Chapter 1, Building with Modules):
<html> 
  <head> 
    <meta charset='UTF-8' /> 
  </head> 
  <body> 
    <h1>Open your console.</h1> 
    <script type="module"> 
      import { main } from './main.js'; 
      main(); 
    </script> 
  </body> 
</html> 
  1. Add a nomodule script tag to the body after the existing module in the <script> tag:
  <body> 
    <h1>Open your console.</h1> 
    <script type="module"> 
      import { main } from './main.js'; 
      main(); 
    </script> 
    <script nomodule type="text/javascript"src="bundle.js"></script> 
  </body> 
  1. Run the webpack build command:
    ./node_modules/.bin/webpack --config webpack.config.js
  1. Open your ES module-compatible browser, open the Developer tools to the Network tab, and visit the URL:
    http://localhost:8000/.
  2. You should see the individual files loaded by the browser:
  1. Open a browser that isn't compatible with ES modules. Open the Developer tools to Network and visit the URL:
    http://localhost:8000/.

  1. You should see the bundle.js file loaded instead: