如何使用 esbuild 设置 React 的全局属性
根据文档,使用 define
是设置构建环境属性的建议方法。
当我运行我的应用程序时,出现进程未定义错误。
我的 esconfig 如下:
await build({
entryPoints: ['./src/index.tsx'],
outdir: './build',
bundle: true,
incremental: true,
metafile: true,
target: 'es6',
loader: { '.png': 'file' },
minify: !dev,
sourcemap: 'inline',
define: { 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development') },
plugins: [
sassPlugin(),
svg(),
copy({
resolveFrom: 'cwd',
assets: {
from: ['./public/*'],
to: ['./build2/*'],
},
}),
copy({
resolveFrom: 'cwd',
assets: {
from: ['./public/images/*'],
to: ['./build2/images/*'],
},
}),
],
watch: dev
? {
onRebuild: (error) => {
if (error) {
console.error(error);
} else {
console.log('rebuild done');
}
},
}
: false,
});
我也欢迎其他设置全局属性来控制配置的方法。
According to the docs using define
is the suggested way of setting the env properties for a build.
When I run my app with I get a process is not defined error.
My esconfig is as follows:
await build({
entryPoints: ['./src/index.tsx'],
outdir: './build',
bundle: true,
incremental: true,
metafile: true,
target: 'es6',
loader: { '.png': 'file' },
minify: !dev,
sourcemap: 'inline',
define: { 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development') },
plugins: [
sassPlugin(),
svg(),
copy({
resolveFrom: 'cwd',
assets: {
from: ['./public/*'],
to: ['./build2/*'],
},
}),
copy({
resolveFrom: 'cwd',
assets: {
from: ['./public/images/*'],
to: ['./build2/images/*'],
},
}),
],
watch: dev
? {
onRebuild: (error) => {
if (error) {
console.error(error);
} else {
console.log('rebuild done');
}
},
}
: false,
});
I'm also open to other ways of setting global properties to control the configuration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到
process is not Defined
错误,因为您实际上没有在define
选项中定义process
,而是定义了process.env。 NODE_ENV
。这可能违反直觉,但这就是
define
选项的使用方式。它实际上并没有定义变量,只是将所有看起来像define
key 的标记替换为相应的值。You are getting
process is not defined
error because you actually do not defineprocess
indefine
option, you do defineprocess.env.NODE_ENV
.It may be counterintuitively but it is how
define
option is used. It doesn't actually defines variable and just replaces all tokens that look likedefine
key with corresponding value.我在构建开始之前编写了一个配置/环境文件。
然后我的反应应用程序只需要知道 env 文件所在的位置。
I've resorted to writing a config/env file before the build starts.
My react app then just needs to know where the env file is located.