Skip to content
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

Fix module resolution for preval.require #38

Merged
merged 3 commits into from Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .all-contributorsrc
Expand Up @@ -86,6 +86,13 @@
"contributions": [
"doc"
]
},
{
"login": "sergeybekrin",
"name": "Sergey Bekrin",
"avatar_url": "https://avatars0.githubusercontent.com/u/9248479?v=4",
"profile": "http://bekrin.me",
"contributions": []
}
]
}
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -12,7 +12,7 @@ Pre-evaluate code at build-time
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-8-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-9-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -340,7 +340,7 @@ Thanks goes to these people ([emoji key][emojis]):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/5610087?v=3" width="100px;"/><br /><sub>Matt Phillips</sub>](http://mattphillips.io)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Code") [📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Tests") | [<img src="https://avatars1.githubusercontent.com/u/28024000?v=3" width="100px;"/><br /><sub>Philip Oliver</sub>](https://twitter.com/philipodev)<br />[🐛](https://github.com/kentcdodds/babel-plugin-preval/issues?q=author%3Aphilipodev "Bug reports") | [<img src="https://avatars2.githubusercontent.com/u/2109702?v=3" width="100px;"/><br /><sub>Sorin Davidoi</sub>](https://toot.cafe/@sorin)<br />[🐛](https://github.com/kentcdodds/babel-plugin-preval/issues?q=author%3Asorin-davidoi "Bug reports") [💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=sorin-davidoi "Code") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=sorin-davidoi "Tests") | [<img src="https://avatars4.githubusercontent.com/u/1127238?v=4" width="100px;"/><br /><sub>Luke Herrington</sub>](https://github.com/infiniteluke)<br />[💡](#example-infiniteluke "Examples") | [<img src="https://avatars4.githubusercontent.com/u/22868432?v=4" width="100px;"/><br /><sub>Lufty Wiranda</sub>](http://instagram.com/luftywiranda13)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=luftywiranda13 "Code") | [<img src="https://avatars0.githubusercontent.com/u/3877773?v=4" width="100px;"/><br /><sub>Oscar</sub>](http://obartra.github.io)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=obartra "Code") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=obartra "Tests") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars1.githubusercontent.com/u/14310216?v=4" width="100px;"/><br /><sub>pro-nasa</sub>](https://github.com/pro-nasa)<br />[📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=pro-nasa "Documentation") |
| [<img src="https://avatars1.githubusercontent.com/u/14310216?v=4" width="100px;"/><br /><sub>pro-nasa</sub>](https://github.com/pro-nasa)<br />[📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=pro-nasa "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/9248479?v=4" width="100px;"/><br /><sub>Sergey Bekrin</sub>](http://bekrin.me)<br /> |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
36 changes: 23 additions & 13 deletions src/__tests__/__snapshots__/index.js.snap
Expand Up @@ -198,6 +198,16 @@ const x = { "a": function a() {

exports[`20. preval 1`] = `

const x = preval.require("../__tests__/fixtures/nested/absolute-path")

↓ ↓ ↓ ↓ ↓ ↓

const x = "<PROJECT_ROOT>/src/__tests__/fixtures/nested/absolute-path.js";

`;

exports[`21. preval 1`] = `

const x = preval.require("./fixtures/identity", SOME_UNKNOWN_VARIABLE)

↓ ↓ ↓ ↓ ↓ ↓
Expand All @@ -206,7 +216,7 @@ Error: <PROJECT_ROOT>/src/__tests__/index.js: preval cannot determine the value

`;

exports[`21. preval 1`] = `
exports[`22. preval 1`] = `

const x = preval.require("./fixtures/compute-one", "should not be here...")

Expand All @@ -216,7 +226,7 @@ Error: <PROJECT_ROOT>/src/__tests__/index.js: \`preval.require\`-ed module (./fi

`;

exports[`22. preval 1`] = `
exports[`23. preval 1`] = `

// @preval
module.exports = 1 + 2 - 1 - 1
Expand All @@ -228,7 +238,7 @@ module.exports = 1;

`;

exports[`23. preval 1`] = `
exports[`24. preval 1`] = `

// @preval
const ten = 9 + 1
Expand All @@ -241,7 +251,7 @@ module.exports = 50;

`;

exports[`24. preval 1`] = `
exports[`25. preval 1`] = `

// @flow
// @preval
Expand All @@ -255,7 +265,7 @@ module.exports = 1;

`;

exports[`25. preval 1`] = `
exports[`26. preval 1`] = `

// @preval
// @flow
Expand All @@ -269,7 +279,7 @@ module.exports = 1;

`;

exports[`26. preval 1`] = `
exports[`27. preval 1`] = `

// @preval
const name = 'Bob Hope'
Expand All @@ -283,7 +293,7 @@ module.exports = ["Bob", "Hope"];

`;

exports[`27. preval 1`] = `
exports[`28. preval 1`] = `

// @preval
const name = 'Bob Hope'
Expand All @@ -298,7 +308,7 @@ module.exports = { "first": "Bob", "last": "Hope" };

`;

exports[`28. preval 1`] = `
exports[`29. preval 1`] = `

// @preval
module.exports = require("./fixtures/compute-one")
Expand All @@ -310,7 +320,7 @@ module.exports = 1;

`;

exports[`29. preval 1`] = `
exports[`30. preval 1`] = `

// @preval
module.exports = require("./fixtures/identity")('hello world')
Expand All @@ -322,7 +332,7 @@ module.exports = "hello world";

`;

exports[`30. preval 1`] = `
exports[`31. preval 1`] = `

// @preval
const id = require("./fixtures/identity")
Expand All @@ -342,7 +352,7 @@ module.exports = 4;

`;

exports[`31. preval 1`] = `
exports[`32. preval 1`] = `

// @preval
const fs = require('fs')
Expand All @@ -355,7 +365,7 @@ module.exports = "# fixture\\n\\nThis is some file thing...\\n";

`;

exports[`32. preval 1`] = `
exports[`33. preval 1`] = `

// @preval
function fib(x) {
Expand All @@ -370,7 +380,7 @@ module.exports = 55;

`;

exports[`34. preval 1`] = `
exports[`35. preval 1`] = `

// @preval
let smth = {}
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/fixtures/nested/absolute-path.js
@@ -0,0 +1,3 @@
// preval.require would either resolve an absolute path or fail to resolve this
// module at all
module.exports = __filename
1 change: 1 addition & 0 deletions src/__tests__/index.js
Expand Up @@ -53,6 +53,7 @@ pluginTester({
'const x = preval.require("./fixtures/compute-one")',
'const x = preval.require("./fixtures/identity", 3)',
'const x = preval.require("./fixtures/multiple-functions")',
'const x = preval.require("../__tests__/fixtures/nested/absolute-path")',
error(
'const x = preval.require("./fixtures/identity", SOME_UNKNOWN_VARIABLE)',
),
Expand Down
13 changes: 7 additions & 6 deletions src/index.js
Expand Up @@ -48,9 +48,7 @@ function prevalPlugin({types: t, template, transformFromAst}) {
}
const string = path.get('quasi').evaluate().value
if (!string) {
throw new Error(
'Unable to determine the value of your preval string',
)
throw new Error('Unable to determine the value of your preval string')
}
const replacement = getReplacement({string, filename})
path.replaceWith(replacement)
Expand Down Expand Up @@ -121,7 +119,7 @@ function prevalPlugin({types: t, template, transformFromAst}) {
}
return result.value
})
const absolutePath = p.join(p.dirname(filename), source.node.value)
const absolutePath = p.resolve(p.dirname(filename), source.node.value)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is actual change, rest of the stuff is formatted by prettier I believe

try {
// allow for transpilation of required modules
require('babel-register')
Expand All @@ -147,7 +145,10 @@ function prevalPlugin({types: t, template, transformFromAst}) {
}

function isPrevalComment(comment) {
const normalisedComment = comment.value.trim().split(' ')[0].trim()
const normalisedComment = comment.value
.trim()
.split(' ')[0]
.trim()
return (
normalisedComment.startsWith('preval') ||
normalisedComment.startsWith('@preval')
Expand All @@ -171,7 +172,7 @@ function looksLike(a, b) {

function isPrimitive(val) {
// eslint-disable-next-line
return val == null || /^[sbn]/.test(typeof val);
return val == null || /^[sbn]/.test(typeof val)
}

/*
Expand Down