Next.js: 添加用于跨页面添加内容的入口文件(如 polyfills)。

创建于 2017-02-03  ·  3评论  ·  资料来源: vercel/next.js

// in next.config.js

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

// or

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

最有用的评论

这是目前的解决方法:

pages/_document.js添加import "../entry/server.js";并将下面的代码添加到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;
  },
};

所有3条评论

这是目前的解决方法:

pages/_document.js添加import "../entry/server.js";并将下面的代码添加到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;
  },
};

上面的代码创建了一个嵌套的entry数组。 这是需要的,还是应该有传播:

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;
  },
};

是的@jcheroske您的建议是正确的。
我会更新原始代码。

我正在关闭这个问题。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

havefive picture havefive  ·  3评论

wagerfield picture wagerfield  ·  3评论

timneutkens picture timneutkens  ·  3评论

irrigator picture irrigator  ·  3评论

swrdfish picture swrdfish  ·  3评论