@atlaspack/babel-plugin-transform-contextual-imports 2.14.2-noselfbuild-8163359c1.0 → 2.14.2-noselfbuild-63bde801d.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.js CHANGED
@@ -1,110 +1,97 @@
1
1
  "use strict";
2
+
2
3
  Object.defineProperty(exports, "__esModule", {
3
- value: true
4
+ value: true
4
5
  });
5
- Object.defineProperty(exports, "default", {
6
- enumerable: true,
7
- get: function() {
8
- return _default;
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;
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
+ }
9
27
  }
10
- });
11
- const _helperpluginutils = require("@babel/helper-plugin-utils");
12
- const isNode = (opts)=>!!('node' in opts && opts.node);
13
- const _default = (0, _helperpluginutils.declare)((api)=>{
14
- const { types: t } = api;
15
- const isImportCondCallExpression = (node)=>{
16
- if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'importCond') {
17
- if (node.arguments.length === 3 && node.arguments.every((arg)=>arg.type === 'StringLiteral')) {
18
- return true;
19
- } else {
20
- // Simple error for incorrect syntax (since it's documented with the type)
21
- throw new Error('importCond must have three string literal arguments');
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));
22
48
  }
49
+ }
23
50
  }
24
- return false;
25
- };
26
- 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')), [
27
- cond
28
- ])), t.memberExpression(t.callExpression(t.identifier('require'), [
29
- ifTrue
30
- ]), t.identifier('default')), t.memberExpression(t.callExpression(t.identifier('require'), [
31
- ifFalse
32
- ]), t.identifier('default')));
33
- const buildNodeObject = (identifier, cond, ifTrue, ifFalse)=>[
34
- // Create object containing imports
35
- t.variableDeclaration('const', [
36
- t.variableDeclarator(identifier, t.objectExpression([
37
- t.objectProperty(t.identifier('ifTrue'), t.memberExpression(t.callExpression(t.identifier('require'), [
38
- ifTrue
39
- ]), t.identifier('default'))),
40
- t.objectProperty(t.identifier('ifFalse'), t.memberExpression(t.callExpression(t.identifier('require'), [
41
- ifFalse
42
- ]), t.identifier('default')))
43
- ]))
44
- ]),
45
- // Create lazy getter via the load property on the object.
46
- // This is node module resolution safe because each time the import is accessed, we re-evaluate the condition.
47
- t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('defineProperty')), [
48
- identifier,
49
- t.stringLiteral('load'),
50
- t.objectExpression([
51
- 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')), [
52
- cond
53
- ])), t.memberExpression(identifier, t.identifier('ifTrue')), t.memberExpression(identifier, t.identifier('ifFalse')))))
54
- ])
55
- ]))
56
- ];
57
- return {
58
- name: '@atlaspack/babel-plugin-transform-contextual-imports',
59
- visitor: {
60
- CallExpression: {
61
- enter (path, state) {
62
- const node = path.node;
63
- if (isImportCondCallExpression(node)) {
64
- const [cond, ifTrue, ifFalse] = node.arguments;
65
- if (!isNode(state.opts)) {
66
- // Replace the importCond call with a conditional require import, as a fallback for environments that don't support Atlaspack
67
- path.replaceWith(buildCondFunction(cond, ifTrue, ifFalse));
68
- }
69
- }
70
- }
71
- },
72
- VariableDeclaration: {
73
- enter (path, state) {
74
- if (isNode(state.opts)) {
75
- if (path.node.declarations.length === 1 && path.node.declarations[0].type === 'VariableDeclarator' && path.node.declarations[0].id.type === 'Identifier') {
76
- const importId = path.node.declarations[0].id;
77
- const call = path.node.declarations[0].init;
78
- // Mark identifier for object so we don't add the load property to it
79
- state.visitedIdentifiers?.add(importId);
80
- if (call && isImportCondCallExpression(call)) {
81
- const [cond, ifTrue, ifFalse] = call.arguments;
82
- // Replace with object containing imports and lazy getter, which allows us to load the correct import based on the condition at runtime
83
- path.replaceWithMultiple(buildNodeObject(importId, cond, ifTrue, ifFalse));
84
- // Add identifier name to set so we can mutate all import usages in the exit pass
85
- state.conditionalImportIdentifiers?.add(importId.name);
86
- }
87
- }
88
- }
89
- }
90
- },
91
- Identifier: {
92
- exit (path, state) {
93
- const identifier = state.conditionalImportIdentifiers?.has(path.node.name);
94
- if (identifier && !state.visitedIdentifiers?.has(path.node)) {
95
- // Add load property to the import usage
96
- const newIdentifer = t.identifier(path.node.name);
97
- path.replaceWith(t.memberExpression(newIdentifer, t.identifier('load')));
98
- state.visitedIdentifiers?.add(newIdentifer);
99
- }
100
- }
101
- },
102
- Program: {
103
- enter (_, state) {
104
- state.conditionalImportIdentifiers = new Set();
105
- state.visitedIdentifiers = new Set();
106
- }
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;
59
+
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;
65
+
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));
68
+
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
+ }
107
72
  }
