const path = require('path');
const version = require('./package.json').version;

// Custom webpack rules
const rules = [
  { test: /\.ts$/, loader: 'ts-loader' },
  { test: /\.js$/, loader: 'source-map-loader' },
  { test: /\.css$/, use: ['style-loader', 'css-loader']}
];

// Packages that shouldn't be bundled but loaded at runtime
const externals = ['@jupyter-widgets/base'];

const resolve = {
  // Add '.ts' and '.tsx' as resolvable extensions.
  extensions: [".webpack.js", ".web.js", ".ts", ".js"]
};

module.exports = [
  /**
   * Notebook extension
   *
   * This bundle only contains the part of the JavaScript that is run on load of
   * the notebook.
   */
  {
    entry: './src/extension.ts',
    output: {
      filename: 'index.js',
      path: path.resolve(__dirname, '{{ cookiecutter.python_package_name }}', 'nbextension', 'static'),
      libraryTarget: 'amd'
    },
    module: {
      rules: rules
    },
    devtool: 'source-map',
    externals,
    resolve,
  },

  {
    // embeddable bundle (e.g. for docs)
    entry: './src/index.ts',
    output: {
      filename: 'embed-bundle.js',
      path: path.resolve(__dirname, 'docs', 'source', '_static'),
      library: "{{ cookiecutter.npm_package_name }}",
      libraryTarget: 'amd'
    },
    module: {
      rules: rules
    },
    devtool: 'source-map',
    externals,
    resolve,
  },
  {// Embeddable {{ cookiecutter.npm_package_name }} bundle
    //
    // This bundle is generally almost identical to the notebook bundle
    // containing the custom widget views and models.
    //
    // The only difference is in the configuration of the webpack public path
    // for the static assets.
    //
    // It will be automatically distributed by unpkg to work with the static
    // widget embedder.
    //
    // The target bundle is always `dist/index.js`, which is the path required
    // by the custom widget embedder.
    //
    entry: './src/index.ts',
    output: {
        filename: 'index.js',
        path: path.resolve(__dirname, 'dist'),
        libraryTarget: 'amd',
        library: "{{ cookiecutter.npm_package_name }}",
        publicPath: 'https://unpkg.com/{{ cookiecutter.npm_package_name }}@' + version + '/dist/'
    },
    devtool: 'source-map',
    module: {
        rules: rules
    },
    externals,
    resolve,
  }
];

@@ -987,388 +987,16 @@
 %0A%0A  
-%7B%0A    // embeddable bundle (e.g. for docs)%0A    entry: './src/index.ts',%0A    output: %7B%0A      filename: 'embed-bundle.js',%0A      path: path.resolve(__dirname, 'docs', 'source', '_static'),%0A      library: %22%7B%7B cookiecutter.npm_package_name %7D%7D%22,%0A      libraryTarget: 'amd'%0A    %7D,%0A    module: %7B%0A      rules: rules%0A    %7D,%0A    devtool: 'source-map',%0A    externals,%0A    resolve,%0A  %7D,%0A  %7B//
+/**%0A   *
  Emb
@@ -1049,26 +1049,22 @@
 ndle%0A   
- //
+*
 %0A   
- //
+*
  This bu
@@ -1075,18 +1075,8 @@
  is 
-generally 
 almo
@@ -1108,82 +1108,25 @@
 ook 
-bundle%0A    // containing the custom widget views and models.%0A    //%0A    //
+extension bundle.
  The
@@ -1130,16 +1130,21 @@
 The only
+%0A   *
  differe
@@ -1201,23 +1201,16 @@
 path
-%0A    //
  for the
  sta
@@ -1201,24 +1201,29 @@
 path for the
+%0A   *
  static asse
@@ -1228,131 +1228,19 @@
 sets
-.%0A    //%0A    // It will be automatically distributed by unpkg to work with the static%0A    // widget embedder
 .%0A   
- //
+*
 %0A   
- //
+*
  The
@@ -1311,18 +1311,16 @@
 ired
+ by
 %0A   
- // by
+*
  the
@@ -1347,19 +1347,22 @@
 der.%0A   
- //
+*/%0A  %7B
 %0A    ent
@@ -1765,12 +1765,473 @@
 lve,%0A  %7D
+,%0A%0A%0A  /**%0A   * Documentation widget bundle%0A   *%0A   * This bundle is used to embed widgets in the package documentation.%0A   */%0A  %7B%0A    entry: './src/index.ts',%0A    output: %7B%0A      filename: 'embed-bundle.js',%0A      path: path.resolve(__dirname, 'docs', 'source', '_static'),%0A      library: %22%7B%7B cookiecutter.npm_package_name %7D%7D%22,%0A      libraryTarget: 'amd'%0A    %7D,%0A    module: %7B%0A      rules: rules%0A    %7D,%0A    devtool: 'source-map',%0A    externals,%0A    resolve,%0A  %7D%0A
 %0A%5D;%0A

Add better comments for the webpack bundles.