Skip to content

Commit

Permalink
--minify/config.minify for scripts (#811)
Browse files Browse the repository at this point in the history
feat: Added `minify` as a configuration option and a cli arg, which will minify code for `dev` and `publish`

resolves #785
  • Loading branch information
JacobMGEvans authored Apr 16, 2022
1 parent d2f5ebb commit 8c2c7b7
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/young-bulldogs-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

feat: Added `minify` as a configuration option and a cli arg, which will minify code for `dev` and `publish`

resolves #785
67 changes: 67 additions & 0 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2176,6 +2176,73 @@ export default{
`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});

it("should minify the script when `--minify` is true (sw)", async () => {
writeWranglerToml({
main: "./index.js",
});
fs.writeFileSync(
"./index.js",
`export
default {
fetch() {
return new Response( "hello Cpt Picard" )
}
}
`
);

mockUploadWorkerRequest({
expectedEntry: 'fetch(){return new Response("hello Cpt Picard")',
});

mockSubDomainRequest();
await runWrangler("publish index.js --minify");
expect(std.out).toMatchInlineSnapshot(`
"Uploaded test-name (TIMINGS)
Published test-name (TIMINGS)
test-name.test-sub-domain.workers.dev"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should minify the script when `minify` in config is true (esm)", async () => {
writeWranglerToml({
main: "./index.js",
legacy_env: false,
env: {
testEnv: {
minify: true,
},
},
});
fs.writeFileSync(
"./index.js",
`export
default {
fetch() {
return new Response( "hello Cpt Picard" )
}
}
`
);

mockUploadWorkerRequest({
env: "testEnv",
expectedType: "esm",
legacyEnv: false,
expectedEntry: `fetch(){return new Response("hello Cpt Picard")`,
});

mockSubDomainRequest();
await runWrangler("publish -e testEnv index.js");
expect(std.out).toMatchInlineSnapshot(`
"Uploaded test-name (testEnv) (TIMINGS)
Published test-name (testEnv) (TIMINGS)
testEnv.test-name.test-sub-domain.workers.dev"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
});

describe("durable object migrations", () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/wrangler/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export async function bundleWorker(
rules: Config["rules"];
watch?: esbuild.WatchMode;
tsconfig: string | undefined;
minify: boolean | undefined;
}
): Promise<BundleResult> {
const {
Expand All @@ -36,6 +37,7 @@ export async function bundleWorker(
rules,
watch,
tsconfig,
minify,
} = options;
const entryDirectory = path.dirname(entry.file);
const moduleCollector = createModuleCollector({
Expand All @@ -62,6 +64,7 @@ export async function bundleWorker(
external: ["__STATIC_CONTENT_MANIFEST"],
format: "esm",
sourcemap: true,
minify,
metafile: true,
conditions: ["worker", "browser"],
...(process.env.NODE_ENV && {
Expand Down
6 changes: 6 additions & 0 deletions packages/wrangler/src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ interface EnvironmentInheritable {
upload?: DeprecatedUpload;
};

/**
* Minify the script before uploading.
* @inheritable
*/
minify: boolean | undefined;

/**
* TODO: remove this as it has been deprecated.
*
Expand Down
9 changes: 9 additions & 0 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export function normalizeAndValidateConfig(
"boolean"
);

validateOptionalProperty(
diagnostics,
"",
"minify",
rawConfig.minify,
"boolean"
);

// TODO: set the default to false to turn on service environments as the default
const isLegacyEnv =
(args as { "legacy-env": boolean | undefined })["legacy-env"] ??
Expand Down Expand Up @@ -802,6 +810,7 @@ function normalizeAndValidateEnvironment(
}
),
zone_id: rawEnv.zone_id,
minify: rawEnv.minify,
};

return environment;
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/dev/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type DevProps = {
compatibilityDate: string;
compatibilityFlags: string[] | undefined;
usageModel: "bundled" | "unbound" | undefined;
minify: boolean | undefined;
build: {
command?: string | undefined;
cwd?: string | undefined;
Expand Down Expand Up @@ -97,6 +98,7 @@ export function DevImplementation(props: DevProps): JSX.Element {
jsxFragment: props.jsxFragment,
serveAssetsFromWorker: !!props.public,
tsconfig: props.tsconfig,
minify: props.minify,
});

// only load the UI if we're running in a supported environment
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/dev/use-esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function useEsbuild({
rules,
serveAssetsFromWorker,
tsconfig,
minify,
}: {
entry: Entry;
destination: string | undefined;
Expand All @@ -34,6 +35,7 @@ export function useEsbuild({
rules: Config["rules"];
serveAssetsFromWorker: boolean;
tsconfig: string | undefined;
minify: boolean | undefined;
}): EsbuildBundle | undefined {
const [bundle, setBundle] = useState<EsbuildBundle>();
const { exit } = useApp();
Expand Down Expand Up @@ -69,6 +71,7 @@ export function useEsbuild({
rules,
watch: watchMode,
tsconfig,
minify,
});

// Capture the `stop()` method to use as the `useEffect()` destructor.
Expand Down Expand Up @@ -104,6 +107,7 @@ export function useEsbuild({
rules,
tsconfig,
exit,
minify,
]);
return bundle;
}
11 changes: 11 additions & 0 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ export async function main(argv: string[]): Promise<void> {
.option("experimental-enable-local-persistence", {
describe: "Enable persistence for this session (only for local mode)",
type: "boolean",
})
.option("minify", {
describe: "Minify the script",
type: "boolean",
});
},
async (args) => {
Expand Down Expand Up @@ -857,6 +861,7 @@ export async function main(argv: string[]): Promise<void> {
zone={zone}
rules={getRules(config)}
legacyEnv={isLegacyEnv(config)}
minify={args.minify ?? config.minify}
build={config.build || {}}
initialMode={args.local ? "local" : "remote"}
jsxFactory={args["jsx-factory"] || config.jsx_factory}
Expand Down Expand Up @@ -1008,6 +1013,10 @@ export async function main(argv: string[]): Promise<void> {
.option("tsconfig", {
describe: "Path to a custom tsconfig.json file",
type: "string",
})
.option("minify", {
describe: "Minify the script",
type: "boolean",
});
},
async (args) => {
Expand Down Expand Up @@ -1061,6 +1070,7 @@ export async function main(argv: string[]): Promise<void> {
routes: args.routes,
assetPaths,
legacyEnv: isLegacyEnv(config),
minify: args.minify,
experimentalPublic: args["experimental-public"] !== undefined,
});
}
Expand Down Expand Up @@ -1257,6 +1267,7 @@ export async function main(argv: string[]): Promise<void> {
zone={undefined}
legacyEnv={isLegacyEnv(config)}
build={config.build || {}}
minify={undefined}
initialMode={args.local ? "local" : "remote"}
jsxFactory={config.jsx_factory}
jsxFragment={config.jsx_fragment}
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Props = {
jsxFragment: string | undefined;
tsconfig: string | undefined;
experimentalPublic: boolean;
minify: boolean | undefined;
};

function sleep(ms: number) {
Expand All @@ -54,6 +55,8 @@ export default async function publish(props: Props): Promise<void> {
const jsxFactory = props.jsxFactory || config.jsx_factory;
const jsxFragment = props.jsxFragment || config.jsx_fragment;

const minify = props.minify ?? config.minify;

const scriptName = props.name;
assert(
scriptName,
Expand Down Expand Up @@ -105,6 +108,7 @@ export default async function publish(props: Props): Promise<void> {
jsxFragment,
rules: props.rules,
tsconfig: props.tsconfig ?? config.tsconfig,
minify,
}
);

Expand Down

0 comments on commit 8c2c7b7

Please sign in to comment.