73
+ }
108
74
  }
109
- };
110
- });
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
+ }
87
+ }
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,8 +1,7 @@
1
1
  {
2
2
  "name": "@atlaspack/babel-plugin-transform-contextual-imports",
3
- "version": "2.14.2-noselfbuild-8163359c1.0",
3
+ "version": "2.14.2-noselfbuild-63bde801d.0",
4
4
  "license": "(MIT OR Apache-2.0)",
5
- "type": "module",
6
5
  "publishConfig": {
7
6
  "access": "public"
8
7
  },
@@ -10,28 +9,12 @@
10
9
  "type": "git",
11
10
  "url": "https://github.com/atlassian-labs/atlaspack.git"
12
11
  },
13
- "main": "lib/index.js",
14
- "types": "lib/index.d.mts",
15
- "exports": {
16
- ".": {
17
- "atlaspack::sources": "./src/index.mts",
18
- "types": [
19
- "./lib/index.d.mts",
20
- "./src/index.mts"
21
- ],
22
- "import": "./lib/index.mjs",
23
- "module-sync": "./lib/index.mjs",
24
- "require": "./lib/index.js",
25
- "default": "./lib/index.js"
26
- },
27
- "./*": "./*"
28
- },
12
+ "main": "./lib/index.js",
13
+ "source": "./src/index.ts",
14
+ "types": "./src/index.ts",
29
15
  "engines": {
30
16
  "node": ">= 16.0.0"
31
17
  },
32
- "scripts": {
33
- "build-tsc": "node ../../../scripts/build-tsc.mjs"
34
- },
35
18
  "dependencies": {
36
19
  "@babel/core": "^7.12.2",
37
20
  "@babel/helper-plugin-utils": "^7.12.2"
@@ -40,5 +23,6 @@
40
23
  "@types/babel__core": "^7.12.2",
41
24
  "@types/babel__helper-plugin-utils": "^7.10.3"
42
25
  },
43
- "gitHead": "8163359c1a8d316bfd4ca81f9f13f94bc5963fe9"
26
+ "type": "commonjs",
27
+ "gitHead": "63bde801db96c5cb339ea9360cbcd0172a26cbba"
44
28
  }
@@ -1,26 +1,20 @@
1
+ // @flow
2
+
1
3
  import * as babel from '@babel/core';
2
4
  import assert from 'assert';
3
- import path from 'path';
4
- import url from 'url';
5
-
6
- const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
7
5
 
8
- const plugin = path.normalize(path.join(__dirname, '..', 'src', 'index.mts'));
6
+ const plugin = require.resolve('../src/index.ts');
9
7
 
