javascript - How to make webpack not use the window object when bundling? - Stack Overflow

I'm making a React ponent library to abstract out some ponents I use in multiple projects. Some pr

I'm making a React ponent library to abstract out some ponents I use in multiple projects. Some projects are made with CRA, some with Gatsby, some might be something else, etc. I used the Neutrino.js framework/toolchain as it was linked on the React docs site, but the issue I ran into is that by default the output files of the build all use the window object, which causes gatsby build to break as window doesn't exist in Node/SSR. Is there a way to make Neutrino/webpack output a bundle that doesn't use window? While searching for a solution and paring to other libraries it seems that ESM is the best but I'm not sure how to get webpack to use it, I think it's currently not supported. Is there another tool I should be using for this?

I'm making a React ponent library to abstract out some ponents I use in multiple projects. Some projects are made with CRA, some with Gatsby, some might be something else, etc. I used the Neutrino.js framework/toolchain as it was linked on the React docs site, but the issue I ran into is that by default the output files of the build all use the window object, which causes gatsby build to break as window doesn't exist in Node/SSR. Is there a way to make Neutrino/webpack output a bundle that doesn't use window? While searching for a solution and paring to other libraries it seems that ESM is the best but I'm not sure how to get webpack to use it, I think it's currently not supported. Is there another tool I should be using for this?

Share Improve this question asked Aug 6, 2020 at 0:23 ROODAYROODAY 8321 gold badge9 silver badges27 bronze badges 1
  • Where would I put the ` import window from 'window';` statement? If I put it in my ponent that gets built in the library, I get a cannot resolve window, and the same happens when I put it inside the app that imports my ponent. (and if I ignore it I get react error 321) – ROODAY Commented Aug 6, 2020 at 2:19
Add a ment  | 

1 Answer 1

Reset to default 9

Add globalObject configuration to your webpack configuration:

output: {
    globalObject: "this",
  },

The default is window

For example:

To make UMD build available on both browsers and Node.js, set output.globalObject option to 'this'.

module.exports = {
  // ...
  output: {
    library: 'myLib',
    libraryTarget: 'umd',
    filename: 'myLib.js',
    globalObject: 'this'
  }
};

-From docs

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744276925a4566375.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信