This repository was archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Use internal node globals impl #93
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# from .gitignore | ||
node_modules/ | ||
.eslintcache | ||
.nyc_output | ||
coverage | ||
# Local Netlify folder | ||
.netlify | ||
|
||
src/node-compat/assets/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export var browser = true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default typeof global !== "undefined" | ||
? global | ||
: typeof self !== "undefined" | ||
? self | ||
: typeof window !== "undefined" | ||
? window | ||
: {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const { randomBytes } = require("crypto"); | ||
const { dirname } = require("path"); | ||
|
||
const inject = require("@rollup/plugin-inject"); | ||
|
||
const PROCESS_PATH = require.resolve("process-es6"); | ||
const BUFFER_PATH = require.resolve("buffer-es6"); | ||
const GLOBAL_PATH = require.resolve("./assets/global.js"); | ||
const BROWSER_PATH = require.resolve("./assets/browser.js"); | ||
const DIRNAME = "\0node-globals:dirname"; | ||
const FILENAME = "\0node-globals:filename"; | ||
|
||
/** @type {{ [str: string]: string | [string, string] }} */ | ||
const injections = { | ||
"process.nextTick": [PROCESS_PATH, "nextTick"], | ||
"process.browser": [BROWSER_PATH, "browser"], | ||
"Buffer.isBuffer": [BUFFER_PATH, "isBuffer"], | ||
global: GLOBAL_PATH, | ||
process: PROCESS_PATH, | ||
Buffer: [BUFFER_PATH, "Buffer"], | ||
__filename: FILENAME, | ||
__dirname: DIRNAME, | ||
}; | ||
|
||
/** @returns {import("rollup").Plugin} */ | ||
function nodeGlobals() { | ||
const dirs = new Map(); | ||
return { | ||
...inject({ | ||
modules: injections, | ||
}), | ||
name: "rollup-plugin-node-globals-netlify", | ||
load(id) { | ||
if (dirs.has(id)) { | ||
return `export default '${dirs.get(id)}'`; | ||
} | ||
}, | ||
resolveId: (importee, importer) => { | ||
if (importee === DIRNAME) { | ||
let id = randomBytes(15).toString("hex"); | ||
dirs.set(id, dirname(importer)); | ||
return id; | ||
} | ||
if (importee === FILENAME) { | ||
let id = randomBytes(15).toString("hex"); | ||
dirs.set(id, importer); | ||
return id; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = nodeGlobals; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems that the reason to change this would be not to lint the new files in
src/node-compat/assets
, is this correct?If so, I think fixing the linting errors in those files should be the fix instead.
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.
those files are going to be injected into the user's bundle.
the lint errors are about the parser that eslint uses, which does not like es modules. so no chance to fix it.
see https://github.com/netlify/netlify-plugin-edge-handlers/runs/1210596968#step:5:32
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.
We should fix
.eslintrc.json
instead to allow linting both ES modules and CommonJS, e.g. usingoverrides
.