@atlaspack/babel-plugin-transform-contextual-imports 2.14.2-unified-f92fba5b6.0 → 2.15.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
- var $b65XQ$babelhelperpluginutils = require("@babel/helper-plugin-utils");
1
+ "use strict";
2
2
 
3
-
4
- function $parcel$defineInteropFlag(a) {
5
- Object.defineProperty(a, '__esModule', {value: true, configurable: true});
6
- }
7
-
8
- function $parcel$export(e, n, v, s) {
9
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
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
- $parcel$defineInteropFlag(module.exports);
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
- $parcel$export(module.exports, "default", () => $35f0bedee55d5f76$export$2e2bcd8739ae039);
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
- const $35f0bedee55d5f76$var$isServer = (opts)=>{
17
- return 'server' in opts && opts.server;
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
- return {
100
- name: '@atlaspack/babel-plugin-transform-contextual-imports',
101
- visitor: {
102
- CallExpression: {
103
- enter (path, state) {
104
- // Preserve server behaviour in deletable code
105
- checkIsServer(path, state);
106
- const node = path.node;
107
- if (isImportCondCallExpression(node)) {
108
- const [cond, ifTrue, ifFalse] = node.arguments;
109
- if (!$35f0bedee55d5f76$var$isNode(state.opts)) // Replace the importCond call with a conditional require import, as a fallback for environments that don't support Atlaspack
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
- //# sourceMappingURL=index.js.map
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.14.2-unified-f92fba5b6.0",
3
+ "version": "2.15.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
  },
@@ -22,6 +23,5 @@
22
23
  "@types/babel__core": "^7.12.2",
23
24
  "@types/babel__helper-plugin-utils": "^7.10.3"
24
25
  },
25
- "type": "commonjs",
26
- "gitHead": "f92fba5b6216e1d94cbd23f3a5d61f7188d49199"
26
+ "type": "commonjs"
27
27
  }
package/src/index.ts CHANGED
@@ -1,9 +1,7 @@
1
- import type {PluginObj, NodePath, types as BabelTypes} from '@babel/core';
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
  };
@@ -22,29 +22,6 @@ describe('@atlaspack/babel-plugin-transform-contextual-imports', () => {
22
22
  );
23
23
  });
24
24
 
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
25
  it('should transform importCond to node lazy code', () => {
49
26
  const input = `
50
27
  const Imported = importCond('CONDITION', 'IF_TRUE', 'IF_FALSE');
package/LICENSE DELETED
@@ -1,201 +0,0 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
-
7
- 1. Definitions.
8
-
9
- "License" shall mean the terms and conditions for use, reproduction,
10
- and distribution as defined by Sections 1 through 9 of this document.
11
-
12
- "Licensor" shall mean the copyright owner or entity authorized by
13
- the copyright owner that is granting the License.
14
-
15
- "Legal Entity" shall mean the union of the acting entity and all
16
- other entities that control, are controlled by, or are under common
17
- control with that entity. For the purposes of this definition,
18
- "control" means (i) the power, direct or indirect, to cause the
19
- direction or management of such entity, whether by contract or
20
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
- outstanding shares, or (iii) beneficial ownership of such entity.
22
-
23
- "You" (or "Your") shall mean an individual or Legal Entity
24
- exercising permissions granted by this License.
25
-
26
- "Source" form shall mean the preferred form for making modifications,
27
- including but not limited to software source code, documentation
28
- source, and configuration files.
29
-
30
- "Object" form shall mean any form resulting from mechanical
31
- transformation or translation of a Source form, including but
32
- not limited to compiled object code, generated documentation,
33
- and conversions to other media types.
34
-
35
- "Work" shall mean the work of authorship, whether in Source or
36
- Object form, made available under the License, as indicated by a
37
- copyright notice that is included in or attached to the work
38
- (an example is provided in the Appendix below).
39
-
40
- "Derivative Works" shall mean any work, whether in Source or Object
41
- form, that is based on (or derived from) the Work and for which the
42
- editorial revisions, annotations, elaborations, or other modifications
43
- represent, as a whole, an original work of authorship. For the purposes
44
- of this License, Derivative Works shall not include works that remain
45
- separable from, or merely link (or bind by name) to the interfaces of,
46
- the Work and Derivative Works thereof.
47
-
48
- "Contribution" shall mean any work of authorship, including
49
- the original version of the Work and any modifications or additions
50
- to that Work or Derivative Works thereof, that is intentionally
51
- submitted to Licensor for inclusion in the Work by the copyright owner
52
- or by an individual or Legal Entity authorized to submit on behalf of
53
- the copyright owner. For the purposes of this definition, "submitted"
54
- means any form of electronic, verbal, or written communication sent
55
- to the Licensor or its representatives, including but not limited to
56
- communication on electronic mailing lists, source code control systems,
57
- and issue tracking systems that are managed by, or on behalf of, the
58
- Licensor for the purpose of discussing and improving the Work, but
59
- excluding communication that is conspicuously marked or otherwise
60
- designated in writing by the copyright owner as "Not a Contribution."
61
-
62
- "Contributor" shall mean Licensor and any individual or Legal Entity
63
- on behalf of whom a Contribution has been received by Licensor and
64
- subsequently incorporated within the Work.
65
-
66
- 2. Grant of Copyright License. Subject to the terms and conditions of
67
- this License, each Contributor hereby grants to You a perpetual,
68
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
- copyright license to reproduce, prepare Derivative Works of,
70
- publicly display, publicly perform, sublicense, and distribute the
71
- Work and such Derivative Works in Source or Object form.
72
-
73
- 3. Grant of Patent License. Subject to the terms and conditions of
74
- this License, each Contributor hereby grants to You a perpetual,
75
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
- (except as stated in this section) patent license to make, have made,
77
- use, offer to sell, sell, import, and otherwise transfer the Work,
78
- where such license applies only to those patent claims licensable
79
- by such Contributor that are necessarily infringed by their
80
- Contribution(s) alone or by combination of their Contribution(s)
81
- with the Work to which such Contribution(s) was submitted. If You
82
- institute patent litigation against any entity (including a
83
- cross-claim or counterclaim in a lawsuit) alleging that the Work
84
- or a Contribution incorporated within the Work constitutes direct
85
- or contributory patent infringement, then any patent licenses
86
- granted to You under this License for that Work shall terminate
87
- as of the date such litigation is filed.
88
-
89
- 4. Redistribution. You may reproduce and distribute copies of the
90
- Work or Derivative Works thereof in any medium, with or without
91
- modifications, and in Source or Object form, provided that You
92
- meet the following conditions:
93
-
94
- (a) You must give any other recipients of the Work or
95
- Derivative Works a copy of this License; and
96
-
97
- (b) You must cause any modified files to carry prominent notices
98
- stating that You changed the files; and
99
-
100
- (c) You must retain, in the Source form of any Derivative Works
101
- that You distribute, all copyright, patent, trademark, and
102
- attribution notices from the Source form of the Work,
103
- excluding those notices that do not pertain to any part of
104
- the Derivative Works; and
105
-
106
- (d) If the Work includes a "NOTICE" text file as part of its
107
- distribution, then any Derivative Works that You distribute must
108
- include a readable copy of the attribution notices contained
109
- within such NOTICE file, excluding those notices that do not
110
- pertain to any part of the Derivative Works, in at least one
111
- of the following places: within a NOTICE text file distributed
112
- as part of the Derivative Works; within the Source form or
113
- documentation, if provided along with the Derivative Works; or,
114
- within a display generated by the Derivative Works, if and
115
- wherever such third-party notices normally appear. The contents
116
- of the NOTICE file are for informational purposes only and
117
- do not modify the License. You may add Your own attribution
118
- notices within Derivative Works that You distribute, alongside
119
- or as an addendum to the NOTICE text from the Work, provided
120
- that such additional attribution notices cannot be construed
121
- as modifying the License.
122
-
123
- You may add Your own copyright statement to Your modifications and
124
- may provide additional or different license terms and conditions
125
- for use, reproduction, or distribution of Your modifications, or
126
- for any such Derivative Works as a whole, provided Your use,
127
- reproduction, and distribution of the Work otherwise complies with
128
- the conditions stated in this License.
129
-
130
- 5. Submission of Contributions. Unless You explicitly state otherwise,
131
- any Contribution intentionally submitted for inclusion in the Work
132
- by You to the Licensor shall be under the terms and conditions of
133
- this License, without any additional terms or conditions.
134
- Notwithstanding the above, nothing herein shall supersede or modify
135
- the terms of any separate license agreement you may have executed
136
- with Licensor regarding such Contributions.
137
-
138
- 6. Trademarks. This License does not grant permission to use the trade
139
- names, trademarks, service marks, or product names of the Licensor,
140
- except as required for reasonable and customary use in describing the
141
- origin of the Work and reproducing the content of the NOTICE file.
142
-
143
- 7. Disclaimer of Warranty. Unless required by applicable law or
144
- agreed to in writing, Licensor provides the Work (and each
145
- Contributor provides its Contributions) on an "AS IS" BASIS,
146
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
- implied, including, without limitation, any warranties or conditions
148
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
- PARTICULAR PURPOSE. You are solely responsible for determining the
150
- appropriateness of using or redistributing the Work and assume any
151
- risks associated with Your exercise of permissions under this License.
152
-
153
- 8. Limitation of Liability. In no event and under no legal theory,
154
- whether in tort (including negligence), contract, or otherwise,
155
- unless required by applicable law (such as deliberate and grossly
156
- negligent acts) or agreed to in writing, shall any Contributor be
157
- liable to You for damages, including any direct, indirect, special,
158
- incidental, or consequential damages of any character arising as a
159
- result of this License or out of the use or inability to use the
160
- Work (including but not limited to damages for loss of goodwill,
161
- work stoppage, computer failure or malfunction, or any and all
162
- other commercial damages or losses), even if such Contributor
163
- has been advised of the possibility of such damages.
164
-
165
- 9. Accepting Warranty or Additional Liability. While redistributing
166
- the Work or Derivative Works thereof, You may choose to offer,
167
- and charge a fee for, acceptance of support, warranty, indemnity,
168
- or other liability obligations and/or rights consistent with this
169
- License. However, in accepting such obligations, You may act only
170
- on Your own behalf and on Your sole responsibility, not on behalf
171
- of any other Contributor, and only if You agree to indemnify,
172
- defend, and hold each Contributor harmless for any liability
173
- incurred by, or claims asserted against, such Contributor by reason
174
- of your accepting any such warranty or additional liability.
175
-
176
- END OF TERMS AND CONDITIONS
177
-
178
- APPENDIX: How to apply the Apache License to your work.
179
-
180
- To apply the Apache License to your work, attach the following
181
- boilerplate notice, with the fields enclosed by brackets "[]"
182
- replaced with your own identifying information. (Don't include
183
- the brackets!) The text should be enclosed in the appropriate
184
- comment syntax for the file format. We also recommend that a
185
- file or class name and description of purpose be included on the
186
- same "printed page" as the copyright notice for easier
187
- identification within third-party archives.
188
-
189
- Copyright (c) 2024 Atlassian US., Inc.
190
-
191
- Licensed under the Apache License, Version 2.0 (the "License");
192
- you may not use this file except in compliance with the License.
193
- You may obtain a copy of the License at
194
-
195
- http://www.apache.org/licenses/LICENSE-2.0
196
-
197
- Unless required by applicable law or agreed to in writing, software
198
- distributed under the License is distributed on an "AS IS" BASIS,
199
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
- See the License for the specific language governing permissions and
201
- limitations under the License.
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,KAAK,oBACdpB,KAAKqB,MAAM,CAACD,IAAI,KAAK,gBACrBpB,KAAKqB,MAAM,CA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Content":["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":"../../../../"}