GithubHelp home page GithubHelp logo

Comments (8)

FlorianRappl avatar FlorianRappl commented on June 6, 2024 1

Problem with a syntax like /foo/ is that it excludes paths. The other problem that I see is the level of verbosity and difficulty of reading, e.g., having slashes would require a backslash, but since that already has a meaning in a string two backslashes would be needed.

Anyway since the LHS would be primarily file based I wanted to opt-in for glob patterns, which just work as strings and are pretty well established. The only problem is that the RHS needs additional variables to cover the complexity of transformation (e.g., react to React).

In general the replacement for wildcards would be a problem that is hard to solve in my opinion (the $u for making an uppercase version of the standard $1 was one trial, but there are countless other cases...). One other option here on the RHS is that a relative file path to a Node.js module could be taken, which exports a function returning that string.

Another radical solution that takes both - a simpler RHS and allowing a regular expression on the LHS is that externals are defined in a Node.js module already. If externals is a string it would be evaluated as a (relative) path to a Node.js module; which exports a function that gets the requested module name (plus its path etc.) and either returns undefined (-> no external) or a string (replacement).

from parcel-plugin-externals.

FlorianRappl avatar FlorianRappl commented on June 6, 2024

Hm great question (and potential addition). Right now the externals are only determined by the peerDependencies and the externals (if used).

Maybe we could do the following regarding the externals: For each key we check if wildcards (*) are used. Additionally, we can check if slashes (/) are used - leading to paths.

Examples:

  • react-* would lead to all packages starting with react-
  • ./node_modules/react-* would lead to all packages located in node_modules that start with react
  • ./node_modules/* would lead to all packages located in node_modules

The replacement rule then could be complicated... Suggestions are defined replacement variables such as $1 (replaced string), $u (replaced string uppercase) etc.

Would be happy to hear how you solve it at the moment and what kind of syntax / options you could image for this feature!

from parcel-plugin-externals.

mfasman95 avatar mfasman95 commented on June 6, 2024

I think the examples you had there for $1 and $u sound like a good way to go.

Edit: On second thought, you may want them to be in the form of ${number}{modifiers} if you plan to have modifiers. $1 should be valid but if you wanted to modify it in some way you would provide $1u, as an example.

from parcel-plugin-externals.

stoically avatar stoically commented on June 6, 2024

RegExp could maybe help, since e.g. "foobar".replace(/(foo)(bar)/, '$2$1'); // barfoo

from parcel-plugin-externals.

FlorianRappl avatar FlorianRappl commented on June 6, 2024

How is that connected to the problem @stoically (maybe I'm missing something)?

Note that the configuration is done in a JSON. Once a wildcard is detected a regex would be used underneath, hence the syntax for defining the replacement on the RHS.

from parcel-plugin-externals.

stoically avatar stoically commented on June 6, 2024

Since you were talking about wildcards, it wasn't clear to me that you had RegExp already in mind, sorry about that.

I was just thinking that it might be easy to implement to have a special syntax to designate the LHS as RegExp, and use the RHS as appropriate replacement string in that case. Maybe if the LHS matches /^\/(.*)\/([gimsuy]+)?$/ it could be treated as RegExp, e.g. "/my(regexp)/": "your$1"

from parcel-plugin-externals.

FlorianRappl avatar FlorianRappl commented on June 6, 2024

Landed in develop and was published as a preview.

Current implementation:
Besides the "standard" ways of declaring externals you can also give it a string, e.g.,

{
  "externals": "./tools/ruleFactory.js"
}

The rule factory module is just a simple Node.js module that exports a function:

const rx = /node_modules\/react-(.*?)\//;

module.exports = function(path) {
  const result = rx.exec(path);

  if (result) {
    const suffix = result[1];
    const name = suffix[0].toUpperCase() + suffix.substr(1);
    return `react-${suffix} => React${name}`;
  }

  return undefined;
};

Essentially, you just return what would usually be in your hands. Since you get full paths you have flexibility, however, that comes with "responsibility".

Let me know what you think!

from parcel-plugin-externals.

FlorianRappl avatar FlorianRappl commented on June 6, 2024

Will be published in v3.

from parcel-plugin-externals.

Related Issues (17)

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.