Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit fe994af

Browse files
committed
Set --env for type = "webpack"/"rust" builds
1 parent 2534818 commit fe994af

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export class MiniflareCore<
308308
const configDir = path.dirname(configPath);
309309

310310
// Add build configuration for webpack and rust builds
311-
populateBuildConfig(config, configDir);
311+
populateBuildConfig(config, configDir, configEnv);
312312

313313
options = splitWranglerConfig(
314314
this.#plugins,

packages/core/src/plugins/build.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ export class BuildPlugin extends Plugin<BuildOptions> implements BuildOptions {
8484

8585
export function populateBuildConfig(
8686
config: WranglerConfig,
87-
configDir: string
87+
configDir: string,
88+
configEnv?: string
8889
): void {
8990
// If there's already a build configuration, or this isn't a "webpack"/"rust"
9091
// type project, leave the config as is
@@ -97,16 +98,20 @@ export function populateBuildConfig(
9798
config.build = { cwd: configDir, upload: { dir: "" } };
9899
assert(config.build.upload); // TypeScript gets annoyed if this isn't here
99100

101+
// Make sure to pass the correct --env to `wrangler build` so the correct
102+
// webpack_config is loaded
103+
const env = configEnv ? ` --env ${configEnv}` : "";
104+
100105
if (config.type === "webpack") {
101-
config.build.command = "wrangler build";
106+
config.build.command = `wrangler build${env}`;
102107
config.build.upload.main = path.join("worker", "script.js");
103108
} else if (config.type === "rust") {
104109
// This script will be included in the root index.js bundle, but rust.mjs
105110
// will be in the plugins subdirectory
106111
const __filename = fileURLToPath(import.meta.url);
107112
const __dirname = path.dirname(__filename);
108113
const rustScript = path.join(__dirname, "plugins", "rust.js");
109-
config.build.command = `wrangler build && ${process.execPath} ${rustScript}`;
114+
config.build.command = `wrangler build${env} && ${process.execPath} ${rustScript}`;
110115
config.build.upload.main = path.join("worker", "generated", "script.js");
111116

112117
// Add wasm binding, script.wasm will be created by rustScript
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
name = "webpack-worker"
22
type = "webpack"
33
compatibility_date = "2021-09-28"
4+
5+
# This environment is only here so Wrangler doesn't complain there aren't any
6+
# when passing "--env dev"
7+
[env.dev.vars]
8+
KEY = "value"

packages/core/test/plugins/build.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,14 @@ webpackTest(
153153
await rimrafPromise(path.join(webpackPath, "worker"));
154154
const mf = useMiniflare(
155155
{ BuildPlugin },
156-
{ wranglerConfigPath: path.join(webpackPath, "wrangler.toml") }
156+
{
157+
wranglerConfigPath: path.join(webpackPath, "wrangler.toml"),
158+
wranglerConfigEnv: "dev",
159+
}
157160
);
158-
await mf.getPlugins(); // Resolves once worker has been built
161+
const plugins = await mf.getPlugins(); // Resolves once worker has been built
162+
// Check correct env used
163+
t.is(plugins.BuildPlugin.buildCommand, "wrangler build --env dev");
159164
t.true(existsSync(path.join(webpackPath, "worker", "script.js")));
160165

161166
const res = await mf.dispatchFetch("http://localhost:8787/");

0 commit comments

Comments
 (0)