Skip to content

Commit

Permalink
convert to esm, plus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joemaller committed Feb 1, 2022
1 parent b999ced commit c833361
Show file tree
Hide file tree
Showing 15 changed files with 4,654 additions and 7,366 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ RUN npm rebuild

COPY webpack.config.js ./
COPY default.config.js ./
COPY zip.mjs ./
COPY zip.js ./
COPY explore.js ./
# COPY browsersync.js ./
COPY lib ./lib
Expand Down
4 changes: 3 additions & 1 deletion default.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ const defaultConfig = {
proxy: 'wordpress',
};

module.exports = defaultConfig;
// module.exports = defaultConfig;

export default defaultConfig;
9 changes: 6 additions & 3 deletions lib/AfterDoneReporterPlugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const chalk = require("chalk");
// const chalk = require("chalk");

import chalk from 'chalk';


/**
* Simple plugin for printing some text after compilation stats are displayed
Expand All @@ -7,7 +10,7 @@ const chalk = require("chalk");
*
* new AfterDoneReporterPlugin({message: "Your Message Here"})
*/
class AfterDoneReporterPlugin {
export default class AfterDoneReporterPlugin {
constructor(options = {}) {
const defaults = {
echo: true,
Expand All @@ -29,4 +32,4 @@ class AfterDoneReporterPlugin {
}
}

module.exports = AfterDoneReporterPlugin;
// module.exports = AfterDoneReporterPlugin;
11 changes: 8 additions & 3 deletions lib/DependencyManifestPlugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const fs = require("fs-extra");
const path = require("path");
// const fs = require("fs-extra");
// const path = require("path");

import fs from "fs-extra";
import path from "path";

class DependencyManifestPlugin {
constructor(options) {
Expand Down Expand Up @@ -64,4 +67,6 @@ class DependencyManifestPlugin {
}
}

module.exports = DependencyManifestPlugin;
export default DependencyManifestPlugin;

// module.exports = DependencyManifestPlugin;
11 changes: 7 additions & 4 deletions lib/ImageminPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ const imageminDevPlugins = [
["svgo", svgoConfig],
];

const plugins = (isProd) => (isProd ? plugins.prod : plugins.dev);
plugins.dev = imageminDevPlugins;
plugins.prod = imageminProdPlugins;
// const plugins = (isProd) => (isProd ? plugins.prod : plugins.dev);
// plugins.dev = imageminDevPlugins;
// plugins.prod = imageminProdPlugins;
// const plugins = (isProd) => (isProd ? imageminDevPlugins : imageminDevPlugins);
const plugins = (isProd) => (isProd ? imageminProdPlugins : imageminDevPlugins);

module.exports = plugins;
export default plugins;
// module.exports = plugins;
7 changes: 4 additions & 3 deletions lib/WatchRunReporterPlugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const chalk = require("chalk");

// const chalk = require("chalk");
import chalk from "chalk";
/**
* Simple plugin for restoring the starting compile message which vanished in
* DevServer v4
Expand Down Expand Up @@ -52,4 +52,5 @@ class WatchRunReporterPlugin {
}
}

module.exports = WatchRunReporterPlugin;
// module.exports = WatchRunReporterPlugin;
export default WatchRunReporterPlugin;
19 changes: 14 additions & 5 deletions lib/buildConfig.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const fs = require("fs-extra");
const path = require("path").posix;
// const fs = require("fs-extra");
// const path = require("path").posix;

const chalk = require("chalk");

const defaultConfig = require("../default.config.js");

import fs from "fs-extra";
import {posix as path} from "path";

import chalk from "chalk";
// const chalk = require("chalk");

// const defaultConfig = require("../default.config.js");

import defaultConfig from "../default.config.js";

/**
* This file encapsulates all the config file massaging and allows
Expand All @@ -12,7 +20,8 @@ const defaultConfig = require("../default.config.js");
* Asynchronous values:
* -
*/
module.exports = (configFile = { config: {} }) => {
// module.exports = (configFile = { config: {} }) => {
export default (configFile = { config: {} }) => {
/**
* Merge configFile onto defaults
*/
Expand Down
31 changes: 22 additions & 9 deletions lib/devserver-proxy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const { resolve } = require("dns").promises;
// const { dns } = require("dns").promises;

const chalk = require("chalk");
const isIp = require("is-ip");
// DO NOT destructure this or we won't be able to mock it
// import {promises as dnsPromises} from "dns";
import dns from "dns";
import chalk from "chalk";
import { isIP } from "is-ip";

/**
* If config.proxy is explicitly not false, return a {proxy} object
* otherwise return an empty object.
*/
module.exports = async (config) => {
export default async (config) => {
let target;

if (
Expand All @@ -27,11 +30,14 @@ module.exports = async (config) => {
)
);

target = await resolve("wordpress").catch(() => false);
/**
* 'wordpress' is the internal hostname used by docker
*/
target = await dns.promises.resolve("wordpress").catch(() => false);
if (target) {
target = "http://" + target.pop();
}
} else if (isIp(config.proxy)) {
} else if (isIP(config.proxy)) {
/**
* Bare IP addresses
*/
Expand All @@ -44,7 +50,8 @@ module.exports = async (config) => {
* Handle plain strings that don't start with http:// or https://
* Attempt to resolve these to IP addresses
*/
target = await resolve(config.proxy).catch(() => false);
target = await dns.promises.resolve(config.proxy).catch(() => false);

if (target) {
target = "http://" + target.pop();
}
Expand All @@ -55,13 +62,20 @@ module.exports = async (config) => {
target = config.proxy;
}

/**
* Note: This is a sticky bit of code. Everything above gets bottlenecked here, and
* if aa previous value of target can't be converted to a URL, the whole proxy will
* return an empty object.
*/
try {
config.proxyUrl = new URL(target);
} catch (err) {
// Invalid URL, unable to configure proxy.
return {};
}

// console.log({ target , proxyUrl: config.proxyUrl});

const proxy = {
"**": {
target,
Expand Down Expand Up @@ -106,16 +120,15 @@ module.exports = async (config) => {
}

Object.keys(proxyRes.headers).forEach((key) => {

let header = proxyRes.headers[key];
// if (header !== undefined) {
if (typeof header == "string") {
header = header.replace(
new RegExp(config.proxyUrl.host, "gi"),
req.headers.host
);
}
res.setHeader(String(key).trim(), header);
// }
});

originalBody = Buffer.concat(originalBody);
Expand Down
File renamed without changes.
Loading

0 comments on commit c833361

Please sign in to comment.