-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Only add forwarding bindings to import names when: - not the entrypoint or - importing for phase > 0 - importing a compiletime binding
- Loading branch information
1 parent
91a74b4
commit 5ad1113
Showing
8 changed files
with
135 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { writeFileSync, mkdtempSync } from 'fs'; | ||
import { execSync } from 'child_process'; | ||
import { compile } from '../../src/sweet'; | ||
import NodeLoader from '../../src/node-loader'; | ||
import { sync as rimraf } from 'rimraf'; | ||
|
||
// write each module to a temp dir, compile them with sweet, | ||
// compile with babel to get commonjs modules, then assert on | ||
// the console log in main.js | ||
export function compileToCjs(t, inputModules, expected) { | ||
let tmp = mkdtempSync(`./`); | ||
let loader = new NodeLoader(tmp); | ||
for (let fname of Object.keys(inputModules)) { | ||
let path = `${tmp}/${fname}`; | ||
writeFileSync(path, inputModules[fname], 'utf8'); | ||
let outfile = compile(path, loader); | ||
writeFileSync(`${tmp}/${fname}.esm`, outfile.code, 'utf8'); | ||
execSync( | ||
`babel --out-file ${tmp}/${fname} ${tmp}/${fname}.esm --no-babelrc --plugins=transform-es2015-modules-commonjs`, | ||
); | ||
} | ||
let result = execSync(`node ${tmp}/main.js`).toString().trim(); | ||
t.is(result, expected); | ||
rimraf(tmp); | ||
} | ||
compileToCjs.title = (title, inputStore, expected) => | ||
`${title} | ||
${Array.from(Object.entries(inputStore)) | ||
.map(([modName, modSrc]) => `${modName}\n----\n${modSrc}\n----`) | ||
.join('\n')} | ||
> ${expected}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { compileToCjs } from './_helpers'; | ||
import test from 'ava'; | ||
|
||
test( | ||
'exporting and importing a variable works', | ||
compileToCjs, | ||
{ | ||
'mod.js': ` | ||
'lang sweet.js'; | ||
export var x = 'foo'; | ||
`, | ||
|
||
'main.js': ` | ||
'lang sweet.js'; | ||
import { x } from './mod.js'; | ||
console.log(x); | ||
`, | ||
}, | ||
'foo', | ||
); | ||
|
||
test( | ||
'exporting and importing a name passing through macro expansion works', | ||
compileToCjs, | ||
{ | ||
'mod.js': ` | ||
'lang sweet.js'; | ||
syntax m = ctx => { | ||
let first = ctx.next().value; | ||
let second = ctx.next().value; | ||
return #\`var \${first} = \${second}\`; | ||
} | ||
export m x 'foo' | ||
`, | ||
|
||
'main.js': ` | ||
'lang sweet.js'; | ||
import { x } from './mod.js'; | ||
console.log(x); | ||
`, | ||
}, | ||
'foo', | ||
); |