-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use inlined import meta plugin to inject url (#68)
* fix: use inlined import meta plugin to inject url * refactor: move into plugins dir * improve types * fix: filename is in second arg * fix: bump cache version to invalidate old caches Co-authored-by: Pooya Parsa <pyapar@gmail.com>
- Loading branch information
Showing
5 changed files
with
49 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { pathToFileURL } from 'url' | ||
import { smart } from '@babel/template' | ||
import type { NodePath, PluginObj } from '@babel/core' | ||
import type { Statement, MemberExpression } from '@babel/types' | ||
|
||
// Based on https://github.com/javiertury/babel-plugin-transform-import-meta/blob/master/src/index.ts v2.1.1 (MIT License) | ||
// Modification: Inlines resolved filename into the code when possible instead of injecting a require | ||
export function TransformImportMetaPlugin (_ctx: any, opts: { filename?: string }) { | ||
return <PluginObj> { | ||
name: 'transform-import-meta', | ||
visitor: { | ||
Program (path) { | ||
const metas: Array<NodePath<MemberExpression>> = [] | ||
|
||
path.traverse({ | ||
MemberExpression (memberExpPath) { | ||
const { node } = memberExpPath | ||
|
||
if ( | ||
node.object.type === 'MetaProperty' && | ||
node.object.meta.name === 'import' && | ||
node.object.property.name === 'meta' && | ||
node.property.type === 'Identifier' && | ||
node.property.name === 'url' | ||
) { | ||
metas.push(memberExpPath) | ||
} | ||
} | ||
}) | ||
|
||
if (metas.length === 0) { | ||
return | ||
} | ||
|
||
for (const meta of metas) { | ||
meta.replaceWith(smart.ast`${opts.filename | ||
? JSON.stringify(pathToFileURL(opts.filename)) | ||
: "require('url').pathToFileURL(__filename).toString()" | ||
}` as Statement) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters