/**
 * @license Copyright 2019 The Lighthouse Authors. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */
'use strict';

const browserify = require('browserify');
const rollup = require('rollup');
const rollupPlugins = require('./rollup-plugins.js');
const fs = require('fs');
const path = require('path');
const bundleBuilder = require('./build-bundle.js');
const {LH_ROOT} = require('../root.js');
const inlineFs = require('./plugins/browserify-inline-fs.js');

const distDir = path.join(LH_ROOT, 'dist', 'lightrider');
const sourceDir = path.join(LH_ROOT, 'clients', 'lightrider');

const bundleOutFile = `${distDir}/report-generator-bundle.js`;
const generatorFilename = `./report/generator/report-generator.js`;

const entrySourceName = 'lightrider-entry.js';
const entryDistName = 'lighthouse-lr-bundle.js';

fs.mkdirSync(distDir, {recursive: true});

function buildEntryPoint() {
  const inFile = `${sourceDir}/${entrySourceName}`;
  const outFile = `${distDir}/${entryDistName}`;
  return bundleBuilder.build(inFile, outFile, {minify: false});
}

/**
 * Browserify and minify the LR report generator.
 */
function buildReportGenerator() {
  browserify(generatorFilename, {standalone: 'ReportGenerator'})
    // Flow report is not used in LR, so don't include flow assets.
    .ignore(require.resolve('../report/generator/flow-report-assets.js'))
    // Transform `fs.readFileSync`, etc into inline strings.
    .transform(inlineFs({verbose: Boolean(process.env.DEBUG)}))
    .bundle((err, src) => {
      if (err) throw err;
      fs.writeFileSync(bundleOutFile, src.toString());
    });
}

async function buildStaticServerBundle() {
  const bundle = await rollup.rollup({
    input: 'lighthouse-cli/test/fixtures/static-server.js',
    plugins: [
      rollupPlugins.shim({
        'es-main': 'export default function() { return false; }',
      }),
      rollupPlugins.commonjs(),
      rollupPlugins.nodeResolve(),
    ],
    external: ['mime-types', 'glob'],
  });

  await bundle.write({
    file: 'dist/lightrider/static-server.js',
    format: 'commonjs',
  });
  await bundle.close();
}

async function run() {
  await Promise.all([
    buildEntryPoint(),
    buildReportGenerator(),
    buildStaticServerBundle(),
  ]);
}

run();

@@ -616,50 +616,8 @@
 ';%0A%0A
-const browserify = require('browserify');%0A
 cons
@@ -642,24 +642,24 @@
 ('rollup');%0A
+
 const rollup
@@ -852,71 +852,8 @@
 s');
-%0Aconst inlineFs = require('./plugins/browserify-inline-fs.js');
 %0A%0Aco
@@ -976,140 +976,8 @@
 );%0A%0A
-const bundleOutFile = %60$%7BdistDir%7D/report-generator-bundle.js%60;%0Aconst generatorFilename = %60./report/generator/report-generator.js%60;%0A%0A
 cons
@@ -1313,161 +1313,115 @@
 %0A%7D%0A%0A
-/**%0A * Browserify and minify the LR report generator.%0A */%0Afunction buildReportGenerator() %7B%0A  browserify(generatorFilename, %7Bstandalone: 'R
+async function buildReportGenerator() %7B%0A  const bundle = await rollup.rollup(%7B%0A    input: 'r
 eport
-G
+/g
 enerator
 '%7D)%0A
@@ -1420,230 +1420,254 @@
 ator
-'%7D)%0A    // Flow report is not used in LR, so don't include flow assets.%0A    .ignore(require.resolve('../report/generator/flow-report-assets.js'))%0A    // Transform %60fs.readFileSync%60, etc into inline strings.%0A    .transform(
+/report-generator.js',%0A    plugins: %5B%0A      rollupPlugins.shim(%7B%0A        %5B%60$%7BLH_ROOT%7D/report/generator/flow-report-assets.js%60%5D: 'export default %7B%7D',%0A      %7D),%0A      rollupPlugins.commonjs(),%0A      rollupPlugins.nodeResolve(),%0A      rollupPlugins.
 inli
@@ -1713,124 +1713,178 @@
 G)%7D)
-)
+,
 %0A    
-.bundle((err, src) =%3E %7B%0A      if (err) throw err;%0A      fs.writeFileSync(bundleOutFile, src.toString());%0A    %7D
+%5D,%0A  %7D);%0A%0A  await bundle.write(%7B%0A    file: 'dist/lightrider/report-generator-bundle.js',%0A    format: 'umd',%0A    name: 'ReportGenerator',%0A  %7D);%0A  await bundle.close(
 );%0A%7D

use rollup to create lr report generator (#13416)