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

babel-loader doesn't run on symlinked files #3898

Closed
vjpr opened this issue Feb 26, 2018 · 9 comments
Closed

babel-loader doesn't run on symlinked files #3898

vjpr opened this issue Feb 26, 2018 · 9 comments

Comments

@vjpr
Copy link

vjpr commented Feb 26, 2018

https://github.com/zeit/next.js/blob/1bcd2e0575c40b577380b0bc5ad5104111cbf510/server/build/webpack.js#L181-L186

The include: [dir] means that only files in the current tree can be transpiled.

Say you have a dir like:

/app
  - next.config.js
  - packages
    - foo
    - symlinked-package -> /src/symlinked-package

The symlinked package is resolved to its "realpath" (/src/symlinked-package instead of /app/packages/symlinked-package) by webpack. The include: [dir] prevents symlinked dirs from being transpiled.

I am wondering what is the reason for having the include option?

Your Environment

Tech Version
next 5
node 8
OS macOS
browser Chrome
etc

Workaround

function supportSymlinkedFilesInNextBabelLoader(config) {
  const nextBabelLoader = config.module.rules.find(({use, include, test}) => {
    if (!use) return
    return use.loader === 'babel-loader' && test.toString() === '/\\.+(js|jsx)$/'
  })
  if (!nextBabelLoader) return
  nextBabelLoader.include = undefined
}
@jthegedus
Copy link
Contributor

@vjpr The discussions here #3018 and here #3732 seem related and might be useful to you.

@majelbstoat
Copy link

majelbstoat commented Apr 26, 2018

@vjpr: having a similar issue with typescript outside of the main root. Managed to solve that part with a modified version of your snippet, thanks! Do any of the modules you're importing themselves import npm packages? I'm having a follow-on issue where, for example, 'immutablejs' cannot be found, because it's looking for it relative to the "real" home of the symlinked code. Trying to figure out whether that's a typescript, ts-loader, or webpack related thing.

For people also having ts issues, here's the modified snippet I'm using:

function supportSymlinkedFilesInNextTypescriptLoader(config) {
  const nextTypescriptLoader = config.module.rules.find(({ use, include, test }) => {
    const isTypescriptLoader = use && use.length && use.find(({ loader }) => loader === 'ts-loader')
    return isTypescriptLoader && test.toString() === '/\\.+(ts|tsx)$/'
  })
  if (!nextTypescriptLoader) return
  nextTypescriptLoader.include = undefined
}

Update: I got this working end-to-end by also setting resolve.symlinks to false. (https://webpack.js.org/configuration/resolve/#resolve-symlinks)

if (!config.resolve) {
  config.resolve = {}
}
config.resolve.symlinks = false

@majelbstoat
Copy link

majelbstoat commented May 20, 2018

Unfortunately, this no longer seems to work for Next 6 and next-plugin-typescript 1.0.0, which moved to using babel-loader as well.

The files are found, and executed, but if the file is outside the main tree, typescript compilation does not happen:

 error  in ./lib/editor/constants/BlockType.ts

 Syntax Error: enum is a reserved word (1:0)

 > 1 | enum BlockType {
     | ^
   2 |   Image = 'image',
   3 |   Text = 'text',

where most of the code resides under ./app and is compiled correctly. If I put exactly the same file under './app/lib, there's no issues.

The updated function I'm using is:

function supportSymlinkedFilesInNextBabelLoader(config) {
  config.module.rules.forEach(({ use, include, test }, i) => {
    const isBabelLoader = use && use.loader === 'babel-loader'
    const isTSFile = test.toString() === '/\\.(ts|tsx)$/'
    const isJSFile = test.toString() === '/\\.(js|jsx)$/'
    if (isBabelLoader && (isTSFile || isJSFile)) {
      delete config.module.rules[i].include
    }
  })
}

Explicitly setting include to have both paths, rather than deleting it, does not work either. Have tried lots of different approaches to get this to work, but no luck. Will be stuck on Next 5.1 until there's a workaround.

@majelbstoat
Copy link

majelbstoat commented Jun 4, 2018

For others struggling with this, in Next 6 the loader is now next-babel-loader not babel-loader. And, you'll need to make sure your .babelrc file is in the root folder so that it is used for both the app-specific code, and the common code (or have one .babelrc in each location.)

With those changes, I have it working for next 6.0.3 and next typescript plugin 1.0.1.

@mstn
Copy link

mstn commented Jun 11, 2018

@majelbstoat thanks for digging out. I wonder if you can share your config files. I am using Next 6 with Typescript. I am facing the same problem with symlinks (lerna).

My settings for the Next app

// next.config.js
const withTypescript = require('@zeit/next-typescript'); // v 1.0.1

module.exports = withTypescript();
// .babelrc
{
    "presets": [
        "next/babel",
        "@zeit/next-typescript/babel"
    ]
}

I copied .babelrc inside symlinked packages without success.

@majelbstoat
Copy link

My babelrc is the same as yours. However, I have a single babelrc file sitting in my root directory. You may need to run with NODE_PRESERVE_SYMLINKS=1. My nodemon looks like this:

  "execMap": {
    "ts": "NODE_PRESERVE_SYMLINKS=1 ts-node --project tsconfig.server.json"
  }

(I'm using a custom server for routing.)

@mstn
Copy link

mstn commented Jun 12, 2018

@majelbstoat thanks. However, as far as I can tell, ts-node should not use babel loader.

@majelbstoat
Copy link

ts-node runs the custom server. webpack/nextjs use babel loader. But symlinks don't work without that environment variable for me 🤷‍♂️ .

@timneutkens
Copy link
Member

Tracking this here: #706

@lock lock bot locked as resolved and limited conversation to collaborators Mar 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants