Skip to content

Commit

Permalink
fix: add support for shimming regenerator-runtime when using swc as a…
Browse files Browse the repository at this point in the history
… transpiler
  • Loading branch information
wessberg committed Nov 15, 2021
1 parent e84d21c commit 1513fc5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/constant/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export const D_TS_EXTENSION = `.d${TS_EXTENSION}`;
export const D_TS_MAP_EXTENSION = `.d${TS_EXTENSION}${SOURCE_MAP_EXTENSION}`;
export const TSBUILDINFO_EXTENSION = `.tsbuildinfo`;

export const ROLLUP_PLUGIN_MULTI_ENTRY_LEGACY = "\0rollup-plugin-multi-entry:entry-point";
export const ROLLUP_PLUGIN_VIRTUAL_PREFIX = `\0virtual:`;

export const KNOWN_EXTENSIONS = new Set([
D_TS_EXTENSION,
D_TS_MAP_EXTENSION,
Expand Down Expand Up @@ -41,6 +44,8 @@ export const BABELRC_FILENAME = ".babelrc";

export const REGENERATOR_RUNTIME_NAME_1 = `${BABEL_RUNTIME_PREFIX_1}regenerator/index.js`;
export const REGENERATOR_RUNTIME_NAME_2 = `${BABEL_RUNTIME_PREFIX_2}regenerator/index.js`;
export const REGENERATOR_RUNTIME_NAME_3 = `regenerator-runtime/runtime.js`;
export const REGENERATOR_RUNTIME_VIRTUAL_SRC = `${ROLLUP_PLUGIN_VIRTUAL_PREFIX}regenerator-runtime`;
export const BABEL_REQUIRE_RUNTIME_HELPER_ESM_REGEXP_1 = new RegExp(`(require\\(["'\`])(${BABEL_RUNTIME_PREFIX_1}helpers/esm/[^"'\`]*)["'\`]\\)`);
export const BABEL_REQUIRE_RUNTIME_HELPER_ESM_REGEXP_2 = new RegExp(`(require\\(["'\`])(${BABEL_RUNTIME_PREFIX_2}helpers/esm/[^"'\`]*)["'\`]\\)`);
export const BABEL_IMPORT_RUNTIME_HELPER_CJS_REGEXP_1 = new RegExp(`(import\\s+\\w+\\s+from\\s+["'\`])(${BABEL_RUNTIME_PREFIX_1}helpers/[^"'/\`]*)["'\`]`);
Expand Down Expand Up @@ -106,5 +111,3 @@ export const FORCED_BABEL_PLUGIN_TRANSFORM_RUNTIME_OPTIONS = {
useESModules: true
} as const;

export const ROLLUP_PLUGIN_MULTI_ENTRY_LEGACY = "\0rollup-plugin-multi-entry:entry-point";
export const ROLLUP_PLUGIN_VIRTUAL_PREFIX = `\0virtual:`;
14 changes: 9 additions & 5 deletions src/plugin/typescript-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {getForcedCompilerOptions} from "../util/get-forced-compiler-options/get-
import {getSourceDescriptionFromEmitOutput} from "../util/get-source-description-from-emit-output/get-source-description-from-emit-output";
import {emitDiagnostics} from "../service/emit/diagnostics/emit-diagnostics";
import {getSupportedExtensions} from "../util/get-supported-extensions/get-supported-extensions";
import {ensureRelative, getExtension, isBabelHelper, isMultiEntryModule, isSwcHelper} from "../util/path/path-util";
import {ensureRelative, getExtension, isBabelHelper, isMultiEntryModule, isRegeneratorRuntime, isSwcHelper} from "../util/path/path-util";
import {takeBundledFilesNames} from "../util/take-bundled-filenames/take-bundled-filenames";
import {TypescriptPluginOptions} from "./typescript-plugin-options";
import {getPluginOptions} from "../util/plugin-options/get-plugin-options";
import {getBrowserslist} from "../util/get-browserslist/get-browserslist";
import {ResolveCache} from "../service/cache/resolve-cache/resolve-cache";
import {JSON_EXTENSION, REGENERATOR_RUNTIME_NAME_1, REGENERATOR_RUNTIME_NAME_2, ROLLUP_PLUGIN_VIRTUAL_PREFIX} from "../constant/constant";
import {JSON_EXTENSION, REGENERATOR_RUNTIME_VIRTUAL_SRC, ROLLUP_PLUGIN_VIRTUAL_PREFIX} from "../constant/constant";
import {REGENERATOR_SOURCE} from "../lib/regenerator/regenerator";
import {createFilter} from "@rollup/pluginutils";
import {mergeTransformers} from "../util/merge-transformers/merge-transformers";
Expand Down Expand Up @@ -436,6 +436,10 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
// Don't proceed if there is no parent (in which case this is an entry module)
if (parent == null) return null;

if (id === "regenerator-runtime") {
return REGENERATOR_RUNTIME_VIRTUAL_SRC;
}

const resolveResult = host.resolve(id, parent);

const pickedResolveResult = resolveResult == null ? undefined : pickResolvedModule(resolveResult, false);
Expand All @@ -448,9 +452,9 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
* being used
*/
load(this: PluginContext, id: string): string | null {
const normalizedId = path.normalize(id);
// Return the alternative source for the regenerator runtime if that file is attempted to be loaded
if (normalizedId.endsWith(REGENERATOR_RUNTIME_NAME_1) || normalizedId.endsWith(REGENERATOR_RUNTIME_NAME_2)) {
if (isRegeneratorRuntime(path.normalize(id))) {

return REGENERATOR_SOURCE;
}
return null;
Expand Down Expand Up @@ -512,4 +516,4 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
}
}
};
}
}
8 changes: 8 additions & 0 deletions src/util/path/path-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
KNOWN_EXTENSIONS,
NODE_MODULES,
NODE_MODULES_MATCH_PATH,
REGENERATOR_RUNTIME_NAME_1,
REGENERATOR_RUNTIME_NAME_2,
REGENERATOR_RUNTIME_NAME_3,
REGENERATOR_RUNTIME_VIRTUAL_SRC,
ROLLUP_PLUGIN_MULTI_ENTRY_LEGACY,
SWC_HELPERS_PREFIX,
TSLIB_NAME
Expand Down Expand Up @@ -48,6 +52,10 @@ export function isBabelHelper(p: string): boolean {
return includesBabelEsmHelper(p) || isBabelCjsHelper(p);
}

export function isRegeneratorRuntime (p: string): boolean {
return p.endsWith(REGENERATOR_RUNTIME_NAME_1) || p.endsWith(REGENERATOR_RUNTIME_NAME_2) || p.endsWith(REGENERATOR_RUNTIME_NAME_3) || p === REGENERATOR_RUNTIME_VIRTUAL_SRC;
}

/**
* Returns true if the given path represents a swc helper
*/
Expand Down

0 comments on commit 1513fc5

Please sign in to comment.