@tamagui/babel-plugin-fully-specified 3.0.0-beta.924.1 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/permanent/cjs/commonjs.js +15 -0
- package/permanent/cjs/commonjs.js.map +1 -1
- package/permanent/cjs/index.js +183 -146
- package/permanent/cjs/index.js.map +1 -1
- package/permanent/esm/commonjs.mjs +18 -3
- package/permanent/esm/commonjs.mjs.map +1 -1
- package/permanent/esm/index.mjs +155 -112
- package/permanent/esm/index.mjs.map +1 -1
- package/src/commonjs.ts +18 -0
- package/src/index.ts +21 -0
- package/types/commonjs.d.ts.map +1 -1
- package/types/index.d.ts.map +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/babel-plugin-fully-specified",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"source": "src/index.ts",
|
|
@@ -48,5 +48,6 @@
|
|
|
48
48
|
"type": "git",
|
|
49
49
|
"url": "git+https://github.com/tamagui/tamagui.git",
|
|
50
50
|
"directory": "code/packages/babel-plugin-fully-specified"
|
|
51
|
-
}
|
|
51
|
+
},
|
|
52
|
+
"releaseSourceCommit": "c156b7cc55e5a6cc5883ebc8189720850ac32dbd"
|
|
52
53
|
}
|
|
@@ -58,7 +58,22 @@ function fullySpecifyCommonJS(api, options) {
|
|
|
58
58
|
moduleSpecifier
|
|
59
59
|
)
|
|
60
60
|
let newModuleSpecifier = moduleSpecifier
|
|
61
|
+
if (
|
|
62
|
+
cjsExtension.startsWith('.native') &&
|
|
63
|
+
((0, import_node_fs.existsSync)(`${resolvedPath}.ios.js`) ||
|
|
64
|
+
(0, import_node_fs.existsSync)(`${resolvedPath}.android.js`))
|
|
65
|
+
) {
|
|
66
|
+
return
|
|
67
|
+
}
|
|
61
68
|
if (isLocalDirectory(resolvedPath)) {
|
|
69
|
+
const indexBase = (0, import_node_path.resolve)(resolvedPath, 'index')
|
|
70
|
+
if (
|
|
71
|
+
cjsExtension.startsWith('.native') &&
|
|
72
|
+
((0, import_node_fs.existsSync)(`${indexBase}.ios.js`) ||
|
|
73
|
+
(0, import_node_fs.existsSync)(`${indexBase}.android.js`))
|
|
74
|
+
) {
|
|
75
|
+
return
|
|
76
|
+
}
|
|
62
77
|
const indexPath = (0, import_node_path.resolve)(
|
|
63
78
|
resolvedPath,
|
|
64
79
|
'index' + jsExtension
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/commonjs.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAsC;AACtC,uBAA0C;AAE3B,SAAR,qBACL,KACA,SAGiB;AACjB,MAAI,cAAc,CAAC;AAEnB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,MACP,eAAe,MAAM,OAAO;AAC1B,cAAM,SAAS,KAAK,IAAI,QAAQ;AAEhC,YACE,OAAO,aAAa,EAAE,MAAM,UAAU,CAAC,KACvC,KAAK,KAAK,UAAU,WAAW,GAC/B;AACA,gBAAM,MAAM,KAAK,KAAK,UAAU,CAAC;AACjC,cAAI,IAAI,SAAS,iBAAiB;AAChC,gBAAI,kBAAkB,IAAI;AAG1B,gBAAI,gBAAgB,WAAW,GAAG,KAAK,gBAAgB,WAAW,GAAG,GAAG;AACtE,oBAAM,WAAW,MAAM,KAAK,KAAK;AACjC,kBAAI,CAAC,SAAU;AAEf,oBAAM,cAAU,0BAAQ,QAAQ;AAChC,oBAAM,eAAe,QAAQ,sBAAsB;AACnD,oBAAM,cAAc;AAIpB,oBAAM,yBAAqB,0BAAQ,eAAe;AAClD,oBAAM,qBAAqB;AAAA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AiB,YAAY,GAAG;AAClC,wBAAM,gBAAY,0BAAQ,cAAc,OAAO;AAC/C,sBACE,aAAa,WAAW,SAAS,UAChC,2BAAW,GAAG,SAAS,SAAS,SAC/B,2BAAW,GAAG,SAAS,aAAa,IACtC;AACA;AAAA,kBACF;AACA,wBAAM,gBAAY,0BAAQ,cAAc,UAAU,WAAW;AAC7D,0BAAI,2BAAW,SAAS,GAAG;AAEzB,wBAAI,CAAC,mBAAmB,SAAS,GAAG,GAAG;AACrC,4CAAsB;AAAA,oBACxB;AACA,0CAAsB,UAAU;AAChC,wBAAI,QAAQ;AACZ;AAAA,kBACF;AAAA,gBACF;AAGA,wBACE,2BAAW,eAAe,WAAW,SACrC,2BAAW,eAAe,YAAY,GACtC;AACA,wCAAsB;AACtB,sBAAI,QAAQ;AACZ;AAAA,gBACF;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,iBAAiB,cAA+B;AACvD,aAAO,2BAAW,YAAY,SAAK,0BAAU,YAAY,EAAE,YAAY;AACzE;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/permanent/cjs/index.js
CHANGED
|
@@ -2,169 +2,185 @@ var __create = Object.create
|
|
|
2
2
|
var __defProp = Object.defineProperty
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf
|
|
6
|
-
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty
|
|
7
7
|
var __export = (target, all) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
})
|
|
19
|
-
return to
|
|
8
|
+
for (var name in all) __defProp(target, name, { get: all[name], enumerable: true })
|
|
9
|
+
}
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if ((from && typeof from === 'object') || typeof from === 'function') {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, {
|
|
15
|
+
get: () => from[key],
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
|
|
17
|
+
})
|
|
20
18
|
}
|
|
19
|
+
return to
|
|
20
|
+
}
|
|
21
21
|
var __toESM = (mod, isNodeMode, target) => (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
22
|
+
(target = mod != null ? __create(__getProtoOf(mod)) : {}),
|
|
23
|
+
__copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule
|
|
29
|
+
? __defProp(target, 'default', { value: mod, enumerable: true })
|
|
30
|
+
: target,
|
|
31
|
+
mod
|
|
32
|
+
)
|
|
33
|
+
)
|
|
34
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, '__esModule', { value: true }), mod)
|
|
35
35
|
var index_exports = {}
|
|
36
36
|
__export(index_exports, {
|
|
37
37
|
default: () => FullySpecified,
|
|
38
38
|
})
|
|
39
39
|
module.exports = __toCommonJS(index_exports)
|
|
40
|
-
var import_node_fs = require('node:fs')
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
var import_node_fs = require('node:fs')
|
|
41
|
+
var import_node_path = require('node:path')
|
|
42
|
+
var t = __toESM(require('@babel/types'))
|
|
43
43
|
const DEFAULT_OPTIONS = {
|
|
44
|
-
ensureFileExists:
|
|
44
|
+
ensureFileExists: true,
|
|
45
45
|
esExtensionDefault: '.mjs',
|
|
46
46
|
tryExtensions: ['.js'],
|
|
47
47
|
esExtensions: ['.mjs'],
|
|
48
|
-
convertProcessEnvToImportMetaEnv:
|
|
48
|
+
convertProcessEnvToImportMetaEnv: false,
|
|
49
49
|
}
|
|
50
50
|
function FullySpecified(api, rawOptions = {}) {
|
|
51
51
|
api.assertVersion(7)
|
|
52
|
-
const options = normalizeOptions(rawOptions)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
52
|
+
const options = normalizeOptions(rawOptions)
|
|
53
|
+
const importDeclarationVisitor = (path, state) => {
|
|
54
|
+
const filePath = state.file.opts.filename
|
|
55
|
+
if (!filePath) return
|
|
56
|
+
const { node } = path
|
|
57
|
+
if (node.importKind === 'type') return
|
|
58
|
+
const originalModuleSpecifier = node.source.value
|
|
59
|
+
const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(
|
|
60
|
+
originalModuleSpecifier,
|
|
61
|
+
{
|
|
62
|
+
filePath,
|
|
63
|
+
options,
|
|
64
|
+
}
|
|
65
|
+
)
|
|
66
|
+
if (fullySpecifiedModuleSpecifier) {
|
|
67
|
+
node.source.value = fullySpecifiedModuleSpecifier
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const exportDeclarationVisitor = (path, state) => {
|
|
71
|
+
const filePath = state.file.opts.filename
|
|
72
|
+
if (!filePath) return
|
|
73
|
+
const { node } = path
|
|
74
|
+
if (node.exportKind === 'type') return
|
|
75
|
+
const source = node.source
|
|
76
|
+
if (!source) return
|
|
77
|
+
const originalModuleSpecifier = source.value
|
|
78
|
+
const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(
|
|
79
|
+
originalModuleSpecifier,
|
|
80
|
+
{
|
|
81
|
+
filePath,
|
|
82
|
+
options,
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
if (fullySpecifiedModuleSpecifier) {
|
|
86
|
+
source.value = fullySpecifiedModuleSpecifier
|
|
84
87
|
}
|
|
88
|
+
}
|
|
89
|
+
const importVisitor = (path, state) => {
|
|
90
|
+
const filePath = state.file.opts.filename
|
|
91
|
+
if (!filePath) return
|
|
92
|
+
const parent = path.parent
|
|
93
|
+
if (parent.type !== 'CallExpression') {
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
const firstArgOfImportCall = parent.arguments[0]
|
|
97
|
+
if (firstArgOfImportCall.type !== 'StringLiteral') {
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
const originalModuleSpecifier = firstArgOfImportCall.value
|
|
101
|
+
const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(
|
|
102
|
+
originalModuleSpecifier,
|
|
103
|
+
{
|
|
104
|
+
filePath,
|
|
105
|
+
options,
|
|
106
|
+
}
|
|
107
|
+
)
|
|
108
|
+
if (fullySpecifiedModuleSpecifier) {
|
|
109
|
+
firstArgOfImportCall.value = fullySpecifiedModuleSpecifier
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const memberExpressionVisitor = (path) => {
|
|
113
|
+
if (!options.convertProcessEnvToImportMetaEnv) return
|
|
114
|
+
const { node } = path
|
|
115
|
+
if (
|
|
116
|
+
node.object.type === 'MemberExpression' &&
|
|
117
|
+
node.object.object.type === 'Identifier' &&
|
|
118
|
+
node.object.object.name === 'process' &&
|
|
119
|
+
node.object.property.type === 'Identifier' &&
|
|
120
|
+
node.object.property.name === 'env'
|
|
121
|
+
) {
|
|
122
|
+
if (node.property.type === 'Identifier' && node.property.name === 'NODE_ENV') {
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
node.object = t.memberExpression(
|
|
126
|
+
t.metaProperty(t.identifier('import'), t.identifier('meta')),
|
|
127
|
+
t.identifier('env')
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
85
131
|
return {
|
|
86
132
|
name: 'babel-plugin-fully-specified',
|
|
87
133
|
visitor: {
|
|
88
134
|
ImportDeclaration: importDeclarationVisitor,
|
|
89
135
|
ExportNamedDeclaration: exportDeclarationVisitor,
|
|
90
136
|
ExportAllDeclaration: exportDeclarationVisitor,
|
|
91
|
-
Import:
|
|
92
|
-
|
|
93
|
-
if (!filePath) return
|
|
94
|
-
const parent = path.parent
|
|
95
|
-
if (parent.type !== 'CallExpression') return
|
|
96
|
-
const firstArgOfImportCall = parent.arguments[0]
|
|
97
|
-
if (firstArgOfImportCall.type !== 'StringLiteral') return
|
|
98
|
-
const originalModuleSpecifier = firstArgOfImportCall.value,
|
|
99
|
-
fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(
|
|
100
|
-
originalModuleSpecifier,
|
|
101
|
-
{
|
|
102
|
-
filePath,
|
|
103
|
-
options,
|
|
104
|
-
}
|
|
105
|
-
)
|
|
106
|
-
fullySpecifiedModuleSpecifier &&
|
|
107
|
-
(firstArgOfImportCall.value = fullySpecifiedModuleSpecifier)
|
|
108
|
-
},
|
|
109
|
-
MemberExpression: (path) => {
|
|
110
|
-
if (!options.convertProcessEnvToImportMetaEnv) return
|
|
111
|
-
const { node } = path
|
|
112
|
-
if (
|
|
113
|
-
node.object.type === 'MemberExpression' &&
|
|
114
|
-
node.object.object.type === 'Identifier' &&
|
|
115
|
-
node.object.object.name === 'process' &&
|
|
116
|
-
node.object.property.type === 'Identifier' &&
|
|
117
|
-
node.object.property.name === 'env'
|
|
118
|
-
) {
|
|
119
|
-
if (node.property.type === 'Identifier' && node.property.name === 'NODE_ENV')
|
|
120
|
-
return
|
|
121
|
-
node.object = t.memberExpression(
|
|
122
|
-
t.metaProperty(t.identifier('import'), t.identifier('meta')),
|
|
123
|
-
t.identifier('env')
|
|
124
|
-
)
|
|
125
|
-
}
|
|
126
|
-
},
|
|
137
|
+
Import: importVisitor,
|
|
138
|
+
MemberExpression: memberExpressionVisitor,
|
|
127
139
|
},
|
|
128
140
|
}
|
|
129
141
|
}
|
|
130
142
|
function normalizeOptions(rawOptions) {
|
|
131
143
|
const options = { ...DEFAULT_OPTIONS, ...rawOptions }
|
|
132
|
-
|
|
144
|
+
if (
|
|
133
145
|
rawOptions.esExtensionDefault &&
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
)
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
146
|
+
!rawOptions.tryExtensions &&
|
|
147
|
+
rawOptions.esExtensionDefault !== DEFAULT_OPTIONS.esExtensionDefault
|
|
148
|
+
) {
|
|
149
|
+
options.tryExtensions = [
|
|
150
|
+
rawOptions.esExtensionDefault,
|
|
151
|
+
...DEFAULT_OPTIONS.tryExtensions.filter(
|
|
152
|
+
(extension) => extension !== rawOptions.esExtensionDefault
|
|
153
|
+
),
|
|
154
|
+
]
|
|
155
|
+
}
|
|
156
|
+
return options
|
|
144
157
|
}
|
|
145
158
|
function getFullySpecifiedModuleSpecifier(
|
|
146
159
|
originalModuleSpecifier,
|
|
147
160
|
{ filePath, options }
|
|
148
161
|
) {
|
|
149
|
-
const fileExt = (0, import_node_path.extname)(filePath)
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
162
|
+
const fileExt = (0, import_node_path.extname)(filePath)
|
|
163
|
+
const fileDir = (0, import_node_path.dirname)(filePath)
|
|
164
|
+
const isDirectory = isLocalDirectory(
|
|
165
|
+
(0, import_node_path.resolve)(fileDir, originalModuleSpecifier)
|
|
166
|
+
)
|
|
167
|
+
const currentModuleExtension = (0, import_node_path.extname)(originalModuleSpecifier)
|
|
168
|
+
const { tryExtensions, esExtensions, esExtensionDefault, ensureFileExists } = options
|
|
169
|
+
const targetModule = evaluateTargetModule({
|
|
170
|
+
moduleSpecifier: originalModuleSpecifier,
|
|
171
|
+
filenameDirectory: fileDir,
|
|
172
|
+
filenameExtension: fileExt,
|
|
173
|
+
currentModuleExtension,
|
|
174
|
+
isDirectory,
|
|
175
|
+
tryExtensions,
|
|
176
|
+
esExtensions,
|
|
177
|
+
esExtensionDefault,
|
|
178
|
+
ensureFileExists,
|
|
179
|
+
})
|
|
180
|
+
if (targetModule === false) {
|
|
181
|
+
return null
|
|
182
|
+
}
|
|
183
|
+
return targetModule
|
|
168
184
|
}
|
|
169
185
|
function isLocalDirectory(absoluteDirectory) {
|
|
170
186
|
return (
|
|
@@ -172,6 +188,15 @@ function isLocalDirectory(absoluteDirectory) {
|
|
|
172
188
|
(0, import_node_fs.lstatSync)(absoluteDirectory).isDirectory()
|
|
173
189
|
)
|
|
174
190
|
}
|
|
191
|
+
function isNativeOutput(esExtensionDefault) {
|
|
192
|
+
return esExtensionDefault.startsWith('.native')
|
|
193
|
+
}
|
|
194
|
+
function hasPlatformSibling(absoluteBasePath) {
|
|
195
|
+
return (
|
|
196
|
+
(0, import_node_fs.existsSync)(`${absoluteBasePath}.ios.js`) ||
|
|
197
|
+
(0, import_node_fs.existsSync)(`${absoluteBasePath}.android.js`)
|
|
198
|
+
)
|
|
199
|
+
}
|
|
175
200
|
function evaluateTargetModule({
|
|
176
201
|
moduleSpecifier,
|
|
177
202
|
currentModuleExtension,
|
|
@@ -185,25 +210,37 @@ function evaluateTargetModule({
|
|
|
185
210
|
}) {
|
|
186
211
|
const targetFile = (0, import_node_path.resolve)(filenameDirectory, moduleSpecifier)
|
|
187
212
|
if (ensureFileExists) {
|
|
188
|
-
|
|
189
|
-
|
|
213
|
+
if (isNativeOutput(esExtensionDefault) && hasPlatformSibling(targetFile)) {
|
|
214
|
+
return false
|
|
215
|
+
}
|
|
216
|
+
for (const extension of tryExtensions) {
|
|
217
|
+
if ((0, import_node_fs.existsSync)(targetFile + extension)) {
|
|
190
218
|
return moduleSpecifier + esExtensionDefault
|
|
191
|
-
|
|
192
|
-
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (currentModuleExtension && !esExtensions.includes(currentModuleExtension)) {
|
|
222
|
+
return false
|
|
223
|
+
}
|
|
193
224
|
if (isDirectory) {
|
|
194
|
-
const indexModuleSpecifier = `${moduleSpecifier.replace(/\/$/, '')}/index
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
225
|
+
const indexModuleSpecifier = `${moduleSpecifier.replace(/\/$/, '')}/index`
|
|
226
|
+
const indexTargetFile = (0, import_node_path.resolve)(
|
|
227
|
+
filenameDirectory,
|
|
228
|
+
indexModuleSpecifier
|
|
229
|
+
)
|
|
230
|
+
if (isNativeOutput(esExtensionDefault) && hasPlatformSibling(indexTargetFile)) {
|
|
231
|
+
return false
|
|
232
|
+
}
|
|
233
|
+
for (const extension of tryExtensions) {
|
|
234
|
+
if ((0, import_node_fs.existsSync)(indexTargetFile + extension)) {
|
|
201
235
|
return indexModuleSpecifier + esExtensionDefault
|
|
236
|
+
}
|
|
237
|
+
}
|
|
202
238
|
}
|
|
203
|
-
} else
|
|
204
|
-
return
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
239
|
+
} else if (esExtensions.includes(filenameExtension)) {
|
|
240
|
+
return moduleSpecifier + esExtensionDefault
|
|
241
|
+
} else {
|
|
242
|
+
return moduleSpecifier + esExtensionDefault
|
|
243
|
+
}
|
|
244
|
+
return false
|
|
208
245
|
}
|
|
209
246
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
|
-
"mappings": "
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAsC;AACtC,uBAA0C;AAG1C,QAAmB;AAoBnB,MAAM,kBAAkB;AAAA,EACtB,kBAAkB;AAAA,EAClB,oBAAoB;AAAA,EACpB,eAAe,CAAC,KAAK;AAAA,EACrB,cAAc,CAAC,MAAM;AAAA,EACrB,kCAAkC;AACpC;AAEe,SAAR,eACL,KACA,aAA6C,CAAC,GACnC;AACX,MAAI,cAAc,CAAC;AAEnB,QAAM,UAAU,iBAAiB,UAAU;AAG3C,QAAM,2BAA2B,CAC/B,MACA,UACG;AACH,UAAM,WAAW,MAAM,KAAK,KAAK;AACjC,QAAI,CAAC,SAAU;AAEf,UAAM,EAAE,KAAK,IAAI;AACjB,QAAI,KAAK,eAAe,OAAQ;AAEhC,UAAM,0BAA0B,KAAK,OAAO;AAC5C,UAAM,gCAAgC;AAAA,MACpC;AAAA,MACA;AAAA,QACE;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAEA,QAAI,+BAA+B;AACjC,WAAK,OAAO,QAAQ;AAAA,IACtB;AAAA,EACF;AAGA,QAAM,2BAA2B,CAC/B,MACA,UACG;AACH,UAAM,WAAW,MAAM,KAAK,KAAK;AACjC,QAAI,CAAC,SAAU;AAEf,UAAM,EAAE,KAAK,IAAI;AACjB,QAAI,KAAK,eAAe,OAAQ;AAEhC,UAAM,SAAS,KAAK;AACpB,QAAI,CAAC,OAAQ;AAEb,UAAM,0BAA0B,OAAO;AACvC,UAA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A,EACA;AACF,GAAwB;AACtB,QAAM,iBAAa,0BAAQ,mBAAmB,eAAe;AAE7D,MAAI,kBAAkB;AACpB,QAAI,eAAe,kBAAkB,KAAK,mBAAmB,UAAU,GAAG;AACxE,aAAO;AAAA,IACT;AAEA,eAAW,aAAa,eAAe;AACrC,cAAI,2BAAW,aAAa,SAAS,GAAG;AACtC,eAAO,kBAAkB;AAAA,MAC3B;AAAA,IACF;AAEA,QAAI,0BAA0B,CAAC,aAAa,SAAS,sBAAsB,GAAG;AAC5E,aAAO;AAAA,IACT;AAEA,QAAI,aAAa;AACf,YAAM,uBAAuB,GAAG,gBAAgB,QAAQ,OAAO,EAAE,CAAC;AAClE,YAAM,sBAAkB,0BAAQ,mBAAmB,oBAAoB;AAEvE,UAAI,eAAe,kBAAkB,KAAK,mBAAmB,eAAe,GAAG;AAC7E,eAAO;AAAA,MACT;AAEA,iBAAW,aAAa,eAAe;AACrC,gBAAI,2BAAW,kBAAkB,SAAS,GAAG;AAC3C,iBAAO,uBAAuB;AAAA,QAChC;AAAA,MACF;AAAA,IACF;AAAA,EACF,WAAW,aAAa,SAAS,iBAAiB,GAAG;AACnD,WAAO,kBAAkB;AAAA,EAC3B,OAAO;AACL,WAAO,kBAAkB;AAAA,EAC3B;AAEA,SAAO;AACT;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync, lstatSync } from 'node:fs'
|
|
2
2
|
import { dirname, extname, resolve } from 'node:path'
|
|
3
|
+
|
|
3
4
|
function fullySpecifyCommonJS(api, options) {
|
|
4
5
|
api.assertVersion(7)
|
|
5
6
|
return {
|
|
@@ -8,9 +9,7 @@ function fullySpecifyCommonJS(api, options) {
|
|
|
8
9
|
CallExpression(path, state) {
|
|
9
10
|
const callee = path.get('callee')
|
|
10
11
|
if (
|
|
11
|
-
callee.isIdentifier({
|
|
12
|
-
name: 'require',
|
|
13
|
-
}) &&
|
|
12
|
+
callee.isIdentifier({ name: 'require' }) &&
|
|
14
13
|
path.node.arguments.length === 1
|
|
15
14
|
) {
|
|
16
15
|
const arg = path.node.arguments[0]
|
|
@@ -33,7 +32,22 @@ function fullySpecifyCommonJS(api, options) {
|
|
|
33
32
|
if (!hasModuleExtension) {
|
|
34
33
|
const resolvedPath = resolve(fileDir, moduleSpecifier)
|
|
35
34
|
let newModuleSpecifier = moduleSpecifier
|
|
35
|
+
if (
|
|
36
|
+
cjsExtension.startsWith('.native') &&
|
|
37
|
+
(existsSync(`${resolvedPath}.ios.js`) ||
|
|
38
|
+
existsSync(`${resolvedPath}.android.js`))
|
|
39
|
+
) {
|
|
40
|
+
return
|
|
41
|
+
}
|
|
36
42
|
if (isLocalDirectory(resolvedPath)) {
|
|
43
|
+
const indexBase = resolve(resolvedPath, 'index')
|
|
44
|
+
if (
|
|
45
|
+
cjsExtension.startsWith('.native') &&
|
|
46
|
+
(existsSync(`${indexBase}.ios.js`) ||
|
|
47
|
+
existsSync(`${indexBase}.android.js`))
|
|
48
|
+
) {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
37
51
|
const indexPath = resolve(resolvedPath, 'index' + jsExtension)
|
|
38
52
|
if (existsSync(indexPath)) {
|
|
39
53
|
if (!newModuleSpecifier.endsWith('/')) {
|
|
@@ -63,5 +77,6 @@ function fullySpecifyCommonJS(api, options) {
|
|
|
63
77
|
function isLocalDirectory(absolutePath) {
|
|
64
78
|
return existsSync(absolutePath) && lstatSync(absolutePath).isDirectory()
|
|
65
79
|
}
|
|
80
|
+
|
|
66
81
|
export { fullySpecifyCommonJS as default }
|
|
67
82
|
//# sourceMappingURL=commonjs.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"commonjs.js","names":[],"sources":["esm/commonjs.js"],"sourcesContent":["import { existsSync, lstatSync } from \"node:fs\";\nimport { dirname, extname, resolve } from \"node:path\";\nfunction fullySpecifyCommonJS(api, options) {\n api.assertVersion(7);\n return {\n name: \"babel-plugin-fully-specified-cjs\",\n visitor: {\n CallExpression(path, state) {\n const callee = path.get(\"callee\");\n if (callee.isIdentifier({ name: \"require\" }) && path.node.arguments.length === 1) {\n const arg = path.node.arguments[0];\n if (arg.type === \"StringLiteral\") {\n let moduleSpecifier = arg.value;\n if (moduleSpecifier.startsWith(\".\") || moduleSpecifier.startsWith(\"/\")) {\n const filePath = state.file.opts.filename;\n if (!filePath) return;\n const fileDir = dirname(filePath);\n const cjsExtension = options.esExtensionDefault || \".cjs\";\n const jsExtension = \".js\";\n const specifierExtension = extname(moduleSpecifier);\n const hasModuleExtension = [\n \".js\",\n \".cjs\",\n \".mjs\",\n \".json\",\n \".node\"\n ].includes(specifierExtension);\n if (!hasModuleExtension) {\n const resolvedPath = resolve(fileDir, moduleSpecifier);\n let newModuleSpecifier = moduleSpecifier;\n if (cjsExtension.startsWith(\".native\") && (existsSync(`${resolvedPath}.ios.js`) || existsSync(`${resolvedPath}.android.js`))) {\n return;\n }\n if (isLocalDirectory(resolvedPath)) {\n const indexBase = resolve(resolvedPath, \"index\");\n if (cjsExtension.startsWith(\".native\") && (existsSync(`${indexBase}.ios.js`) || existsSync(`${indexBase}.android.js`))) {\n return;\n }\n const indexPath = resolve(resolvedPath, \"index\" + jsExtension);\n if (existsSync(indexPath)) {\n if (!newModuleSpecifier.endsWith(\"/\")) {\n newModuleSpecifier += \"/\";\n }\n newModuleSpecifier += \"index\" + cjsExtension;\n arg.value = newModuleSpecifier;\n return;\n }\n }\n if (existsSync(resolvedPath + jsExtension) || existsSync(resolvedPath + cjsExtension)) {\n newModuleSpecifier += cjsExtension;\n arg.value = newModuleSpecifier;\n return;\n }\n }\n }\n }\n }\n }\n }\n };\n}\nfunction isLocalDirectory(absolutePath) {\n return existsSync(absolutePath) && lstatSync(absolutePath).isDirectory();\n}\nexport {\n fullySpecifyCommonJS as default\n};\n//# sourceMappingURL=commonjs.js.map\n"],"mappings":";;;;AAEA,SAAS,qBAAqB,KAAK,SAAS;CAC1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,GAAG;QACrF,sBAAsB;QACtB,IAAI,QAAQ;QACZ;OACF;MACF;KACF;IACF;GACF;EACF,EACF;CACF;AACF;AACA,SAAS,iBAAiB,cAAc;CACtC,OAAO,WAAW,YAAY,KAAK,UAAU,YAAY,CAAC,CAAC,YAAY;AACzE"}
|
package/permanent/esm/index.mjs
CHANGED
|
@@ -1,133 +1,162 @@
|
|
|
1
1
|
import { existsSync, lstatSync } from 'node:fs'
|
|
2
2
|
import { dirname, extname, resolve } from 'node:path'
|
|
3
3
|
import * as t from '@babel/types'
|
|
4
|
+
|
|
4
5
|
const DEFAULT_OPTIONS = {
|
|
5
|
-
ensureFileExists:
|
|
6
|
+
ensureFileExists: true,
|
|
6
7
|
esExtensionDefault: '.mjs',
|
|
7
8
|
tryExtensions: ['.js'],
|
|
8
9
|
esExtensions: ['.mjs'],
|
|
9
|
-
convertProcessEnvToImportMetaEnv:
|
|
10
|
+
convertProcessEnvToImportMetaEnv: false,
|
|
10
11
|
}
|
|
11
12
|
function FullySpecified(api, rawOptions = {}) {
|
|
12
13
|
api.assertVersion(7)
|
|
13
|
-
const options = normalizeOptions(rawOptions)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
14
|
+
const options = normalizeOptions(rawOptions)
|
|
15
|
+
const importDeclarationVisitor = (path, state) => {
|
|
16
|
+
const filePath = state.file.opts.filename
|
|
17
|
+
if (!filePath) return
|
|
18
|
+
const { node } = path
|
|
19
|
+
if (node.importKind === 'type') return
|
|
20
|
+
const originalModuleSpecifier = node.source.value
|
|
21
|
+
const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(
|
|
22
|
+
originalModuleSpecifier,
|
|
23
|
+
{
|
|
24
|
+
filePath,
|
|
25
|
+
options,
|
|
26
|
+
}
|
|
27
|
+
)
|
|
28
|
+
if (fullySpecifiedModuleSpecifier) {
|
|
29
|
+
node.source.value = fullySpecifiedModuleSpecifier
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const exportDeclarationVisitor = (path, state) => {
|
|
33
|
+
const filePath = state.file.opts.filename
|
|
34
|
+
if (!filePath) return
|
|
35
|
+
const { node } = path
|
|
36
|
+
if (node.exportKind === 'type') return
|
|
37
|
+
const source = node.source
|
|
38
|
+
if (!source) return
|
|
39
|
+
const originalModuleSpecifier = source.value
|
|
40
|
+
const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(
|
|
41
|
+
originalModuleSpecifier,
|
|
42
|
+
{
|
|
43
|
+
filePath,
|
|
44
|
+
options,
|
|
45
|
+
}
|
|
46
|
+
)
|
|
47
|
+
if (fullySpecifiedModuleSpecifier) {
|
|
48
|
+
source.value = fullySpecifiedModuleSpecifier
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const importVisitor = (path, state) => {
|
|
52
|
+
const filePath = state.file.opts.filename
|
|
53
|
+
if (!filePath) return
|
|
54
|
+
const parent = path.parent
|
|
55
|
+
if (parent.type !== 'CallExpression') {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
const firstArgOfImportCall = parent.arguments[0]
|
|
59
|
+
if (firstArgOfImportCall.type !== 'StringLiteral') {
|
|
60
|
+
return
|
|
45
61
|
}
|
|
62
|
+
const originalModuleSpecifier = firstArgOfImportCall.value
|
|
63
|
+
const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(
|
|
64
|
+
originalModuleSpecifier,
|
|
65
|
+
{
|
|
66
|
+
filePath,
|
|
67
|
+
options,
|
|
68
|
+
}
|
|
69
|
+
)
|
|
70
|
+
if (fullySpecifiedModuleSpecifier) {
|
|
71
|
+
firstArgOfImportCall.value = fullySpecifiedModuleSpecifier
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const memberExpressionVisitor = (path) => {
|
|
75
|
+
if (!options.convertProcessEnvToImportMetaEnv) return
|
|
76
|
+
const { node } = path
|
|
77
|
+
if (
|
|
78
|
+
node.object.type === 'MemberExpression' &&
|
|
79
|
+
node.object.object.type === 'Identifier' &&
|
|
80
|
+
node.object.object.name === 'process' &&
|
|
81
|
+
node.object.property.type === 'Identifier' &&
|
|
82
|
+
node.object.property.name === 'env'
|
|
83
|
+
) {
|
|
84
|
+
if (node.property.type === 'Identifier' && node.property.name === 'NODE_ENV') {
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
node.object = t.memberExpression(
|
|
88
|
+
t.metaProperty(t.identifier('import'), t.identifier('meta')),
|
|
89
|
+
t.identifier('env')
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
46
93
|
return {
|
|
47
94
|
name: 'babel-plugin-fully-specified',
|
|
48
95
|
visitor: {
|
|
49
96
|
ImportDeclaration: importDeclarationVisitor,
|
|
50
97
|
ExportNamedDeclaration: exportDeclarationVisitor,
|
|
51
98
|
ExportAllDeclaration: exportDeclarationVisitor,
|
|
52
|
-
Import:
|
|
53
|
-
|
|
54
|
-
if (!filePath) return
|
|
55
|
-
const parent = path.parent
|
|
56
|
-
if (parent.type !== 'CallExpression') return
|
|
57
|
-
const firstArgOfImportCall = parent.arguments[0]
|
|
58
|
-
if (firstArgOfImportCall.type !== 'StringLiteral') return
|
|
59
|
-
const originalModuleSpecifier = firstArgOfImportCall.value,
|
|
60
|
-
fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(
|
|
61
|
-
originalModuleSpecifier,
|
|
62
|
-
{
|
|
63
|
-
filePath,
|
|
64
|
-
options,
|
|
65
|
-
}
|
|
66
|
-
)
|
|
67
|
-
fullySpecifiedModuleSpecifier &&
|
|
68
|
-
(firstArgOfImportCall.value = fullySpecifiedModuleSpecifier)
|
|
69
|
-
},
|
|
70
|
-
MemberExpression: (path) => {
|
|
71
|
-
if (!options.convertProcessEnvToImportMetaEnv) return
|
|
72
|
-
const { node } = path
|
|
73
|
-
if (
|
|
74
|
-
node.object.type === 'MemberExpression' &&
|
|
75
|
-
node.object.object.type === 'Identifier' &&
|
|
76
|
-
node.object.object.name === 'process' &&
|
|
77
|
-
node.object.property.type === 'Identifier' &&
|
|
78
|
-
node.object.property.name === 'env'
|
|
79
|
-
) {
|
|
80
|
-
if (node.property.type === 'Identifier' && node.property.name === 'NODE_ENV')
|
|
81
|
-
return
|
|
82
|
-
node.object = t.memberExpression(
|
|
83
|
-
t.metaProperty(t.identifier('import'), t.identifier('meta')),
|
|
84
|
-
t.identifier('env')
|
|
85
|
-
)
|
|
86
|
-
}
|
|
87
|
-
},
|
|
99
|
+
Import: importVisitor,
|
|
100
|
+
MemberExpression: memberExpressionVisitor,
|
|
88
101
|
},
|
|
89
102
|
}
|
|
90
103
|
}
|
|
91
104
|
function normalizeOptions(rawOptions) {
|
|
92
|
-
const options = {
|
|
93
|
-
|
|
105
|
+
const options = {
|
|
106
|
+
...DEFAULT_OPTIONS,
|
|
107
|
+
...rawOptions,
|
|
108
|
+
}
|
|
109
|
+
if (
|
|
94
110
|
rawOptions.esExtensionDefault &&
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
)
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
111
|
+
!rawOptions.tryExtensions &&
|
|
112
|
+
rawOptions.esExtensionDefault !== DEFAULT_OPTIONS.esExtensionDefault
|
|
113
|
+
) {
|
|
114
|
+
options.tryExtensions = [
|
|
115
|
+
rawOptions.esExtensionDefault,
|
|
116
|
+
...DEFAULT_OPTIONS.tryExtensions.filter(
|
|
117
|
+
(extension) => extension !== rawOptions.esExtensionDefault
|
|
118
|
+
),
|
|
119
|
+
]
|
|
120
|
+
}
|
|
121
|
+
return options
|
|
105
122
|
}
|
|
106
123
|
function getFullySpecifiedModuleSpecifier(
|
|
107
124
|
originalModuleSpecifier,
|
|
108
125
|
{ filePath, options }
|
|
109
126
|
) {
|
|
110
|
-
const fileExt = extname(filePath)
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
+
const fileExt = extname(filePath)
|
|
128
|
+
const fileDir = dirname(filePath)
|
|
129
|
+
const isDirectory = isLocalDirectory(resolve(fileDir, originalModuleSpecifier))
|
|
130
|
+
const currentModuleExtension = extname(originalModuleSpecifier)
|
|
131
|
+
const { tryExtensions, esExtensions, esExtensionDefault, ensureFileExists } = options
|
|
132
|
+
const targetModule = evaluateTargetModule({
|
|
133
|
+
moduleSpecifier: originalModuleSpecifier,
|
|
134
|
+
filenameDirectory: fileDir,
|
|
135
|
+
filenameExtension: fileExt,
|
|
136
|
+
currentModuleExtension,
|
|
137
|
+
isDirectory,
|
|
138
|
+
tryExtensions,
|
|
139
|
+
esExtensions,
|
|
140
|
+
esExtensionDefault,
|
|
141
|
+
ensureFileExists,
|
|
142
|
+
})
|
|
143
|
+
if (targetModule === false) {
|
|
144
|
+
return null
|
|
145
|
+
}
|
|
146
|
+
return targetModule
|
|
127
147
|
}
|
|
128
148
|
function isLocalDirectory(absoluteDirectory) {
|
|
129
149
|
return existsSync(absoluteDirectory) && lstatSync(absoluteDirectory).isDirectory()
|
|
130
150
|
}
|
|
151
|
+
function isNativeOutput(esExtensionDefault) {
|
|
152
|
+
return esExtensionDefault.startsWith('.native')
|
|
153
|
+
}
|
|
154
|
+
function hasPlatformSibling(absoluteBasePath) {
|
|
155
|
+
return (
|
|
156
|
+
existsSync(`${absoluteBasePath}.ios.js`) ||
|
|
157
|
+
existsSync(`${absoluteBasePath}.android.js`)
|
|
158
|
+
)
|
|
159
|
+
}
|
|
131
160
|
function evaluateTargetModule({
|
|
132
161
|
moduleSpecifier,
|
|
133
162
|
currentModuleExtension,
|
|
@@ -141,22 +170,36 @@ function evaluateTargetModule({
|
|
|
141
170
|
}) {
|
|
142
171
|
const targetFile = resolve(filenameDirectory, moduleSpecifier)
|
|
143
172
|
if (ensureFileExists) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
173
|
+
if (isNativeOutput(esExtensionDefault) && hasPlatformSibling(targetFile)) {
|
|
174
|
+
return false
|
|
175
|
+
}
|
|
176
|
+
for (const extension of tryExtensions) {
|
|
177
|
+
if (existsSync(targetFile + extension)) {
|
|
178
|
+
return moduleSpecifier + esExtensionDefault
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (currentModuleExtension && !esExtensions.includes(currentModuleExtension)) {
|
|
182
|
+
return false
|
|
183
|
+
}
|
|
148
184
|
if (isDirectory) {
|
|
149
|
-
const indexModuleSpecifier = `${moduleSpecifier.replace(/\/$/, '')}/index
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
185
|
+
const indexModuleSpecifier = `${moduleSpecifier.replace(/\/$/, '')}/index`
|
|
186
|
+
const indexTargetFile = resolve(filenameDirectory, indexModuleSpecifier)
|
|
187
|
+
if (isNativeOutput(esExtensionDefault) && hasPlatformSibling(indexTargetFile)) {
|
|
188
|
+
return false
|
|
189
|
+
}
|
|
190
|
+
for (const extension of tryExtensions) {
|
|
191
|
+
if (existsSync(indexTargetFile + extension)) {
|
|
153
192
|
return indexModuleSpecifier + esExtensionDefault
|
|
193
|
+
}
|
|
194
|
+
}
|
|
154
195
|
}
|
|
155
|
-
} else
|
|
156
|
-
return
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
196
|
+
} else if (esExtensions.includes(filenameExtension)) {
|
|
197
|
+
return moduleSpecifier + esExtensionDefault
|
|
198
|
+
} else {
|
|
199
|
+
return moduleSpecifier + esExtensionDefault
|
|
200
|
+
}
|
|
201
|
+
return false
|
|
160
202
|
}
|
|
203
|
+
|
|
161
204
|
export { FullySpecified as default }
|
|
162
205
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["esm/index.js"],"sourcesContent":["import { existsSync, lstatSync } from \"node:fs\";\nimport { dirname, extname, resolve } from \"node:path\";\nimport * as t from \"@babel/types\";\nconst DEFAULT_OPTIONS = {\n ensureFileExists: true,\n esExtensionDefault: \".mjs\",\n tryExtensions: [\".js\"],\n esExtensions: [\".mjs\"],\n convertProcessEnvToImportMetaEnv: false\n};\nfunction FullySpecified(api, rawOptions = {}) {\n api.assertVersion(7);\n const options = normalizeOptions(rawOptions);\n const importDeclarationVisitor = (path, state) => {\n const filePath = state.file.opts.filename;\n if (!filePath) return;\n const { node } = path;\n if (node.importKind === \"type\") return;\n const originalModuleSpecifier = node.source.value;\n const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(\n originalModuleSpecifier,\n {\n filePath,\n options\n }\n );\n if (fullySpecifiedModuleSpecifier) {\n node.source.value = fullySpecifiedModuleSpecifier;\n }\n };\n const exportDeclarationVisitor = (path, state) => {\n const filePath = state.file.opts.filename;\n if (!filePath) return;\n const { node } = path;\n if (node.exportKind === \"type\") return;\n const source = node.source;\n if (!source) return;\n const originalModuleSpecifier = source.value;\n const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(\n originalModuleSpecifier,\n {\n filePath,\n options\n }\n );\n if (fullySpecifiedModuleSpecifier) {\n source.value = fullySpecifiedModuleSpecifier;\n }\n };\n const importVisitor = (path, state) => {\n const filePath = state.file.opts.filename;\n if (!filePath) return;\n const parent = path.parent;\n if (parent.type !== \"CallExpression\") {\n return;\n }\n const firstArgOfImportCall = parent.arguments[0];\n if (firstArgOfImportCall.type !== \"StringLiteral\") {\n return;\n }\n const originalModuleSpecifier = firstArgOfImportCall.value;\n const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(\n originalModuleSpecifier,\n {\n filePath,\n options\n }\n );\n if (fullySpecifiedModuleSpecifier) {\n firstArgOfImportCall.value = fullySpecifiedModuleSpecifier;\n }\n };\n const memberExpressionVisitor = (path) => {\n if (!options.convertProcessEnvToImportMetaEnv) return;\n const { node } = path;\n if (node.object.type === \"MemberExpression\" && node.object.object.type === \"Identifier\" && node.object.object.name === \"process\" && node.object.property.type === \"Identifier\" && node.object.property.name === \"env\") {\n if (node.property.type === \"Identifier\" && node.property.name === \"NODE_ENV\") {\n return;\n }\n node.object = t.memberExpression(\n t.metaProperty(t.identifier(\"import\"), t.identifier(\"meta\")),\n t.identifier(\"env\")\n );\n }\n };\n return {\n name: \"babel-plugin-fully-specified\",\n visitor: {\n ImportDeclaration: importDeclarationVisitor,\n ExportNamedDeclaration: exportDeclarationVisitor,\n ExportAllDeclaration: exportDeclarationVisitor,\n Import: importVisitor,\n MemberExpression: memberExpressionVisitor\n }\n };\n}\nfunction normalizeOptions(rawOptions) {\n const options = { ...DEFAULT_OPTIONS, ...rawOptions };\n if (rawOptions.esExtensionDefault && !rawOptions.tryExtensions && rawOptions.esExtensionDefault !== DEFAULT_OPTIONS.esExtensionDefault) {\n options.tryExtensions = [\n rawOptions.esExtensionDefault,\n ...DEFAULT_OPTIONS.tryExtensions.filter(\n (extension) => extension !== rawOptions.esExtensionDefault\n )\n ];\n }\n return options;\n}\nfunction getFullySpecifiedModuleSpecifier(originalModuleSpecifier, {\n filePath,\n options\n}) {\n const fileExt = extname(filePath);\n const fileDir = dirname(filePath);\n const isDirectory = isLocalDirectory(resolve(fileDir, originalModuleSpecifier));\n const currentModuleExtension = extname(originalModuleSpecifier);\n const { tryExtensions, esExtensions, esExtensionDefault, ensureFileExists } = options;\n const targetModule = evaluateTargetModule({\n moduleSpecifier: originalModuleSpecifier,\n filenameDirectory: fileDir,\n filenameExtension: fileExt,\n currentModuleExtension,\n isDirectory,\n tryExtensions,\n esExtensions,\n esExtensionDefault,\n ensureFileExists\n });\n if (targetModule === false) {\n return null;\n }\n return targetModule;\n}\nfunction isLocalDirectory(absoluteDirectory) {\n return existsSync(absoluteDirectory) && lstatSync(absoluteDirectory).isDirectory();\n}\nfunction isNativeOutput(esExtensionDefault) {\n return esExtensionDefault.startsWith(\".native\");\n}\nfunction hasPlatformSibling(absoluteBasePath) {\n return existsSync(`${absoluteBasePath}.ios.js`) || existsSync(`${absoluteBasePath}.android.js`);\n}\nfunction evaluateTargetModule({\n moduleSpecifier,\n currentModuleExtension,\n isDirectory,\n filenameDirectory,\n filenameExtension,\n tryExtensions,\n esExtensions,\n esExtensionDefault,\n ensureFileExists\n}) {\n const targetFile = resolve(filenameDirectory, moduleSpecifier);\n if (ensureFileExists) {\n if (isNativeOutput(esExtensionDefault) && hasPlatformSibling(targetFile)) {\n return false;\n }\n for (const extension of tryExtensions) {\n if (existsSync(targetFile + extension)) {\n return moduleSpecifier + esExtensionDefault;\n }\n }\n if (currentModuleExtension && !esExtensions.includes(currentModuleExtension)) {\n return false;\n }\n if (isDirectory) {\n const indexModuleSpecifier = `${moduleSpecifier.replace(/\\/$/, \"\")}/index`;\n const indexTargetFile = resolve(filenameDirectory, indexModuleSpecifier);\n if (isNativeOutput(esExtensionDefault) && hasPlatformSibling(indexTargetFile)) {\n return false;\n }\n for (const extension of tryExtensions) {\n if (existsSync(indexTargetFile + extension)) {\n return indexModuleSpecifier + esExtensionDefault;\n }\n }\n }\n } else if (esExtensions.includes(filenameExtension)) {\n return moduleSpecifier + esExtensionDefault;\n } else {\n return moduleSpecifier + esExtensionDefault;\n }\n return false;\n}\nexport {\n FullySpecified as default\n};\n//# sourceMappingURL=index.js.map\n"],"mappings":";;;;;AAGA,MAAM,kBAAkB;CACtB,kBAAkB;C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AAW,iBAAiB,KAAK,UAAU,iBAAiB,CAAC,CAAC,YAAY;AACnF;AACA,SAAS,eAAe,oBAAoB;CAC1C,OAAO,mBAAmB,WAAW,SAAS;AAChD;AACA,SAAS,mBAAmB,kBAAkB;CAC5C,OAAO,WAAW,GAAG,iBAAiB,QAAQ,KAAK,WAAW,GAAG,iBAAiB,YAAY;AAChG;AACA,SAAS,qBAAqB,EAC5B,iBACA,wBACA,aACA,mBACA,mBACA,eACA,cACA,oBACA,oBACC;CACD,MAAM,aAAa,QAAQ,mBAAmB,eAAe;CAC7D,IAAI,kBAAkB;EACpB,IAAI,eAAe,kBAAkB,KAAK,mBAAmB,UAAU,GAAG;GACxE,OAAO;EACT;EACA,KAAK,MAAM,aAAa,eAAe;GACrC,IAAI,WAAW,aAAa,SAAS,GAAG;IACtC,OAAO,kBAAkB;GAC3B;EACF;EACA,IAAI,0BAA0B,CAAC,aAAa,SAAS,sBAAsB,GAAG;GAC5E,OAAO;EACT;EACA,IAAI,aAAa;GACf,MAAM,uBAAuB,GAAG,gBAAgB,QAAQ,OAAO,EAAE,EAAE;GACnE,MAAM,kBAAkB,QAAQ,mBAAmB,oBAAoB;GACvE,IAAI,eAAe,kBAAkB,KAAK,mBAAmB,eAAe,GAAG;IAC7E,OAAO;GACT;GACA,KAAK,MAAM,aAAa,eAAe;IACrC,IAAI,WAAW,kBAAkB,SAAS,GAAG;KAC3C,OAAO,uBAAuB;IAChC;GACF;EACF;CACF,OAAO,IAAI,aAAa,SAAS,iBAAiB,GAAG;EACnD,OAAO,kBAAkB;CAC3B,OAAO;EACL,OAAO,kBAAkB;CAC3B;CACA,OAAO;AACT"}
|
package/src/commonjs.ts
CHANGED
|
@@ -46,8 +46,26 @@ export default function fullySpecifyCommonJS(
|
|
|
46
46
|
const resolvedPath = resolve(fileDir, moduleSpecifier)
|
|
47
47
|
let newModuleSpecifier = moduleSpecifier
|
|
48
48
|
|
|
49
|
+
// native outputs keep platform-split modules extensionless so
|
|
50
|
+
// the react native bundler resolves .ios/.android over .native
|
|
51
|
+
if (
|
|
52
|
+
cjsExtension.startsWith('.native') &&
|
|
53
|
+
(existsSync(`${resolvedPath}.ios.js`) ||
|
|
54
|
+
existsSync(`${resolvedPath}.android.js`))
|
|
55
|
+
) {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
49
59
|
// Check if the moduleSpecifier is a directory with an index.cjs file
|
|
50
60
|
if (isLocalDirectory(resolvedPath)) {
|
|
61
|
+
const indexBase = resolve(resolvedPath, 'index')
|
|
62
|
+
if (
|
|
63
|
+
cjsExtension.startsWith('.native') &&
|
|
64
|
+
(existsSync(`${indexBase}.ios.js`) ||
|
|
65
|
+
existsSync(`${indexBase}.android.js`))
|
|
66
|
+
) {
|
|
67
|
+
return
|
|
68
|
+
}
|
|
51
69
|
const indexPath = resolve(resolvedPath, 'index' + jsExtension)
|
|
52
70
|
if (existsSync(indexPath)) {
|
|
53
71
|
// Append '/' if not present
|
package/src/index.ts
CHANGED
|
@@ -236,6 +236,19 @@ function isLocalDirectory(absoluteDirectory: string) {
|
|
|
236
236
|
return existsSync(absoluteDirectory) && lstatSync(absoluteDirectory).isDirectory()
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
+
// native outputs keep platform-split modules extensionless so the react
|
|
240
|
+
// native bundler resolves .ios/.android over .native at bundle time
|
|
241
|
+
function isNativeOutput(esExtensionDefault: string) {
|
|
242
|
+
return esExtensionDefault.startsWith('.native')
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function hasPlatformSibling(absoluteBasePath: string) {
|
|
246
|
+
return (
|
|
247
|
+
existsSync(`${absoluteBasePath}.ios.js`) ||
|
|
248
|
+
existsSync(`${absoluteBasePath}.android.js`)
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
|
|
239
252
|
function evaluateTargetModule({
|
|
240
253
|
moduleSpecifier,
|
|
241
254
|
currentModuleExtension,
|
|
@@ -250,6 +263,10 @@ function evaluateTargetModule({
|
|
|
250
263
|
const targetFile = resolve(filenameDirectory, moduleSpecifier)
|
|
251
264
|
|
|
252
265
|
if (ensureFileExists) {
|
|
266
|
+
if (isNativeOutput(esExtensionDefault) && hasPlatformSibling(targetFile)) {
|
|
267
|
+
return false
|
|
268
|
+
}
|
|
269
|
+
|
|
253
270
|
for (const extension of tryExtensions) {
|
|
254
271
|
if (existsSync(targetFile + extension)) {
|
|
255
272
|
return moduleSpecifier + esExtensionDefault
|
|
@@ -264,6 +281,10 @@ function evaluateTargetModule({
|
|
|
264
281
|
const indexModuleSpecifier = `${moduleSpecifier.replace(/\/$/, '')}/index`
|
|
265
282
|
const indexTargetFile = resolve(filenameDirectory, indexModuleSpecifier)
|
|
266
283
|
|
|
284
|
+
if (isNativeOutput(esExtensionDefault) && hasPlatformSibling(indexTargetFile)) {
|
|
285
|
+
return false
|
|
286
|
+
}
|
|
287
|
+
|
|
267
288
|
for (const extension of tryExtensions) {
|
|
268
289
|
if (existsSync(indexTargetFile + extension)) {
|
|
269
290
|
return indexModuleSpecifier + esExtensionDefault
|
package/types/commonjs.d.ts.map
CHANGED
|
@@ -6,6 +6,6 @@
|
|
|
6
6
|
],
|
|
7
7
|
"version": 3,
|
|
8
8
|
"sourcesContent": [
|
|
9
|
-
"import { existsSync, lstatSync } from 'node:fs'\nimport { dirname, extname, resolve } from 'node:path'\n\nexport default function fullySpecifyCommonJS(\n api: any,\n options: {\n esExtensionDefault: string\n }\n): babel.PluginObj {\n api.assertVersion(7)\n\n return {\n name: 'babel-plugin-fully-specified-cjs',\n visitor: {\n CallExpression(path, state) {\n const callee = path.get('callee')\n\n if (\n callee.isIdentifier({ name: 'require' }) &&\n path.node.arguments.length === 1\n ) {\n const arg = path.node.arguments[0]\n if (arg.type === 'StringLiteral') {\n let moduleSpecifier = arg.value\n\n // Skip built-in modules and node_modules\n if (moduleSpecifier.startsWith('.') || moduleSpecifier.startsWith('/')) {\n const filePath = state.file.opts.filename\n if (!filePath) return // Cannot determine file path\n\n const fileDir = dirname(filePath)\n const cjsExtension = options.esExtensionDefault || '.cjs'\n const jsExtension = '.js'\n\n // Check if moduleSpecifier already has a module extension. extname alone\n // is wrong here: dotted basenames like ./foo.generated have no extension.\n const specifierExtension = extname(moduleSpecifier)\n const hasModuleExtension = [\n '.js',\n '.cjs',\n '.mjs',\n '.json',\n '.node',\n ].includes(specifierExtension)\n if (!hasModuleExtension) {\n const resolvedPath = resolve(fileDir, moduleSpecifier)\n let newModuleSpecifier = moduleSpecifier\n\n // Check if the moduleSpecifier is a directory with an index.cjs file\n if (isLocalDirectory(resolvedPath)) {\n const indexPath = resolve(resolvedPath, 'index' + jsExtension)\n if (existsSync(indexPath)) {\n // Append '/' if not present\n if (!newModuleSpecifier.endsWith('/')) {\n newModuleSpecifier += '/'\n }\n newModuleSpecifier += 'index' + cjsExtension\n arg.value = newModuleSpecifier\n return\n }\n }\n\n // Check if source file exists (try .js first, then target extension)\n if (\n existsSync(resolvedPath + jsExtension) ||\n existsSync(resolvedPath + cjsExtension)\n ) {\n newModuleSpecifier += cjsExtension\n arg.value = newModuleSpecifier\n return\n }\n }\n }\n }\n }\n },\n },\n } satisfies babel.PluginObj\n}\n\nfunction isLocalDirectory(absolutePath: string): boolean {\n return existsSync(absolutePath) && lstatSync(absolutePath).isDirectory()\n}\n"
|
|
9
|
+
"import { existsSync, lstatSync } from 'node:fs'\nimport { dirname, extname, resolve } from 'node:path'\n\nexport default function fullySpecifyCommonJS(\n api: any,\n options: {\n esExtensionDefault: string\n }\n): babel.PluginObj {\n api.assertVersion(7)\n\n return {\n name: 'babel-plugin-fully-specified-cjs',\n visitor: {\n CallExpression(path, state) {\n const callee = path.get('callee')\n\n if (\n callee.isIdentifier({ name: 'require' }) &&\n path.node.arguments.length === 1\n ) {\n const arg = path.node.arguments[0]\n if (arg.type === 'StringLiteral') {\n let moduleSpecifier = arg.value\n\n // Skip built-in modules and node_modules\n if (moduleSpecifier.startsWith('.') || moduleSpecifier.startsWith('/')) {\n const filePath = state.file.opts.filename\n if (!filePath) return // Cannot determine file path\n\n const fileDir = dirname(filePath)\n const cjsExtension = options.esExtensionDefault || '.cjs'\n const jsExtension = '.js'\n\n // Check if moduleSpecifier already has a module extension. extname alone\n // is wrong here: dotted basenames like ./foo.generated have no extension.\n const specifierExtension = extname(moduleSpecifier)\n const hasModuleExtension = [\n '.js',\n '.cjs',\n '.mjs',\n '.json',\n '.node',\n ].includes(specifierExtension)\n if (!hasModuleExtension) {\n const resolvedPath = resolve(fileDir, moduleSpecifier)\n let newModuleSpecifier = moduleSpecifier\n\n // native outputs keep platform-split modules extensionless so\n // the react native bundler resolves .ios/.android over .native\n if (\n cjsExtension.startsWith('.native') &&\n (existsSync(`${resolvedPath}.ios.js`) ||\n existsSync(`${resolvedPath}.android.js`))\n ) {\n return\n }\n\n // Check if the moduleSpecifier is a directory with an index.cjs file\n if (isLocalDirectory(resolvedPath)) {\n const indexBase = resolve(resolvedPath, 'index')\n if (\n cjsExtension.startsWith('.native') &&\n (existsSync(`${indexBase}.ios.js`) ||\n existsSync(`${indexBase}.android.js`))\n ) {\n return\n }\n const indexPath = resolve(resolvedPath, 'index' + jsExtension)\n if (existsSync(indexPath)) {\n // Append '/' if not present\n if (!newModuleSpecifier.endsWith('/')) {\n newModuleSpecifier += '/'\n }\n newModuleSpecifier += 'index' + cjsExtension\n arg.value = newModuleSpecifier\n return\n }\n }\n\n // Check if source file exists (try .js first, then target extension)\n if (\n existsSync(resolvedPath + jsExtension) ||\n existsSync(resolvedPath + cjsExtension)\n ) {\n newModuleSpecifier += cjsExtension\n arg.value = newModuleSpecifier\n return\n }\n }\n }\n }\n }\n },\n },\n } satisfies babel.PluginObj\n}\n\nfunction isLocalDirectory(absolutePath: string): boolean {\n return existsSync(absolutePath) && lstatSync(absolutePath).isDirectory()\n}\n"
|
|
10
10
|
]
|
|
11
11
|
}
|
package/types/index.d.ts.map
CHANGED
|
@@ -6,6 +6,6 @@
|
|
|
6
6
|
],
|
|
7
7
|
"version": 3,
|
|
8
8
|
"sourcesContent": [
|
|
9
|
-
"import { existsSync, lstatSync } from 'node:fs'\nimport { dirname, extname, resolve } from 'node:path'\n\nimport type { ConfigAPI, NodePath, PluginObj, PluginPass } from '@babel/core'\nimport * as t from '@babel/types'\nimport type {\n ExportAllDeclaration,\n ExportNamedDeclaration,\n Import,\n ImportDeclaration,\n MemberExpression,\n} from '@babel/types'\n\nexport interface FullySpecifiedOptions {\n ensureFileExists: boolean\n esExtensionDefault: string\n /** List of all extensions which we try to find. */\n tryExtensions: Array<string>\n /** List of extensions that can run in Node.js or in the Browser. */\n esExtensions: Array<string>\n /** Convert process.env.XYZ to import.meta.env.XYZ */\n convertProcessEnvToImportMetaEnv?: boolean\n}\n\nconst DEFAULT_OPTIONS = {\n ensureFileExists: true,\n esExtensionDefault: '.mjs',\n tryExtensions: ['.js'],\n esExtensions: ['.mjs'],\n convertProcessEnvToImportMetaEnv: false,\n} satisfies FullySpecifiedOptions\n\nexport default function FullySpecified(\n api: ConfigAPI,\n rawOptions: Partial<FullySpecifiedOptions> = {}\n): PluginObj {\n api.assertVersion(7)\n\n const options = normalizeOptions(rawOptions)\n\n /** For `import ... from '...'`. */\n const importDeclarationVisitor = (\n path: NodePath<ImportDeclaration>,\n state: PluginPass\n ) => {\n const filePath = state.file.opts.filename\n if (!filePath) return // cannot determine file path therefore cannot proceed\n\n const { node } = path\n if (node.importKind === 'type') return // is a type-only import, skip\n\n const originalModuleSpecifier = node.source.value\n const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(\n originalModuleSpecifier,\n {\n filePath,\n options,\n }\n )\n\n if (fullySpecifiedModuleSpecifier) {\n node.source.value = fullySpecifiedModuleSpecifier\n }\n }\n\n /** For `export ... from '...'`. */\n const exportDeclarationVisitor = (\n path: NodePath<ExportNamedDeclaration> | NodePath<ExportAllDeclaration>,\n state: PluginPass\n ) => {\n const filePath = state.file.opts.filename\n if (!filePath) return // cannot determine file path therefore cannot proceed\n\n const { node } = path\n if (node.exportKind === 'type') return // is a type-only export, skip\n\n const source = node.source\n if (!source) return // is not a re-export, skip\n\n const originalModuleSpecifier = source.value\n const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(\n originalModuleSpecifier,\n {\n filePath,\n options,\n }\n )\n\n if (fullySpecifiedModuleSpecifier) {\n source.value = fullySpecifiedModuleSpecifier\n }\n }\n\n /** For dynamic `import()`s. */\n const importVisitor = (path: NodePath<Import>, state) => {\n const filePath = state.file.opts.filename\n if (!filePath) return // cannot determine file path therefore cannot proceed\n\n const parent = path.parent\n if (parent.type !== 'CallExpression') {\n return // we expect the usage of `import` is a call to it, e.g.: `import('...')`, other usages are not supported\n }\n\n const firstArgOfImportCall = parent.arguments[0]\n if (firstArgOfImportCall.type !== 'StringLiteral') {\n return // we expect the first argument of `import` to be a string, e.g.: `import('./myModule')`, other types are not supported\n }\n\n const originalModuleSpecifier = firstArgOfImportCall.value\n const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(\n originalModuleSpecifier,\n {\n filePath,\n options,\n }\n )\n\n if (fullySpecifiedModuleSpecifier) {\n firstArgOfImportCall.value = fullySpecifiedModuleSpecifier\n }\n }\n\n /** For converting process.env to import.meta.env */\n const memberExpressionVisitor = (path: NodePath<MemberExpression>) => {\n if (!options.convertProcessEnvToImportMetaEnv) return\n\n const { node } = path\n\n // Check if this is process.env.SOMETHING\n if (\n node.object.type === 'MemberExpression' &&\n node.object.object.type === 'Identifier' &&\n node.object.object.name === 'process' &&\n node.object.property.type === 'Identifier' &&\n node.object.property.name === 'env'\n ) {\n // Skip process.env.NODE_ENV - leave it as is\n if (node.property.type === 'Identifier' && node.property.name === 'NODE_ENV') {\n return\n }\n\n // Replace process.env with import.meta.env\n node.object = t.memberExpression(\n t.metaProperty(t.identifier('import'), t.identifier('meta')),\n t.identifier('env')\n )\n }\n }\n\n return {\n name: 'babel-plugin-fully-specified',\n visitor: {\n ImportDeclaration: importDeclarationVisitor,\n ExportNamedDeclaration: exportDeclarationVisitor,\n ExportAllDeclaration: exportDeclarationVisitor,\n Import: importVisitor,\n MemberExpression: memberExpressionVisitor,\n },\n }\n}\n\nfunction normalizeOptions(\n rawOptions: Partial<FullySpecifiedOptions>\n): FullySpecifiedOptions {\n const options = { ...DEFAULT_OPTIONS, ...rawOptions }\n\n if (\n rawOptions.esExtensionDefault &&\n !rawOptions.tryExtensions &&\n rawOptions.esExtensionDefault !== DEFAULT_OPTIONS.esExtensionDefault\n ) {\n options.tryExtensions = [\n rawOptions.esExtensionDefault,\n ...DEFAULT_OPTIONS.tryExtensions.filter(\n (extension) => extension !== rawOptions.esExtensionDefault\n ),\n ]\n }\n\n return options\n}\n\n/**\n * Returns a fully specified [module specifier](https://tc39.es/ecma262/multipage/ecmascript-language-scripts-and-modules.html#prod-ModuleSpecifier) (or `null` if it can't be determined or shouldn't be transformed).\n */\nfunction getFullySpecifiedModuleSpecifier(\n /**\n * The original module specifier in the code.\n *\n * For example, `'./foo'` for `import { foo } from './foo'`.\n */\n originalModuleSpecifier: string,\n {\n filePath,\n options,\n }: {\n /**\n * The absolute file path of the file being transformed.\n *\n * Normally this can be obtained from the 2nd parameter in a visitor function (often named as `state`): `state.file.opts.filename`.\n */\n filePath: string\n /** Options that users pass to babel-plugin-fully-specified. */\n options: FullySpecifiedOptions\n }\n): string | null {\n const fileExt = extname(filePath)\n const fileDir = dirname(filePath)\n\n const isDirectory = isLocalDirectory(resolve(fileDir, originalModuleSpecifier))\n\n const currentModuleExtension = extname(originalModuleSpecifier)\n\n const { tryExtensions, esExtensions, esExtensionDefault, ensureFileExists } = options\n\n const targetModule = evaluateTargetModule({\n moduleSpecifier: originalModuleSpecifier,\n filenameDirectory: fileDir,\n filenameExtension: fileExt,\n currentModuleExtension,\n isDirectory,\n tryExtensions,\n esExtensions,\n esExtensionDefault,\n ensureFileExists,\n })\n\n if (targetModule === false) {\n return null\n }\n\n return targetModule\n}\n\nfunction isLocalDirectory(absoluteDirectory: string) {\n return existsSync(absoluteDirectory) && lstatSync(absoluteDirectory).isDirectory()\n}\n\nfunction evaluateTargetModule({\n moduleSpecifier,\n currentModuleExtension,\n isDirectory,\n filenameDirectory,\n filenameExtension,\n tryExtensions,\n esExtensions,\n esExtensionDefault,\n ensureFileExists,\n}: any): string | false {\n const targetFile = resolve(filenameDirectory, moduleSpecifier)\n\n if (ensureFileExists) {\n for (const extension of tryExtensions) {\n if (existsSync(targetFile + extension)) {\n return moduleSpecifier + esExtensionDefault\n }\n }\n\n if (currentModuleExtension && !esExtensions.includes(currentModuleExtension)) {\n return false\n }\n\n if (isDirectory) {\n const indexModuleSpecifier = `${moduleSpecifier.replace(/\\/$/, '')}/index`\n const indexTargetFile = resolve(filenameDirectory, indexModuleSpecifier)\n\n for (const extension of tryExtensions) {\n if (existsSync(indexTargetFile + extension)) {\n return indexModuleSpecifier + esExtensionDefault\n }\n }\n }\n } else if (esExtensions.includes(filenameExtension)) {\n return moduleSpecifier + esExtensionDefault\n } else {\n return moduleSpecifier + esExtensionDefault\n }\n\n return false\n}\n"
|
|
9
|
+
"import { existsSync, lstatSync } from 'node:fs'\nimport { dirname, extname, resolve } from 'node:path'\n\nimport type { ConfigAPI, NodePath, PluginObj, PluginPass } from '@babel/core'\nimport * as t from '@babel/types'\nimport type {\n ExportAllDeclaration,\n ExportNamedDeclaration,\n Import,\n ImportDeclaration,\n MemberExpression,\n} from '@babel/types'\n\nexport interface FullySpecifiedOptions {\n ensureFileExists: boolean\n esExtensionDefault: string\n /** List of all extensions which we try to find. */\n tryExtensions: Array<string>\n /** List of extensions that can run in Node.js or in the Browser. */\n esExtensions: Array<string>\n /** Convert process.env.XYZ to import.meta.env.XYZ */\n convertProcessEnvToImportMetaEnv?: boolean\n}\n\nconst DEFAULT_OPTIONS = {\n ensureFileExists: true,\n esExtensionDefault: '.mjs',\n tryExtensions: ['.js'],\n esExtensions: ['.mjs'],\n convertProcessEnvToImportMetaEnv: false,\n} satisfies FullySpecifiedOptions\n\nexport default function FullySpecified(\n api: ConfigAPI,\n rawOptions: Partial<FullySpecifiedOptions> = {}\n): PluginObj {\n api.assertVersion(7)\n\n const options = normalizeOptions(rawOptions)\n\n /** For `import ... from '...'`. */\n const importDeclarationVisitor = (\n path: NodePath<ImportDeclaration>,\n state: PluginPass\n ) => {\n const filePath = state.file.opts.filename\n if (!filePath) return // cannot determine file path therefore cannot proceed\n\n const { node } = path\n if (node.importKind === 'type') return // is a type-only import, skip\n\n const originalModuleSpecifier = node.source.value\n const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(\n originalModuleSpecifier,\n {\n filePath,\n options,\n }\n )\n\n if (fullySpecifiedModuleSpecifier) {\n node.source.value = fullySpecifiedModuleSpecifier\n }\n }\n\n /** For `export ... from '...'`. */\n const exportDeclarationVisitor = (\n path: NodePath<ExportNamedDeclaration> | NodePath<ExportAllDeclaration>,\n state: PluginPass\n ) => {\n const filePath = state.file.opts.filename\n if (!filePath) return // cannot determine file path therefore cannot proceed\n\n const { node } = path\n if (node.exportKind === 'type') return // is a type-only export, skip\n\n const source = node.source\n if (!source) return // is not a re-export, skip\n\n const originalModuleSpecifier = source.value\n const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(\n originalModuleSpecifier,\n {\n filePath,\n options,\n }\n )\n\n if (fullySpecifiedModuleSpecifier) {\n source.value = fullySpecifiedModuleSpecifier\n }\n }\n\n /** For dynamic `import()`s. */\n const importVisitor = (path: NodePath<Import>, state) => {\n const filePath = state.file.opts.filename\n if (!filePath) return // cannot determine file path therefore cannot proceed\n\n const parent = path.parent\n if (parent.type !== 'CallExpression') {\n return // we expect the usage of `import` is a call to it, e.g.: `import('...')`, other usages are not supported\n }\n\n const firstArgOfImportCall = parent.arguments[0]\n if (firstArgOfImportCall.type !== 'StringLiteral') {\n return // we expect the first argument of `import` to be a string, e.g.: `import('./myModule')`, other types are not supported\n }\n\n const originalModuleSpecifier = firstArgOfImportCall.value\n const fullySpecifiedModuleSpecifier = getFullySpecifiedModuleSpecifier(\n originalModuleSpecifier,\n {\n filePath,\n options,\n }\n )\n\n if (fullySpecifiedModuleSpecifier) {\n firstArgOfImportCall.value = fullySpecifiedModuleSpecifier\n }\n }\n\n /** For converting process.env to import.meta.env */\n const memberExpressionVisitor = (path: NodePath<MemberExpression>) => {\n if (!options.convertProcessEnvToImportMetaEnv) return\n\n const { node } = path\n\n // Check if this is process.env.SOMETHING\n if (\n node.object.type === 'MemberExpression' &&\n node.object.object.type === 'Identifier' &&\n node.object.object.name === 'process' &&\n node.object.property.type === 'Identifier' &&\n node.object.property.name === 'env'\n ) {\n // Skip process.env.NODE_ENV - leave it as is\n if (node.property.type === 'Identifier' && node.property.name === 'NODE_ENV') {\n return\n }\n\n // Replace process.env with import.meta.env\n node.object = t.memberExpression(\n t.metaProperty(t.identifier('import'), t.identifier('meta')),\n t.identifier('env')\n )\n }\n }\n\n return {\n name: 'babel-plugin-fully-specified',\n visitor: {\n ImportDeclaration: importDeclarationVisitor,\n ExportNamedDeclaration: exportDeclarationVisitor,\n ExportAllDeclaration: exportDeclarationVisitor,\n Import: importVisitor,\n MemberExpression: memberExpressionVisitor,\n },\n }\n}\n\nfunction normalizeOptions(\n rawOptions: Partial<FullySpecifiedOptions>\n): FullySpecifiedOptions {\n const options = { ...DEFAULT_OPTIONS, ...rawOptions }\n\n if (\n rawOptions.esExtensionDefault &&\n !rawOptions.tryExtensions &&\n rawOptions.esExtensionDefault !== DEFAULT_OPTIONS.esExtensionDefault\n ) {\n options.tryExtensions = [\n rawOptions.esExtensionDefault,\n ...DEFAULT_OPTIONS.tryExtensions.filter(\n (extension) => extension !== rawOptions.esExtensionDefault\n ),\n ]\n }\n\n return options\n}\n\n/**\n * Returns a fully specified [module specifier](https://tc39.es/ecma262/multipage/ecmascript-language-scripts-and-modules.html#prod-ModuleSpecifier) (or `null` if it can't be determined or shouldn't be transformed).\n */\nfunction getFullySpecifiedModuleSpecifier(\n /**\n * The original module specifier in the code.\n *\n * For example, `'./foo'` for `import { foo } from './foo'`.\n */\n originalModuleSpecifier: string,\n {\n filePath,\n options,\n }: {\n /**\n * The absolute file path of the file being transformed.\n *\n * Normally this can be obtained from the 2nd parameter in a visitor function (often named as `state`): `state.file.opts.filename`.\n */\n filePath: string\n /** Options that users pass to babel-plugin-fully-specified. */\n options: FullySpecifiedOptions\n }\n): string | null {\n const fileExt = extname(filePath)\n const fileDir = dirname(filePath)\n\n const isDirectory = isLocalDirectory(resolve(fileDir, originalModuleSpecifier))\n\n const currentModuleExtension = extname(originalModuleSpecifier)\n\n const { tryExtensions, esExtensions, esExtensionDefault, ensureFileExists } = options\n\n const targetModule = evaluateTargetModule({\n moduleSpecifier: originalModuleSpecifier,\n filenameDirectory: fileDir,\n filenameExtension: fileExt,\n currentModuleExtension,\n isDirectory,\n tryExtensions,\n esExtensions,\n esExtensionDefault,\n ensureFileExists,\n })\n\n if (targetModule === false) {\n return null\n }\n\n return targetModule\n}\n\nfunction isLocalDirectory(absoluteDirectory: string) {\n return existsSync(absoluteDirectory) && lstatSync(absoluteDirectory).isDirectory()\n}\n\n// native outputs keep platform-split modules extensionless so the react\n// native bundler resolves .ios/.android over .native at bundle time\nfunction isNativeOutput(esExtensionDefault: string) {\n return esExtensionDefault.startsWith('.native')\n}\n\nfunction hasPlatformSibling(absoluteBasePath: string) {\n return (\n existsSync(`${absoluteBasePath}.ios.js`) ||\n existsSync(`${absoluteBasePath}.android.js`)\n )\n}\n\nfunction evaluateTargetModule({\n moduleSpecifier,\n currentModuleExtension,\n isDirectory,\n filenameDirectory,\n filenameExtension,\n tryExtensions,\n esExtensions,\n esExtensionDefault,\n ensureFileExists,\n}: any): string | false {\n const targetFile = resolve(filenameDirectory, moduleSpecifier)\n\n if (ensureFileExists) {\n if (isNativeOutput(esExtensionDefault) && hasPlatformSibling(targetFile)) {\n return false\n }\n\n for (const extension of tryExtensions) {\n if (existsSync(targetFile + extension)) {\n return moduleSpecifier + esExtensionDefault\n }\n }\n\n if (currentModuleExtension && !esExtensions.includes(currentModuleExtension)) {\n return false\n }\n\n if (isDirectory) {\n const indexModuleSpecifier = `${moduleSpecifier.replace(/\\/$/, '')}/index`\n const indexTargetFile = resolve(filenameDirectory, indexModuleSpecifier)\n\n if (isNativeOutput(esExtensionDefault) && hasPlatformSibling(indexTargetFile)) {\n return false\n }\n\n for (const extension of tryExtensions) {\n if (existsSync(indexTargetFile + extension)) {\n return indexModuleSpecifier + esExtensionDefault\n }\n }\n }\n } else if (esExtensions.includes(filenameExtension)) {\n return moduleSpecifier + esExtensionDefault\n } else {\n return moduleSpecifier + esExtensionDefault\n }\n\n return false\n}\n"
|
|
10
10
|
]
|
|
11
11
|
}
|