babel-plugin-named-asset-import 1.0.0-next.47d2d941 → 1.0.0-next.91

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 +21 -0
  2. package/index.js +76 -43
  3. package/package.json +17 -5
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013-present, Facebook, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.js CHANGED
@@ -5,55 +5,88 @@ 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(path, { opts: { loaderMap } }) {
11
- const sourcePath = path.node.source.value;
12
- const ext = extname(sourcePath).substr(1);
13
-
14
- if (visited.has(path.node) || sourcePath.indexOf('!') !== -1) {
38
+ ExportNamedDeclaration(path, { opts: { loaderMap } }) {
39
+ if (!path.node.source) {
15
40
  return;
16
41
  }
17
42
 
18
- if (loaderMap[ext]) {
19
- path.replaceWithMultiple(
20
- path.node.specifiers.map(specifier => {
21
- if (t.isImportDefaultSpecifier(specifier)) {
22
- const newDefaultImport = t.importDeclaration(
23
- [
24
- t.importDefaultSpecifier(
25
- t.identifier(specifier.local.name)
26
- ),
27
- ],
28
- t.stringLiteral(sourcePath)
29
- );
30
-
31
- visited.add(newDefaultImport);
32
- return newDefaultImport;
33
- }
34
-
35
- const newImport = t.importDeclaration(
36
- [
37
- t.importSpecifier(
38
- t.identifier(specifier.local.name),
39
- t.identifier(specifier.imported.name)
40
- ),
41
- ],
42
- t.stringLiteral(
43
- loaderMap[ext][specifier.imported.name]
44
- ? loaderMap[ext][specifier.imported.name].replace(
45
- /\[path\]/,
46
- sourcePath
47
- )
48
- : sourcePath
49
- )
50
- );
51
-
52
- visited.add(newImport);
53
- return newImport;
54
- })
43
+ replaceMatchingSpecifiers(path, loaderMap, (specifier, sourcePath) => {
44
+ if (t.isExportDefaultSpecifier(specifier)) {
45
+ return t.exportDeclaration(
46
+ [t.exportDefaultSpecifier(t.identifier(specifier.local.name))],
47
+ t.stringLiteral(sourcePath)
48
+ );
49
+ }
50
+
51
+ return t.exportNamedDeclaration(
52
+ null,
53
+ [
54
+ t.exportSpecifier(
55
+ t.identifier(specifier.local.name),
56
+ t.identifier(specifier.exported.name)
57
+ ),
58
+ ],
59
+ t.stringLiteral(
60
+ generateNewSourcePath(loaderMap, specifier.local.name, sourcePath)
61
+ )
55
62
  );
56
- }
63
+ });
64
+ },
65
+ ImportDeclaration(path, { opts: { loaderMap } }) {
66
+ replaceMatchingSpecifiers(path, loaderMap, (specifier, sourcePath) => {
67
+ if (t.isImportDefaultSpecifier(specifier)) {
68
+ return t.importDeclaration(
69
+ [t.importDefaultSpecifier(t.identifier(specifier.local.name))],
70
+ t.stringLiteral(sourcePath)
71
+ );
72
+ }
73
+
74
+ return t.importDeclaration(
75
+ [
76
+ t.importSpecifier(
77
+ t.identifier(specifier.local.name),
78
+ t.identifier(specifier.imported.name)
79
+ ),
80
+ ],
81
+ t.stringLiteral(
82
+ generateNewSourcePath(
83
+ loaderMap,
84
+ specifier.imported.name,
85
+ sourcePath
86
+ )
87
+ )
88
+ );
89
+ });
57
90
  },
58
91
  },
59
92
  };
package/package.json CHANGED
@@ -1,17 +1,29 @@
1
1
  {
2
2
  "name": "babel-plugin-named-asset-import",
3
- "version": "1.0.0-next.47d2d941",
3
+ "version": "1.0.0-next.91+1465357b",
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": [
12
16
  "index.js"
13
17
  ],
14
18
  "peerDependencies": {
15
- "@babel/core": "7.0.0-beta.38"
16
- }
19
+ "@babel/core": "^7.1.0"
20
+ },
21
+ "devDependencies": {
22
+ "babel-plugin-tester": "^8.0.1",
23
+ "jest": "^27.1.0"
24
+ },
25
+ "scripts": {
26
+ "test": "jest"
27
+ },
28
+ "gitHead": "1465357b842eae16b2b52243d2e55db9a17f0425"
17
29
  }