react-native-builder-bob 0.25.0-next.0 → 0.25.0-next.2
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/babel.js +17 -7
- package/lib/babel.js.map +1 -1
- package/package.json +2 -2
package/lib/babel.js
CHANGED
|
@@ -19,6 +19,14 @@ const isDirectory = filename => {
|
|
|
19
19
|
})?.isDirectory() ?? false;
|
|
20
20
|
return exists;
|
|
21
21
|
};
|
|
22
|
+
const isModule = (filename, ext) => {
|
|
23
|
+
const exts = ['.js', '.ts', '.jsx', '.tsx', ext];
|
|
24
|
+
|
|
25
|
+
// Metro won't resolve these extensions if explicit extension is provided
|
|
26
|
+
// So we can't add extension to these files
|
|
27
|
+
const additional = ['native', 'android', 'ios', 'web'];
|
|
28
|
+
return exts.some(ext => isFile(`${filename}.${ext}`) && additional.every(add => !isFile(`${filename}.${add}.${ext}`)));
|
|
29
|
+
};
|
|
22
30
|
const isTypeImport = node => 'importKind' in node && node.importKind === 'type' || 'exportKind' in node && node.exportKind === 'type';
|
|
23
31
|
const assertFilename = filename => {
|
|
24
32
|
if (filename == null) {
|
|
@@ -66,18 +74,20 @@ function _default(api, {
|
|
|
66
74
|
// Skip folder imports
|
|
67
75
|
const filename = _path.default.resolve(_path.default.dirname(state.filename), node.source.value);
|
|
68
76
|
|
|
69
|
-
//
|
|
70
|
-
if (isFile(
|
|
71
|
-
node.source.value
|
|
77
|
+
// Replace .ts extension with .js if file with extension is explicitly imported
|
|
78
|
+
if (isFile(filename)) {
|
|
79
|
+
node.source.value = node.source.value.replace(/\.tsx?$/, `.${extension}`);
|
|
72
80
|
return;
|
|
73
81
|
}
|
|
74
82
|
|
|
75
|
-
//
|
|
76
|
-
if (
|
|
77
|
-
node.source.value
|
|
83
|
+
// Add extension if .ts file or file with extension exists
|
|
84
|
+
if (isModule(filename, extension)) {
|
|
85
|
+
node.source.value += `.${extension}`;
|
|
78
86
|
return;
|
|
79
87
|
}
|
|
80
|
-
|
|
88
|
+
|
|
89
|
+
// Expand folder imports to index and add extension
|
|
90
|
+
if (isDirectory(filename) && isModule(_path.default.join(filename, 'index'), extension)) {
|
|
81
91
|
node.source.value = node.source.value.replace(/\/?$/, `/index.${extension}`);
|
|
82
92
|
return;
|
|
83
93
|
}
|
package/lib/babel.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"babel.js","names":["_fs","_interopRequireDefault","require","_path","obj","__esModule","default","isFile","filename","exists","fs","lstatSync","throwIfNoEntry","isDirectory","isTypeImport","node","importKind","exportKind","assertFilename","Error","_default","api","alias","extension","assertVersion","aliasImports","state","source","value","root","cwd","key","Object","entries","startsWith","resolved","path","relative","dirname","resolve","replace","addExtension","join","name","visitor","ImportDeclaration","ExportNamedDeclaration","ExportAllDeclaration"],"sources":["../src/babel.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport type { ConfigAPI, NodePath, PluginObj, PluginPass } from '@babel/core';\nimport type {\n ImportDeclaration,\n ExportAllDeclaration,\n ExportNamedDeclaration,\n} from '@babel/types';\n\ntype Options = {\n alias?: Record<string, string>;\n extension?: 'cjs' | 'mjs';\n};\n\nconst isFile = (filename: string): boolean => {\n const exists =\n fs.lstatSync(filename, { throwIfNoEntry: false })?.isFile() ?? false;\n\n return exists;\n};\n\nconst isDirectory = (filename: string): boolean => {\n const exists =\n fs.lstatSync(filename, { throwIfNoEntry: false })?.isDirectory() ?? false;\n\n return exists;\n};\n\nconst isTypeImport = (\n node: ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration\n) =>\n ('importKind' in node && node.importKind === 'type') ||\n ('exportKind' in node && node.exportKind === 'type');\n\nconst assertFilename: (\n filename: string | null | undefined\n) => asserts filename is string = (filename) => {\n if (filename == null) {\n throw new Error(\"Couldn't find a filename for the current file.\");\n }\n};\n\nexport default function (\n api: ConfigAPI,\n { alias, extension }: Options\n): PluginObj {\n api.assertVersion(7);\n\n function aliasImports(\n {\n node,\n }: NodePath<\n ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration\n >,\n state: PluginPass\n ) {\n if (\n alias == null ||\n // Skip type imports as they'll be removed\n isTypeImport(node) ||\n // Skip imports without a source\n !node.source?.value\n ) {\n return;\n }\n\n assertFilename(state.filename);\n\n const root = state.cwd;\n const source = node.source.value;\n\n for (const [key, value] of Object.entries(alias)) {\n if (source === key || source.startsWith(`${key}/`)) {\n const resolved = value.startsWith('.')\n ? path.relative(\n path.dirname(state.filename),\n path.resolve(root, value)\n )\n : value;\n\n node.source.value = source.replace(key, resolved);\n return;\n }\n }\n }\n\n function addExtension(\n {\n node,\n }: NodePath<\n ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration\n >,\n state: PluginPass\n ) {\n if (\n extension == null ||\n // Skip type imports as they'll be removed\n isTypeImport(node) ||\n // Skip non-relative imports\n !node.source?.value.startsWith('.')\n ) {\n return;\n }\n\n assertFilename(state.filename);\n\n // Skip folder imports\n const filename = path.resolve(\n path.dirname(state.filename),\n node.source.value\n );\n\n //
|
|
1
|
+
{"version":3,"file":"babel.js","names":["_fs","_interopRequireDefault","require","_path","obj","__esModule","default","isFile","filename","exists","fs","lstatSync","throwIfNoEntry","isDirectory","isModule","ext","exts","additional","some","every","add","isTypeImport","node","importKind","exportKind","assertFilename","Error","_default","api","alias","extension","assertVersion","aliasImports","state","source","value","root","cwd","key","Object","entries","startsWith","resolved","path","relative","dirname","resolve","replace","addExtension","join","name","visitor","ImportDeclaration","ExportNamedDeclaration","ExportAllDeclaration"],"sources":["../src/babel.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport type { ConfigAPI, NodePath, PluginObj, PluginPass } from '@babel/core';\nimport type {\n ImportDeclaration,\n ExportAllDeclaration,\n ExportNamedDeclaration,\n} from '@babel/types';\n\ntype Options = {\n alias?: Record<string, string>;\n extension?: 'cjs' | 'mjs';\n};\n\nconst isFile = (filename: string): boolean => {\n const exists =\n fs.lstatSync(filename, { throwIfNoEntry: false })?.isFile() ?? false;\n\n return exists;\n};\n\nconst isDirectory = (filename: string): boolean => {\n const exists =\n fs.lstatSync(filename, { throwIfNoEntry: false })?.isDirectory() ?? false;\n\n return exists;\n};\n\nconst isModule = (filename: string, ext: string): boolean => {\n const exts = ['.js', '.ts', '.jsx', '.tsx', ext];\n\n // Metro won't resolve these extensions if explicit extension is provided\n // So we can't add extension to these files\n const additional = ['native', 'android', 'ios', 'web'];\n\n return exts.some(\n (ext) =>\n isFile(`${filename}.${ext}`) &&\n additional.every((add) => !isFile(`${filename}.${add}.${ext}`))\n );\n};\n\nconst isTypeImport = (\n node: ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration\n) =>\n ('importKind' in node && node.importKind === 'type') ||\n ('exportKind' in node && node.exportKind === 'type');\n\nconst assertFilename: (\n filename: string | null | undefined\n) => asserts filename is string = (filename) => {\n if (filename == null) {\n throw new Error(\"Couldn't find a filename for the current file.\");\n }\n};\n\nexport default function (\n api: ConfigAPI,\n { alias, extension }: Options\n): PluginObj {\n api.assertVersion(7);\n\n function aliasImports(\n {\n node,\n }: NodePath<\n ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration\n >,\n state: PluginPass\n ) {\n if (\n alias == null ||\n // Skip type imports as they'll be removed\n isTypeImport(node) ||\n // Skip imports without a source\n !node.source?.value\n ) {\n return;\n }\n\n assertFilename(state.filename);\n\n const root = state.cwd;\n const source = node.source.value;\n\n for (const [key, value] of Object.entries(alias)) {\n if (source === key || source.startsWith(`${key}/`)) {\n const resolved = value.startsWith('.')\n ? path.relative(\n path.dirname(state.filename),\n path.resolve(root, value)\n )\n : value;\n\n node.source.value = source.replace(key, resolved);\n return;\n }\n }\n }\n\n function addExtension(\n {\n node,\n }: NodePath<\n ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration\n >,\n state: PluginPass\n ) {\n if (\n extension == null ||\n // Skip type imports as they'll be removed\n isTypeImport(node) ||\n // Skip non-relative imports\n !node.source?.value.startsWith('.')\n ) {\n return;\n }\n\n assertFilename(state.filename);\n\n // Skip folder imports\n const filename = path.resolve(\n path.dirname(state.filename),\n node.source.value\n );\n\n // Replace .ts extension with .js if file with extension is explicitly imported\n if (isFile(filename)) {\n node.source.value = node.source.value.replace(/\\.tsx?$/, `.${extension}`);\n return;\n }\n\n // Add extension if .ts file or file with extension exists\n if (isModule(filename, extension)) {\n node.source.value += `.${extension}`;\n return;\n }\n\n // Expand folder imports to index and add extension\n if (\n isDirectory(filename) &&\n isModule(path.join(filename, 'index'), extension)\n ) {\n node.source.value = node.source.value.replace(\n /\\/?$/,\n `/index.${extension}`\n );\n return;\n }\n }\n\n return {\n name: '@builder-bob/babel-plugin',\n visitor: {\n ImportDeclaration(path, state) {\n aliasImports(path, state);\n addExtension(path, state);\n },\n ExportNamedDeclaration(path, state) {\n aliasImports(path, state);\n addExtension(path, state);\n },\n ExportAllDeclaration(path, state) {\n aliasImports(path, state);\n addExtension(path, state);\n },\n },\n };\n}\n"],"mappings":";;;;;;AAAA,IAAAA,GAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,KAAA,GAAAF,sBAAA,CAAAC,OAAA;AAAwB,SAAAD,uBAAAG,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAaxB,MAAMG,MAAM,GAAIC,QAAgB,IAAc;EAC5C,MAAMC,MAAM,GACVC,WAAE,CAACC,SAAS,CAACH,QAAQ,EAAE;IAAEI,cAAc,EAAE;EAAM,CAAC,CAAC,EAAEL,MAAM,CAAC,CAAC,IAAI,KAAK;EAEtE,OAAOE,MAAM;AACf,CAAC;AAED,MAAMI,WAAW,GAAIL,QAAgB,IAAc;EACjD,MAAMC,MAAM,GACVC,WAAE,CAACC,SAAS,CAACH,QAAQ,EAAE;IAAEI,cAAc,EAAE;EAAM,CAAC,CAAC,EAAEC,WAAW,CAAC,CAAC,IAAI,KAAK;EAE3E,OAAOJ,MAAM;AACf,CAAC;AAED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ACjCR,IAAI,CAACY,MAAM,CAACC,KAAK,IAAK,IAAGL,SAAU,EAAC;MACpC;IACF;;IAEA;IACA,IACEjB,WAAW,CAACL,QAAQ,CAAC,IACrBM,QAAQ,CAAC6B,aAAI,CAACM,IAAI,CAACzC,QAAQ,EAAE,OAAO,CAAC,EAAEsB,SAAS,CAAC,EACjD;MACAR,IAAI,CAACY,MAAM,CAACC,KAAK,GAAGb,IAAI,CAACY,MAAM,CAACC,KAAK,CAACY,OAAO,CAC3C,MAAM,EACL,UAASjB,SAAU,EACtB,CAAC;MACD;IACF;EACF;EAEA,OAAO;IACLoB,IAAI,EAAE,2BAA2B;IACjCC,OAAO,EAAE;MACPC,iBAAiBA,CAACT,IAAI,EAAEV,KAAK,EAAE;QAC7BD,YAAY,CAACW,IAAI,EAAEV,KAAK,CAAC;QACzBe,YAAY,CAACL,IAAI,EAAEV,KAAK,CAAC;MAC3B,CAAC;MACDoB,sBAAsBA,CAACV,IAAI,EAAEV,KAAK,EAAE;QAClCD,YAAY,CAACW,IAAI,EAAEV,KAAK,CAAC;QACzBe,YAAY,CAACL,IAAI,EAAEV,KAAK,CAAC;MAC3B,CAAC;MACDqB,oBAAoBA,CAACX,IAAI,EAAEV,KAAK,EAAE;QAChCD,YAAY,CAACW,IAAI,EAAEV,KAAK,CAAC;QACzBe,YAAY,CAACL,IAAI,EAAEV,KAAK,CAAC;MAC3B;IACF;EACF,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-builder-bob",
|
|
3
|
-
"version": "0.25.0-next.
|
|
3
|
+
"version": "0.25.0-next.2",
|
|
4
4
|
"description": "CLI to build JavaScript files for React Native libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-native",
|
|
@@ -80,5 +80,5 @@
|
|
|
80
80
|
"concurrently": "^7.2.2",
|
|
81
81
|
"jest": "^29.7.0"
|
|
82
82
|
},
|
|
83
|
-
"gitHead": "
|
|
83
|
+
"gitHead": "b38cae977bfb1608e45505e37b8165cf3175e1f1"
|
|
84
84
|
}
|