@atlaspack/babel-plugin-transform-contextual-imports 2.14.1-canary.40 → 2.14.1-canary.401
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/CHANGELOG.md +46 -0
- package/dist/index.js +116 -0
- package/lib/index.js +107 -149
- package/lib/types/index.d.ts +16 -0
- package/package.json +7 -3
- package/src/index.ts +41 -145
- package/test/babel-plugin-transform-contextual-imports.test.ts +162 -0
- package/tsconfig.json +4 -9
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/index.js.map +0 -1
- package/test/babel-plugin-transform-contextual-imports.test.js +0 -72
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,51 @@
|
|
|
1
1
|
# @atlaspack/babel-plugin-transform-contextual-imports
|
|
2
2
|
|
|
3
|
+
## 2.16.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#861](https://github.com/atlassian-labs/atlaspack/pull/861) [`ca42992`](https://github.com/atlassian-labs/atlaspack/commit/ca429925dab731600c2decd571b5245d361ca953) Thanks [@at-nathan](https://github.com/at-nathan)! - Ignore transform on type references
|
|
8
|
+
|
|
9
|
+
## 2.16.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#843](https://github.com/atlassian-labs/atlaspack/pull/843) [`f97acf2`](https://github.com/atlassian-labs/atlaspack/commit/f97acf242ed210841db3403d3a5f224f5eff5ab5) Thanks [@nickrobson](https://github.com/nickrobson)! - Transform importCond in JSX, ungate binding-based transform
|
|
14
|
+
|
|
15
|
+
## 2.16.0
|
|
16
|
+
|
|
17
|
+
### Minor Changes
|
|
18
|
+
|
|
19
|
+
- [#795](https://github.com/atlassian-labs/atlaspack/pull/795) [`336eb1e`](https://github.com/atlassian-labs/atlaspack/commit/336eb1ef59542b7488991ff13da815f87c26645c) Thanks [@JakeLane](https://github.com/JakeLane)! - Fix binding replacement behaviour for contextual imports babel plugin
|
|
20
|
+
|
|
21
|
+
## 2.15.3
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- [#785](https://github.com/atlassian-labs/atlaspack/pull/785) [`0e7dd5e`](https://github.com/atlassian-labs/atlaspack/commit/0e7dd5ec6fbe05aa9e0bb5775a9d0975f206a922) Thanks [@matt-koko](https://github.com/matt-koko)! - We need to re-publish every package in Atlaspack with the corrected types field.
|
|
26
|
+
|
|
27
|
+
## 2.15.2
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- [#742](https://github.com/atlassian-labs/atlaspack/pull/742) [`ee040bb`](https://github.com/atlassian-labs/atlaspack/commit/ee040bb6428f29b57d892ddd8107e29077d08ffd) Thanks [@yamadapc](https://github.com/yamadapc)! - Internal changes and bug fixes to environmentDeduplication flag
|
|
32
|
+
|
|
33
|
+
## 2.15.1
|
|
34
|
+
|
|
35
|
+
### Patch Changes
|
|
36
|
+
|
|
37
|
+
- [#720](https://github.com/atlassian-labs/atlaspack/pull/720) [`d2fd849`](https://github.com/atlassian-labs/atlaspack/commit/d2fd849770fe6305e9c694bd97b1bd905abd9d94) Thanks [@alshdavid](https://github.com/alshdavid)! - Migrate to TypeScript
|
|
38
|
+
|
|
39
|
+
## 2.15.0
|
|
40
|
+
|
|
41
|
+
### Minor Changes
|
|
42
|
+
|
|
43
|
+
- [#640](https://github.com/atlassian-labs/atlaspack/pull/640) [`dbb4072`](https://github.com/atlassian-labs/atlaspack/commit/dbb40721ebeb45990a14ba04e6b44e7f836fb32d) Thanks [@JakeLane](https://github.com/JakeLane)! - Clean up conditional bundling feature flags
|
|
44
|
+
|
|
45
|
+
### Patch Changes
|
|
46
|
+
|
|
47
|
+
- [#682](https://github.com/atlassian-labs/atlaspack/pull/682) [`a5ed1b4`](https://github.com/atlassian-labs/atlaspack/commit/a5ed1b414498560f393ff491af4da25b6e8dde56) Thanks [@alshdavid](https://github.com/alshdavid)! - Updating build system
|
|
48
|
+
|
|
3
49
|
## 2.14.1
|
|
4
50
|
|
|
5
51
|
### Patch Changes
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const helper_plugin_utils_1 = require("@babel/helper-plugin-utils");
|
|
4
|
+
const isNode = (opts) => !!('node' in opts && opts.node);
|
|
5
|
+
exports.default = (0, helper_plugin_utils_1.declare)((api) => {
|
|
6
|
+
const { types: t } = api;
|
|
7
|
+
const isImportCondCallExpression = (node) => {
|
|
8
|
+
if (node.type === 'CallExpression' &&
|
|
9
|
+
node.callee.type === 'Identifier' &&
|
|
10
|
+
node.callee.name === 'importCond') {
|
|
11
|
+
if (node.arguments.length === 3 &&
|
|
12
|
+
node.arguments.every((arg) => arg.type === 'StringLiteral')) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
// Simple error for incorrect syntax (since it's documented with the type)
|
|
17
|
+
throw new Error('importCond must have three string literal arguments');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
};
|
|
22
|
+
const buildCondFunction = (cond, ifTrue, ifFalse) => t.conditionalExpression(t.logicalExpression('&&', t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), t.callExpression(t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), [cond])), t.memberExpression(t.callExpression(t.identifier('require'), [ifTrue]), t.identifier('default')), t.memberExpression(t.callExpression(t.identifier('require'), [ifFalse]), t.identifier('default')));
|
|
23
|
+
const buildNodeObject = (identifier, cond, ifTrue, ifFalse) => [
|
|
24
|
+
// Create object containing imports
|
|
25
|
+
t.variableDeclaration('const', [
|
|
26
|
+
t.variableDeclarator(identifier, t.objectExpression([
|
|
27
|
+
t.objectProperty(t.identifier('ifTrue'), t.memberExpression(t.callExpression(t.identifier('require'), [ifTrue]), t.identifier('default'))),
|
|
28
|
+
t.objectProperty(t.identifier('ifFalse'), t.memberExpression(t.callExpression(t.identifier('require'), [ifFalse]), t.identifier('default'))),
|
|
29
|
+
])),
|
|
30
|
+
]),
|
|
31
|
+
// Create lazy getter via the load property on the object.
|
|
32
|
+
// This is node module resolution safe because each time the import is accessed, we re-evaluate the condition.
|
|
33
|
+
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('defineProperty')), [
|
|
34
|
+
identifier,
|
|
35
|
+
t.stringLiteral('load'),
|
|
36
|
+
t.objectExpression([
|
|
37
|
+
t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([], t.conditionalExpression(t.logicalExpression('&&', t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), t.callExpression(t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), [cond])), t.memberExpression(identifier, t.identifier('ifTrue')), t.memberExpression(identifier, t.identifier('ifFalse'))))),
|
|
38
|
+
]),
|
|
39
|
+
])),
|
|
40
|
+
];
|
|
41
|
+
return {
|
|
42
|
+
name: '@atlaspack/babel-plugin-transform-contextual-imports',
|
|
43
|
+
visitor: {
|
|
44
|
+
CallExpression: {
|
|
45
|
+
enter(path, state) {
|
|
46
|
+
const node = path.node;
|
|
47
|
+
if (isImportCondCallExpression(node)) {
|
|
48
|
+
const [cond, ifTrue, ifFalse] = node.arguments;
|
|
49
|
+
if (!isNode(state.opts)) {
|
|
50
|
+
// Replace the importCond call with a conditional require import, as a fallback for environments that don't support Atlaspack
|
|
51
|
+
path.replaceWith(buildCondFunction(cond, ifTrue, ifFalse));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
VariableDeclaration: {
|
|
57
|
+
enter(path, state) {
|
|
58
|
+
if (isNode(state.opts)) {
|
|
59
|
+
if (path.node.declarations.length === 1 &&
|
|
60
|
+
path.node.declarations[0].type === 'VariableDeclarator' &&
|
|
61
|
+
path.node.declarations[0].id.type === 'Identifier') {
|
|
62
|
+
const importId = path.node.declarations[0].id;
|
|
63
|
+
const call = path.node.declarations[0].init;
|
|
64
|
+
// Mark identifier for object so we don't add the load property to it
|
|
65
|
+
state.visitedIdentifiers?.add(importId);
|
|
66
|
+
if (call && isImportCondCallExpression(call)) {
|
|
67
|
+
const [cond, ifTrue, ifFalse] = call.arguments;
|
|
68
|
+
// Replace with object containing imports and lazy getter, which allows us to load the correct import based on the condition at runtime
|
|
69
|
+
path.replaceWithMultiple(buildNodeObject(importId, cond, ifTrue, ifFalse));
|
|
70
|
+
// Add the binding to set so we can mutate all references to this binding in the exit pass
|
|
71
|
+
const binding = path.scope.getBinding(importId.name);
|
|
72
|
+
if (binding) {
|
|
73
|
+
state.conditionalImportBindings?.add(binding);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
ReferencedIdentifier: {
|
|
81
|
+
exit(path, state) {
|
|
82
|
+
if (!isNode(state.opts)) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (path.parentPath.isTSType()) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (state.visitedIdentifiers?.has(path.node)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const binding = path.scope.getBinding(path.node.name);
|
|
92
|
+
if (binding && state.conditionalImportBindings?.has(binding)) {
|
|
93
|
+
if (path.isJSXIdentifier()) {
|
|
94
|
+
// Add load property to the import usage
|
|
95
|
+
const newIdentifer = t.jsxIdentifier(path.node.name);
|
|
96
|
+
path.replaceWith(t.jsxMemberExpression(newIdentifer, t.jsxIdentifier('load')));
|
|
97
|
+
state.visitedIdentifiers?.add(newIdentifer);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
// Add load property to the import usage
|
|
101
|
+
const newIdentifer = t.identifier(path.node.name);
|
|
102
|
+
path.replaceWith(t.memberExpression(newIdentifer, t.identifier('load')));
|
|
103
|
+
state.visitedIdentifiers?.add(newIdentifer);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
Program: {
|
|
109
|
+
enter(_, state) {
|
|
110
|
+
state.conditionalImportBindings = new Set();
|
|
111
|
+
state.visitedIdentifiers = new Set();
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -1,159 +1,117 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
function _helperPluginUtils() {
|
|
8
|
+
const data = require("@babel/helper-plugin-utils");
|
|
9
|
+
_helperPluginUtils = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
10
13
|
}
|
|
14
|
+
const isNode = opts => !!('node' in opts && opts.node);
|
|
15
|
+
var _default = exports.default = (0, _helperPluginUtils().declare)(api => {
|
|
16
|
+
const {
|
|
17
|
+
types: t
|
|
18
|
+
} = api;
|
|
19
|
+
const isImportCondCallExpression = node => {
|
|
20
|
+
if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'importCond') {
|
|
21
|
+
if (node.arguments.length === 3 && node.arguments.every(arg => arg.type === 'StringLiteral')) {
|
|
22
|
+
return true;
|
|
23
|
+
} else {
|
|
24
|
+
// Simple error for incorrect syntax (since it's documented with the type)
|
|
25
|
+
throw new Error('importCond must have three string literal arguments');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
};
|
|
30
|
+
const buildCondFunction = (cond, ifTrue, ifFalse) => t.conditionalExpression(t.logicalExpression('&&', t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), t.callExpression(t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), [cond])), t.memberExpression(t.callExpression(t.identifier('require'), [ifTrue]), t.identifier('default')), t.memberExpression(t.callExpression(t.identifier('require'), [ifFalse]), t.identifier('default')));
|
|
31
|
+
const buildNodeObject = (identifier, cond, ifTrue, ifFalse) => [
|
|
32
|
+
// Create object containing imports
|
|
33
|
+
t.variableDeclaration('const', [t.variableDeclarator(identifier, t.objectExpression([t.objectProperty(t.identifier('ifTrue'), t.memberExpression(t.callExpression(t.identifier('require'), [ifTrue]), t.identifier('default'))), t.objectProperty(t.identifier('ifFalse'), t.memberExpression(t.callExpression(t.identifier('require'), [ifFalse]), t.identifier('default')))]))]),
|
|
34
|
+
// Create lazy getter via the load property on the object.
|
|
35
|
+
// This is node module resolution safe because each time the import is accessed, we re-evaluate the condition.
|
|
36
|
+
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('defineProperty')), [identifier, t.stringLiteral('load'), t.objectExpression([t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([], t.conditionalExpression(t.logicalExpression('&&', t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), t.callExpression(t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), [cond])), t.memberExpression(identifier, t.identifier('ifTrue')), t.memberExpression(identifier, t.identifier('ifFalse')))))])]))];
|
|
37
|
+
return {
|
|
38
|
+
name: '@atlaspack/babel-plugin-transform-contextual-imports',
|
|
39
|
+
visitor: {
|
|
40
|
+
CallExpression: {
|
|
41
|
+
enter(path, state) {
|
|
42
|
+
const node = path.node;
|
|
43
|
+
if (isImportCondCallExpression(node)) {
|
|
44
|
+
const [cond, ifTrue, ifFalse] = node.arguments;
|
|
45
|
+
if (!isNode(state.opts)) {
|
|
46
|
+
// Replace the importCond call with a conditional require import, as a fallback for environments that don't support Atlaspack
|
|
47
|
+
path.replaceWith(buildCondFunction(cond, ifTrue, ifFalse));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
VariableDeclaration: {
|
|
53
|
+
enter(path, state) {
|
|
54
|
+
if (isNode(state.opts)) {
|
|
55
|
+
if (path.node.declarations.length === 1 && path.node.declarations[0].type === 'VariableDeclarator' && path.node.declarations[0].id.type === 'Identifier') {
|
|
56
|
+
var _state$visitedIdentif;
|
|
57
|
+
const importId = path.node.declarations[0].id;
|
|
58
|
+
const call = path.node.declarations[0].init;
|
|
11
59
|
|
|
12
|
-
|
|
60
|
+
// Mark identifier for object so we don't add the load property to it
|
|
61
|
+
(_state$visitedIdentif = state.visitedIdentifiers) === null || _state$visitedIdentif === void 0 || _state$visitedIdentif.add(importId);
|
|
62
|
+
if (call && isImportCondCallExpression(call)) {
|
|
63
|
+
const [cond, ifTrue, ifFalse] = call.arguments;
|
|
13
64
|
|
|
14
|
-
|
|
65
|
+
// Replace with object containing imports and lazy getter, which allows us to load the correct import based on the condition at runtime
|
|
66
|
+
path.replaceWithMultiple(buildNodeObject(importId, cond, ifTrue, ifFalse));
|
|
15
67
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const { types: t } = api;
|
|
22
|
-
const isImportCondCallExpression = (node)=>{
|
|
23
|
-
if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'importCond') {
|
|
24
|
-
if (node.arguments.length === 3 && node.arguments.every((arg)=>arg.type === 'StringLiteral')) return true;
|
|
25
|
-
else // Simple error for incorrect syntax (since it's documented with the type)
|
|
26
|
-
throw new Error('importCond must have three string literal arguments');
|
|
27
|
-
}
|
|
28
|
-
return false;
|
|
29
|
-
};
|
|
30
|
-
const buildCondFunction = (cond, ifTrue, ifFalse)=>t.conditionalExpression(t.logicalExpression('&&', t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), t.callExpression(t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), [
|
|
31
|
-
cond
|
|
32
|
-
])), t.memberExpression(t.callExpression(t.identifier('require'), [
|
|
33
|
-
ifTrue
|
|
34
|
-
]), t.identifier('default')), t.memberExpression(t.callExpression(t.identifier('require'), [
|
|
35
|
-
ifFalse
|
|
36
|
-
]), t.identifier('default')));
|
|
37
|
-
const buildNodeObject = (identifier, cond, ifTrue, ifFalse)=>[
|
|
38
|
-
// Create object containing imports
|
|
39
|
-
t.variableDeclaration('const', [
|
|
40
|
-
t.variableDeclarator(identifier, t.objectExpression([
|
|
41
|
-
t.objectProperty(t.identifier('ifTrue'), t.memberExpression(t.callExpression(t.identifier('require'), [
|
|
42
|
-
ifTrue
|
|
43
|
-
]), t.identifier('default'))),
|
|
44
|
-
t.objectProperty(t.identifier('ifFalse'), t.memberExpression(t.callExpression(t.identifier('require'), [
|
|
45
|
-
ifFalse
|
|
46
|
-
]), t.identifier('default')))
|
|
47
|
-
]))
|
|
48
|
-
]),
|
|
49
|
-
// Create lazy getter via the load property on the object.
|
|
50
|
-
// This is node module resolution safe because each time the import is accessed, we re-evaluate the condition.
|
|
51
|
-
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('defineProperty')), [
|
|
52
|
-
identifier,
|
|
53
|
-
t.stringLiteral('load'),
|
|
54
|
-
t.objectExpression([
|
|
55
|
-
t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([], t.conditionalExpression(t.logicalExpression('&&', t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), t.callExpression(t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), [
|
|
56
|
-
cond
|
|
57
|
-
])), t.memberExpression(identifier, t.identifier('ifTrue')), t.memberExpression(identifier, t.identifier('ifFalse')))))
|
|
58
|
-
])
|
|
59
|
-
]))
|
|
60
|
-
];
|
|
61
|
-
const buildServerObject = (identUid, cond, ifTrue, ifFalse)=>[
|
|
62
|
-
// Create object containing imports
|
|
63
|
-
t.variableDeclaration('const', [
|
|
64
|
-
t.variableDeclarator(t.identifier(identUid), t.objectExpression([
|
|
65
|
-
t.objectProperty(t.identifier('ifTrue'), t.memberExpression(t.callExpression(t.identifier('require'), [
|
|
66
|
-
ifTrue
|
|
67
|
-
]), t.identifier('default'))),
|
|
68
|
-
t.objectProperty(t.identifier('ifFalse'), t.memberExpression(t.callExpression(t.identifier('require'), [
|
|
69
|
-
ifFalse
|
|
70
|
-
]), t.identifier('default')))
|
|
71
|
-
]))
|
|
72
|
-
]),
|
|
73
|
-
// Create lazy getter via the load property on the object
|
|
74
|
-
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('defineProperty')), [
|
|
75
|
-
t.identifier(identUid),
|
|
76
|
-
t.stringLiteral('load'),
|
|
77
|
-
t.objectExpression([
|
|
78
|
-
t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([], t.conditionalExpression(t.logicalExpression('&&', t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), t.callExpression(t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')), [
|
|
79
|
-
cond
|
|
80
|
-
])), t.memberExpression(t.identifier(identUid), t.identifier('ifTrue')), t.memberExpression(t.identifier(identUid), t.identifier('ifFalse')))))
|
|
81
|
-
])
|
|
82
|
-
]))
|
|
83
|
-
];
|
|
84
|
-
const checkIsServer = (path, state)=>{
|
|
85
|
-
if (path.node.callee.type === 'Identifier' && path.node.callee.name === 'importCond') {
|
|
86
|
-
if (path.node.arguments.length == 3 && path.node.arguments.every((arg)=>arg.type === 'StringLiteral')) {
|
|
87
|
-
const [cond, ifTrue, ifFalse] = path.node.arguments;
|
|
88
|
-
if ($35f0bedee55d5f76$var$isServer(state.opts)) {
|
|
89
|
-
// Make module pass lazy in ssr
|
|
90
|
-
const identUid = path.scope.generateUid(`${cond.value}$${ifTrue.value}$${ifFalse.value}`);
|
|
91
|
-
state.importNodes ??= [];
|
|
92
|
-
state.importNodes.push(...buildServerObject(identUid, cond, ifTrue, ifFalse));
|
|
93
|
-
// Replace call expression with reference to lazy object getter
|
|
94
|
-
path.replaceWith(t.memberExpression(t.identifier(identUid), t.identifier('load')));
|
|
68
|
+
// Add the binding to set so we can mutate all references to this binding in the exit pass
|
|
69
|
+
const binding = path.scope.getBinding(importId.name);
|
|
70
|
+
if (binding) {
|
|
71
|
+
var _state$conditionalImp;
|
|
72
|
+
(_state$conditionalImp = state.conditionalImportBindings) === null || _state$conditionalImp === void 0 || _state$conditionalImp.add(binding);
|
|
95
73
|
}
|
|
74
|
+
}
|
|
96
75
|
}
|
|
76
|
+
}
|
|
97
77
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
path.replaceWithMultiple(buildNodeObject(importId, cond, ifTrue, ifFalse));
|
|
126
|
-
// Add identifier name to set so we can mutate all import usages in the exit pass
|
|
127
|
-
state.conditionalImportIdentifiers?.add(importId.name);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
Identifier: {
|
|
134
|
-
exit (path, state) {
|
|
135
|
-
const identifier = state.conditionalImportIdentifiers?.has(path.node.name);
|
|
136
|
-
if (identifier && !state.visitedIdentifiers?.has(path.node)) {
|
|
137
|
-
// Add load property to the import usage
|
|
138
|
-
const newIdentifer = t.identifier(path.node.name);
|
|
139
|
-
path.replaceWith(t.memberExpression(newIdentifer, t.identifier('load')));
|
|
140
|
-
state.visitedIdentifiers?.add(newIdentifer);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
},
|
|
144
|
-
Program: {
|
|
145
|
-
enter (_, state) {
|
|
146
|
-
state.conditionalImportIdentifiers = new Set();
|
|
147
|
-
state.visitedIdentifiers = new Set();
|
|
148
|
-
},
|
|
149
|
-
exit (path, state) {
|
|
150
|
-
if (state.importNodes) // If there's an import node, add it to the top of the body
|
|
151
|
-
path.unshiftContainer('body', state.importNodes);
|
|
152
|
-
}
|
|
78
|
+
},
|
|
79
|
+
ReferencedIdentifier: {
|
|
80
|
+
exit(path, state) {
|
|
81
|
+
var _state$visitedIdentif2, _state$conditionalImp2;
|
|
82
|
+
if (!isNode(state.opts)) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (path.parentPath.isTSType()) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if ((_state$visitedIdentif2 = state.visitedIdentifiers) !== null && _state$visitedIdentif2 !== void 0 && _state$visitedIdentif2.has(path.node)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const binding = path.scope.getBinding(path.node.name);
|
|
92
|
+
if (binding && (_state$conditionalImp2 = state.conditionalImportBindings) !== null && _state$conditionalImp2 !== void 0 && _state$conditionalImp2.has(binding)) {
|
|
93
|
+
if (path.isJSXIdentifier()) {
|
|
94
|
+
var _state$visitedIdentif3;
|
|
95
|
+
// Add load property to the import usage
|
|
96
|
+
const newIdentifer = t.jsxIdentifier(path.node.name);
|
|
97
|
+
path.replaceWith(t.jsxMemberExpression(newIdentifer, t.jsxIdentifier('load')));
|
|
98
|
+
(_state$visitedIdentif3 = state.visitedIdentifiers) === null || _state$visitedIdentif3 === void 0 || _state$visitedIdentif3.add(newIdentifer);
|
|
99
|
+
} else {
|
|
100
|
+
var _state$visitedIdentif4;
|
|
101
|
+
// Add load property to the import usage
|
|
102
|
+
const newIdentifer = t.identifier(path.node.name);
|
|
103
|
+
path.replaceWith(t.memberExpression(newIdentifer, t.identifier('load')));
|
|
104
|
+
(_state$visitedIdentif4 = state.visitedIdentifiers) === null || _state$visitedIdentif4 === void 0 || _state$visitedIdentif4.add(newIdentifer);
|
|
153
105
|
}
|
|
106
|
+
}
|
|
154
107
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
108
|
+
},
|
|
109
|
+
Program: {
|
|
110
|
+
enter(_, state) {
|
|
111
|
+
state.conditionalImportBindings = new Set();
|
|
112
|
+
state.visitedIdentifiers = new Set();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { PluginObj, types as BabelTypes } from '@babel/core';
|
|
2
|
+
import type { Binding } from '@babel/traverse';
|
|
3
|
+
interface Opts {
|
|
4
|
+
/** Use node safe import cond syntax */
|
|
5
|
+
node?: boolean;
|
|
6
|
+
}
|
|
7
|
+
interface State {
|
|
8
|
+
/** Plugin options */
|
|
9
|
+
opts: Opts;
|
|
10
|
+
/** Set of bindings that need to be mutated after import was transformed */
|
|
11
|
+
conditionalImportBindings?: Set<Binding>;
|
|
12
|
+
/** Set of identifiers that have been visited in the exit pass, to avoid adding the load property multiple times */
|
|
13
|
+
visitedIdentifiers?: Set<BabelTypes.Identifier | BabelTypes.JSXIdentifier>;
|
|
14
|
+
}
|
|
15
|
+
declare const _default: (api: object, options: Record<string, any> | null | undefined, dirname: string) => PluginObj<State>;
|
|
16
|
+
export default _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/babel-plugin-transform-contextual-imports",
|
|
3
|
-
"version": "2.14.1-canary.
|
|
3
|
+
"version": "2.14.1-canary.401+340caf69a",
|
|
4
4
|
"license": "(MIT OR Apache-2.0)",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
},
|
|
12
12
|
"main": "./lib/index.js",
|
|
13
13
|
"source": "./src/index.ts",
|
|
14
|
+
"types": "./lib/types/index.d.ts",
|
|
14
15
|
"engines": {
|
|
15
16
|
"node": ">= 16.0.0"
|
|
16
17
|
},
|
|
@@ -23,5 +24,8 @@
|
|
|
23
24
|
"@types/babel__helper-plugin-utils": "^7.10.3"
|
|
24
25
|
},
|
|
25
26
|
"type": "commonjs",
|
|
26
|
-
"
|
|
27
|
-
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
|
|
29
|
+
},
|
|
30
|
+
"gitHead": "340caf69a4143bbea119745e2e29a3516d44d522"
|
|
31
|
+
}
|