Skip to content

Commit b0f94b9

Browse files
author
Kent C. Dodds
authored
feat: drop support for transpiling the code that is being prevaled (#59)
It cause way too many weird bugs that take a TON of time to track down. BREAKING CHANGE: Code that's being prevaled will no longer be transpiled. You should either transpile it ahead of time or only write the code in a version of JavaScript that runs natively in the version of node you're running.
1 parent d8be253 commit b0f94b9

File tree

9 files changed

+40
-155
lines changed

9 files changed

+40
-155
lines changed

README.md

Lines changed: 10 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ See more below.
9494
- [Use with `babel-plugin-macros`](#use-with-babel-plugin-macros)
9595
- [Examples](#examples)
9696
- [Notes](#notes)
97-
- [Limitations](#limitations)
98-
- [Code transpilation](#code-transpilation)
9997
- [FAQ](#faq)
10098
- [How is this different from prepack?](#how-is-this-different-from-prepack)
10199
- [How is this different from webpack loaders?](#how-is-this-different-from-webpack-loaders)
@@ -122,9 +120,8 @@ Important notes:
122120

123121
1. All code run by `preval` is _not_ run in a sandboxed environment
124122
2. All code _must_ run synchronously.
125-
3. The code string that preval evaluates will be transpiled, however, only that
126-
code string will be prevaled. Learn more about this in the
127-
[limitations](#limitations) section below.
123+
3. Code that is run by preval is not transpiled so it must run natively in the
124+
version of node you're running. (cannot use es modules).
128125

129126
> You may like to watch
130127
> [this YouTube video](https://www.youtube.com/watch?v=1queadQ0048&list=PLV5CVI1eNcJgCrPH_e6d57KRUTiDZgs0u)
@@ -339,77 +336,13 @@ Ok:
339336
}
340337
```
341338

342-
## Limitations
343-
344-
### Code transpilation
345-
346-
It's recommended that code you want to preval be written in a way that works in
347-
the version of Node that you're running. That said, some code will be
348-
transpiled. If you for some strange reason _really_ want to write your preval
349-
code in a way that requires it to be transpiled, then expand here:
350-
351-
<details>
352-
<summary>Expand (beware)</summary>
353-
354-
Here are a few examples of what will and wont be transpiled by preval:
355-
356-
> This is assuming that you've configured babel to support ESModule syntax but
357-
> you're using a node version which does not support ESModules natively.
358-
359-
```javascript
360-
// a.js // this file can use ESModules. Regular babel handles that.
361-
import value from /* preval */ './b'
362-
363-
// b.js // this file can use ESModules. preval handles that.
364-
import c from './c'
365-
export default c
366-
367-
// c.js // this file cannot have ESModules in it.
368-
// Neither preval nor babel will transpile this file's contents
369-
module.exports = 'c'
370-
```
371-
372-
```javascript
373-
// this file can use ESModules
374-
var x = preval`
375-
// this string can use ESModules
376-
import b from 'b.js'
377-
// however b.js and it's dependents cannot
378-
`
379-
```
380-
381-
```javascript
382-
// this file can use ESModules
383-
// and b.js will be transpiled and evaluated
384-
// by preval so it can use ESModules too
385-
// but if b.js has dependencies then it cannot.
386-
var x = preval.require('./b.js')
387-
```
388-
389-
It's also notable that you can use preval in preval:
390-
391-
```javascript
392-
// this file can use ESModules
393-
var x = preval`
394-
// this string can use ESModules
395-
import b from /* preval */ 'b.js'
396-
// b can use ESModules (but not it's dependencies).
397-
`
398-
```
399-
400-
Again, this is admittedly pretty confusing, so it's strongly recommended that
401-
any files you preval are written in a way that they work in node without needing
402-
transpilation.
403-
404-
</details>
405-
406339
## FAQ
407340

408341
### How is this different from prepack?
409342

410-
[`prepack`][prepack] is intended to be run on your final bundle after you've run your
411-
webpack/etc magic on it. It does a TON of stuff, but the idea is that your code
412-
should work with or without prepack.
343+
[`prepack`][prepack] is intended to be run on your final bundle after you've run
344+
your webpack/etc magic on it. It does a TON of stuff, but the idea is that your
345+
code should work with or without prepack.
413346

414347
`babel-plugin-preval` is intended to let you write code that would _not_ work
415348
otherwise. Doing things like reading something from the file system are not
@@ -420,14 +353,15 @@ possible in the browser (or with prepack), but `preval` enables you to do this.
420353
This plugin was inspired by webpack's [val-loader][val-loader]. The benefit of
421354
using this over that loader (or any other loader) is that it integrates with
422355
your existing babel pipeline. This is especially useful for the server where
423-
you're probably not bundling your code with [`webpack`][webpack], but you may be using
424-
babel. (If you're not using either, configuring babel for this would be easier
425-
than configuring webpack for `val-loader`).
356+
you're probably not bundling your code with [`webpack`][webpack], but you may be
357+
using babel. (If you're not using either, configuring babel for this would be
358+
easier than configuring webpack for `val-loader`).
426359

427360
In addition, you can implement pretty much any webpack loader using
428361
`babel-plugin-preval`.
429362

430-
If you want to learn more, check `webpack` documentations about [`loaders`][webpack-loaders].
363+
If you want to learn more, check `webpack` documentations about
364+
[`loaders`][webpack-loaders].
431365

432366
## Inspiration
433367

src/__tests__/.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/__tests__/__snapshots__/index.js.snap

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const x = preval\`
1717
1818
const x = {
1919
"val": "# fixture\\n\\nThis is some file thing...\\n",
20-
"getSplit": function getSplit(splitDelimiter) {
20+
"getSplit": function (splitDelimiter) {
2121
return x.val.split(splitDelimiter);
2222
}
2323
};
@@ -34,19 +34,6 @@ const x = "<PROJECT_ROOT>/src/__tests__/index.js";
3434
3535
`;
3636
37-
exports[`preval can use preval in preval: can use preval in preval 1`] = `
38-
39-
var x = preval\`
40-
import message from /* preval */ './fixtures/es6'
41-
export default message
42-
\`
43-
44-
↓ ↓ ↓ ↓ ↓ ↓
45-
46-
var x = "this string used exported default";
47-
48-
`;
49-
5037
exports[`preval dynamic error: dynamic error 1`] = `
5138
5239
const x = preval\`module.exports = "\${dynamic}"\`
@@ -70,6 +57,16 @@ module.exports = undefined;
7057
7158
`;
7259
60+
exports[`preval handles transpiled modules (uses default export): handles transpiled modules (uses default export) 1`] = `
61+
62+
let one = preval.require('./fixtures/es6-identity.js', 1)
63+
64+
↓ ↓ ↓ ↓ ↓ ↓
65+
66+
let one = 1;
67+
68+
`;
69+
7370
exports[`preval import comment (with extras before): import comment (with extras before) 1`] = `
7471
7572
import x from /* this is extra stuff */ /* preval */ "./fixtures/compute-one.js"
@@ -242,32 +239,6 @@ preval\`module.exports = "foo"\`
242239
243240
`;
244241
245-
exports[`preval transpiled: transpiled 1`] = `
246-
247-
export default preval\`
248-
import path from 'path'
249-
export default path.join(__dirname, 'something/whatever.js').replace(__dirname, '')
250-
\`
251-
252-
↓ ↓ ↓ ↓ ↓ ↓
253-
254-
export default "/something/whatever.js";
255-
256-
`;
257-
258-
exports[`preval transpiles string contents: transpiles string contents 1`] = `
259-
260-
var x = preval\`
261-
import one from './fixtures/compute-one'
262-
export default one
263-
\`
264-
265-
↓ ↓ ↓ ↓ ↓ ↓
266-
267-
var x = 1;
268-
269-
`;
270-
271242
exports[`preval with flow after: with flow after 1`] = `
272243
273244
// @preval
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
export default acceptsArg
1+
/* eslint-disable */
2+
// codegen can't accept non-transpiled esmodules
3+
// but it can handle transpiled esmodules
4+
// so we're simulating a pre-transpiled module here
5+
Object.defineProperty(exports, '__esModule', {
6+
value: true,
7+
})
28

3-
function acceptsArg(arg) {
4-
return arg
9+
exports.default = function(input) {
10+
return input
511
}

src/__tests__/fixtures/es6.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/__tests__/fixtures/transpiled.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/__tests__/index.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ pluginTester({
3939
\`
4040
`,
4141
'a-bunch-of-checks': fixture('a-bunch-of-checks'),
42-
transpiled: fixture('transpiled'),
4342
'dynamic error': error(
4443
`const x = preval\`module.exports = "\${dynamic}"\``,
4544
),
@@ -93,17 +92,8 @@ pluginTester({
9392
// @preval
9493
/* comment */
9594
`),
96-
'transpiles string contents': `
97-
var x = preval\`
98-
import one from './fixtures/compute-one'
99-
export default one
100-
\`
101-
`,
102-
'can use preval in preval': `
103-
var x = preval\`
104-
import message from /* preval */ './fixtures/es6'
105-
export default message
106-
\`
95+
'handles transpiled modules (uses default export)': `
96+
let one = preval.require('./fixtures/es6-identity.js', 1)
10797
`,
10898
},
10999
})

src/helpers.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
const p = require('path')
2-
const requireFromString = require('require-from-string')
2+
const requireFromStringOfCode = require('require-from-string')
33
const objectToAST = require('./object-to-ast')
44

55
module.exports = {
66
getReplacement,
7-
transformAndRequire,
7+
requireFromString,
88
}
99

10-
function transformAndRequire({
10+
function requireFromString({
1111
string: stringToPreval,
12-
fileOpts,
12+
fileOpts: {filename},
1313
args = [],
14-
babel,
1514
}) {
16-
const {filename, plugins, presets} = fileOpts
17-
const {code} = babel.transform(stringToPreval, {
18-
filename,
19-
plugins,
20-
presets,
21-
})
22-
let mod = requireFromString(code, filename)
15+
let mod = requireFromStringOfCode(String(stringToPreval), filename)
2316
mod = mod && mod.__esModule ? mod.default : mod
2417

2518
if (typeof mod === 'function') {
@@ -39,6 +32,6 @@ function transformAndRequire({
3932
}
4033

4134
function getReplacement({string, fileOpts, args, babel}) {
42-
const mod = transformAndRequire({string, fileOpts, args, babel})
35+
const mod = requireFromString({string, fileOpts, args, babel})
4336
return objectToAST(mod, {babel, fileOptions: fileOpts})
4437
}

src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const p = require('path')
22
const fs = require('fs')
33
// const printAST = require('ast-pretty-print')
4-
const {getReplacement, transformAndRequire} = require('./helpers')
4+
const {getReplacement, requireFromString} = require('./helpers')
55

66
module.exports = prevalPlugin
77

@@ -88,10 +88,9 @@ function prevalPlugin(babel) {
8888
let argValues
8989
if (prevalComment !== 'preval') {
9090
const arg = prevalComment.replace(/preval\((.*)\)/, '$1').trim()
91-
const argValue = transformAndRequire({
91+
const argValue = requireFromString({
9292
string: `module.exports = ${arg}`,
9393
fileOpts,
94-
babel,
9594
})
9695
argValues = [argValue]
9796
}

0 commit comments

Comments
 (0)