Skip to content
This repository was archived by the owner on May 2, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .eslintignore
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/
63 changes: 10 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "npm run format && npm run test:dev",
"format": "run-s format:*",
"format:lint": "eslint --ignore-path .gitignore --fix --cache --format=codeframe --max-warnings=0 \"src/**/*.js\"",
"format:lint": "eslint --ignore-path .eslintignore --fix --cache --format=codeframe --max-warnings=0 \"src/**/*.js\"",
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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. using overrides.

"format:prettier": "prettier --ignore-path .gitignore --write --loglevel warn \"*.{js,md,yml,json}\" \"!**/package-lock.json\" \"!package-lock.json\"",
"test:dev": "ava",
"test:ci": "nyc -r lcovonly -r text -r json ava"
Expand All @@ -22,16 +22,18 @@
"@babel/preset-env": "^7.11.5",
"@rollup/plugin-babel": "^5.2.0",
"@rollup/plugin-commonjs": "^15.0.0",
"@rollup/plugin-inject": "^4.0.2",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@types/node": "^14.0.27",
"buffer-es6": "^4.9.3",
"del": "^5.1.0",
"make-dir": "^3.1.0",
"node-fetch": "^2.6.1",
"path-type": "^4.0.0",
"process-es6": "^0.11.6",
"rollup": "^2.23.1",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-terser": "^7.0.2",
"typescript": "^3.9.7"
},
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ const del = require("del");
const makeDir = require("make-dir");
const { isDirectory } = require("path-type");
const rollup = require("rollup");
const { terser } = require("rollup-plugin-terser");
const nodeBuiltins = require("rollup-plugin-node-builtins");
const nodeGlobals = require("rollup-plugin-node-globals");
const { terser } = require("rollup-plugin-terser");

const babel = nodeBabel.babel;
const resolve = nodeResolve.nodeResolve;

const { LOCAL_OUT_DIR, MANIFEST_FILE, MAIN_FILE, CONTENT_TYPE } = require("./consts");
const nodeGlobals = require("./node-compat/globals");
const uploadBundle = require("./upload");

function getShasum(buf) {
Expand Down
1 change: 1 addition & 0 deletions src/node-compat/assets/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var browser = true;
7 changes: 7 additions & 0 deletions src/node-compat/assets/global.js
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
: {};
53 changes: 53 additions & 0 deletions src/node-compat/globals.js
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;