danez / pirates Goto Github PK
View Code? Open in Web Editor NEWProperly hijack require
License: MIT License
Properly hijack require
License: MIT License
master
branch failed. π¨I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.
You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. Iβm sure you can resolve this πͺ.
Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.
Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master
branch. You can also manually restart the failed CI job that runs semantic-release.
If you are not sure how to resolve this, here is some links that can help you:
If those donβt help, or if this issue is reporting something you think isnβt right, you can always ask the humans behind semantic-release.
The npm token configured in the NPM_TOKEN
environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/
.
If you are using Two-Factor Authentication, make configure the auth-only
level is supported. semantic-release cannot publish with the default auth-and-writes
level.
Please make sure to set the NPM_TOKEN
environment variable in your CI with the exact value of the npm token.
Good luck with your project β¨
Your semantic-release bot π¦π
node -r @babel/register ./script.mjs
worked in node.js 12.1.0 or below (as long as --experimental-modules
was not passed to node). Since 12.2.0 this no longer works due to nodejs/node#27417. The problem is pirates
now wraps a function which throws ERR_REQUIRE_ESM
. It looks like the esm
module calls require.extensions['.mjs']()
if it exists and checks for the node.js core exception to know if it should wrap or replace the existing function. Maybe pirates
could do something similar?
From @nicolo-ribaudo on https://babeljs.slack.com/:
We should overwrite that overwrite that hook as such:
- If the file shouldn't be compiled by Babel, call the original hook (which will throw)
- If the file is compiled by Babel and it's source type is still module, call the original hook
- Otherwise, handle it like if it was a .js file
My idea is if the hooks do not transpile from ESM to CJS the original require.extensions['.js']
would throw a SyntaxError
exception at which point the original require.extensions['.mjs']
could be called to throw ERR_REQUIRE_ESM?
I am trying to use ts-node/register
-> babel-register
to ensure my code can compiled with babel-plugin. Finally i find this repository.Could you provide an example for realize this goal?
Hi All,
Just continuing istanbuljs/nyc#70 and babel/babel#3062.
To recap: I will be taking ideas from istanbul's and nyc's require-hook implementations, and trying to make something that:
Super easy require-hook injection, with a super simple API that Just Works(tm), feeding hooks into each other in the order they are added. API concept:
var pirates = require('pirates');
// ...
pirates.addCompiler('.js', function (filename) {
return filename.indexOf('foo') !== -1;
}, function (module, filename) {
return this.addFile(module, filename).content; // Just return the new source for the module.
});
Using getters/setters, automatic fixing of hooks that don't use pirates, and still passing all requires through the hooks in order. API concept:
require('pirates/register');
// For some weird reason, part of my app is in coffee script, and some is in ES6.
require('coffee-script/register');
require('babel-core/regsister');
require('some-bad-module/register');
???
Discuss.
cc @bcoe, @gotwarlost, @stefanpenner, @jamestalmage, @sebmck
function addHook(hook, opts) {
}
where hook
is the hook to be added, the only required argument (that hooks .js
files by default) and
opts
is an optional object with extensions
and matcher
properties supported on it.
node_modules
logic in your code. istanbul, for instance, allows you to disregard its default excludes and hook all files under node_modules as well. You could support a hookNodeModules
property that is false by default but can be turned on by the caller.cc @bcoe @jamestalmage @novemberborn for inputs
I understand why pirates exists but I'm still not sure what it does. A project with a provocative name deserves an explanation. :-)
Thanks.
Before this can even be considered for adoption, it needs a much more thorough and easy to understand test suite.
If I run into an issue with a library, the first place I look to make sure I am using it correctly is the tests. A single test, that relies on confusing process forking and and pulls in half a dozen fixtures at once just doesn't cut it.
@novemberborn had the great idea of not doing actual source transforms, but simple text once. That is the idea behind fake-module-system
. I think it makes the tests in capture-require
really easy to understand.
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates are currently rate-limited. Click on a checkbox below to force their creation now.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
@babel/cli
, @babel/core
, @babel/preset-env
)These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.
.github/workflows/build.yml
actions/checkout v3
actions/setup-node v3
actions/cache v3
codecov/codecov-action v3
actions/checkout v3
actions/setup-node v3
actions/cache v3
actions/checkout v3
actions/setup-node v3
actions/cache v3
.github/workflows/codeql-analysis.yml
actions/checkout v3
github/codeql-action v2
github/codeql-action v2
github/codeql-action v2
.github/workflows/ossar-analysis.yml
actions/checkout v3
github/ossar-action v1
github/codeql-action v2
package.json
@babel/cli 7.22.10
@babel/core 7.22.11
@babel/preset-env 7.22.14
ava 1.4.1
babel-core 7.0.0-bridge.0
babel-eslint 10.1.0
babel-plugin-istanbul 5.2.0
cross-env 5.2.1
decache 4.6.2
eslint 5.16.0
eslint-config-prettier 4.3.0
eslint-plugin-import 2.28.0
eslint-plugin-prettier 3.4.1
mock-require 3.0.3
nyc 13.3.0
prettier 1.19.1
rewire 4.0.1
rimraf 3.0.2
node >= 6
main
branch failed. π¨I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.
You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. Iβm sure you can fix this πͺ.
Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.
Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main
branch. You can also manually restart the failed CI job that runs semantic-release.
If you are not sure how to resolve this, here are some links that can help you:
If those donβt help, or if this issue is reporting something you think isnβt right, you can always ask the humans behind semantic-release.
The npm token configured in the NPM_TOKEN
environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/
.
If you are using Two Factor Authentication for your account, set its level to "Authorization only" in your account settings. semantic-release cannot publish with the default "
Authorization and writes" level.
Please make sure to set the NPM_TOKEN
environment variable in your CI with the exact value of the npm token.
Good luck with your project β¨
Your semantic-release bot π¦π
The publish that just occurred is empty, causing errors when require('pirates')
ing as the main
lib/index.js
cannot be found.
/node_modules/pirates $ ls -a
. .. LICENSE README.md index.d.ts package.json
I have a problem since upgrading to babel 7. I import css files through css-loader.
The reason is that it tries to load css files as js. Not sure how to avoid that though.
import styles from './flexboxgrid.css';
components/flexbox/flexboxgrid.css:1
(function (exports, require, module, __filename, __dirname) { .visible-xs,
^
SyntaxError: Unexpected token .
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Module._extensions..js (module.js:663:10)
at Object.newLoader [as .js] (/Users/project/node_modules/pirates/lib/index.js:88:7)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/project/shared/router/index.js:10:1)
at Module._compile (module.js:652:30)
at Module._compile (/Users/project/node_modules/pirates/lib/index.js:83:24)
at Module._extensions..js (module.js:663:10)
at Object.newLoader [as .js] (/Users/project/node_modules/pirates/lib/index.js:88:7)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/project/shared/App/index.js:8:1)
at Module._compile (module.js:652:30)
I have an issue where pirates throws an error for one of my hooks very rare and exclusively in CI
It happens on this line
https://github.com/danez/pirates/blob/main/src/index.js#L124
Would you accept a PR where error message would be extended with a filename and extension maybe?
I have a simple ./lib/fix.js
that uses pirates for some preprocessing, tapping into the .js
extension, and this works perfectly fine for the project as local project. However, if I create a bin script and then install my package globally, pirates seems to do nothing at all.
Running locally, test.js:
require('../../lib/fix');
require('./runtest');
fix file:
'use strict';
var path = require('path');
console.log("RUNNING FIX"); // logs both locally and globally
module.exports = require('pirates').addHook(s => {
s = s.replace(/'/g, '`');
s = s.replace(/`use strict`/, "'use strict'");
if (!s.match(/use strict/)) { s = "'use strict'\n\n" + s; }
return s;
}, {
exts: ['.js'],
matcher: n => {
console.log(n); // logs fine locally, do NOT log after global install
(n.indexOf(path.join('mahjong','src')) > -1)
}
});
This works fine locally, but if I install my package as a global cli tool, using the following my-test-cli
bin script:
#! /usr/bin/env node
require('../src/client/cli/test.js');
and the following package.json section:
"bin": {
"my-test-cli": "./bin/my-test-cli"
},
then the fix file gets loaded, the first console.log runs, but the console logs in the matcher
function never trigger.
Any idea why that might be?
hey,
I'm trying to add a hook with a custom matcher, that works on all extensions except a small blacklist.
I am expecting the matcher to give me all files (.scss, .css, .json, etc),
but to my surprise, I'm only getting '.js' files!
The use of path.extname to extract the extension of a require (done here) is actually an inherent bug, as path
does the extension extract based on the assumption that the entire string is a pure filename, whereas node requires don't have to conform to these. Webpack loaders take advantage of this, so you can do something like this:
require("file!basefile.txt?name=monkey.d.luffy");
Which will copy the "basefile.txt" resource as the file "monkey.d.luffy" somewhere else. In strings like these, path
cannot extract the extension, as it assumes the strings is a true file path rather than a URI, and so it will find .luffy
as extension.
It would probably worth to at the very least strip off URI arguments, using something like
function shouldCompile(filename, exts, matcher, ignoreNodeModules) {
if (typeof filename !== 'string') return false;
// strip URI arguments if necessary (used by webpack etc.)
var qpos = filename.indexOf('?');
if (qpos > -1) filename = filename.substring(0,qpos);
if (exts.indexOf(path.extname(filename)) === -1) return false;
filename = path.resolve(filename);
if (ignoreNodeModules && nodeModulesRegex.test(filename)) return false;
if (matcher && typeof matcher === 'function') return !!matcher(filename);
return true;
}```
I have the following problem with esm standard-things/esm#794
where the mainFields are not take in consideration
can I use pirates to fix this?
I currently watch pirates, and it is badly polluting my GitHub feed.
Just use david
to make sure things stay up to date.
I was looking into switching nyc from append-transform to pirates but ran into an issue. One of the nyc tests verifies that instrumentation still works when someone sets require.extensions['.js']
after nyc installs the require hook. This test determined that setting require.extensions['.js'] = hookFunction
removes the hook installed by pirates. Is this intentional or would you be open to addressing this?
I'm using the following code:
var pirates = require("pirates");
var babel = require("babel-core");
var loaders = {
latex: require('../lib/latex-loader'),
pre: require('../lib/pre-loader'),
p: require('../lib/p-loader'),
babel: function(source) {
return babel.transform(source, {
presets: ['es2015', 'react']
}).code;
}
};
function convert(source, filename) {
source = loaders.p(source);
source = loaders.pre(source);
source = loaders.latex(source);
source = loaders.babel(source);
return source;
}
module.exports = pirates.addHook(convert, { exts: ['.js','.jsx'] });
however, this generates the following log:
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\sections\catmullmoulding\index.js
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\sections\polybezier\index.js
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\sections\shapes\index.js
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\sections\projections\index.js
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\sections\offsetting\index.js
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\sections\graduatedoffset\index.js
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\sections\circles\index.js
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\sections\circles_cubic\index.js
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\sections\arcapproximation\index.js
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\Navigation.jsx
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\components\Navigation.jsx
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\stylesheets\style.less
J:\Junctions\Users\Mike\Documents\Git projects\projects\BezierInfo-2\node_modules\babel-core\lib\transformation\file\index.js:548
throw err;
^
SyntaxError: unknown: Unexpected token (1:11)
Which... well, is odd. Why are there pirates all over my .less
file? That shouldn't even make it in, it has no .js
or .jsx
extension. Yes, I'm require()
ing them, but they don't match the file extension, so why is pirates treating them as match?
Hi,
I am working on a project which has pirates
as a transitive dependency, and now get an exception after upgrading from 4.0.1
to 4.0.3
. The commit which seems to cause my problems seems to be this one:
As far as I can tell, the error occurs in this library here which uses @babel/register
which uses pirates
as a dependency: https://github.com/complate/complate-fractal/blob/master/src/transpiler.js#L6
The code that I am having problems with is as follows:
require('@babel/register')({
extensions: ['.jsx'],
ignore: [], // activates transpilation for third-party modules
presets: ['@babel/preset-env'],
plugins: [
['@babel/plugin-transform-react-jsx', {
pragma: 'createElement',
pragmaFrag: 'Fragment'
}]
]
})
// activate support for ES6 import/export syntax (required for non-JSX ES6 modules, notably complate-stream)
require = require('esm')(module) // eslint-disable-line no-global-assign
I believe this worked previously because for extensions which are not explicitly specified here, the default used to be to use the default JS loader. As far as I can tell, the require('esm')(module)
line now throws an error if there are any imports on the path which do not use a known extension. If I pass in ['.jsx', '.mjs', '.cjs']
as extensions in L6 of that file, then the error no longer occurs for my project.
I am willing to make a PR to the complate-fractal
project to change that line there, but it feels like the wrong place to correct it because I actually would prefer to use the JavaScript loader to load .cjs
and .mjs
files. Here it is also not possible to leave off the extensions
because then babel tries to register the JSX loader for all the .js
files as well and that causes problems when loading some dependencies (e.g. prettier). For me, specifying the .cjs
and .mjs
in the extension list here also feels like a smell -- currently the code will work if I modify that, but if in the future my transitive dependencies are updated to include imports from different file extensions, then there is a likelihood that this will break in the code again.
Here is my demo project which currently throws an error when running: https://github.com/joyheron/complate-fractal-demo
I've created a branch to demonstrate that downgrading the pirates
version to 4.0.1
solves the issue: https://github.com/joyheron/complate-fractal-demo/tree/complate-fractal-works-with-pirates-4.0.1
Right now pirates
assumes that Module_.extensions['.js']
points to the original javascript extension the first time it sees it. That is an improper assumption, as it could have been changed already by any one of the following:
npm@2
environment (so there is no de-duping regardless).Reference:
I saw a pirates package with the description saying it's gonna hijack something on my requires
I used it in my project to see if it intercepts dynamic import(url)
calls too and seems like it doesn't?
Hi thanks for this great script, I am using it for the new riotjs ssr module. I have a question: is it possible to compile the required files asynchronously?
import {addHook} from 'pirates'
addHook(
async function(source, filename) {
return await compile(source)
},
{ exts: ['.riot'] }
)
At https://github.com/ariporad/pirates/blob/master/src/index.js#L72, the JS loader is used as a fallback for the old loader if one isn't already registered. This pollutes the require.extensions
state and doesn't revert it back to what it was originally.
This is affecting some custom module loading code I have for ts-node
where I first check for the existence of a registered module hook before initialising my own:
if (!require.extensions['.ts']) {
// ts-node can only handle being registered once, see https://github.com/TypeStrong/ts-node/issues/409
require('ts-node').register({ project });
}
This no longer works when executed after a ts hook has been added and reverted by this library (via third party code) as the default js loader is now registered for .ts
.
Perhaps this could be fixed by utilising this library ourselves but in the meantime I propose fixing this by not assigning the JS loader as a fallback if one doesn't already exist for an extension but instead just deleting the one that was added.
At the moment this line https://github.com/ariporad/pirates/blob/master/src/index.js#L76 allows you to change code but it does not let you return both newCode and a newFilename. Pirates should allow authors to return either an object that has newCode and/or newFilename properties. If a newFilename is returned then the hook would use that when calling compile.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.