babel-plugin-named-asset-import 0.2.3 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/index.js +79 -41
  2. package/package.json +11 -6
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,11 +1,15 @@
1
1
  {
2
2
  "name": "babel-plugin-named-asset-import",
3
- "version": "0.2.3",
3
+ "version": "0.3.2",
4
4
  "description": "Babel plugin for named asset imports in Create React App",
5
- "repository": "facebookincubator/create-react-app",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/facebook/create-react-app.git",
8
+ "directory": "packages/babel-plugin-named-asset-import"
9
+ },
6
10
  "license": "MIT",
7
11
  "bugs": {
8
- "url": "https://github.com/facebookincubator/create-react-app/issues"
12
+ "url": "https://github.com/facebook/create-react-app/issues"
9
13
  },
10
14
  "main": "index.js",
11
15
  "files": [
@@ -15,10 +19,11 @@
15
19
  "@babel/core": "^7.1.0"
16
20
  },
17
21
  "devDependencies": {
18
- "babel-plugin-tester": "^5.5.1",
19
- "jest": "^23.6.0"
22
+ "babel-plugin-tester": "^6.0.1",
23
+ "jest": "24.7.1"
20
24
  },
21
25
  "scripts": {
22
26
  "test": "jest"
23
- }
27
+ },
28
+ "gitHead": "265c1592dcf3853122dcdd50e1bfc1e43f697fbe"
24
29
  }