Skip to content

Commit 51adeef

Browse files
authored
feat: mfsu support assets files (umijs#6981)
1 parent 8473510 commit 51adeef

3 files changed

Lines changed: 25 additions & 16 deletions

File tree

packages/bundler-webpack/src/getConfig/getConfig.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ export default async function getConfig(
298298
}),
299299
);
300300

301+
const staticDir = mfsu ? 'mf-static' : 'static';
302+
301303
// prettier-ignore
302304
webpackConfig.module
303305
.rule('images')
@@ -306,13 +308,13 @@ export default async function getConfig(
306308
.loader(require.resolve('@umijs/deps/compiled/url-loader'))
307309
.options({
308310
limit: config.inlineLimit || 10000,
309-
name: 'static/[name].[hash:8].[ext]',
311+
name: `${staticDir}/[name].[hash:8].[ext]`,
310312
// require 图片的时候不用加 .default
311313
esModule: false,
312314
fallback: {
313315
loader: require.resolve('@umijs/deps/compiled/file-loader'),
314316
options: {
315-
name: 'static/[name].[hash:8].[ext]',
317+
name: `${staticDir}/[name].[hash:8].[ext]`,
316318
esModule: false,
317319
},
318320
}
@@ -325,7 +327,7 @@ export default async function getConfig(
325327
.use('file-loader')
326328
.loader(require.resolve('@umijs/deps/compiled/file-loader'))
327329
.options({
328-
name: 'static/[name].[hash:8].[ext]',
330+
name: `${staticDir}/[name].[hash:8].[ext]`,
329331
esModule: false,
330332
});
331333

@@ -336,7 +338,7 @@ export default async function getConfig(
336338
.use('file-loader')
337339
.loader(require.resolve('@umijs/deps/compiled/file-loader'))
338340
.options({
339-
name: 'static/[name].[hash:8].[ext]',
341+
name: `${staticDir}/[name].[hash:8].[ext]`,
340342
esModule: false,
341343
});
342344

packages/preset-built-in/src/plugins/features/mfsu/getDepReExportContent.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,34 @@ export async function getDepReExportContent(opts: {
88
filePath?: string;
99
importFrom: string;
1010
}) {
11+
// Support CSS
1112
if (
1213
opts.filePath &&
1314
/\.(css|less|scss|sass|stylus|styl)$/.test(opts.filePath)
1415
) {
1516
return `import '${opts.importFrom}';`;
1617
}
1718

19+
// Support Assets Files
20+
if (
21+
opts.filePath &&
22+
/\.(json|svg|png|jpe?g|gif|webp|ico|eot|woff|woff2|ttf|txt|text|md)$/.test(
23+
opts.filePath,
24+
)
25+
) {
26+
return `
27+
import _ from '${opts.importFrom}';
28+
export default _;`.trim();
29+
}
30+
1831
try {
19-
if (opts.filePath && !/\.(js|jsx|mjs|ts|tsx|json)$/.test(opts.filePath)) {
32+
if (opts.filePath && !/\.(js|jsx|mjs|ts|tsx)$/.test(opts.filePath)) {
2033
const matchResult = opts.filePath.match(/\.([a-zA-Z]+)$/);
2134
throw new Error(
2235
`${matchResult ? matchResult[0] : 'file type'} not support!`,
2336
);
2437
}
2538

26-
if (isJsonFile(opts.filePath)) {
27-
return [`import _ from '${opts.importFrom}';`, `export default _;`].join(
28-
'\n',
29-
);
30-
}
31-
3239
const { exports, isCJS } = await parseWithCJSSupport(
3340
opts.content,
3441
opts.filePath,
@@ -137,10 +144,6 @@ export const cjsModeEsmParser = (code: string) => {
137144
.map((result) => result[1]);
138145
};
139146

140-
function isJsonFile(filePath?: string) {
141-
return filePath && filePath.endsWith('.json');
142-
}
143-
144147
async function parseWithCJSSupport(content: string, filePath?: string) {
145148
// Support tsx and jsx
146149
if (filePath && /\.(tsx|jsx)$/.test(filePath)) {

packages/preset-built-in/src/plugins/features/mfsu/mfsu.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,11 @@ export default function (api: IApi) {
253253
api.addBeforeMiddlewares(() => {
254254
return (req, res, next) => {
255255
const path = req.path;
256-
if (path.startsWith('/mf-va_') || path.startsWith('/mf-dep_')) {
256+
if (
257+
path.startsWith('/mf-va_') ||
258+
path.startsWith('/mf-dep_') ||
259+
path.startsWith('/mf-static/')
260+
) {
257261
depBuilder.onBuildComplete(() => {
258262
const filePath = path
259263
.replace(new RegExp(`^${publicPath}`), '/')

0 commit comments

Comments
 (0)