Skip to content

fix: Test suite now uses config files again #1623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/cli/lib/lib/webpack/transform-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ module.exports = async function (env, webpackConfig, isServer = false) {
);
}

const m = require('esm')(module)(myConfig);
let m = require('esm')(module)(myConfig);

// The line above results in an empty object w/ Jest,
// so we need to do the following in order to load it:
if (Object.keys(m).length === 0) {
try {
m = require(myConfig);
} catch {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is just making sure that it's actually empty? (and we don't care about an error in that case)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the idea, yeah. By this point we've already ensured the file exists so if it's imported in some capacity and still empty, that's either a file with no exports or whatever this issue is with esm in our test suite. If a user does provide us with an empty config, that shouldn't be an issue.

The try ... catch I believe is unnecessary and I'll remove in a second. Left over fragment that I don't believe will do anything ever.

}

const transformers = parseConfig((m && m.default) || m);

Expand Down
8 changes: 3 additions & 5 deletions packages/cli/tests/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,12 @@ describe('preact build', () => {
});

it('should use custom `preact.config.js`', async () => {
// app with custom template set via preact.config.js
// app with stable output name via preact.config.js
let dir = await subject('custom-webpack');
await build(dir);

let file = join(dir, 'build/index.html');
let html = await readFile(file, 'utf-8');

looksLike(html, images.webpack);
let file = join(dir, 'build/bundle.js');
expect(existsSync(file)).toBe(true);
});

it('should use template from the code folder', async () => {
Expand Down
15 changes: 0 additions & 15 deletions packages/cli/tests/images/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,6 @@ exports.prerender.htmlSafe = `
</body>
`;

exports.webpack = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>preact-webpack</title>
</head>
<body>
<h1>Guess what</h1>
<h2>This is an app with custom template</h2>
{{ ... }}
</body>
</html>
`;

exports.template = `
<!DOCTYPE html>
<html lang="en">
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/tests/lib/output.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { existsSync, mkdirSync } = require('fs');
const copy = require('ncp');
const { resolve } = require('path');
const { promisify } = require('util');
Expand All @@ -16,6 +17,9 @@ function tmpDir() {
async function subject(name) {
let src = resolve(subjects, name);
let dest = tmpDir();
if (!existsSync(dest)) {
mkdirSync(dest, { recursive: true });
}
await promisify(copy)(src, dest);
return dest;
}
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/tests/subjects/custom-webpack/preact.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const path = require('path');

module.exports = function (config, env, helpers) {
if (env.ssr) return;
helpers.setHtmlTemplate(config, path.resolve(__dirname, './template.html'));
module.exports = function (config) {
config.output.filename = '[name].js';
};
6 changes: 0 additions & 6 deletions packages/cli/tests/subjects/preload-chunks/preact.config.js

This file was deleted.