@atlaspack/babel-plugin-transform-contextual-imports 2.14.2-unified-f165f7d09.0 → 2.15.1-typescript-08dcc1c9b.0
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
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @atlaspack/babel-plugin-transform-contextual-imports
|
|
2
2
|
|
|
3
|
+
## 2.15.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#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
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#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
|
|
12
|
+
|
|
3
13
|
## 2.14.1
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
package/lib/index.js
CHANGED
|
@@ -1,159 +1,97 @@
|
|
|
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
|
+
var _state$conditionalImp;
|
|
64
|
+
const [cond, ifTrue, ifFalse] = call.arguments;
|
|
13
65
|
|
|
14
|
-
|
|
66
|
+
// Replace with object containing imports and lazy getter, which allows us to load the correct import based on the condition at runtime
|
|
67
|
+
path.replaceWithMultiple(buildNodeObject(importId, cond, ifTrue, ifFalse));
|
|
15
68
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
const $35f0bedee55d5f76$var$isNode = (opts)=>!!('node' in opts && opts.node);
|
|
20
|
-
var $35f0bedee55d5f76$export$2e2bcd8739ae039 = (0, $b65XQ$babelhelperpluginutils.declare)((api)=>{
|
|
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')));
|
|
95
|
-
}
|
|
69
|
+
// Add identifier name to set so we can mutate all import usages in the exit pass
|
|
70
|
+
(_state$conditionalImp = state.conditionalImportIdentifiers) === null || _state$conditionalImp === void 0 || _state$conditionalImp.add(importId.name);
|
|
71
|
+
}
|
|
96
72
|
}
|
|
73
|
+
}
|
|
97
74
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
path.replaceWith(buildCondFunction(cond, ifTrue, ifFalse));
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
},
|
|
114
|
-
VariableDeclaration: {
|
|
115
|
-
enter (path, state) {
|
|
116
|
-
if ($35f0bedee55d5f76$var$isNode(state.opts)) {
|
|
117
|
-
if (path.node.declarations.length === 1 && path.node.declarations[0].type === 'VariableDeclarator' && path.node.declarations[0].id.type === 'Identifier') {
|
|
118
|
-
const importId = path.node.declarations[0].id;
|
|
119
|
-
const call = path.node.declarations[0].init;
|
|
120
|
-
// Mark identifier for object so we don't add the load property to it
|
|
121
|
-
state.visitedIdentifiers?.add(importId);
|
|
122
|
-
if (call && isImportCondCallExpression(call)) {
|
|
123
|
-
const [cond, ifTrue, ifFalse] = call.arguments;
|
|
124
|
-
// Replace with object containing imports and lazy getter, which allows us to load the correct import based on the condition at runtime
|
|
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
|
-
}
|
|
153
|
-
}
|
|
75
|
+
},
|
|
76
|
+
Identifier: {
|
|
77
|
+
exit(path, state) {
|
|
78
|
+
var _state$conditionalImp2, _state$visitedIdentif2;
|
|
79
|
+
const identifier = (_state$conditionalImp2 = state.conditionalImportIdentifiers) === null || _state$conditionalImp2 === void 0 ? void 0 : _state$conditionalImp2.has(path.node.name);
|
|
80
|
+
if (identifier && !((_state$visitedIdentif2 = state.visitedIdentifiers) !== null && _state$visitedIdentif2 !== void 0 && _state$visitedIdentif2.has(path.node))) {
|
|
81
|
+
var _state$visitedIdentif3;
|
|
82
|
+
// Add load property to the import usage
|
|
83
|
+
const newIdentifer = t.identifier(path.node.name);
|
|
84
|
+
path.replaceWith(t.memberExpression(newIdentifer, t.identifier('load')));
|
|
85
|
+
(_state$visitedIdentif3 = state.visitedIdentifiers) === null || _state$visitedIdentif3 === void 0 || _state$visitedIdentif3.add(newIdentifer);
|
|
86
|
+
}
|
|
154
87
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
88
|
+
},
|
|
89
|
+
Program: {
|
|
90
|
+
enter(_, state) {
|
|
91
|
+
state.conditionalImportIdentifiers = new Set();
|
|
92
|
+
state.visitedIdentifiers = new Set();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/babel-plugin-transform-contextual-imports",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.15.1-typescript-08dcc1c9b.0",
|
|
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": "./src/index.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
|
+
"scripts": {
|
|
28
|
+
"check-ts": "tsc --noEmit"
|
|
29
|
+
},
|
|
30
|
+
"gitHead": "08dcc1c9bcdc6ab931d55e05ccc0f45669de2f22"
|
|
27
31
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import type {PluginObj,
|
|
1
|
+
import type {PluginObj, types as BabelTypes} from '@babel/core';
|
|
2
2
|
import {declare} from '@babel/helper-plugin-utils';
|
|
3
3
|
|
|
4
4
|
interface Opts {
|
|
5
|
-
/** @deprecated Use "node" instead */
|
|
6
|
-
server?: boolean;
|
|
7
5
|
/** Use node safe import cond syntax */
|
|
8
6
|
node?: boolean;
|
|
9
7
|
}
|
|
@@ -11,19 +9,12 @@ interface Opts {
|
|
|
11
9
|
interface State {
|
|
12
10
|
/** Plugin options */
|
|
13
11
|
opts: Opts;
|
|
14
|
-
/** @deprecated Statement types didn't work so using any */
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
-
importNodes?: any[];
|
|
17
12
|
/** Set of identifier names that need to be mutated after import was transformed */
|
|
18
13
|
conditionalImportIdentifiers?: Set<string>;
|
|
19
14
|
/** Set of identifiers that have been visited in the exit pass, to avoid adding the load property multiple times */
|
|
20
15
|
visitedIdentifiers?: Set<BabelTypes.Identifier>;
|
|
21
16
|
}
|
|
22
17
|
|
|
23
|
-
const isServer = (opts: Opts) => {
|
|
24
|
-
return 'server' in opts && opts.server;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
18
|
const isNode = (opts: Opts): boolean => !!('node' in opts && opts.node);
|
|
28
19
|
|
|
29
20
|
export default declare((api): PluginObj<State> => {
|
|
@@ -158,124 +149,11 @@ export default declare((api): PluginObj<State> => {
|
|
|
158
149
|
),
|
|
159
150
|
];
|
|
160
151
|
|
|
161
|
-
const buildServerObject = (
|
|
162
|
-
identUid: string,
|
|
163
|
-
cond: BabelTypes.StringLiteral,
|
|
164
|
-
ifTrue: BabelTypes.StringLiteral,
|
|
165
|
-
ifFalse: BabelTypes.StringLiteral,
|
|
166
|
-
) => [
|
|
167
|
-
// Create object containing imports
|
|
168
|
-
t.variableDeclaration('const', [
|
|
169
|
-
t.variableDeclarator(
|
|
170
|
-
t.identifier(identUid),
|
|
171
|
-
t.objectExpression([
|
|
172
|
-
t.objectProperty(
|
|
173
|
-
t.identifier('ifTrue'),
|
|
174
|
-
t.memberExpression(
|
|
175
|
-
t.callExpression(t.identifier('require'), [ifTrue]),
|
|
176
|
-
t.identifier('default'),
|
|
177
|
-
),
|
|
178
|
-
),
|
|
179
|
-
t.objectProperty(
|
|
180
|
-
t.identifier('ifFalse'),
|
|
181
|
-
t.memberExpression(
|
|
182
|
-
t.callExpression(t.identifier('require'), [ifFalse]),
|
|
183
|
-
t.identifier('default'),
|
|
184
|
-
),
|
|
185
|
-
),
|
|
186
|
-
]),
|
|
187
|
-
),
|
|
188
|
-
]),
|
|
189
|
-
|
|
190
|
-
// Create lazy getter via the load property on the object
|
|
191
|
-
t.expressionStatement(
|
|
192
|
-
t.callExpression(
|
|
193
|
-
t.memberExpression(
|
|
194
|
-
t.identifier('Object'),
|
|
195
|
-
t.identifier('defineProperty'),
|
|
196
|
-
),
|
|
197
|
-
[
|
|
198
|
-
t.identifier(identUid),
|
|
199
|
-
t.stringLiteral('load'),
|
|
200
|
-
t.objectExpression([
|
|
201
|
-
t.objectProperty(
|
|
202
|
-
t.identifier('get'),
|
|
203
|
-
t.arrowFunctionExpression(
|
|
204
|
-
[],
|
|
205
|
-
t.conditionalExpression(
|
|
206
|
-
t.logicalExpression(
|
|
207
|
-
'&&',
|
|
208
|
-
t.memberExpression(
|
|
209
|
-
t.identifier('globalThis'),
|
|
210
|
-
t.identifier('__MCOND'),
|
|
211
|
-
),
|
|
212
|
-
t.callExpression(
|
|
213
|
-
t.memberExpression(
|
|
214
|
-
t.identifier('globalThis'),
|
|
215
|
-
t.identifier('__MCOND'),
|
|
216
|
-
),
|
|
217
|
-
[cond],
|
|
218
|
-
),
|
|
219
|
-
),
|
|
220
|
-
t.memberExpression(
|
|
221
|
-
t.identifier(identUid),
|
|
222
|
-
t.identifier('ifTrue'),
|
|
223
|
-
),
|
|
224
|
-
t.memberExpression(
|
|
225
|
-
t.identifier(identUid),
|
|
226
|
-
t.identifier('ifFalse'),
|
|
227
|
-
),
|
|
228
|
-
),
|
|
229
|
-
),
|
|
230
|
-
),
|
|
231
|
-
]),
|
|
232
|
-
],
|
|
233
|
-
),
|
|
234
|
-
),
|
|
235
|
-
];
|
|
236
|
-
|
|
237
|
-
const checkIsServer = (
|
|
238
|
-
path: NodePath<BabelTypes.CallExpression>,
|
|
239
|
-
state: State,
|
|
240
|
-
) => {
|
|
241
|
-
if (
|
|
242
|
-
path.node.callee.type === 'Identifier' &&
|
|
243
|
-
path.node.callee.name === 'importCond'
|
|
244
|
-
) {
|
|
245
|
-
if (
|
|
246
|
-
path.node.arguments.length == 3 &&
|
|
247
|
-
path.node.arguments.every((arg) => arg.type === 'StringLiteral')
|
|
248
|
-
) {
|
|
249
|
-
const [cond, ifTrue, ifFalse] = path.node.arguments;
|
|
250
|
-
|
|
251
|
-
if (isServer(state.opts)) {
|
|
252
|
-
// Make module pass lazy in ssr
|
|
253
|
-
const identUid = path.scope.generateUid(
|
|
254
|
-
`${cond.value}$${ifTrue.value}$${ifFalse.value}`,
|
|
255
|
-
);
|
|
256
|
-
|
|
257
|
-
state.importNodes ??= [];
|
|
258
|
-
state.importNodes.push(
|
|
259
|
-
...buildServerObject(identUid, cond, ifTrue, ifFalse),
|
|
260
|
-
);
|
|
261
|
-
|
|
262
|
-
// Replace call expression with reference to lazy object getter
|
|
263
|
-
path.replaceWith(
|
|
264
|
-
t.memberExpression(t.identifier(identUid), t.identifier('load')),
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
};
|
|
270
|
-
|
|
271
152
|
return {
|
|
272
153
|
name: '@atlaspack/babel-plugin-transform-contextual-imports',
|
|
273
154
|
visitor: {
|
|
274
155
|
CallExpression: {
|
|
275
156
|
enter(path, state) {
|
|
276
|
-
// Preserve server behaviour in deletable code
|
|
277
|
-
checkIsServer(path, state);
|
|
278
|
-
|
|
279
157
|
const node = path.node;
|
|
280
158
|
if (isImportCondCallExpression(node)) {
|
|
281
159
|
const [cond, ifTrue, ifFalse] = node.arguments;
|
|
@@ -335,12 +213,6 @@ export default declare((api): PluginObj<State> => {
|
|
|
335
213
|
state.conditionalImportIdentifiers = new Set();
|
|
336
214
|
state.visitedIdentifiers = new Set();
|
|
337
215
|
},
|
|
338
|
-
exit(path, state) {
|
|
339
|
-
if (state.importNodes) {
|
|
340
|
-
// If there's an import node, add it to the top of the body
|
|
341
|
-
path.unshiftContainer('body', state.importNodes);
|
|
342
|
-
}
|
|
343
|
-
},
|
|
344
216
|
},
|
|
345
217
|
},
|
|
346
218
|
};
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
1
|
import * as babel from '@babel/core';
|
|
4
2
|
import assert from 'assert';
|
|
5
3
|
|
|
@@ -22,29 +20,6 @@ describe('@atlaspack/babel-plugin-transform-contextual-imports', () => {
|
|
|
22
20
|
);
|
|
23
21
|
});
|
|
24
22
|
|
|
25
|
-
it('should transform importCond to server (deprecated) lazy code', () => {
|
|
26
|
-
const input = `
|
|
27
|
-
importCond('CONDITION', 'IF_TRUE', 'IF_FALSE');
|
|
28
|
-
`;
|
|
29
|
-
let {code: transformed} = babel.transformSync(input, {
|
|
30
|
-
configFile: false,
|
|
31
|
-
presets: [],
|
|
32
|
-
plugins: [[plugin, {server: true}]],
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
assert(
|
|
36
|
-
transformed ===
|
|
37
|
-
`const _CONDITION$IF_TRUE$IF_FALSE = {
|
|
38
|
-
ifTrue: require('IF_TRUE').default,
|
|
39
|
-
ifFalse: require('IF_FALSE').default
|
|
40
|
-
};
|
|
41
|
-
Object.defineProperty(_CONDITION$IF_TRUE$IF_FALSE, "load", {
|
|
42
|
-
get: () => globalThis.__MCOND && globalThis.__MCOND('CONDITION') ? _CONDITION$IF_TRUE$IF_FALSE.ifTrue : _CONDITION$IF_TRUE$IF_FALSE.ifFalse
|
|
43
|
-
});
|
|
44
|
-
_CONDITION$IF_TRUE$IF_FALSE.load;`,
|
|
45
|
-
);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
23
|
it('should transform importCond to node lazy code', () => {
|
|
49
24
|
const input = `
|
|
50
25
|
const Imported = importCond('CONDITION', 'IF_TRUE', 'IF_FALSE');
|
package/tsconfig.json
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"esModuleInterop": true,
|
|
6
|
-
"forceConsistentCasingInFileNames": true,
|
|
7
|
-
"strict": true,
|
|
8
|
-
"skipLibCheck": true
|
|
9
|
-
},
|
|
10
|
-
"include": ["src/*"],
|
|
11
|
-
"exclude": ["node_modules"]
|
|
2
|
+
"extends": "../../../tsconfig.json",
|
|
3
|
+
"include": ["src"]
|
|
12
4
|
}
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;AAsBA,MAAMY,iCAAWA,CAACP;IAChB,OAAO,YAAYA,QAAQA,KAAKH,MAAM;AACxC;AAEA,MAAMW,+BAASA,CAACR,OAAwB,CAAC,CAAE,CAAA,UAAUA,QAAQA,KAAKF,IAAI,AAAJA;IAElE,2CAAeH,CAAAA,GAAAA,qCAAAA,EAAQ,CAACc;IACtB,MAAM,EAACE,OAAOC,CAAPD,EAAS,GAAGF;IAEnB,MAAMI,6BAA6BA,CACjCf;QAQA,IACEA,KAAKoB,IAAI,KA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nD;YACF;QACF;IACF;AACF","sources":["packages/utils/babel-plugin-transform-contextual-imports/src/index.ts"],"sourcesContent":["import type {PluginObj, NodePath, types as BabelTypes} from '@babel/core';\nimport {declare} from '@babel/helper-plugin-utils';\n\ninterface Opts {\n /** @deprecated Use \"node\" instead */\n server?: boolean;\n /** Use node safe import cond syntax */\n node?: boolean;\n}\n\ninterface State {\n /** Plugin options */\n opts: Opts;\n /** @deprecated Statement types didn't work so using any */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n importNodes?: any[];\n /** Set of identifier names that need to be mutated after import was transformed */\n conditionalImportIdentifiers?: Set<string>;\n /** Set of identifiers that have been visited in the exit pass, to avoid adding the load property multiple times */\n visitedIdentifiers?: Set<BabelTypes.Identifier>;\n}\n\nconst isServer = (opts: Opts) => {\n return 'server' in opts && opts.server;\n};\n\nconst isNode = (opts: Opts): boolean => !!('node' in opts && opts.node);\n\nexport default declare((api): PluginObj<State> => {\n const {types: t} = api;\n\n const isImportCondCallExpression = (\n node: BabelTypes.Node,\n ): node is BabelTypes.CallExpression & {\n arguments: [\n BabelTypes.StringLiteral,\n BabelTypes.StringLiteral,\n BabelTypes.StringLiteral,\n ];\n } => {\n if (\n node.type === 'CallExpression' &&\n node.callee.type === 'Identifier' &&\n node.callee.name === 'importCond'\n ) {\n if (\n node.arguments.length === 3 &&\n node.arguments.every(\n (arg): arg is BabelTypes.StringLiteral =>\n arg.type === 'StringLiteral',\n )\n ) {\n return true;\n } else {\n // Simple error for incorrect syntax (since it's documented with the type)\n throw new Error('importCond must have three string literal arguments');\n }\n }\n\n return false;\n };\n\n const buildCondFunction = (\n cond: BabelTypes.StringLiteral,\n ifTrue: BabelTypes.StringLiteral,\n ifFalse: BabelTypes.StringLiteral,\n ) =>\n t.conditionalExpression(\n t.logicalExpression(\n '&&',\n t.memberExpression(t.identifier('globalThis'), t.identifier('__MCOND')),\n t.callExpression(\n t.memberExpression(\n t.identifier('globalThis'),\n t.identifier('__MCOND'),\n ),\n [cond],\n ),\n ),\n t.memberExpression(\n t.callExpression(t.identifier('require'), [ifTrue]),\n t.identifier('default'),\n ),\n t.memberExpression(\n t.callExpression(t.identifier('require'), [ifFalse]),\n t.identifier('default'),\n ),\n );\n\n const buildNodeObject = (\n identifier: BabelTypes.Identifier,\n cond: BabelTypes.StringLiteral,\n ifTrue: BabelTypes.StringLiteral,\n ifFalse: BabelTypes.StringLiteral,\n ) => [\n // Create object containing imports\n t.variableDeclaration('const', [\n t.variableDeclarator(\n identifier,\n t.objectExpression([\n t.objectProperty(\n t.identifier('ifTrue'),\n t.memberExpression(\n t.callExpression(t.identifier('require'), [ifTrue]),\n t.identifier('default'),\n ),\n ),\n t.objectProperty(\n t.identifier('ifFalse'),\n t.memberExpression(\n t.callExpression(t.identifier('require'), [ifFalse]),\n t.identifier('default'),\n ),\n ),\n ]),\n ),\n ]),\n\n // Create lazy getter via the load property on the object.\n // This is node module resolution safe because each time the import is accessed, we re-evaluate the condition.\n t.expressionStatement(\n t.callExpression(\n t.memberExpression(\n t.identifier('Object'),\n t.identifier('defineProperty'),\n ),\n [\n identifier,\n t.stringLiteral('load'),\n t.objectExpression([\n t.objectProperty(\n t.identifier('get'),\n t.arrowFunctionExpression(\n [],\n t.conditionalExpression(\n t.logicalExpression(\n '&&',\n t.memberExpression(\n t.identifier('globalThis'),\n t.identifier('__MCOND'),\n ),\n t.callExpression(\n t.memberExpression(\n t.identifier('globalThis'),\n t.identifier('__MCOND'),\n ),\n [cond],\n ),\n ),\n t.memberExpression(identifier, t.identifier('ifTrue')),\n t.memberExpression(identifier, t.identifier('ifFalse')),\n ),\n ),\n ),\n ]),\n ],\n ),\n ),\n ];\n\n const buildServerObject = (\n identUid: string,\n cond: BabelTypes.StringLiteral,\n ifTrue: BabelTypes.StringLiteral,\n ifFalse: BabelTypes.StringLiteral,\n ) => [\n // Create object containing imports\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(identUid),\n t.objectExpression([\n t.objectProperty(\n t.identifier('ifTrue'),\n t.memberExpression(\n t.callExpression(t.identifier('require'), [ifTrue]),\n t.identifier('default'),\n ),\n ),\n t.objectProperty(\n t.identifier('ifFalse'),\n t.memberExpression(\n t.callExpression(t.identifier('require'), [ifFalse]),\n t.identifier('default'),\n ),\n ),\n ]),\n ),\n ]),\n\n // Create lazy getter via the load property on the object\n t.expressionStatement(\n t.callExpression(\n t.memberExpression(\n t.identifier('Object'),\n t.identifier('defineProperty'),\n ),\n [\n t.identifier(identUid),\n t.stringLiteral('load'),\n t.objectExpression([\n t.objectProperty(\n t.identifier('get'),\n t.arrowFunctionExpression(\n [],\n t.conditionalExpression(\n t.logicalExpression(\n '&&',\n t.memberExpression(\n t.identifier('globalThis'),\n t.identifier('__MCOND'),\n ),\n t.callExpression(\n t.memberExpression(\n t.identifier('globalThis'),\n t.identifier('__MCOND'),\n ),\n [cond],\n ),\n ),\n t.memberExpression(\n t.identifier(identUid),\n t.identifier('ifTrue'),\n ),\n t.memberExpression(\n t.identifier(identUid),\n t.identifier('ifFalse'),\n ),\n ),\n ),\n ),\n ]),\n ],\n ),\n ),\n ];\n\n const checkIsServer = (\n path: NodePath<BabelTypes.CallExpression>,\n state: State,\n ) => {\n if (\n path.node.callee.type === 'Identifier' &&\n path.node.callee.name === 'importCond'\n ) {\n if (\n path.node.arguments.length == 3 &&\n path.node.arguments.every((arg) => arg.type === 'StringLiteral')\n ) {\n const [cond, ifTrue, ifFalse] = path.node.arguments;\n\n if (isServer(state.opts)) {\n // Make module pass lazy in ssr\n const identUid = path.scope.generateUid(\n `${cond.value}$${ifTrue.value}$${ifFalse.value}`,\n );\n\n state.importNodes ??= [];\n state.importNodes.push(\n ...buildServerObject(identUid, cond, ifTrue, ifFalse),\n );\n\n // Replace call expression with reference to lazy object getter\n path.replaceWith(\n t.memberExpression(t.identifier(identUid), t.identifier('load')),\n );\n }\n }\n }\n };\n\n return {\n name: '@atlaspack/babel-plugin-transform-contextual-imports',\n visitor: {\n CallExpression: {\n enter(path, state) {\n // Preserve server behaviour in deletable code\n checkIsServer(path, state);\n\n const node = path.node;\n if (isImportCondCallExpression(node)) {\n const [cond, ifTrue, ifFalse] = node.arguments;\n if (!isNode(state.opts)) {\n // Replace the importCond call with a conditional require import, as a fallback for environments that don't support Atlaspack\n path.replaceWith(buildCondFunction(cond, ifTrue, ifFalse));\n }\n }\n },\n },\n VariableDeclaration: {\n enter(path, state) {\n if (isNode(state.opts)) {\n if (\n path.node.declarations.length === 1 &&\n path.node.declarations[0].type === 'VariableDeclarator' &&\n path.node.declarations[0].id.type === 'Identifier'\n ) {\n const importId = path.node.declarations[0].id;\n const call = path.node.declarations[0].init;\n\n // Mark identifier for object so we don't add the load property to it\n state.visitedIdentifiers?.add(importId);\n\n if (call && isImportCondCallExpression(call)) {\n const [cond, ifTrue, ifFalse] = call.arguments;\n\n // Replace with object containing imports and lazy getter, which allows us to load the correct import based on the condition at runtime\n path.replaceWithMultiple(\n buildNodeObject(importId, cond, ifTrue, ifFalse),\n );\n\n // Add identifier name to set so we can mutate all import usages in the exit pass\n state.conditionalImportIdentifiers?.add(importId.name);\n }\n }\n }\n },\n },\n Identifier: {\n exit(path, state) {\n const identifier = state.conditionalImportIdentifiers?.has(\n path.node.name,\n );\n if (identifier && !state.visitedIdentifiers?.has(path.node)) {\n // Add load property to the import usage\n const newIdentifer = t.identifier(path.node.name);\n path.replaceWith(\n t.memberExpression(newIdentifer, t.identifier('load')),\n );\n state.visitedIdentifiers?.add(newIdentifer);\n }\n },\n },\n Program: {\n enter(_, state) {\n state.conditionalImportIdentifiers = new Set();\n state.visitedIdentifiers = new Set();\n },\n exit(path, state) {\n if (state.importNodes) {\n // If there's an import node, add it to the top of the body\n path.unshiftContainer('body', state.importNodes);\n }\n },\n },\n },\n };\n});\n"],"names":["declare","Opts","server","node","State","opts","importNodes","conditionalImportIdentifiers","Set","visitedIdentifiers","BabelTypes","Identifier","isServer","isNode","api","PluginObj","types","t","isImportCondCallExpression","Node","CallExpression","arguments","StringLiteral","type","callee","name","length","every","arg","Error","buildCondFunction","cond","ifTrue","ifFalse","conditionalExpression","logicalExpression","memberExpression","identifier","callExpression","buildNodeObject","variableDeclaration","variableDeclarator","objectExpression","objectProperty","expressionStatement","stringLiteral","arrowFunctionExpression","buildServerObject","identUid","checkIsServer","path","NodePath","state","scope","generateUid","value","push","replaceWith","visitor","enter","VariableDeclaration","declarations","id","importId","call","init","add","replaceWithMultiple","exit","has","newIdentifer","Program","_","unshiftContainer"],"version":3,"file":"index.js.map","sourceRoot":"../../../../"}
|