Skip to content

Commit

Permalink
fix: use inlined import meta plugin to inject url (#68)
Browse files Browse the repository at this point in the history
* 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
danielroe and pi0 authored Jun 20, 2022
1 parent 9126459 commit b52bb17
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"acorn": "latest",
"babel-plugin-dynamic-import-node": "latest",
"babel-plugin-parameter-decorator": "latest",
"babel-plugin-transform-import-meta": "latest",
"create-require": "latest",
"cross-env": "latest",
"destr": "latest",
Expand Down
3 changes: 2 additions & 1 deletion src/babel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { transformSync, TransformOptions as BabelTransformOptions } from '@babel/core'
import { TransformOptions, TRANSFORM_RESULT } from './types'
import { TransformImportMetaPlugin } from './plugins/babel-plugin-transform-import-meta'

export default function transform (opts: TransformOptions): TRANSFORM_RESULT {
const _opts: BabelTransformOptions = {
Expand All @@ -13,7 +14,7 @@ export default function transform (opts: TransformOptions): TRANSFORM_RESULT {
plugins: [
[require('@babel/plugin-transform-modules-commonjs'), { allowTopLevelThis: true }],
[require('babel-plugin-dynamic-import-node'), { noInterop: true }],
[require('babel-plugin-transform-import-meta')],
[TransformImportMetaPlugin, { filename: opts.filename }],
[require('@babel/plugin-syntax-class-properties')]
]
}
Expand Down
2 changes: 1 addition & 1 deletion src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const defaults: JITIOptions = {
sourceMaps: _EnvSourceMaps !== undefined ? !!_EnvSourceMaps : false,
interopDefault: false,
esmResolve: _EnvESMResolve || false,
cacheVersion: '6',
cacheVersion: '7',
legacy: lt(process.version || '0.0.0', '14.0.0'),
extensions: ['.js', '.mjs', '.cjs', '.ts']
}
Expand Down
44 changes: 44 additions & 0 deletions src/plugins/babel-plugin-transform-import-meta.ts
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)
}
}
}
}
}
12 changes: 2 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@
"@babel/helper-validator-option" "^7.16.7"
"@babel/plugin-transform-typescript" "^7.16.7"

"@babel/template@^7.16.7", "@babel/template@^7.4.4":
"@babel/template@^7.16.7":
version "7.16.7"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.16.7.tgz#8d126c8701fde4d66b264b3eba3d96f07666d155"
integrity sha512-I8j/x8kHUrbYRTUxXrrMbfCa7jxkE7tZre39x3kjr9hvI82cK1FfqLygotcWN5kdPGWcLdWMHpSBavse5tWw3w==
Expand Down Expand Up @@ -890,14 +890,6 @@ babel-plugin-parameter-decorator@latest:
resolved "https://registry.yarnpkg.com/babel-plugin-parameter-decorator/-/babel-plugin-parameter-decorator-1.0.16.tgz#1c889c3a1f3bbf03801fcbc2e95b8bae7468df3f"
integrity sha512-yUT2WPTUg1JaPmRGRSF557m1HJ9vdFQInRWOkiOyO5a9HhqlXffJu+fQ2xd5+qU/35ICMrrk9eWKsHCairKA9w==

babel-plugin-transform-import-meta@latest:
version "2.1.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-import-meta/-/babel-plugin-transform-import-meta-2.1.0.tgz#7ba03fb60800975c8546861f8d88b4e66ce097fc"
integrity sha512-pcjJWEARZNgqPdXaBU9xA5oC9dYrgvn8Ya3XusQUuIuRB/3NEVXMI3bnzOJG1GaWWmFzp9OyDnp1w3aZo7VCRQ==
dependencies:
"@babel/template" "^7.4.4"
tslib "^2.2.0"

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
Expand Down Expand Up @@ -3652,7 +3644,7 @@ tslib@^1.8.1:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.2.0, tslib@latest:
tslib@latest:
version "2.3.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==
Expand Down

0 comments on commit b52bb17

Please sign in to comment.