@atlaspack/babel-plugin-transform-contextual-imports 2.14.2-noselfbuild-4bb690920.0 → 2.14.2-noselfbuild-d72cb5e47.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/lib/index.d.mts +0 -4
- package/lib/index.js +0 -49
- package/lib/index.mjs +0 -45
- package/package.json +3 -2
- package/src/index.mts +1 -129
- package/test/babel-plugin-transform-contextual-imports.test.mts +0 -27
- package/lib/index.cjs +0 -7
package/lib/index.d.mts
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import type { PluginObj, types as BabelTypes } from '@babel/core';
|
|
2
2
|
interface Opts {
|
|
3
|
-
/** @deprecated Use "node" instead */
|
|
4
|
-
server?: boolean;
|
|
5
3
|
/** Use node safe import cond syntax */
|
|
6
4
|
node?: boolean;
|
|
7
5
|
}
|
|
8
6
|
interface State {
|
|
9
7
|
/** Plugin options */
|
|
10
8
|
opts: Opts;
|
|
11
|
-
/** @deprecated Statement types didn't work so using any */
|
|
12
|
-
importNodes?: any[];
|
|
13
9
|
/** Set of identifier names that need to be mutated after import was transformed */
|
|
14
10
|
conditionalImportIdentifiers?: Set<string>;
|
|
15
11
|
/** Set of identifiers that have been visited in the exit pass, to avoid adding the load property multiple times */
|
package/lib/index.js
CHANGED
|
@@ -9,9 +9,6 @@ Object.defineProperty(exports, "default", {
|
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
11
|
const _helperpluginutils = require("@babel/helper-plugin-utils");
|
|
12
|
-
const isServer = (opts)=>{
|
|
13
|
-
return 'server' in opts && opts.server;
|
|
14
|
-
};
|
|
15
12
|
const isNode = (opts)=>!!('node' in opts && opts.node);
|
|
16
13
|
const _default = (0, _helperpluginutils.declare)((api)=>{
|
|
17
14
|
const { types: t } = api;
|
|
@@ -57,51 +54,11 @@ const _default = (0, _helperpluginutils.declare)((api)=>{
|
|
|
57
54
|
])
|
|
58
55
|
]))
|
|
59
56
|
];
|
|
60
|
-
const buildServerObject = (identUid, cond, ifTrue, ifFalse)=>[
|
|
61
|
-
// Create object containing imports
|
|
62
|
-
t.variableDeclaration('const', [
|
|
63
|
-
t.variableDeclarator(t.identifier(identUid), t.objectExpression([
|
|
64
|
-
t.objectProperty(t.identifier('ifTrue'), t.memberExpression(t.callExpression(t.identifier('require'), [
|
|
65
|
-
ifTrue
|
|
66
|
-
]), t.identifier('default'))),
|
|
67
|
-
t.objectProperty(t.identifier('ifFalse'), t.memberExpression(t.callExpression(t.identifier('require'), [
|
|
68
|
-
ifFalse
|
|
69
|
-
]), t.identifier('default')))
|
|
70
|
-
]))
|
|
71
|
-
]),
|
|
72
|
-
// Create lazy getter via the load property on the object
|
|
73
|
-
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('defineProperty')), [
|
|
74
|
-
t.identifier(identUid),
|
|
75
|
-
t.stringLiteral('load'),
|
|
76
|
-
t.objectExpression([
|
|
77
|
-
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')), [
|
|
78
|
-
cond
|
|
79
|
-
])), t.memberExpression(t.identifier(identUid), t.identifier('ifTrue')), t.memberExpression(t.identifier(identUid), t.identifier('ifFalse')))))
|
|
80
|
-
])
|
|
81
|
-
]))
|
|
82
|
-
];
|
|
83
|
-
const checkIsServer = (path, state)=>{
|
|
84
|
-
if (path.node.callee.type === 'Identifier' && path.node.callee.name === 'importCond') {
|
|
85
|
-
if (path.node.arguments.length == 3 && path.node.arguments.every((arg)=>arg.type === 'StringLiteral')) {
|
|
86
|
-
const [cond, ifTrue, ifFalse] = path.node.arguments;
|
|
87
|
-
if (isServer(state.opts)) {
|
|
88
|
-
// Make module pass lazy in ssr
|
|
89
|
-
const identUid = path.scope.generateUid(`${cond.value}$${ifTrue.value}$${ifFalse.value}`);
|
|
90
|
-
state.importNodes ??= [];
|
|
91
|
-
state.importNodes.push(...buildServerObject(identUid, cond, ifTrue, ifFalse));
|
|
92
|
-
// Replace call expression with reference to lazy object getter
|
|
93
|
-
path.replaceWith(t.memberExpression(t.identifier(identUid), t.identifier('load')));
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
57
|
return {
|
|
99
58
|
name: '@atlaspack/babel-plugin-transform-contextual-imports',
|
|
100
59
|
visitor: {
|
|
101
60
|
CallExpression: {
|
|
102
61
|
enter (path, state) {
|
|
103
|
-
// Preserve server behaviour in deletable code
|
|
104
|
-
checkIsServer(path, state);
|
|
105
62
|
const node = path.node;
|
|
106
63
|
if (isImportCondCallExpression(node)) {
|
|
107
64
|
const [cond, ifTrue, ifFalse] = node.arguments;
|
|
@@ -146,12 +103,6 @@ const _default = (0, _helperpluginutils.declare)((api)=>{
|
|
|
146
103
|
enter (_, state) {
|
|
147
104
|
state.conditionalImportIdentifiers = new Set();
|
|
148
105
|
state.visitedIdentifiers = new Set();
|
|
149
|
-
},
|
|
150
|
-
exit (path, state) {
|
|
151
|
-
if (state.importNodes) {
|
|
152
|
-
// If there's an import node, add it to the top of the body
|
|
153
|
-
path.unshiftContainer('body', state.importNodes);
|
|
154
|
-
}
|
|
155
106
|
}
|
|
156
107
|
}
|
|
157
108
|
}
|
package/lib/index.mjs
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import { declare } from '@babel/helper-plugin-utils';
|
|
2
|
-
const isServer = (opts) => {
|
|
3
|
-
return 'server' in opts && opts.server;
|
|
4
|
-
};
|
|
5
2
|
const isNode = (opts) => !!('node' in opts && opts.node);
|
|
6
3
|
export default declare((api) => {
|
|
7
4
|
const { types: t } = api;
|
|
@@ -39,47 +36,11 @@ export default declare((api) => {
|
|
|
39
36
|
]),
|
|
40
37
|
])),
|
|
41
38
|
];
|
|
42
|
-
const buildServerObject = (identUid, cond, ifTrue, ifFalse) => [
|
|
43
|
-
// Create object containing imports
|
|
44
|
-
t.variableDeclaration('const', [
|
|
45
|
-
t.variableDeclarator(t.identifier(identUid), t.objectExpression([
|
|
46
|
-
t.objectProperty(t.identifier('ifTrue'), t.memberExpression(t.callExpression(t.identifier('require'), [ifTrue]), t.identifier('default'))),
|
|
47
|
-
t.objectProperty(t.identifier('ifFalse'), t.memberExpression(t.callExpression(t.identifier('require'), [ifFalse]), t.identifier('default'))),
|
|
48
|
-
])),
|
|
49
|
-
]),
|
|
50
|
-
// Create lazy getter via the load property on the object
|
|
51
|
-
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('defineProperty')), [
|
|
52
|
-
t.identifier(identUid),
|
|
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')), [cond])), t.memberExpression(t.identifier(identUid), t.identifier('ifTrue')), t.memberExpression(t.identifier(identUid), t.identifier('ifFalse'))))),
|
|
56
|
-
]),
|
|
57
|
-
])),
|
|
58
|
-
];
|
|
59
|
-
const checkIsServer = (path, state) => {
|
|
60
|
-
if (path.node.callee.type === 'Identifier' &&
|
|
61
|
-
path.node.callee.name === 'importCond') {
|
|
62
|
-
if (path.node.arguments.length == 3 &&
|
|
63
|
-
path.node.arguments.every((arg) => arg.type === 'StringLiteral')) {
|
|
64
|
-
const [cond, ifTrue, ifFalse] = path.node.arguments;
|
|
65
|
-
if (isServer(state.opts)) {
|
|
66
|
-
// Make module pass lazy in ssr
|
|
67
|
-
const identUid = path.scope.generateUid(`${cond.value}$${ifTrue.value}$${ifFalse.value}`);
|
|
68
|
-
state.importNodes ??= [];
|
|
69
|
-
state.importNodes.push(...buildServerObject(identUid, cond, ifTrue, ifFalse));
|
|
70
|
-
// Replace call expression with reference to lazy object getter
|
|
71
|
-
path.replaceWith(t.memberExpression(t.identifier(identUid), t.identifier('load')));
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
39
|
return {
|
|
77
40
|
name: '@atlaspack/babel-plugin-transform-contextual-imports',
|
|
78
41
|
visitor: {
|
|
79
42
|
CallExpression: {
|
|
80
43
|
enter(path, state) {
|
|
81
|
-
// Preserve server behaviour in deletable code
|
|
82
|
-
checkIsServer(path, state);
|
|
83
44
|
const node = path.node;
|
|
84
45
|
if (isImportCondCallExpression(node)) {
|
|
85
46
|
const [cond, ifTrue, ifFalse] = node.arguments;
|
|
@@ -127,12 +88,6 @@ export default declare((api) => {
|
|
|
127
88
|
state.conditionalImportIdentifiers = new Set();
|
|
128
89
|
state.visitedIdentifiers = new Set();
|
|
129
90
|
},
|
|
130
|
-
exit(path, state) {
|
|
131
|
-
if (state.importNodes) {
|
|
132
|
-
// If there's an import node, add it to the top of the body
|
|
133
|
-
path.unshiftContainer('body', state.importNodes);
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
91
|
},
|
|
137
92
|
},
|
|
138
93
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/babel-plugin-transform-contextual-imports",
|
|
3
|
-
"version": "2.14.2-noselfbuild-
|
|
3
|
+
"version": "2.14.2-noselfbuild-d72cb5e47.0",
|
|
4
4
|
"license": "(MIT OR Apache-2.0)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"./src/index.mts"
|
|
21
21
|
],
|
|
22
22
|
"import": "./lib/index.mjs",
|
|
23
|
+
"module-sync": "./lib/index.mjs",
|
|
23
24
|
"require": "./lib/index.js",
|
|
24
25
|
"default": "./lib/index.js"
|
|
25
26
|
},
|
|
@@ -39,5 +40,5 @@
|
|
|
39
40
|
"@types/babel__core": "^7.12.2",
|
|
40
41
|
"@types/babel__helper-plugin-utils": "^7.10.3"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "d72cb5e4768e0d47790c3c9b7ce171f10c79cc85"
|
|
43
44
|
}
|
package/src/index.mts
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
|
};
|
|
@@ -28,33 +28,6 @@ describe('@atlaspack/babel-plugin-transform-contextual-imports', () => {
|
|
|
28
28
|
);
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
it('should transform importCond to server (deprecated) lazy code', () => {
|
|
32
|
-
const input = `
|
|
33
|
-
importCond('CONDITION', 'IF_TRUE', 'IF_FALSE');
|
|
34
|
-
`;
|
|
35
|
-
const result = babel.transformSync(input, {
|
|
36
|
-
configFile: false,
|
|
37
|
-
presets: [],
|
|
38
|
-
plugins: [[plugin, {server: true}]],
|
|
39
|
-
});
|
|
40
|
-
if (!result) {
|
|
41
|
-
throw new Error('Unable to produce result');
|
|
42
|
-
}
|
|
43
|
-
const {code: transformed} = result;
|
|
44
|
-
|
|
45
|
-
assert(
|
|
46
|
-
transformed ===
|
|
47
|
-
`const _CONDITION$IF_TRUE$IF_FALSE = {
|
|
48
|
-
ifTrue: require('IF_TRUE').default,
|
|
49
|
-
ifFalse: require('IF_FALSE').default
|
|
50
|
-
};
|
|
51
|
-
Object.defineProperty(_CONDITION$IF_TRUE$IF_FALSE, "load", {
|
|
52
|
-
get: () => globalThis.__MCOND && globalThis.__MCOND('CONDITION') ? _CONDITION$IF_TRUE$IF_FALSE.ifTrue : _CONDITION$IF_TRUE$IF_FALSE.ifFalse
|
|
53
|
-
});
|
|
54
|
-
_CONDITION$IF_TRUE$IF_FALSE.load;`,
|
|
55
|
-
);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
31
|
it('should transform importCond to node lazy code', () => {
|
|
59
32
|
const input = `
|
|
60
33
|
const Imported = importCond('CONDITION', 'IF_TRUE', 'IF_FALSE');
|