Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
feat: add ignore support
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Apr 30, 2023
1 parent 75bb36e commit 093ea8c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-queens-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'esbuild-plugin-dynamic-import': minor
---

feat: add ignore support
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ import { dynamicImport } from 'esbuild-plugin-dynamic-import'
esbuild.build({
plugins: [
dynamicImport({
// filter: // optional
// loader: // optional
// filter: // optional, default: /\.([cm]?[jt]s|[jt]sx)$/
// ignore: // optional, default: /\bnode_modules\b/
// loader: // optional, default: adjust with path extension
}),
],
})
Expand Down
100 changes: 50 additions & 50 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs/promises'
import { dirname } from 'node:path'
import { dirname, extname } from 'node:path'

import { type Loader, type Plugin } from 'esbuild'
import glob from 'fast-glob'
Expand All @@ -23,80 +23,80 @@ const LOADERS = {

export interface DynamicImportPluginOptions {
filter?: RegExp
ignore?: RegExp
loader?: Loader
}

export const dynamicImport = ({
filter,
filter = /\.([cm]?[jt]s|[jt]sx)$/,
ignore = /\bnode_modules\b/,
loader,
}: DynamicImportPluginOptions = {}): Plugin => ({
name: 'dynamic-import',
// eslint-disable-next-line sonarjs/cognitive-complexity
setup(build) {
build.onLoad(
{ filter: filter || /\.([cm]?[jt]s|[jt]sx)$/ },
async ({ path }) => {
const ext = path.slice(path.lastIndexOf('.')) as keyof typeof LOADERS
build.onLoad({ filter }, async ({ path }) => {
if (ignore.test(path)) {
return
}

let contents = await fs.readFile(path, 'utf8')
const ext = extname(path) as keyof typeof LOADERS

const matches = contents.matchAll(DYNAMIC_IMPORT_REGEX)
let contents = await fs.readFile(path, 'utf8')

let dynamicImportIndex = -1
const matches = contents.matchAll(DYNAMIC_IMPORT_REGEX)

for (const match of matches) {
const [full, _, __, importPath] = match
let dynamicImportIndex = -1

const plainPath = importPath.replaceAll(COMMENTS_REGEX, '').trim()
for (const match of matches) {
const [full, _, __, importPath] = match

if (
(!plainPath.startsWith('`./') && !plainPath.startsWith('`../')) ||
!plainPath.endsWith('`') ||
!plainPath.includes('${')
) {
continue
}
const plainPath = importPath.replaceAll(COMMENTS_REGEX, '').trim()

const globImport = plainPath
.replaceAll(/\$\{[\s\S]*\}/g, '*')
.slice(1, -1)
if (
(!plainPath.startsWith('`./') && !plainPath.startsWith('`../')) ||
!plainPath.endsWith('`') ||
!plainPath.includes('${')
) {
continue
}

let paths = await glob(globImport, {
cwd: dirname(path),
globstar: false,
})
const globImport = plainPath
.replaceAll(/\$\{[\s\S]*\}/g, '*')
.slice(1, -1)

let paths = await glob(globImport, {
cwd: dirname(path),
globstar: false,
})

if (globImport.lastIndexOf('*') > globImport.lastIndexOf('.')) {
paths = paths.map(p => {
const index = p.lastIndexOf('.')
return index > 0 ? p.slice(0, index) : p
})
}
if (globImport.lastIndexOf('*') > globImport.lastIndexOf('.')) {
paths = paths.map(p => {
const index = p.lastIndexOf('.')
return index > 0 ? p.slice(0, index) : p
})
}

contents =
`function __dynamicImportRuntime${++dynamicImportIndex}__(path) { switch (path) {
contents =
`function __dynamicImportRuntime${++dynamicImportIndex}__(path) { switch (path) {
${paths.map(p => `case '${p}': return import('${p}');`).join('\n')}
${`default: return new Promise(function(resolve, reject) {
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(
reject.bind(null, new Error("Unknown variable dynamic import: " + path))
);
})\n`} }
}\n\n` +
contents.replace(
full,
full.replace(
'import',
`__dynamicImportRuntime${dynamicImportIndex}__`,
),
)
}
return dynamicImportIndex === -1
? null
: {
contents,
loader: loader || LOADERS[ext],
}
},
)
contents.replace(
full,
full.replace(
'import',
`__dynamicImportRuntime${dynamicImportIndex}__`,
),
)
}
return dynamicImportIndex === -1
? null
: { contents, loader: loader || LOADERS[ext] }
})
},
})

0 comments on commit 093ea8c

Please sign in to comment.