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

chore(esbuild): set utf-8 charset for esbuild #10021

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/bundler-esbuild/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export async function build(opts: IOpts) {
return await buildWithESBuild({
entryPoints: opts.entry,
bundle: true,
charset: 'utf8',
format: opts.format || 'iife',
logLevel: 'error',
// splitting: true,
Expand Down
6 changes: 5 additions & 1 deletion packages/bundler-esbuild/src/plugins/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ export function style({
namespace: inlineStyle ? 'style-content' : 'file',
},
async (args) => {
const options = { entryPoints: [args.path], ...opt };
const options: BuildOptions = {
entryPoints: [args.path],
charset: 'utf8',
...opt,
};
const { errors, warnings, outputFiles } = await esbuild.build(
options,
);
Expand Down
1 change: 1 addition & 0 deletions packages/bundler-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function parseModuleSync(opts: { content: string; path: string }) {
if (opts.path.endsWith('.tsx') || opts.path.endsWith('.jsx')) {
try {
content = transformSync(content, {
charset: 'utf8',
loader: extname(opts.path).slice(1) as Loader,
format: 'esm',
}).code;
Expand Down
1 change: 1 addition & 0 deletions packages/bundler-vite/src/plugins/svgr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default function svgrPlugin(
}

const result = await transform(componentCode, {
charset: 'utf8',
loader: 'jsx',
sourcefile: id,
sourcemap: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/bundler-webpack/src/config/compressPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export async function addCompressPlugin(opts: IOpts) {
if (jsMinifier === JSMinifier.esbuild) {
minify = TerserPlugin.esbuildMinify;
terserOptions = {
charset: 'utf8',
target: esbuildTarget,
// remove all comments
legalComments: 'none',
Expand Down Expand Up @@ -87,6 +88,7 @@ export async function addCompressPlugin(opts: IOpts) {
if (cssMinifier === CSSMinifier.esbuild) {
cssMinify = CSSMinimizerWebpackPlugin.esbuildMinify;
minimizerOptions = {
charset: 'utf8',
target: esbuildTarget,
} as EsbuildOpts;
} else if (cssMinifier === CSSMinifier.cssnano) {
Expand Down
1 change: 1 addition & 0 deletions packages/bundler-webpack/src/loader/svgr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const tranformSvg = callbackify(
async (contents: string, options: Config, state: Partial<State>) => {
const jsCode = await transform(contents, options, state);
const result = await defaultEsbuildTransform(jsCode, {
charset: 'utf8',
loader: 'tsx',
target: 'es2015',
});
Expand Down
1 change: 1 addition & 0 deletions packages/mfsu/src/dep/getModuleExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function getModuleExports({
if (filePath && /\.(tsx|jsx)$/.test(filePath)) {
content = (
await transform(content, {
charset: 'utf8',
sourcemap: false,
sourcefile: filePath,
format: 'esm',
Expand Down
4 changes: 3 additions & 1 deletion packages/mfsu/src/loader/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { init, parse } from '@umijs/bundler-utils/compiled/es-module-lexer';
import {
Loader as EsbuildLoader,
transform as transformInternal,
type TransformOptions,
} from '@umijs/bundler-utils/compiled/esbuild';
import { extname } from 'path';
import type { LoaderContext } from 'webpack';
Expand All @@ -19,8 +20,9 @@ async function esbuildTranspiler(
const filePath = this.resourcePath;
const ext = extname(filePath).slice(1) as EsbuildLoader;

const transformOptions = {
const transformOptions: TransformOptions = {
...otherOptions,
charset: 'utf8',
target: options.target ?? 'es2015',
loader: ext ?? 'js',
sourcemap: this.sourceMap,
Expand Down
2 changes: 2 additions & 0 deletions packages/plugins/src/utils/modelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class Model {
// to reduce unexpected ast problem
const loader = extname(this.file).slice(1) as Loader;
const result = transformSync(content, {
charset: 'utf8',
loader,
sourcemap: false,
minify: false,
Expand Down Expand Up @@ -180,6 +181,7 @@ export class ModelUtils {
// to reduce unexpected ast problem
const loader = extname(file).slice(1) as Loader;
const result = transformSync(content, {
charset: 'utf8',
loader,
sourcemap: false,
minify: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default async function (api: IApi, apiRoutes: IRoute[]) {
);

await esbuild.build({
charset: 'utf8',
format: 'cjs',
platform: 'node',
bundle: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default async function (api: IApi, apiRoutes: IRoute[]) {
const pkg = require(join(api.cwd, './package.json'));

await esbuild.build({
charset: 'utf8',
format: 'cjs',
platform: 'node',
bundle: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ${clientLoaderDefines.join('\n')}
// core/loader.js 会被 core/route.ts 引用,将每个 route 的 clientLoader 注入进去
api.onBeforeCompiler(async () => {
await esbuild.build({
charset: 'utf8',
format: 'esm',
platform: 'browser',
target: 'esnext',
Expand Down
1 change: 1 addition & 0 deletions packages/preset-umi/src/features/mpa/extractExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export async function extractExports(opts: {
exportName: string;
}) {
const res = await esbuild.build({
charset: 'utf8',
format: 'cjs',
platform: 'browser',
target: 'esnext',
Expand Down
4 changes: 2 additions & 2 deletions packages/preset-umi/src/features/ssr/builder/assets-loader.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import esbuild from '@umijs/bundler-utils/compiled/esbuild';
import type { Plugin } from '@umijs/bundler-utils/compiled/esbuild';
import { winPath } from '@umijs/utils';
import { existsSync, readFileSync, statSync } from 'fs';
import { join } from 'path';
import { ensureLastSlash } from './css-loader';

const NAMESPACE = 'staticAssets';

export function assetsLoader(opts: { cwd: string }): esbuild.Plugin {
export function assetsLoader(opts: { cwd: string }): Plugin {
const assetsFilter = /\.(png|jpg|jpeg|gif|woff|woff2|ttf|eot|mp3|mp4)$/;
return {
name: 'assets-loader',
Expand Down
1 change: 1 addition & 0 deletions packages/preset-umi/src/features/ssr/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function build(opts: { api: IApi; watch?: boolean }) {
// TODO: 支持通用的 alias
// TODO: external all import from package.json.dependencies
await esbuild.build({
charset: 'utf8',
format: 'cjs',
platform: 'node',
target: 'esnext',
Expand Down
4 changes: 2 additions & 2 deletions packages/preset-umi/src/features/ssr/builder/css-loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import esbuild from '@umijs/bundler-utils/compiled/esbuild';
import type { Plugin } from '@umijs/bundler-utils/compiled/esbuild';
import { parcelCSS } from '@umijs/bundler-webpack/dist/parcelCSS';
import { winPath } from '@umijs/utils';
import { readFileSync } from 'fs';
Expand Down Expand Up @@ -30,7 +30,7 @@ export function getClassNames(code: Buffer, filename: string) {
return Object.keys(exports || {});
}

export function cssLoader(opts: { cwd: string }): esbuild.Plugin {
export function cssLoader(opts: { cwd: string }): Plugin {
return {
name: 'css-loader',
setup(build) {
Expand Down
7 changes: 5 additions & 2 deletions packages/preset-umi/src/features/ssr/builder/less-loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import esbuild, { PartialMessage } from '@umijs/bundler-utils/compiled/esbuild';
import type {
PartialMessage,
Plugin,
} from '@umijs/bundler-utils/compiled/esbuild';
import less from '@umijs/bundler-utils/compiled/less';
import { winPath } from '@umijs/utils';
import { readFileSync } from 'fs';
Expand All @@ -8,7 +11,7 @@ import { ensureLastSlash, getClassNames, hashString } from './css-loader';
export const lessLoader = (opts: {
cwd: string;
lessOptions?: Less.Options;
}): esbuild.Plugin => {
}): Plugin => {
const { lessOptions = {} } = opts;
return {
name: 'less-loader',
Expand Down
4 changes: 2 additions & 2 deletions packages/preset-umi/src/features/ssr/builder/svg-loader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import esbuild from '@umijs/bundler-utils/compiled/esbuild';
import type { Plugin } from '@umijs/bundler-utils/compiled/esbuild';
import { readFileSync } from 'fs';
import { dirname, resolve } from 'path';

// esbuild plugin for umi.server.js bundler to handle svg assets
function svgLoader(opts: { cwd: string }): esbuild.Plugin {
function svgLoader(opts: { cwd: string }): Plugin {
opts;
return {
name: 'svg-loader',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class AutoUpdateSrcCodeCache {
await esBuild({
entryPoints: files,
bundle: false,
charset: 'utf8',
outdir: this.cachePath,
outbase: this.srcPath,
loader: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ async function esbuildTransform(
await esBuild({
entryPoints: files,
bundle: false,
charset: 'utf8',
outdir: opts.cachePath,
outbase: opts.srcPath,
loader: {
Expand Down
1 change: 1 addition & 0 deletions packages/preset-umi/src/libs/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export async function getContent(path: string) {
// es-module-lexer don't support jsx
if (path.endsWith('.tsx') || path.endsWith('.jsx')) {
content = transformSync(content, {
charset: 'utf8',
loader: extname(path).slice(1) as Loader,
format: 'esm',
}).code;
Expand Down
1 change: 1 addition & 0 deletions packages/testing/src/transformers/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const createTransformer = (
const result = transformSync(rawCode, {
...options,
...(config.globals['jest-esbuild'] as UserOptions),
charset: 'utf8',
loader: userOptions.loader || (extname(path).slice(1) as Loader),
sourcefile: path,
sourcesContent: false,
Expand Down