diff --git a/package.json b/package.json index 79c1f3dd..db172ba2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/babel.ts b/src/babel.ts index ece97ccc..49c7e982 100644 --- a/src/babel.ts +++ b/src/babel.ts @@ -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 = { @@ -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')] ] } diff --git a/src/jiti.ts b/src/jiti.ts index 48e5f862..025a1ece 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -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'] } diff --git a/src/plugins/babel-plugin-transform-import-meta.ts b/src/plugins/babel-plugin-transform-import-meta.ts new file mode 100644 index 00000000..1f75d48c --- /dev/null +++ b/src/plugins/babel-plugin-transform-import-meta.ts @@ -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 { + name: 'transform-import-meta', + visitor: { + Program (path) { + const metas: Array> = [] + + 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) + } + } + } + } +} diff --git a/yarn.lock b/yarn.lock index 95963d1b..d3b08f18 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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== @@ -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" @@ -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==