parcel-bundler / parcel

The zero configuration build tool for the web. πŸ“¦πŸš€
https://parceljs.org
MIT License
43.49k stars 2.27k forks source link

Parcel doesn't seem to support the use of bundle-text and glob expressions at the same time #7936

Closed ShirasawaSama closed 2 years ago

ShirasawaSama commented 2 years ago

πŸ› bug report

A text file can be normally bundled when I use this code (bundle-text:):

import text from 'bundle-text:@types/node/index.d.ts'

console.log(text)

But if I use glob expression, only an empty object {} is returned:

import text from 'bundle-text:@types/node/*.d.ts'

console.log(text) // Empty object: {}

πŸŽ› Configuration (.babelrc, package.json, cli command)

.parcelrc:

{
  "extends": "@parcel/config-default",
  "resolvers": ["@parcel/resolver-glob", "..."]
}

πŸ€” Expected Behavior

Can return the contents of multiple files normally:

{
  index: '....',
  http: '...',
  os: '....',
  ...
}

😯 Current Behavior

Only one empty object was returned:

{}

πŸ’ Possible Solution

packages/parcel-transformer-glob-text/index.js:

const { Transformer } = require('@parcel/plugin')
const { join, relative } = require('path')
const { glob } = require('@parcel/utils')
const fs = require('fs').promises

module.exports = new Transformer({
  async transform ({ asset }) {
    const data = { }
    const root = join(asset.filePath, '..') + '/'
    for (const file of await glob(join(asset.filePath, (asset.query.keys().next().value || '').replace(/%/g, '*')), fs, { onlyFiles: true })) {
      data[relative(root, file)] = await fs.readFile(file, 'utf-8')
    }
    asset.type = 'js'
    asset.setCode('module.exports = ' + JSON.stringify(data))
    return [asset]
  }
})

.parcelrc:

{
  "extends": "@parcel/config-default",
  "resolvers": ["@parcel/resolver-glob", "..."],
  "transformers": {
    "text-glob:*": ["parcel-transformer-glob-text", "..."]
  }
}

index.js:

import data from 'text-glob:@types/node/index.d.ts?../%.d.ts'

console.log(data)

πŸ”¦ Context

πŸ’» Code Sample

🌍 Your Environment

Software Version(s)
Parcel 2.4.1
Node 16.0.0
npm/Yarn 3.0.2 (yarn)
Operating System Windows 11
devongovett commented 2 years ago

I think the problem is actually that you're trying to use the glob resolver with a package inside node_modules rather than a relative path. Pipelines should already be working properly. Closing as a duplicate of #7945.