Skip to content

Commit

Permalink
Merge branch 'master' into fix/sucrase-aliased-imports
Browse files Browse the repository at this point in the history
# By Tiger Oakes (2) and others
# Via GitHub
* master:
  feat(wasm): Switch to TypeScript & named exports (rollup#363)
  feat(node-resolve): Add default export (rollup#361)
  fix (sucrase): resolve directory imports (rollup#390)
  docs(typescript): update readme examples (rollup#391)

# Conflicts:
#	packages/sucrase/test/snapshots/test.js.md
#	packages/sucrase/test/snapshots/test.js.snap
#	packages/sucrase/test/test.js
#	pnpm-lock.yaml
  • Loading branch information
larrybotha committed May 20, 2020
2 parents ecccb3c + b899a82 commit 756da46
Show file tree
Hide file tree
Showing 21 changed files with 163 additions and 46 deletions.
6 changes: 4 additions & 2 deletions packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const defaults = {
};
export const DEFAULTS = deepFreeze(deepMerge({}, defaults));

export const nodeResolve = (opts = {}) => {
export function nodeResolve(opts = {}) {
const options = Object.assign({}, defaults, opts);
const { customResolveOptions, extensions, jail } = options;
const warnings = [];
Expand Down Expand Up @@ -258,4 +258,6 @@ export const nodeResolve = (opts = {}) => {
return idToPackageInfo.get(id);
}
};
};
}

export default nodeResolve;
5 changes: 3 additions & 2 deletions packages/node-resolve/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const DEFAULTS: {
resolveOnly: [];
};

export interface Options {
export interface RollupNodeResolveOptions {
/**
* If `true`, instructs the plugin to use the `"browser"` property in `package.json`
* files to specify alternative files to load for bundling. This is useful when
Expand Down Expand Up @@ -88,4 +88,5 @@ export interface Options {
/**
* Locate modules using the Node resolution algorithm, for using third party modules in node_modules
*/
export const nodeResolve: (options?: Options) => Plugin;
export function nodeResolve(options?: RollupNodeResolveOptions): Plugin;
export default nodeResolve;
8 changes: 6 additions & 2 deletions packages/sucrase/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ module.exports = function sucrase(opts = {}) {
resolveId(importee, importer) {
if (importer && /^[./]/.test(importee)) {
const resolved = path.resolve(importer ? path.dirname(importer) : process.cwd(), importee);
// resolve in the same order that TypeScript resolves modules
const resolvedFilename = [`${resolved}.ts`, `${resolved}/index.ts`].find((filename) =>
fs.existsSync(filename)
);

if (!fs.existsSync(resolved) && fs.existsSync(`${resolved}.ts`)) {
return `${resolved}.ts`;
if (resolvedFilename) {
return resolvedFilename;
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (a: number, b: number): number => a * b;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (c: number, d: number): number => c * d;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Should not be imported';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dirImport from './example-a';
import fileImport from './example-b';

t.snapshot(dirImport.toString());
t.snapshot(fileImport.toString());
14 changes: 9 additions & 5 deletions packages/sucrase/test/snapshots/test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@ Generated by [AVA](https://ava.li).
'() => React.createElement(\'div\', { id: "foo", __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 1}}, "hello world" )'

## converts flow
## converts jsx with custom jsxPragma

> Snapshot 1
'(a, b) => a * b'
'() => FakeReactCreateElement(\'div\', { id: "foo", __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 1}}, "hello world" )'

## converts typescript

> Snapshot 1
'(a, b) => a * b'

## converts jsx with custom jsxPragma
## converts typescript with aliases

> Snapshot 1
'() => FakeReactCreateElement(\'div\', { id: "foo", __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 1}}, "hello world" )'
'(a, b) => a * b'

## converts typescript with aliases
## resolves typescript directory imports

> Snapshot 1
'(a, b) => a * b'

> Snapshot 2
'(c, d) => c * d'
Binary file modified packages/sucrase/test/snapshots/test.js.snap
Binary file not shown.
15 changes: 15 additions & 0 deletions packages/sucrase/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,20 @@ test('converts typescript with aliases', async (t) => {
]
});
t.plan(1);

return testBundle(t, bundle);
});

test('resolves typescript directory imports', async (t) => {
const bundle = await rollup({
input: 'fixtures/typescript-resolve-directory/main.js',
plugins: [
sucrase({
transforms: ['typescript']
})
]
});
t.plan(2);

return testBundle(t, bundle);
});
6 changes: 3 additions & 3 deletions packages/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ Though it is not recommended, it is possible to configure this plugin to handle

```js
// rollup.config.js
import typescript from 'rollup-plugin-typescript';
import commonjs from 'rollup-plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';

export default {
input: './main.ts',
Expand Down Expand Up @@ -175,7 +175,7 @@ After adding `acorn-jsx` plugin, your Rollup config would look like the followin

```js
import jsx from 'acorn-jsx';
import typescript from 'rollup-plugin-typescript';
import typescript from '@rollup/plugin-typescript';

export default {
// … other options …
Expand Down
8 changes: 4 additions & 4 deletions packages/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ npm install @rollup/plugin-wasm --save-dev
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:

```js
import wasm from '@rollup/plugin-wasm';
import { wasm } from '@rollup/plugin-wasm';

export default {
input: 'src/index.js',
Expand Down Expand Up @@ -66,9 +66,9 @@ int main() {
Compile the file using `emscripten`, or the online [WasmFiddle](https://wasdk.github.io/WasmFiddle//) tool. Then import and instantiate the resulting file:

```js
import wasm from './sample.wasm';
import sample from './sample.wasm';

wasm({ ...imports }).then(({ instance }) => {
sample({ ...imports }).then(({ instance }) => {
console.log(instance.exports.main());
});
```
Expand All @@ -90,7 +90,7 @@ wasm({
This means that the exports can be accessed immediately.

```js
import module from './sample.wasm';
import sample from './sample.wasm';

const instance = sample({ ...imports });

Expand Down
8 changes: 6 additions & 2 deletions packages/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Jamen Marz <jamenmarz+gh@gmail.com>",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/wasm/#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/index",
"main": "dist/index.js",
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
Expand All @@ -29,6 +29,7 @@
},
"files": [
"dist",
"types",
"README.md",
"LICENSE"
],
Expand All @@ -46,6 +47,7 @@
"rollup": "^1.20.0||^2.0.0"
},
"devDependencies": {
"@rollup/plugin-typescript": "^4.1.1",
"del-cli": "^3.0.0",
"rollup": "^2.0.0",
"source-map": "^0.7.3"
Expand All @@ -61,5 +63,7 @@
"contributors": [
"Jamen Marz <jamenmarz+gh@gmail.com>",
"Colin Eberhardt <colin.eberhardt@gmail.com>"
]
],
"module": "dist/index.es.js",
"types": "types/index.d.ts"
}
17 changes: 7 additions & 10 deletions packages/wasm/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
const { builtinModules } = require('module');
import typescript from '@rollup/plugin-typescript';

const pkg = require('./package.json');
import { createConfig } from '../../shared/rollup.config';

const dependencies = Object.keys(pkg.dependencies || {});
import pkg from './package.json';

export default [
{
input: 'src/index.js',
output: { exports: 'named', file: 'dist/index.js', format: 'cjs' },
external: [...builtinModules, ...dependencies]
}
];
export default {
...createConfig(pkg),
plugins: [typescript()]
};
10 changes: 7 additions & 3 deletions packages/wasm/src/index.js → packages/wasm/src/index.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { readFile } from 'fs';
import { resolve } from 'path';

export default function wasm(options = {}) {
options = Object.assign({}, options); // eslint-disable-line no-param-reassign
import { Plugin } from 'rollup';

import { RollupWasmOptions } from '../types';

export function wasm(options: RollupWasmOptions = {}): Plugin {
const syncFiles = (options.sync || []).map((x) => resolve(x));

return {
Expand Down Expand Up @@ -49,13 +51,15 @@ export default function wasm(options = {}) {
}
`.trim(),

// eslint-disable-next-line consistent-return
transform(code, id) {
if (code && /\.wasm$/.test(id)) {
const src = Buffer.from(code, 'binary').toString('base64');
const sync = syncFiles.indexOf(id) !== -1;
return `export default function(imports){return _loadWasmModule(${+sync}, '${src}', imports)}`;
}
return null;
}
};
}

export default wasm;
15 changes: 5 additions & 10 deletions packages/wasm/test/test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { rollup } from 'rollup';
import test from 'ava';

// eslint-disable-next-line no-unused-vars, import/no-unresolved, import/extensions
import wasm from '../dist/index';
import { getCode } from '../../../util/test';

const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;
import wasm from '../';

const generateCode = async (bundle) => {
const { output } = await bundle.generate({ format: 'cjs' });
const [{ code }] = output;
return code;
};
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;

const testBundle = async (t, bundle) => {
const code = await generateCode(bundle);
const code = await getCode(bundle);
const func = new AsyncFunction('t', `let result;\n\n${code}\n\nreturn result;`);
return func(t);
};
Expand Down Expand Up @@ -75,7 +70,7 @@ try {
input: 'test/fixtures/worker.js',
plugins: [wasm()]
});
const code = await generateCode(bundle);
const code = await getCode(bundle);
const executeWorker = () => {
const worker = new Worker(code, { eval: true });
return new Promise((resolve, reject) => {
Expand Down
14 changes: 14 additions & 0 deletions packages/wasm/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Plugin } from 'rollup';

export interface RollupWasmOptions {
/**
* Specifies an array of strings that each represent a WebAssembly file to load synchronously.
*/
sync?: readonly string[];
}

/**
* 🍣 A Rollup which allows importing and bundling [WebAssembly modules](http://webassembly.org).
*/
export function wasm(options?: RollupWasmOptions): Plugin;
export default wasm;
44 changes: 44 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 756da46

Please sign in to comment.