-
Notifications
You must be signed in to change notification settings - Fork 27.7k
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
Add support for transpile packages installed via npm #3319
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { resolve, join, sep } from 'path' | ||
import { resolve, join, sep, dirname } from 'path' | ||
import { createHash } from 'crypto' | ||
import { realpathSync, existsSync } from 'fs' | ||
import { realpathSync, existsSync, readFileSync } from 'fs' | ||
import webpack from 'webpack' | ||
import glob from 'glob-promise' | ||
import WriteFilePlugin from 'write-file-webpack-plugin' | ||
|
@@ -203,6 +203,24 @@ export default async function createCompiler (dir, { buildId, dev = false, quiet | |
}) | ||
} | ||
|
||
function getPackageJsonPath (filePath) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using https://www.npmjs.com/package/find-root instead function getPackageJson (filePath) {
const pkgRoot = findRoot(filePath)
const packageJsonPath = path.resolve(pkgRoot, 'package.json')
return packageJsonPath
} |
||
let filePathDirname = dirname(filePath) | ||
if (filePathDirname === dir) { | ||
return join(dir, 'package.json') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to check whether this file exists or probably return |
||
} | ||
|
||
while (dir !== filePathDirname) { | ||
const packageJsonPath = join(filePathDirname, 'package.json') | ||
if (existsSync(packageJsonPath)) { | ||
return packageJsonPath | ||
} | ||
filePathDirname = dirname(filePathDirname) | ||
} | ||
} | ||
|
||
const packageJsonToCopy = {} | ||
const copiedPackageJson = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if this is persisted across builds (when rebuilding) |
||
|
||
const rules = (dev ? [{ | ||
test: /\.(js|jsx)(\?[^?]*)?$/, | ||
loader: 'hot-self-accept-loader', | ||
|
@@ -224,6 +242,12 @@ export default async function createCompiler (dir, { buildId, dev = false, quiet | |
include: [dir, nextPagesDir], | ||
exclude (str) { | ||
if (shouldTranspileModule(str)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works for sure. And if that requires a ES6+ module, how we are going to handle it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works for SSR since node will look for
During my testing Later today or tomorrow (when I have time basically) I will add those to the PR together with the changes to the readme. As I said this solution still doesn't work with linked packages though - I will try to tackle that at the end and see if it is easy/doable to fix. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's pretty interesting. Nice work. Looking forward to take this in. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @arunoda edit: in order for this to work we need to copy the |
||
if (!packageJsonToCopy[str]) { | ||
const packageJsonPath = getPackageJsonPath(str) | ||
if (packageJsonPath) { | ||
packageJsonToCopy[str.replace(dir, '')] = packageJsonPath | ||
} | ||
} | ||
return false | ||
} | ||
|
||
|
@@ -243,12 +267,22 @@ export default async function createCompiler (dir, { buildId, dev = false, quiet | |
} | ||
|
||
const filePath = file.slice(0, -(from.length)) + to | ||
|
||
if (existsSync(filePath)) { | ||
throw new Error(`Both ${from} and ${to} file found. Please make sure you only have one of both.`) | ||
} | ||
} | ||
}, | ||
copyPackageJson (interpolatedName) { | ||
const packageJsonPath = packageJsonToCopy[interpolatedName.replace(/^dist/, '')] | ||
|
||
if (packageJsonPath && !copiedPackageJson[packageJsonPath]) { | ||
copiedPackageJson[packageJsonPath] = true | ||
return { | ||
interpolatedName: join('dist', packageJsonPath.replace(dir, '')), | ||
content: readFileSync(packageJsonPath, 'utf-8') | ||
} | ||
} | ||
}, | ||
// By default, our babel config does not transpile ES2015 module syntax because | ||
// webpack knows how to handle them. (That's how it can do tree-shaking) | ||
// But Node.js doesn't know how to handle them. So, we have to transpile them here. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should/could be a separate loader