10
8
  describe('@atlaspack/babel-plugin-transform-contextual-imports', () => {
11
9
  it('should transform importCond to inline requires', () => {
12
10
  const input = `
13
11
  const Imported = importCond('CONDITION', 'IF_TRUE', 'IF_FALSE');
14
12
  `;
15
- const result = babel.transformSync(input, {
13
+ const {code: transformed} = babel.transformSync(input, {
16
14
  configFile: false,
17
15
  presets: [],
18
16
  plugins: [plugin],
19
17
  });
20
- if (!result) {
21
- throw new Error('Unable to produce result');
22
- }
23
- const {code: transformed} = result;
24
18
 
25
19
  assert.equal(
26
20
  transformed,
@@ -34,15 +28,11 @@ describe('@atlaspack/babel-plugin-transform-contextual-imports', () => {
34
28
 
35
29
  console.log(Imported, Imported.someProperty);
36
30
  `;
37
- const result = babel.transformSync(input, {
31
+ const {code: transformed} = babel.transformSync(input, {
38
32
  configFile: false,
39
33
  presets: [],
40
34
  plugins: [[plugin, {node: true}]],
41
35
  });
42
- if (!result) {
43
- throw new Error('Unable to produce result');
44
- }
45
- const {code: transformed} = result;
46
36
 
47
37
  assert.equal(
48
38
  transformed,
package/tsconfig.json CHANGED
@@ -1,4 +1,12 @@
1
1
  {
2
- "extends": "../../../tsconfig.json",
3
- "include": ["src"]
2
+ "compilerOptions": {
3
+ "target": "es2016",
4
+ "module": "commonjs",
5
+ "esModuleInterop": true,
6
+ "forceConsistentCasingInFileNames": true,
7
+ "strict": true,
8
+ "skipLibCheck": true
9
+ },
10
+ "include": ["src/*"],
11
+ "exclude": ["node_modules"]
4
12
  }
package/lib/index.d.mts DELETED
@@ -1,15 +0,0 @@
1
- import type { PluginObj, types as BabelTypes } from '@babel/core';
2
- interface Opts {
3
- /** Use node safe import cond syntax */
4
- node?: boolean;
5
- }
6
- interface State {
7
- /** Plugin options */
8
- opts: Opts;
9
- /** Set of identifier names that need to be mutated after import was transformed */
10
- conditionalImportIdentifiers?: Set<string>;
11
- /** Set of identifiers that have been visited in the exit pass, to avoid adding the load property multiple times */
12
- visitedIdentifiers?: Set<BabelTypes.Identifier>;
13
- }
14
- declare const _default: (api: object, options: Record<string, any> | null | undefined, dirname: string) => PluginObj<State>;
15
- export default _default;
package/lib/index.mjs DELETED
@@ -1,94 +0,0 @@
1
- import { declare } from '@babel/helper-plugin-utils';
2
- const isNode = (opts) => !!('node' in opts && opts.node);
3
- export default declare((api) => {
4
- const { types: t } = api;
5
- const isImportCondCallExpression = (node) => {
6
- if (node.type === 'CallExpression' &&
7
- node.callee.type === 'Identifier' &&
8
- node.callee.name === 'importCond') {
9
- if (node.arguments.length === 3 &&
10
- node.arguments.every((arg) => arg.type === 'StringLiteral')) {
11
- return true;
12
- }
13
- else {
14
- // Simple error for incorrect syntax (since it's documented with the type)
15
- throw new Error('importCond must have three string literal arguments');
16
- }
17
- }
18
- return false;
19
- };
20
- 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')));
21
- const buildNodeObject = (identifier, cond, ifTrue, ifFalse) => [
22
- // Create object containing imports
23
- t.variableDeclaration('const', [
24
- t.variableDeclarator(identifier, t.objectExpression([
25
- t.objectProperty(t.identifier('ifTrue'), t.memberExpression(t.callExpression(t.identifier('require'), [ifTrue]), t.identifier('default'))),
26
- t.objectProperty(t.identifier('ifFalse'), t.memberExpression(t.callExpression(t.identifier('require'), [ifFalse]), t.identifier('default'))),
27
- ])),
28
- ]),
29
- // Create lazy getter via the load property on the object.
30
- // This is node module resolution safe because each time the import is accessed, we re-evaluate the condition.
31
- t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('defineProperty')), [
32
- identifier,
33
- t.stringLiteral('load'),
34
- t.objectExpression([
35
- 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'))))),
36
- ]),
37
- ])),
38
- ];
39
- return {
40
- name: '@atlaspack/babel-plugin-transform-contextual-imports',
41
- visitor: {
42
- CallExpression: {
43
- enter(path, state) {
44
- const node = path.node;
45
- if (isImportCondCallExpression(node)) {
46
- const [cond, ifTrue, ifFalse] = node.arguments;
47
- if (!isNode(state.opts)) {
48
- // Replace the importCond call with a conditional require import, as a fallback for environments that don't support Atlaspack
49
- path.replaceWith(buildCondFunction(cond, ifTrue, ifFalse));
50
- }
51
- }
52
- },
53
- },
54
- VariableDeclaration: {
55
- enter(path, state) {
56
- if (isNode(state.opts)) {
57
- if (path.node.declarations.length === 1 &&
58
- path.node.declarations[0].type === 'VariableDeclarator' &&
59
- path.node.declarations[0].id.type === 'Identifier') {
60
- const importId = path.node.declarations[0].id;
61
- const call = path.node.declarations[0].init;
62
- // Mark identifier for object so we don't add the load property to it
63
- state.visitedIdentifiers?.add(importId);
64
- if (call && isImportCondCallExpression(call)) {
65
- const [cond, ifTrue, ifFalse] = call.arguments;
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));
68
- // Add identifier name to set so we can mutate all import usages in the exit pass
69
- state.conditionalImportIdentifiers?.add(importId.name);
70
- }
71
- }
72
- }
73
- },
74
- },
75
- Identifier: {
76
- exit(path, state) {
77
- const identifier = state.conditionalImportIdentifiers?.has(path.node.name);
78
- if (identifier && !state.visitedIdentifiers?.has(path.node)) {
79
- // Add load property to the import usage
80
- const newIdentifer = t.identifier(path.node.name);
81
- path.replaceWith(t.memberExpression(newIdentifer, t.identifier('load')));
82
- state.visitedIdentifiers?.add(newIdentifer);
83
- }
84
- },
85
- },
86
- Program: {
87
- enter(_, state) {
88
- state.conditionalImportIdentifiers = new Set();
89
- state.visitedIdentifiers = new Set();
90
- },
91
- },
92
- },
93
- };
94
- });
package/lib/package.json DELETED
@@ -1 +0,0 @@
1
- { "type": "commonjs" }
File without changes