Skip to content

Commit 39d0745

Browse files
author
Sergii.Kliuchnyk
committed
require can resolve current working dir
1 parent 09749be commit 39d0745

10 files changed

Lines changed: 46 additions & 16 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ language: node_js
22
node_js:
33
- "10"
44
before_script:
5-
- npm install react react-dom --no-save
5+
- npm run peer

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ const requireJSX = engine.require || require('express-engine-jsx/require');
185185

186186
This is a function which you can use as regular `require` but this one can run jsx files. It checks if path is jsx file and if it is then `requireJSX` will [convert](#convert) this file to js code and then run it.
187187

188-
It also takes optional second parameter - `currentWorkingDir` which should be an absolute path to file directory which calls `require` in case when main path is relative.
188+
It also can take optional second parameter - `currentWorkingDir` which should be an absolute path to file directory which calls `require` in case when you call `require` from some unusual place like debugger console.
189189

190190
Every compiled jsx file will be cached to `requireJSX.cache` object where key will be path to jsx file and value will be value of `module.exports` inside jsx file, usually react component.
191191
You can delete any key in this cache, requireJSX will recompile jsx file on next call.

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "express-engine-jsx",
3-
"version": "3.1.0",
3+
"version": "3.2.0",
44
"description": "JSX engine for ExpressJS",
55
"main": "index.js",
66
"types": "index.d.ts",
77
"scripts": {
8-
"test": "NODE_ENV=production ./node_modules/.bin/mocha -R spec tests"
8+
"test": "NODE_ENV=production ./node_modules/.bin/mocha -R spec tests",
9+
"peer": "npm i react react-dom --no-save"
910
},
1011
"repository": {
1112
"type": "git",
@@ -29,7 +30,8 @@
2930
"@babel/plugin-transform-react-jsx": "^7.12.16",
3031
"@babel/template": "^7.12.13",
3132
"@babel/traverse": "^7.12.13",
32-
"@babel/types": "^7.12.13"
33+
"@babel/types": "^7.12.13",
34+
"callsites": "^3.1.0"
3335
},
3436
"devDependencies": {
3537
"@types/babylon": "^6.16.5",

require.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {resolve, isAbsolute} = require('path');
1+
const {resolve, isAbsolute, dirname} = require('path');
22
const fs = require('fs');
33
const convert = require('./convert');
44
const run = require('./run');
@@ -29,7 +29,11 @@ function requireJSX(path, currentWorkingDir) {
2929
currentWorkingDir = null;
3030
}
3131
else if (!currentWorkingDir) {
32-
throw new Error(`Relative path. Required currentWorkingDir parameter`);
32+
const site = require('callsites')()[1];
33+
34+
currentWorkingDir = site && dirname(site.getFileName());
35+
36+
if (!currentWorkingDir || !isAbsolute(currentWorkingDir)) throw new Error('currentWorkingDir required');
3337
}
3438
else if (!isAbsolute(currentWorkingDir)) {
3539
throw new Error('currentWorkingDir must be absolute path');
@@ -43,13 +47,13 @@ function requireJSX(path, currentWorkingDir) {
4347

4448
if (cache[path]) return cache[path];
4549

46-
if (fs.existsSync(path + '.js')) {
50+
if (fs.existsSync(path + '.js') || fs.existsSync(resolve(path, 'index.js'))) {
4751
return require(path);
4852
}
4953

50-
const pathJSX = path + '.jsx';
54+
var pathJSX;
5155

52-
if (!fs.existsSync(pathJSX)) {
56+
if (!fs.existsSync((pathJSX = path + '.jsx')) && !fs.existsSync((pathJSX = resolve(path, 'index.jsx')))) {
5357
throw new Error(`JSX file not found ${JSON.stringify(path)}`);
5458
}
5559

@@ -64,8 +68,13 @@ function resolveJSX(path) {
6468
try {
6569
path = require.resolve(path + '.jsx');
6670
}
67-
catch (e) {
68-
return null;
71+
catch (e1) {
72+
try {
73+
path = require.resolve(resolve(path, 'index.jsx'));
74+
}
75+
catch (e2) {
76+
return null;
77+
}
6978
}
7079

7180
return path.replace(/\.jsx$/, '');

tests/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,12 @@ describe('convert', function () {
142142
}));`
143143
);
144144
});
145+
146+
it('should require', function () {
147+
const Layout = requireJSX('./views/layout');
148+
const Users = requireJSX('./views/app/components/users');
149+
150+
expect(Layout).to.be.a('function');
151+
expect(Users).to.be.a('function');
152+
});
145153
});
File renamed without changes.

tests/views/app/helpers/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 2;

tests/views/app/users.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
var Layout = require('../layout');
2-
var Users = require('./components/users');
3-
var helper = require('./helpers/script');
1+
const Layout = require('../layout');
2+
const Users = require('./components/users');
3+
const helper1 = require('./helpers/script');
4+
const helper2 = require('./helpers/sub');
5+
6+
if (helper1 !== 1) throw new Error('helper1');
7+
if (helper2 !== 2) throw new Error('helper2');
48

59
<Layout>
610
<Users users={users}/>

0 commit comments

Comments
 (0)