~glacambre/firenvim

78851635815ef7d0a8fd1a19df3c7ae24c95dce4 — glacambre 20 days ago f0b6ebc
webpack: delete target directories before rebuilding

This enables removing old/temporary/testing files that wouldn't be
overwritten.
1 files changed, 73 insertions(+), 57 deletions(-)

M webpack.config.js
M webpack.config.js => webpack.config.js +73 -57
@@ 1,3 1,4 @@
const fs = require("fs");
const path = require("path");
const ProvidePlugin = require("webpack").ProvidePlugin;
const CopyWebPackPlugin = require("copy-webpack-plugin");


@@ 79,73 80,88 @@ const package_json = JSON.parse(require("fs").readFileSync(path.join(__dirname, 
const chrome_target_dir = path.join(__dirname, "target", "chrome")
const firefox_target_dir = path.join(__dirname, "target", "firefox")

const chromeConfig = (config, env) => Object.assign(deepCopy(config), {
  output: {
    path: chrome_target_dir,
  },
  plugins: [new CopyWebPackPlugin({ patterns: CopyWebPackFiles.map(file => ({
    from: file,
    to: chrome_target_dir,
    transform: (content, src) => {
      if (path.basename(src) === "manifest.json") {
        content = content.toString()
          .replace('BROWSER_SPECIFIC_SETTINGS', '"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk3pkgh862ElxtREZVPLxVNbiFWo9SnvZtZXZavNvs2GsUTY/mB9yHTPBGJiBMJh6J0l+F5JZivXDG7xdQsVD5t39CL3JGtt93M2svlsNkOEYIMM8tHbp69shNUKKjZOfT3t+aZyigK2OUm7PKedcPeHtMoZAY5cC4L1ytvgo6lge+VYQiypKF87YOsO/BGcs3D+MMdS454tLBuMp6LxMqICQEo/Q7nHGC3eubtL3B09s0l17fJeq/kcQphczKbUFhTVnNnIV0JX++UCWi+BP4QOpyk5FqI6+SVi+gxUosbQPOmZR4xCAbWWpg3OqMk4LqHaWpsBfkW9EUt6EMMMAfQIDAQAB"')
          .replace("FIRENVIM_VERSION", package_json.version)
          .replace("PACKAGE_JSON_DESCRIPTION", package_json.description)
        // Chrome doesn't support svgs in its manifest
          .replace(/"128": *"firenvim\.svg"/g, '"128": "firenvim128.png",\n'
            + '      "16": "firenvim16.png",\n'
            + '      "48": "firenvim48.png"'
          )
          .replace(/"default_icon": "firenvim.svg"/, '"default_icon": "firenvim128.png"')
        ;
        if (env.endsWith("testing")) {
          content = content.replace(`content.js`, `content.js", "testing.js`);
const chromeConfig = (config, env) => {
  const result = Object.assign(deepCopy(config), {
    output: {
      path: chrome_target_dir,
    },
    plugins: [new CopyWebPackPlugin({ patterns: CopyWebPackFiles.map(file => ({
      from: file,
      to: chrome_target_dir,
      transform: (content, src) => {
        if (path.basename(src) === "manifest.json") {
          content = content.toString()
            .replace('BROWSER_SPECIFIC_SETTINGS', '"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk3pkgh862ElxtREZVPLxVNbiFWo9SnvZtZXZavNvs2GsUTY/mB9yHTPBGJiBMJh6J0l+F5JZivXDG7xdQsVD5t39CL3JGtt93M2svlsNkOEYIMM8tHbp69shNUKKjZOfT3t+aZyigK2OUm7PKedcPeHtMoZAY5cC4L1ytvgo6lge+VYQiypKF87YOsO/BGcs3D+MMdS454tLBuMp6LxMqICQEo/Q7nHGC3eubtL3B09s0l17fJeq/kcQphczKbUFhTVnNnIV0JX++UCWi+BP4QOpyk5FqI6+SVi+gxUosbQPOmZR4xCAbWWpg3OqMk4LqHaWpsBfkW9EUt6EMMMAfQIDAQAB"')
            .replace("FIRENVIM_VERSION", package_json.version)
            .replace("PACKAGE_JSON_DESCRIPTION", package_json.description)
          // Chrome doesn't support svgs in its manifest
            .replace(/"128": *"firenvim\.svg"/g, '"128": "firenvim128.png",\n'
              + '      "16": "firenvim16.png",\n'
              + '      "48": "firenvim48.png"'
            )
            .replace(/"default_icon": "firenvim.svg"/, '"default_icon": "firenvim128.png"')
          ;
          if (env.endsWith("testing")) {
            content = content.replace(`content.js`, `content.js", "testing.js`);
          }
        }
        return content;
      }
      return content;
    })).concat([16, 48, 128].map(n => ({
      from: "static/firenvim.svg",
      to: chrome_target_dir,
      transformPath: () => `firenvim${n}.png`,
      transform: (content) => sharp(content).resize(n, n).toBuffer(),
    })))}),
      new ProvidePlugin({ "browser": "webextension-polyfill" })
    ]
  });
  try {
    fs.rmdirSync(result.output.path, { recursive: true })
  } catch (e) {
    console.log(`Could not delete output dir (${e.message})`);
  }
  return result;
}

const firefoxConfig = (config, env) => {
  const result = Object.assign(deepCopy(config), {
    output: {
      path: firefox_target_dir,
    },
  })).concat([16, 48, 128].map(n => ({
    from: "static/firenvim.svg",
    to: chrome_target_dir,
    transformPath: (target) => `firenvim${n}.png`,
    transform: (content, src) => sharp(content).resize(n, n).toBuffer(),
  })
  ))
  }),
  new ProvidePlugin({
    "browser": "webextension-polyfill"
  })]
});

const firefoxConfig = (config, env) => Object.assign(deepCopy(config), {
  output: {
    path: firefox_target_dir,
  },
  plugins: [new CopyWebPackPlugin({ patterns: CopyWebPackFiles.map(file => ({
    from: file,
    to: firefox_target_dir,
    transform: (content, src) => {
      switch(path.basename(src)) {
        case "manifest.json":
          content = content.toString().replace("BROWSER_SPECIFIC_SETTINGS",
    plugins: [new CopyWebPackPlugin({
      patterns: CopyWebPackFiles.map(file => ({
        from: file,
        to: firefox_target_dir,
        transform: (content, src) => {
          switch(path.basename(src)) {
            case "manifest.json":
              content = content.toString().replace("BROWSER_SPECIFIC_SETTINGS",
`  "browser_specific_settings": {
    "gecko": {
      "id": "firenvim@lacamb.re",
      "strict_min_version": "69.0"
    }
  }`)
            .replace("FIRENVIM_VERSION", package_json.version)
            .replace("PACKAGE_JSON_DESCRIPTION", package_json.description)
          ;
          if (env.endsWith("testing")) {
            content = content.replace(`content.js`, `content.js", "testing.js`);
                .replace("FIRENVIM_VERSION", package_json.version)
                .replace("PACKAGE_JSON_DESCRIPTION", package_json.description)
              ;
              if (env.endsWith("testing")) {
                content = content.replace(`content.js`, `content.js", "testing.js`);
              }
          }
      }
      return content;
    }
  }))})]
});
          return content;
        }
      }))
    })]
  });
  try {
    fs.rmdirSync(result.output.path, { recursive: true })
  } catch (e) {
    console.log(`Could not delete output dir (${e.message})`);
  }
  return result;
}

module.exports = env => {
  if (env === undefined){