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

fix(webpack): allow transpilation of .cjs files #10240

Closed
wants to merge 1 commit into from

Conversation

gnuletik
Copy link

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

When trying to transpile a file from node_modules through the build.transpile property, files ending with .cjs were previously ignored.

This PR fixes it.

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (PR: #)
  • I have added tests to cover my changes (if not applicable, please state why)
  • All new and existing tests are passing.

@gnuletik
Copy link
Author

gnuletik commented Feb 1, 2022

@danielroe thanks for the approval !

For anyone interested in this feature before it gets merged, you can extend your webpack config like this :

// nuxt.config.ts
export default {
// ...
  build: {
    extend (config: any, { isDev, isLegacy }: any) {
      // https://github.com/nuxt/nuxt.js/pull/10240
      const babelConfig = {
        configFile: false,
        babelrc: false,
        cacheDirectory: true,
        plugins: [
          '@nuxtjs/composition-api/dist/babel-plugin'
        ],
        presets: [
          ['@nuxt/babel-preset-app/src/index.js', {
            corejs: { version: 3 },
            jsx: { compositionAPI: true }
          }]
        ],
        envName: 'client'
      }

      if (!isDev && isLegacy) {
        config.module.rules.push({
          test: /\.c?jsx?$/i,
          exclude: (file: string) => {
            file = file.split(/node_modules(.*)/)[1]

            // not exclude files outside node_modules
            if (!file) {
              return false
            }

            const cjsTranspile = [/defu/]

            // item in transpile can be string or regex object
            return !cjsTranspile.some(module => module.test(file))
          },
          loader: 'babel-loader',
          options: babelConfig
        })
      }
   }
// ...
}

You can edit cjsTranspile to update the list of modules to transpile.

@pi0
Copy link
Member

pi0 commented Jun 23, 2022

Thank you so much for making this PR @gnuletik ❤️ I'm closing it in favor of #10340 which has updated tests.

@pi0 pi0 closed this Jun 23, 2022
@kyumoon
Copy link

kyumoon commented Aug 9, 2022

@gnuletik thanks for your config code
in my case, i had to change the test regex to test: /\.cjsx?$/i,

@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants