Skip to content

Commit

Permalink
Add support for wildcard importing ESM (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
denkristoffer committed Sep 7, 2022
1 parent b6be8c6 commit a47e72e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ Each section in the config can have these options:
It could be a path `"index.js"`, a [pattern] `"dist/app-*.js"`
or an array `["index.js", "dist/app-*.js", "!dist/app-exclude.js"]`.
* **import**: partial import to test tree-shaking. It could be `"{ lib }"`
to test `import { lib } from 'lib'` or `{ "a.js": "{ a }", "b.js": "{ b }" }`
to test `import { lib } from 'lib'`, `*` to test `import * as all from 'lib'` or `{ "a.js": "{ a }", "b.js": "{ b }" }`
to test multiple files.
* **limit**: size or time limit for files from the `path` option. It should be
a string with a number and unit, separated by a space.
Expand Down
10 changes: 10 additions & 0 deletions packages/esbuild/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,13 @@ it('supports import with multiple files', async () => {
})
).toBe(18)
})

it('supports wildcard imports', async () => {
expect(
await getSize({
import: {
[fixture('module.js')]: '*'
}
})
).toBe(191)
})
10 changes: 8 additions & 2 deletions packages/size-limit/process-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ module.exports = async function processImport(check, output) {

let loader = ''
for (let i in check.import) {
let imports = `${check.import[i]}`
let list = check.import[i].replace(/}|{/g, '').trim()

if (check.import[i] === '*') {
imports = `${check.import[i]} as all`
list = `all`
}

loader +=
`import ${check.import[i]} from ${JSON.stringify(i)}\n` +
`console.log(${list})\n`
`import ${imports} from ${JSON.stringify(i)}\n` + `console.log(${list})\n`
}
await mkdirp(output)
let entry = join(output, 'index.js')
Expand Down

0 comments on commit a47e72e

Please sign in to comment.