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

allow cjs config files on type module projects #8253

Conversation

mrdaniellewis
Copy link
Contributor

↪️ Pull Request

Allow plugins with .js config files to also work with a .cjs extension.

If you have a project with "type": "module" then currently it is impossible to have a javascript config file for a plugin. Either you get a error telling you require isn't allowed using modules, or if you use modules you get an error telling you modules can't be used with require. If you use a cjs file you get a JSON parse error.

This PR allows loadConfig to load a cjs file as js file instead of trying to load it as a json file.

It also updates all plugins that accept a js config file to also accept a cjs config file.

Also see previously reported #6830

💻 Examples

For example if you use the pug transformer, you can now have a config file called:

pug.config.cjs.

Previously it would only allow pug.config.js

🚨 Test instructions

Create a project with "type": "module" in the package.json.

Attempt to use a javascript config file with, for example, a pug template.

// .pugrc.js
module.exports = {
  filters: {
    'custom-filter': function (text, options) {
      return 'FILTERED: ' + text;
    }
  }
}
// index.pug
:custom-filter()
    Hello!

If the .pugrc.js is renamed .pugrc.cjs this will now work without an error.

✔️ PR Todo

  • Added/updated unit tests for this change
  • Filled out test instructions (In case there aren't any unit tests)
  • Included links to related issues/PRs

Copy link
Member

@devongovett devongovett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me

packages/core/utils/src/config.js Outdated Show resolved Hide resolved
@devongovett devongovett merged commit 085a7aa into parcel-bundler:v2 Jul 31, 2022
@optimistiks
Copy link

gorakong pushed a commit that referenced this pull request Nov 3, 2022
* upstream/v2: (22 commits)
  Cross compile toolchains are built into docker image already
  Also fix release build
  Update centos node version
  v2.7.0
  Changelog for v2.7.0
  Use placeholder expression when replacing unused symbols (#8358)
  Lint (#8359)
  Add support for errorRecovery option in @parcel/transformer-css (#8352)
  VS Code Extension for Parcel (#8139)
  Add multi module compilation for elm (#8076)
  Bump terser from 5.7.2 to 5.14.2 (#8322)
  Bump node-forge from 1.2.1 to 1.3.0 (#8271)
  allow cjs config files on type module projects (#8253)
  inject script for hmr when there is only normal script in html (#8330)
  feat: support react refresh for @emotion/react (#8205)
  Update index.d.ts (#8293)
  remove charset from jsloader script set (#8346)
  Log resolved targets in verbose log level (#8254)
  Fix missing module in large app from Experimental Bundler (#8303)
  [Symbol Propagation] Non-deterministic bundle hashes (#8212)
  ...
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

Successfully merging this pull request may close these issues.

3 participants