Skip to content

Commit

Permalink
#873@trivial: Continue on implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Jul 4, 2023
1 parent 5d28f9e commit bd2ac68
Show file tree
Hide file tree
Showing 18 changed files with 849 additions and 74 deletions.
621 changes: 621 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/global-registrator/.eslintrc.cjs
@@ -1,9 +1,9 @@
module.exports = require('happy-dom/.eslintrc');
module.exports = require('happy-dom/.eslintrc.cjs');

for (const override of module.exports.overrides.filter(
(override) => override.files.includes('*.ts') || override.files.includes('*.js')
)) {
override.exports = override.exports.filter((item) => !item.includes('jest'));
override.extends = override.extends.filter((item) => !item.includes('jest'));
override.plugins = override.plugins.filter((item) => !item.includes('jest'));

for (const name of Object.keys(override.rules)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/global-registrator/.prettierrc.cjs
@@ -1 +1 @@
module.exports = require('../happy-dom/.prettierrc.js');
module.exports = require('../happy-dom/.prettierrc.cjs');
43 changes: 35 additions & 8 deletions packages/global-registrator/package.json
Expand Up @@ -6,23 +6,49 @@
"repository": "https://github.com/capricorn86/happy-dom",
"author": "David Ortner",
"description": "Use Happy DOM globally in a Node.js environment for testing.",
"main": "cjs/index.js",
"module": "lib/index.js",
"main": "lib/index.js",
"type": "module",
"exports": {
".": {
"import": "./lib/index.js",
"require": "./cjs/index.js",
"require": "./cjs/index.cjs",
"default": "./lib/index.js"
},
"./lib/*.js": {
"import": "./lib/*.js",
"require": "./cjs/*.js",
"require": "./cjs/*.cjs",
"default": "./lib/*.js"
},
"./lib/*.ts": {
"import": "./lib/*.ts",
"require": "./cjs/*.ts",
"default": "./lib/*.ts"
},
"./lib/*.map": {
"import": "./lib/*.map",
"require": "./cjs/*.map",
"default": "./lib/*.map"
},
"./cjs/*.js": {
"import": "./lib/*.js",
"require": "./cjs/*.cjs",
"default": "./lib/*.js"
},
"./cjs/*.ts": {
"import": "./lib/*.ts",
"require": "./cjs/*.ts",
"default": "./lib/*.ts"
},
"./cjs/*.map": {
"import": "./lib/*.map",
"require": "./cjs/*.map",
"default": "./lib/*.map"
},
"./src/*.ts": "./src/*.ts",
"./package.json": "./package.json"
"./package.json": "./package.json",
"./.eslintrc": "./.eslintrc.js"
},
"keywords": [
"keywords": [
"jsdom",
"happy",
"dom",
Expand All @@ -39,11 +65,12 @@
"access": "public"
},
"scripts": {
"compile": "tsc && tsc --module CommonJS --outDir cjs",
"compile": "tsc && tsc --module CommonJS --outDir cjs && npm run change-cjs-file-extension",
"change-cjs-file-extension": "node ../happy-dom/bin/change-file-extension.cjs --dir=./cjs --fromExt=.js --toExt=.cjs",
"watch": "npm run compile && tsc -w --preserveWatchOutput",
"lint": "eslint --ignore-path .gitignore --max-warnings 0 .",
"lint:fix": "eslint --ignore-path .gitignore --max-warnings 0 --fix .",
"test": "tsc --project ./test && node ./tmp/react/React.test.js",
"test": "tsc --project ./test && node ../happy-dom/bin/change-file-extension.cjs --dir=./tmp --fromExt=.js --toExt=.cjs && node ./tmp/react/React.test.cjs",
"test:debug": "tsc --project ./test && node --inspect-brk ./tmp/react/React.test.js"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/global-registrator/src/index.ts
@@ -1,3 +1,3 @@
import GlobalRegistrator from './GlobalRegistrator';
import GlobalRegistrator from './GlobalRegistrator.js';

export { GlobalRegistrator };
4 changes: 2 additions & 2 deletions packages/global-registrator/test/react/React.test.tsx
@@ -1,7 +1,7 @@
import GlobalRegistrator from '../../cjs/GlobalRegistrator';
import GlobalRegistrator from '../../cjs/GlobalRegistrator.cjs';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactComponent from './ReactComponent';
import ReactComponent from './ReactComponent.js';

GlobalRegistrator.register();

Expand Down
5 changes: 1 addition & 4 deletions packages/global-registrator/tsconfig.json
Expand Up @@ -7,7 +7,7 @@
"declaration": true,
"declarationMap": true,
"module": "es2020",
"moduleResolution": "node",
"moduleResolution": "node16",
"esModuleInterop": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
Expand All @@ -22,9 +22,6 @@
"composite": true,
"incremental": true,
"jsx": "react",
"typeRoots": [
"node_modules/@types"
],
"types": [
"node"
],
Expand Down
100 changes: 100 additions & 0 deletions packages/happy-dom/bin/change-file-extension.cjs
@@ -0,0 +1,100 @@
/* eslint-disable no-console*/
/* eslint-disable @typescript-eslint/no-var-requires*/

const Path = require('path');
const FS = require('fs');

process.on('unhandledRejection', (reason) => {
console.error(reason);
process.exit(1);
});

main();

function getArguments() {
const args = {
dir: null,
fromExt: null,
toExt: null
};

for (const arg of process.argv) {
if (arg.startsWith('--dir=')) {
args.dir = arg.split('=')[1];
} else if (arg.startsWith('--fromExt=')) {
args.fromExt = arg.split('=')[1];
} else if (arg.startsWith('--toExt=')) {
args.toExt = arg.split('=')[1];
}
}

return args;
}

async function readDirectory(directory) {
const resolvedDirectory = Path.resolve(directory);
const files = await FS.promises.readdir(resolvedDirectory);
const statsPromises = [];
let allFiles = [];

for (const file of files) {
const filePath = Path.join(resolvedDirectory, file);
statsPromises.push(
FS.promises.stat(filePath).then((stats) => {
if (stats.isDirectory()) {
return readDirectory(filePath).then((files) => (allFiles = allFiles.concat(files)));
}
allFiles.push(filePath);
})
);
}

await Promise.all(statsPromises);

return allFiles;
}

async function renameFiles(files, args) {
const newFiles = files.map((file) => ({
oldPath: file,
newPath: file.replace(args.fromExt, args.toExt)
}));
const writePromises = [];

for (const file of newFiles) {
writePromises.push(
FS.promises.readFile(file.oldPath).then((content) => {
return FS.promises
.writeFile(
file.newPath,
content
.toString()
.replace(
new RegExp(`${args.fromExt.replace('.', '\\.')}\\.map`, 'g'),
`${args.toExt}.map`
)
.replace(
new RegExp(`${args.fromExt.replace('.', '\\.')}(["'])`, 'g'),
`${args.toExt}$1`
)
)
.then(() => {
if (file.oldPath !== file.newPath) {
return FS.promises.unlink(file.oldPath);
}
});
})
);
}

await Promise.all(writePromises);
}

async function main() {
const args = getArguments();
if (!args.dir || !args.fromExt || !args.toExt) {
throw new Error('Invalid arguments');
}
const files = await readDirectory(args.dir);
await renameFiles(files, args);
}
38 changes: 32 additions & 6 deletions packages/happy-dom/package.json
Expand Up @@ -6,23 +6,48 @@
"repository": "https://github.com/capricorn86/happy-dom",
"author": "David Ortner",
"description": "Happy DOM is a JavaScript implementation of a web browser without its graphical user interface. It includes many web standards from WHATWG DOM and HTML.",
"main": "cjs/index.js",
"module": "lib/index.js",
"main": "lib/index.js",
"type": "module",
"exports": {
".": {
"import": "./lib/index.js",
"require": "./cjs/index.js",
"require": "./cjs/index.cjs",
"default": "./lib/index.js"
},
"./lib/*.js": {
"import": "./lib/*.js",
"require": "./cjs/*.js",
"require": "./cjs/*.cjs",
"default": "./lib/*.js"
},
"./lib/*.ts": {
"import": "./lib/*.ts",
"require": "./cjs/*.ts",
"default": "./lib/*.ts"
},
"./lib/*.map": {
"import": "./lib/*.map",
"require": "./cjs/*.map",
"default": "./lib/*.map"
},
"./cjs/*.js": {
"import": "./lib/*.js",
"require": "./cjs/*.cjs",
"default": "./lib/*.js"
},
"./cjs/*.ts": {
"import": "./lib/*.ts",
"require": "./cjs/*.ts",
"default": "./lib/*.ts"
},
"./cjs/*.map": {
"import": "./lib/*.map",
"require": "./cjs/*.map",
"default": "./lib/*.map"
},
"./src/*.ts": "./src/*.ts",
"./package.json": "./package.json",
"./.eslintrc": "./.eslintrc.js"
"./.eslintrc.cjs": "./.eslintrc.cjs",
"./.prettierrc.cjs": "./.prettierrc.cjs"
},
"keywords": [
"jsdom",
Expand All @@ -40,7 +65,8 @@
"access": "public"
},
"scripts": {
"compile": "tsc && tsc --module CommonJS --outDir cjs",
"compile": "tsc && tsc --module CommonJS --outDir cjs && npm run change-cjs-file-extension",
"change-cjs-file-extension": "node ./bin/change-file-extension.cjs --dir=./cjs --fromExt=.js --toExt=.cjs",
"watch": "tsc -w --preserveWatchOutput",
"lint": "eslint --ignore-path .gitignore --max-warnings 0 .",
"lint:fix": "eslint --ignore-path .gitignore --max-warnings 0 --fix .",
Expand Down
4 changes: 2 additions & 2 deletions packages/integration-test/.eslintrc.cjs
@@ -1,8 +1,8 @@
module.exports = require('happy-dom/.eslintrc');
module.exports = require('happy-dom/.eslintrc.cjs');

const override = module.exports.overrides.find((override) => override.files.includes('*.js'));

override.exports = override.exports.filter((item) => !item.includes('jest'));
override.extends = override.extends.filter((item) => !item.includes('jest'));
override.plugins = override.plugins.filter((item) => !item.includes('jest'));

for (const name of Object.keys(override.rules)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/integration-test/.prettierrc.cjs
@@ -1 +1 @@
module.exports = require('../happy-dom/.prettierrc.js');
module.exports = require('../happy-dom/.prettierrc.cjs');
32 changes: 2 additions & 30 deletions packages/integration-test/test/index.js
@@ -1,34 +1,6 @@
import ChildProcess from 'node:child_process';
import FS from 'node:fs';
import Path from 'node:path';

// ES6 tests
await Promise.all([
import('./tests/Fetch.test.js'),
import('./tests/XMLHttpRequest.test.js'),
import('./tests/WindowGlobals.test.js')
import('./tests/WindowGlobals.test.js'),
import('./tests/CommonJS.test.cjs')
]);

// CommonJS test
const packageJSONPath = Path.resolve('./package.json');
const packageJSONContent = FS.readFileSync(packageJSONPath);

FS.writeFileSync(packageJSONPath, packageJSONContent.replace('"type": "module",', ''));

ChildProcess.exec('node ./tests/CommonJS.test.js', {}, (error, stdout, stderr) => {
FS.writeFileSync(packageJSONPath, packageJSONContent);

if (stdout) {
console.log(stdout);
}
if (stderr) {
console.error(stderr);
}
if (error) {
console.error(stderr);
}

if (error || stderr) {
process.exit(1);
}
});
@@ -1,4 +1,4 @@
const { Window } = 'happy-dom';
const { Window } = require('happy-dom');

const window = new Window();
const document = window.document;
Expand Down
2 changes: 1 addition & 1 deletion packages/integration-test/test/tests/WindowGlobals.test.js
Expand Up @@ -8,7 +8,7 @@ describe('WindowGlobals', () => {
window.addEventListener('error', (event) => (error = event.error));
window.document.write(`
<script>
if(() => {}).constructor !== Function) {
if((() => {}).constructor !== Function) {
throw new Error('Error');
}
</script>
Expand Down
Expand Up @@ -13,7 +13,6 @@ describe('XMLHttpRequest', () => {
});

const server = express.listen(3000);

const request = new window.XMLHttpRequest();

request.open('GET', 'http://localhost:3000/get/json', true);
Expand Down

0 comments on commit bd2ac68

Please sign in to comment.