Skip to content

Commit

Permalink
refactor: Make config file handling consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Mar 6, 2022
1 parent ccafe17 commit b9613c1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
24 changes: 10 additions & 14 deletions packages/cli/lib/lib/webpack/render-html-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const HtmlWebpackExcludeAssetsPlugin = require('html-webpack-exclude-assets-plug
const HtmlWebpackPlugin = require('html-webpack-plugin');
const prerender = require('./prerender');
const createLoadManifest = require('./create-load-manifest');
const { esmImport, warn } = require('../../util');
const { esmImport, tryResolveConfig, warn } = require('../../util');

const PREACT_FALLBACK_URL = '/200.html';

Expand Down Expand Up @@ -106,9 +106,16 @@ module.exports = async function (config) {
let pages = [{ url: '/' }];

if (config.prerenderUrls) {
if (existsSync(resolve(cwd, config.prerenderUrls))) {
const prerenderUrls = tryResolveConfig(
cwd,
config.prerenderUrls,
config.prerenderUrls === 'prerender-urls.json',
config.verbose
);

if (prerenderUrls) {
try {
let result = esmImport(resolve(cwd, config.prerenderUrls));
let result = esmImport(prerenderUrls);

if (typeof result.default !== 'undefined') {
result = result.default;
Expand All @@ -129,17 +136,6 @@ module.exports = async function (config) {
}`
);
}
// don't warn if the default file doesn't exist
} else if (
config.prerenderUrls !== 'prerender-urls.json' ||
config.verbose
) {
warn(
`prerenderUrls file (${resolve(
cwd,
config.prerenderUrls
)}) doesn't exist, using default!`
);
}
}
/**
Expand Down
37 changes: 18 additions & 19 deletions packages/cli/lib/lib/webpack/transform-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { resolve } = require('path');
const webpack = require('webpack');
const { stat } = require('fs').promises;
const { error, esmImport } = require('../../util');
const { error, esmImport, tryResolveConfig, warn } = require('../../util');

const FILE = 'preact.config';
const EXTENSIONS = ['js', 'json'];
Expand Down Expand Up @@ -95,27 +95,26 @@ module.exports = async function (env, webpackConfig, isServer = false) {
env.config !== 'preact.config.js'
? { configFile: env.config, isDefault: false }
: await findConfig(env);
env.config = configFile;
let myConfig = resolve(env.cwd, env.config);

const cliConfig = tryResolveConfig(
env.cwd,
configFile,
isDefault,
env.verbose
);

if (!cliConfig) return;

let m;
try {
m = esmImport(myConfig);
} catch (err) {
const notFound =
err.message.includes('Cannot find module') ||
err.message.includes('Qualified path resolution failed');
if (notFound && isDefault) return;
if (notFound) {
throw new Error(
`Failed to load preact-cli config!\nFile ${env.config} not found.\n`
);
}
throw new Error(
`Failed to load preact-cli config!\n${
env.verbose ? err.stack : err.message
}\n`
m = esmImport(cliConfig);
} catch (error) {
warn(
`Failed to load preact-cli config file, using default!\n${
env.verbose ? error.stack : error.message
}`
);
return;
}

const transformers = parseConfig((m && m.default) || m);
Expand All @@ -134,7 +133,7 @@ module.exports = async function (env, webpackConfig, isServer = false) {
options
);
} catch (err) {
throw new Error((`Error at ${myConfig}: \n` + err && err.stack) || err);
throw new Error((`Error at ${cliConfig}: \n` + err && err.stack) || err);
}
}
};
Expand Down
13 changes: 11 additions & 2 deletions packages/cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ exports.info = function (text, code) {
code && process.exit(code);
};

exports.warn = function (text, code) {
const warn = (exports.warn = function (text, code) {
process.stdout.write(symbols.warning + yellow(' WARN ') + text + '\n');
code && process.exit(code);
};
});

exports.error = function (text, code = 1) {
process.stderr.write(symbols.error + red(' ERROR ') + text + '\n');
Expand Down Expand Up @@ -80,3 +80,12 @@ exports.isPortFree = async function (port) {
return false;
}
};

exports.tryResolveConfig = function (cwd, file, isDefault, verbose) {
const path = resolve(cwd, file);
if (existsSync(path)) {
return path;
} else if (!isDefault || verbose) {
warn(`${resolve(cwd, file)} doesn't exist, using default!`);
}
};
14 changes: 11 additions & 3 deletions packages/cli/tests/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ describe('config files', () => {
it(`should load the '${dataFormat}' file in ${moduleFormat}`, async () => {
let dir = await subject('multiple-config-files');

await expect(
build(dir, { config: `preactConfig/${moduleFormat}/${dataFormat}` })
).resolves.not.toThrow();
const logSpy = jest.spyOn(process.stdout, 'write');

await build(dir, {
config: `preactConfig/${moduleFormat}/${dataFormat}`,
});

expect(logSpy).not.toHaveBeenCalledWith(
expect.stringContaining(
'Failed to load preact-cli config file, using default!'
)
);
});
});
});
Expand Down

0 comments on commit b9613c1

Please sign in to comment.