Snowpack: Support Aliases Patterns

Created on 9 Jun 2020  Â·  6Comments  Â·  Source: snowpackjs/snowpack

Current Behaviour

Snowpack 2.0 supports precise aliasing through this configuration:

snowpack.config.js

module.exports = {
    installOptions: {
        alias: {
            'react': 'preact-compat',
            'react-dom': 'preact-compat',
            'my-lib': '@my-company/my-lib',
        },
    },
}

So it is possible to do

import {Something} from 'my-lib'

but if I try to import

import {Button} from 'my-lib/ui/button'
import {Input} from 'my-lib/ui/input'

Snowpack fails miserably with

✖ Package "my-lib/ui/button" not found. Have you installed it?

Expected Behaviour

The expected behaviour is that the alias is handled as matching pattern.

Or at lease the alias configuration should allow to write patterns for rewriting the imports. Example

module.exports = {
    installOptions: {
        alias: {
            'react': 'preact-compat',
            'react-dom': 'preact-compat',
            'my-lib': '@my-company/my-lib',
            'my-lib/*': '@my-company/my-lib/*',
        },
    },
}

Update 1

I tried with

module.exports = {
    installOptions: {
        alias: {
            'react': 'preact-compat',
            'react-dom': 'preact-compat',
        },
        rollup: {
            plugins: [
                require('@rollup/plugin-alias')({
                    entries: [
                        {find: /my-lib/, replacement: '@my-company/my-lib'},
                    ],
                }),
            ],
        },
    },
}

but it does not work.

good first issue help wanted

All 6 comments

See this section on Regular Expression matching: https://www.npmjs.com/package/@rollup/plugin-alias#regular-expression-aliases

+1, we should add support for this, maybe even by default. Option: Convert every alias key to a regex when we pass it to the rollup alias plugin that we use internally: "package-name" -> /^package-name\//. This package could come in handy: https://github.com/sindresorhus/escape-string-regexp

@daniele-orlando would love your help if you're willing to create a PR!

Hi dear @FredKSchott, I have given an overview to the internal of Snowpack and I don't feel confident enough to make any relevant change to it, because I don't have a global picture of how Snowpack works, so it is very likely I would break something else. Unfortunately at the moment I don't have time to study Snowpack.

Hi @FredKSchott Interested in this feature, could you give me some initial hints on how to implement it?

Sure thing, I'd love to help whoever wants to take a stab at it. After thinking about it a bit, I think that we should only support aliasing a full package name, and not the package-name/* or the long list of individual files that were mentioned above. That keeps both the interface and the implementation simple.

There are a few places to make changes to support this:

  1. rollupPluginAlias() - Properly set the find& replacement values to something like:
  find: new Regex(`^${alias}(\/?.*)`),
  replacement: `${mod}$1`
  1. The two other uses of installAlias would need to be updated to match via something like startsWith, instead of a direct string comparison.

I think this is handled in #603, awaiting 2.7 release?

Edit: released in 2.7.0! Please see the top-level alias docs: https://snowpack.dev

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gr2m picture gr2m  Â·  6Comments

FredKSchott picture FredKSchott  Â·  5Comments

mfolkeseth picture mfolkeseth  Â·  5Comments

FredKSchott picture FredKSchott  Â·  6Comments

devongovett picture devongovett  Â·  4Comments