GithubHelp home page GithubHelp logo

pirates's People

Contributors

ariporad avatar buckles1170 avatar danez avatar dependabot[bot] avatar evocateur avatar exe-boss avatar greenkeeperio-bot avatar jdalton avatar papb avatar renovate-bot avatar renovate[bot] avatar simenb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pirates's Issues

The automated release is failing 🚨

🚨 The automated release from the 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.


Invalid npm token.

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.js 12.2.0 breaks patching of `.mjs` files

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?

CC @bcoe @jdalton

Initial Discussion

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:

  1. 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.
    });
  2. 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');
  3. ???

Discuss.

cc @bcoe, @gotwarlost, @stefanpenner, @jamestalmage, @sebmck

Some proposals for changes before this module starts getting used everywhere

  • Could we simplify the method signature of the hook function and make it extensible for future changes instead of doing the dance in the parse args method? My proposal:
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.

  • Please do not bake the 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

Please add a 'What' to the readme

I understand why pirates exists but I'm still not sure what it does. A project with a provocative name deserves an explanation. :-)

Thanks.

Needs tests.

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.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update actions/cache action to v4
  • chore(deps): update actions/checkout action to v4
  • chore(deps): update actions/setup-node action to v4
  • chore(deps): update codecov/codecov-action action to v4
  • chore(deps): update dependency ava to v6
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency rewire to v7
  • chore(deps): update github/codeql-action action to v3
  • πŸ” Create all rate-limited PRs at once πŸ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.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
npm
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

  • Check this box to trigger a request for Renovate to run again on this repository

The automated release is failing 🚨

🚨 The automated release from the 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.


Invalid npm token.

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 πŸ“¦πŸš€

`4.0.2` contains no files

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

Pirates breaks css-loader module import

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)

doesn't work when executing a bin script?

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?

add hook without specifying exts

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!

possible solutions:

  • support regex in exts
  • allow exts = null, ['*'] or [] for catch all

path.extname for extension checks will break with webpack loaders

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;
}```

Please drop greenkeeper.

I currently watch pirates, and it is badly polluting my GitHub feed.

Just use david to make sure things stay up to date.

Setting require.extensions overwrites hook installed by pirates.

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?

why are there Pirates in my .less files, when I tell it to only hook for .js and .jsx?

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?

Reverting to undefined loader breaks library depending on this behavior

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:

cf74daf

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

Protect against the default `.js` extension being replaced before pirates gets a chance to store `originalJsExtension`

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:

  1. Another module that doesn't use pirates.
  2. Another module that uses a semver incompatible version of pirates (so it's not deduped).
  3. Another module that uses pirates in an npm@2 environment (so there is no de-duping regardless).

Reference:

Async compilation

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'] }
)

Don't restore original JS loader to non-JS extensions when reverting

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.