Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace babel preset-env utils with helper module imports #345

Merged
merged 2 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@averjs/babel-preset-app/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test('dynamic import', () => {

test('rest spread should use assign polyfill', async () => {
const { code } = transformFactory('const a = { ...b };');
await expect(code).toMatch('core-js/modules/es6.object.assign.js');
await expect(code).toMatch('core-js/modules/es6.object.assign');
});

test('regenerator runtime should be included on client', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@babel/helper-module-imports';
19 changes: 16 additions & 3 deletions packages/@averjs/babel-preset-app/lib/polyfillsPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { PluginObj } from '@babel/core';
import { NodePath, PluginObj } from '@babel/core';
import { Program } from '@babel/types';
import { BabelOptions } from '.';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { addSideEffect } = require('@babel/helper-module-imports');

function getModulePath(mod: string) {
const modPath =
mod === 'regenerator-runtime'
? 'regenerator-runtime/runtime'
: `core-js/modules/${mod}`;
return modPath;
}

function createImport(path: NodePath<Program>, mod: string) {
return addSideEffect(path, getModulePath(mod));
}

// add polyfill imports to the first file encountered.
export default function (): PluginObj {
Expand All @@ -16,8 +31,6 @@ export default function (): PluginObj {
}

const { polyfills } = state.opts as BabelOptions;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { createImport } = require('@babel/preset-env/lib/utils');
// imports are injected in reverse order
polyfills
?.slice()
Expand Down