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

Where to use this #1

Closed
ephemer opened this issue Jun 3, 2019 · 1 comment
Closed

Where to use this #1

ephemer opened this issue Jun 3, 2019 · 1 comment

Comments

@ephemer
Copy link

ephemer commented Jun 3, 2019

Hi, thanks for the scripts. Just wondering where to use them? Wasn’t aware we could use npm modules on CF workers?

@shalomvolchok
Copy link
Collaborator

Yes, you can use npm modules on a CF worker. Just use Webpack to bundle one final script.

If this was your worker script worker.js:

import cloudflareEdgeProxy from "cloudflare-edge-proxy";

const config = {
    defaultBackend: "https://example.com"
};

const proxy = cloudflareEdgeProxy(config);

addEventListener("fetch", event => {
    event.respondWith(proxy(event));
});

Then you could build a single bundle for CF with a Webpack config something like this:

module.exports = {
    entry: { worker: "./worker" },
    target: "webworker",
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: [
                            [
                                "@babel/preset-env",
                                {
                                    targets: { chrome: "70" }
                                }
                            ]
                        ]
                    }
                }
            }
        ]
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants