Next.js: Add entry file for adding something across pages (like polyfills).

Created on 3 Feb 2017  路  3Comments  路  Source: vercel/next.js

// in next.config.js

module.exports = {
  entry: "./entry",
};

// or

module.exports = {
  entry: {
    client: "./entry/client",
    server: "./entry/server",
  },
};

Most helpful comment

Here is a workaround for now:

Adding import "../entry/server.js"; in pages/_document.js and adding the code below to next.config.js.

module.exports = {
  webpack: (config) => {
    const entryFactory = config.entry;
    config.entry = () => (
      entryFactory()
        .then((entry) => {
          entry["main.js"] = [
            "./entry/client.js",
            ...entry["main.js"],
          ];
          return entry;
        })
    );
    return config;
  },
};

All 3 comments

Here is a workaround for now:

Adding import "../entry/server.js"; in pages/_document.js and adding the code below to next.config.js.

module.exports = {
  webpack: (config) => {
    const entryFactory = config.entry;
    config.entry = () => (
      entryFactory()
        .then((entry) => {
          entry["main.js"] = [
            "./entry/client.js",
            ...entry["main.js"],
          ];
          return entry;
        })
    );
    return config;
  },
};

The above code makes a nested entry array. Is that what's needed, or should there be a spread:

module.exports = {
  webpack: (config) => {
    const entryFactory = config.entry;
    config.entry = () => (
      entryFactory()
        .then((entry) => {
          entry["main.js"] = [
            "./entry/client.js",
            ...entry["main.js"], // <-- Flatten the array?
          ];
          return entry;
        })
    );
    return config;
  },
};

Yep @jcheroske your suggestion is correct.
I'll update the original code.

And I'm closing this issue.

Was this page helpful?
0 / 5 - 0 ratings