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

Use module type "es2015" or newer instead of commonjs #28

Open
raysuelzer opened this issue Feb 23, 2021 · 4 comments
Open

Use module type "es2015" or newer instead of commonjs #28

raysuelzer opened this issue Feb 23, 2021 · 4 comments

Comments

@raysuelzer
Copy link

raysuelzer commented Feb 23, 2021

I'm wondering if there is any reason that this module is being built using "commonjs" in the tsconfig?

  "compilerOptions": {
    ...
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
    ...
}

CommonJS modules are not tree shakable by webpack which results in increased bundle sizes (and compiler warnings). (see: https://webpack.js.org/guides/tree-shaking/ )

For reference: Apollo client builds an es2015 module. I think this is probably a safe default. I don't imagine this would impact anything other than reduced bundle size for those importing the library.

Happy to put in a PR with this change, I opened another PR which will drop your bundle size by not exporting the entirety of lodash with your package.

@seanaguinaga
Copy link

I get this build error when using nextjs 12





> Build error occurred
14:09:07.538 | Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /vercel/path0/node_modules/lodash-es/lodash.js
14:09:07.539 | require() of ES modules is not supported.
14:09:07.542 | require() of /vercel/path0/node_modules/lodash-es/lodash.js from /vercel/path0/node_modules/@nerdwallet/apollo-cache-policies/dist/cache/InvalidationPolicyCache.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
14:09:07.543 | Instead rename lodash.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /vercel/path0/node_modules/lodash-es/package.json.
14:09:07.543

<br class="Apple-interchange-newline" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;">

@danReynolds
Copy link
Collaborator

I get this build error when using nextjs 12





> Build error occurred
14:09:07.538 | Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /vercel/path0/node_modules/lodash-es/lodash.js
14:09:07.539 | require() of ES modules is not supported.
14:09:07.542 | require() of /vercel/path0/node_modules/lodash-es/lodash.js from /vercel/path0/node_modules/@nerdwallet/apollo-cache-policies/dist/cache/InvalidationPolicyCache.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
14:09:07.543 | Instead rename lodash.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /vercel/path0/node_modules/lodash-es/package.json.
14:09:07.543

<br class="Apple-interchange-newline" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;">

This should be fixed in the latest version, let me know if it's not.

@seanaguinaga
Copy link

seanaguinaga commented Jan 27, 2022

It works for me

I use this server side and client side, maybe I can skip that on the server?, but I have to use transpiration to not get a server side error

// next.config.js
const withTM = require("next-transpile-modules")([
  "@nerdwallet/apollo-cache-policies",
]); // pass the modules you would like to see transpiled

/**
 * @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
 **/
const nextConfig = {
...
};

module.exports = withTM(nextConfig);

@danReynolds
Copy link
Collaborator

danReynolds commented Jan 27, 2022

It works for me

I use this server side and client side, maybe I can skip that on the server?, but I have to use transpiration to not get a server side error

// next.config.js
const withTM = require("next-transpile-modules")([
  "@nerdwallet/apollo-cache-policies",
]); // pass the modules you would like to see transpiled

/**
 * @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
 **/
const nextConfig = {
...
};

module.exports = withTM(nextConfig);

Gotcha, ya my next thing to do is support both common JS and ESM with a dual setup, before it was just common JS. I haven't done that before so anyone reading this who is familiar feel free to reach out or set up a PR :)

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

3 participants