babel-plugin-named-asset-import 0.2.3-next.c662dfb0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/LICENSE +0 -0
  2. package/index.js +79 -41
  3. package/package.json +8 -1
package/LICENSE CHANGED
File without changes
package/index.js CHANGED
@@ -5,60 +5,98 @@ const { extname } = require('path');
5
5
  function namedAssetImportPlugin({ types: t }) {
6
6
  const visited = new WeakSet();
7
7
 
8
+ function generateNewSourcePath(loaderMap, moduleName, sourcePath) {
9
+ const ext = extname(sourcePath).substr(1);
10
+ const extMap = loaderMap[ext];
11
+ return extMap[moduleName]
12
+ ? extMap[moduleName].replace(/\[path\]/, sourcePath)
13
+ : sourcePath;
14
+ }
15
+
16
+ function replaceMatchingSpecifiers(path, loaderMap, callback) {
17
+ const sourcePath = path.node.source.value;
18
+ const ext = extname(sourcePath).substr(1);
19
+
20
+ if (visited.has(path.node) || sourcePath.indexOf('!') !== -1) {
21
+ return;
22
+ }
23
+
24
+ if (loaderMap[ext]) {
25
+ path.replaceWithMultiple(
26
+ path.node.specifiers.map(specifier => {
27
+ const newSpecifier = callback(specifier, sourcePath);
28
+ visited.add(newSpecifier);
29
+
30
+ return newSpecifier;
31
+ })
32
+ );
33
+ }
34
+ }
35
+
8
36
  return {
9
37
  visitor: {
10
- ImportDeclaration(
38
+ ExportNamedDeclaration(
11
39
  path,
12
40
  {
13
41
  opts: { loaderMap },
14
42
  }
15
43
  ) {
16
- const sourcePath = path.node.source.value;
17
- const ext = extname(sourcePath).substr(1);
18
-
19
- if (visited.has(path.node) || sourcePath.indexOf('!') !== -1) {
44
+ if (!path.node.source) {
20
45
  return;
21
46
  }
22
47
 
23
- if (loaderMap[ext]) {
24
- path.replaceWithMultiple(
25
- path.node.specifiers.map(specifier => {
26
- if (t.isImportDefaultSpecifier(specifier)) {
27
- const newDefaultImport = t.importDeclaration(
28
- [
29
- t.importDefaultSpecifier(
30
- t.identifier(specifier.local.name)
31
- ),
32
- ],
33
- t.stringLiteral(sourcePath)
34
- );
35
-
36
- visited.add(newDefaultImport);
37
- return newDefaultImport;
38
- }
39
-
40
- const newImport = t.importDeclaration(
41
- [
42
- t.importSpecifier(
43
- t.identifier(specifier.local.name),
44
- t.identifier(specifier.imported.name)
45
- ),
46
- ],
47
- t.stringLiteral(
48
- loaderMap[ext][specifier.imported.name]
49
- ? loaderMap[ext][specifier.imported.name].replace(
50
- /\[path\]/,
51
- sourcePath
52
- )
53
- : sourcePath
54
- )
55
- );
48
+ replaceMatchingSpecifiers(path, loaderMap, (specifier, sourcePath) => {
49
+ if (t.isExportDefaultSpecifier(specifier)) {
50
+ return t.exportDeclaration(
51
+ [t.exportDefaultSpecifier(t.identifier(specifier.local.name))],
52
+ t.stringLiteral(sourcePath)
53
+ );
54
+ }
56
55
 
57
- visited.add(newImport);
58
- return newImport;
59
- })
56
+ return t.exportNamedDeclaration(
57
+ null,
58
+ [
59
+ t.exportSpecifier(
60
+ t.identifier(specifier.local.name),
61
+ t.identifier(specifier.exported.name)
62
+ ),
63
+ ],
64
+ t.stringLiteral(
65
+ generateNewSourcePath(loaderMap, specifier.local.name, sourcePath)
66
+ )
60
67
  );
68
+ });
69
+ },
70
+ ImportDeclaration(
71
+ path,
72
+ {
73
+ opts: { loaderMap },
61
74
  }
75
+ ) {
76
+ replaceMatchingSpecifiers(path, loaderMap, (specifier, sourcePath) => {
77
+ if (t.isImportDefaultSpecifier(specifier)) {
78
+ return t.importDeclaration(
79
+ [t.importDefaultSpecifier(t.identifier(specifier.local.name))],
80
+ t.stringLiteral(sourcePath)
81
+ );
82
+ }
83
+
84
+ return t.importDeclaration(
85
+ [
86
+ t.importSpecifier(
87
+ t.identifier(specifier.local.name),
88
+ t.identifier(specifier.imported.name)
89
+ ),
90
+ ],
91
+ t.stringLiteral(
92
+ generateNewSourcePath(
93
+ loaderMap,
94
+ specifier.imported.name,
95
+ sourcePath
96
+ )
97
+ )
98
+ );
99
+ });
62
100
  },
63
101
  },
64
102
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-named-asset-import",
3
- "version": "0.2.3-next.c662dfb0",
3
+ "version": "0.3.1",
4
4
  "description": "Babel plugin for named asset imports in Create React App",
5
5
  "repository": "facebookincubator/create-react-app",
6
6
  "license": "MIT",
@@ -13,5 +13,12 @@
13
13
  ],
14
14
  "peerDependencies": {
15
15
  "@babel/core": "^7.1.0"
16
+ },
17
+ "devDependencies": {
18
+ "babel-plugin-tester": "^5.5.1",
19
+ "jest": "^23.6.0"
20
+ },
21
+ "scripts": {
22
+ "test": "jest"
16
23
  }
17
24
  }