Skip to content

Commit

Permalink
feat(mfsu): esbuild loader support mjs (#10970)
Browse files Browse the repository at this point in the history
* feat(mfsu): esbuild loader support mjs

* chore: code refactoring

* chore: 规则改为映射
  • Loading branch information
xierenyuan authored Apr 19, 2023
1 parent 5b684e9 commit 9aa7289
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/mfsu/src/loader/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ import { extname } from 'path';
import type { LoaderContext } from 'webpack';
import type { IEsbuildLoaderOpts } from '../types';

const LOADER_MAP = {
// js
js: 'js',
cjs: 'js',
mjs: 'js',
jsx: 'jsx',
cjsx: 'jsx',
mjsx: 'jsx',
// ts
ts: 'ts',
cts: 'ts',
mts: 'ts',
tsx: 'tsx',
ctsx: 'tsx',
mtsx: 'tsx',
} satisfies Record<string, EsbuildLoader>;

async function esbuildTranspiler(
this: LoaderContext<IEsbuildLoaderOpts>,
source: string,
Expand All @@ -17,12 +34,14 @@ async function esbuildTranspiler(
const transform = implementation?.transform || transformInternal;

const filePath = this.resourcePath;
const ext = extname(filePath).slice(1) as EsbuildLoader;

const ext = extname(filePath).slice(1) as keyof typeof LOADER_MAP;
const loader = LOADER_MAP[ext] ?? 'default';

const transformOptions = {
...otherOptions,
target: options.target ?? 'es2015',
loader: ext ?? 'js',
loader: loader as EsbuildLoader,
sourcemap: this.sourceMap,
sourcefile: filePath,
};
Expand Down

0 comments on commit 9aa7289

Please sign in to comment.