Skip to content

Commit

Permalink
fix(prepare): only hande source file (#10400)
Browse files Browse the repository at this point in the history
* fix(prepare): only hande source file

* refactor: last index of

* chore: remove unused code

* chore: code style
  • Loading branch information
fz6m committed Feb 7, 2023
1 parent e7d0ad8 commit 3dcfc85
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 41 deletions.
3 changes: 3 additions & 0 deletions packages/preset-umi/src/features/clientLoader/clientLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ const loader: { [ext: string]: esbuild.Loader } = {
'.eot': 'file',
'.flac': 'file',
'.gif': 'file',
'.htm': 'file',
'.html': 'file',
'.ico': 'file',
'.icon': 'file',
'.jpeg': 'file',
'.jpg': 'file',
'.js': 'jsx',
Expand Down
3 changes: 3 additions & 0 deletions packages/preset-umi/src/features/mpa/extractExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export async function extractExports(opts: {
'.eot': 'text',
'.flac': 'text',
'.gif': 'text',
'.htm': 'text',
'.html': 'text',
'.ico': 'text',
'.icon': 'text',
'.jpeg': 'text',
'.jpg': 'text',
'.js': 'jsx',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ export function esbuildExternalPlugin(): Plugin {
return {
name: 'esbuildExternalPlugin',
setup(build) {
// externals extensions
externalsExtensions.forEach((ext) => {
// /\.abc?query$/
const filter = new RegExp(`\.${ext}(\\?.*)?$`);
build.onResolve({ filter }, () => {
build.onResolve({ filter: /.*/ }, (args) => {
// only handle js/ts file
if (!isSource(args.path)) {
return {
external: true,
};
});
});
// external deps
build.onResolve({ filter: /.*/ }, (args) => {
}

if (args.path.startsWith('.')) {
return null;
}
Expand Down Expand Up @@ -49,34 +45,21 @@ export function esbuildExternalPlugin(): Plugin {
};
}

const externalsExtensions = [
'aac',
'css',
'less',
'sass',
'scss',
'eot',
'flac',
'gif',
'html',
'htm',
'ico',
'icon',
'jpeg',
'jpg',
'json',
'md',
'mdx',
'mp3',
'mp4',
'ogg',
'otf',
'png',
'svg',
'ttf',
'wav',
'webm',
'webp',
'woff',
'woff2',
];
function parseExt(file: string) {
const ext = path.extname(file);
const idx = ext.indexOf('?');
if (idx > 0) {
return ext.slice(0, idx);
}
return ext;
}

const SOURCE_REG = /\.(t|j)sx?$/;
function isSource(file: string) {
if (SOURCE_REG.test(file)) {
return true;
}
// allow import without ext
const ext = parseExt(file);
return !ext;
}
5 changes: 4 additions & 1 deletion packages/preset-umi/src/features/ssr/builder/builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import esbuild from '@umijs/bundler-utils/compiled/esbuild';
import { isMonorepo, logger, aliasUtils } from '@umijs/utils';
import { aliasUtils, isMonorepo, logger } from '@umijs/utils';
import { resolve } from 'path';
import { IApi } from '../../../types';
import { absServerBuildPath, esbuildUmiPlugin } from '../utils';
Expand Down Expand Up @@ -71,7 +71,10 @@ export const loader: { [ext: string]: esbuild.Loader } = {
'.eot': 'file',
'.flac': 'file',
'.gif': 'file',
'.htm': 'file',
'.html': 'file',
'.ico': 'file',
'.icon': 'file',
'.jpeg': 'file',
'.jpg': 'file',
'.js': 'jsx',
Expand Down

0 comments on commit 3dcfc85

Please sign in to comment.