Skip to content

Commit

Permalink
new: Polyfill node modules for other platforms. (#26)
Browse files Browse the repository at this point in the history
* Add dep.

* Start outputs.

* Remove sourcemaps.

* Test polyfills.

* Test generators.

* Test async/await.
  • Loading branch information
milesj authored Feb 7, 2021
1 parent ee9bc3c commit 89f8294
Show file tree
Hide file tree
Showing 14 changed files with 14,445 additions and 86 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"rimraf": "^3.0.2",
"rollup": "^2.38.5",
"rollup-plugin-node-externals": "^2.2.0",
"rollup-plugin-polyfill-node": "^0.5.0",
"rollup-plugin-visualizer": "^4.2.0",
"semver": "^7.3.4",
"spdx-license-list": "^6.4.0",
Expand Down
19 changes: 8 additions & 11 deletions src/babel/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ export function getBabelOutputConfig(
): ConfigStructure {
const plugins: PluginItem[] = [];
const presets: PluginItem[] = [];
const isFuture = support !== 'legacy' && support !== 'stable';

// ENVIRONMENT

Expand All @@ -168,27 +167,25 @@ export function getBabelOutputConfig(

// Use `Object.assign` when available
// https://babeljs.io/docs/en/babel-plugin-transform-destructuring#usebuiltins
if (isFuture) {
if (support === 'current' || support === 'experimental') {
plugins.push(
[resolve('@babel/plugin-transform-destructuring'), { useBuiltIns: true }],
[resolve('@babel/plugin-proposal-object-rest-spread'), { useBuiltIns: true }],
);
}

if (platform === 'browser' || platform === 'native') {
if ((platform === 'browser' || platform === 'native') && support !== 'experimental') {
// Transform async/await into Promises
plugins.push([
resolve('babel-plugin-transform-async-to-promises'),
{ inlineHelpers: true, target: isFuture ? 'es6' : 'es5' },
{ inlineHelpers: true, target: 'es5' },
]);

// Transform generators for legacy
if (!isFuture) {
plugins.push([
resolve('@babel/plugin-transform-runtime'),
{ helpers: false, regenerator: true, useESModules: format === 'esm' },
]);
}
// Transform generators to Regenerator
plugins.push([
resolve('@babel/plugin-transform-runtime'),
{ helpers: false, regenerator: true, useESModules: format === 'esm' },
]);
}

// Support env expression shortcuts
Expand Down
10 changes: 9 additions & 1 deletion src/rollup/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import { ModuleFormat, OutputOptions, RollupOptions } from 'rollup';
import externals from 'rollup-plugin-node-externals';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import visualizer from 'rollup-plugin-visualizer';
import { getBabelInputPlugin, getBabelOutputPlugin } from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
Expand Down Expand Up @@ -138,6 +139,7 @@ export function getRollupOutputConfig(
export function getRollupConfig(artifact: BundleArtifact, features: FeatureFlags): RollupOptions {
const inputPath = artifact.getInputPath();
const packagePath = path.resolve(artifact.package.packageJsonPath.path());
const isNode = artifact.platform === 'node';

const config: RollupOptions = {
cache: artifact.cache,
Expand All @@ -148,7 +150,7 @@ export function getRollupConfig(artifact: BundleArtifact, features: FeatureFlags
plugins: [
// Mark all dependencies in `package.json` as external
externals({
builtins: true,
builtins: isNode,
deps: true,
devDeps: true,
optDeps: true,
Expand All @@ -172,6 +174,12 @@ export function getRollupConfig(artifact: BundleArtifact, features: FeatureFlags
treeshake: true,
};

// Polyfill node modules when platform is not node
if (!isNode) {
// @ts-expect-error Types dont match
config.plugins!.unshift(nodePolyfills());
}

// Analyze the bundle for debugging purposes
if (features.analyze) {
config.plugins!.push(
Expand Down
8 changes: 2 additions & 6 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
FORMATS_NATIVE,
FORMATS_NODE,
PLATFORMS,
SUPPORTS,
} from './constants';
import {
AnalyzeType,
Expand Down Expand Up @@ -52,12 +53,7 @@ const format = string<Format>(DEFAULT_FORMAT)

// SUPPORT

const support = string<Support>(DEFAULT_SUPPORT).oneOf([
'legacy',
'stable',
'current',
'experimental',
]);
const support = string<Support>(DEFAULT_SUPPORT).oneOf(SUPPORTS);

// BLUEPRINTS

Expand Down
13 changes: 13 additions & 0 deletions tests/__fixtures__/examples/async-await.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export async function wait() {
return new Promise((resolve) => {
setTimeout(resolve, 100);
});
}

export async function run() {
try {
await wait();
} catch {
throw new Error();
}
}
7 changes: 7 additions & 0 deletions tests/__fixtures__/examples/generators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function* gen() {}

export function runGen() {
for (const iterator of gen()) {
console.log(iterator);
}
}
10 changes: 10 additions & 0 deletions tests/__fixtures__/examples/node-polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import path from 'path';
import { EventEmitter } from 'events';

export const test = path.join('foo', 'bar');

export const emitter = new EventEmitter();

export class Example extends EventEmitter {
log() {}
}
3 changes: 3 additions & 0 deletions tests/__fixtures__/examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "examples"
}
Loading

0 comments on commit 89f8294

Please sign in to comment.