Skip to content

Commit 9aeddb0

Browse files
authored
Allow import statements via esnext (#290)
* install `rewrite-imports` * apply `rewrite-imports` behavior * add imports test & fixtures * update `esnext` readme
1 parent 141d302 commit 9aeddb0

File tree

6 files changed

+23
-12
lines changed

6 files changed

+23
-12
lines changed

packages/esnext/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const Script = require('vm').Script;
44
const dirname = require('path').dirname;
5+
const rImports = require('rewrite-imports');
56
const req = require('require-like');
67

78
const fn = 'function*';
@@ -10,18 +11,19 @@ const box = {};
1011
module.exports = function (file, data) {
1112
// setup mock env
1213
Object.assign(box, global);
13-
box.module = { exports:exports };
14+
box.module = { exports };
1415
box.exports = exports;
1516
box.require = req(file);
1617

1718
box.__dirname = dirname(file);
1819
box.__filename = file;
1920

2021
const scr = new Script(
21-
data.replace(new RegExp('await', 'gi'), 'yield')
22-
.replace(/export /gi, 'exports.')
23-
.replace(/default async function/gi, `default = ${fn}`)
24-
.replace(/async function(\s)?.+?(?=\()/gi, str => str.trim().split(' ').pop().concat(` = ${fn} `))
22+
rImports(data)
23+
.replace(/await/gi, 'yield')
24+
.replace(/export /gi, 'exports.')
25+
.replace(/default async function/gi, `default = ${fn}`)
26+
.replace(/async function(\s)?.+?(?=\()/gi, str => str.trim().split(' ').pop().concat(` = ${fn} `))
2527
);
2628
// `eval()` new content
2729
scr.runInNewContext(box);

packages/esnext/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"url": "https://lukeed.com"
1515
},
1616
"dependencies": {
17-
"require-like": "^0.1.2"
17+
"require-like": "^0.1.2",
18+
"rewrite-imports": "^1.0.0"
1819
},
1920
"devDependencies": {
2021
"bluebird": "^3.5.0"

packages/esnext/readme.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$ npm install --save-dev @taskr/esnext
99
```
1010

11-
**That's it!** :tada: You've now enabled `async`/`await` syntax for your `taskfile.js`!
11+
**That's it!** :tada: You've now enabled `async`/`await` and `import` syntax for your `taskfile.js`!
1212

1313
> **Note:** This will NOT compile your ES6 files into ES5. You must download and setup [`@taskr/babel`](https://npmjs.com/package/@taskr/babel) or [`@taskr/buble`](https://npmjs.com/package/@taskr/buble) for that.
1414
@@ -18,6 +18,7 @@ A `taskfile.js` may also include `require()` statements (not shown).
1818

1919
```js
2020
// taskfile.js
21+
import { foo, bar as baz } from './bat';
2122

2223
export default async function (task) {
2324
await task.source('src/*.js') // etc...

packages/esnext/test/fixtures/bar.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.bar = 'hello';
2+
3+
exports.baz = 'world';
+5-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
const aaa = 42;
2-
const foo = require('./foo');
2+
import foo from './foo';
3+
import { bar as quz, baz as qut } from './bar';
34

45
export default async function () {
56
await this.source('src/*.js').target('dist');
67
};
78

89
export async function foo () {
910
await this.clear('dist');
10-
return await this.start('bar', {val: aaa});
11+
return await this.start('bar', { val:aaa });
1112
}
1213

1314
export async function bar(o) {
1415
return `${foo()}: ${o ? o.val : aaa}`;
1516
}
1617

1718
export async function baz(one, two) {
18-
return {one, two};
19+
return { one, two };
1920
}
2021

2122
export async function bat (one, two) {
22-
return {one, two};
23+
return `${quz} ${qut}`;
2324
}

packages/esnext/test/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const hasYield = f => /yield/i.test(f.toString());
1313
const isGenerator = f => f.constructor.name === 'GeneratorFunction';
1414

1515
test('@taskr/esnext', co(function * (t) {
16-
t.plan(25);
16+
t.plan(26);
1717

1818
t.equal(typeof fn, 'function', 'export a function');
1919

@@ -42,4 +42,7 @@ test('@taskr/esnext', co(function * (t) {
4242

4343
const val2 = yield co(out.baz)('foo', 'bar');
4444
t.deepEqual(val2, {one: 'foo', two: 'bar'}, 'accepts & handles multiple parameters');
45+
46+
const val3 = yield co(out.bat)();
47+
t.equal(val3, 'hello world', 'handles aliased import partials correctly');
4548
}));

0 commit comments

Comments
 (0)