babel-plugin-reactylon 1.0.8 → 1.1.1

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/README.md CHANGED
@@ -83,7 +83,7 @@ export default defineConfig({
83
83
  ```
84
84
 
85
85
  ### Additional configurations
86
- Additionally you can manually define extra side effects (see [Babylon.js ES6 support FAQ](https://doc.babylonjs.com/setup/frameworkPackages/es6Support/#faq)) or specify a custom `node_modules` path (useful if you are working with a monorepo setup).
86
+ Additionally you can manually define extra side effects (see [Babylon.js ES6 support FAQ](https://doc.babylonjs.com/setup/frameworkPackages/es6Support/#faq)).
87
87
 
88
88
  ```js
89
89
  ['babel-plugin-reactylon', {
@@ -91,8 +91,6 @@ Additionally you can manually define extra side effects (see [Babylon.js ES6 sup
91
91
  sideEffects: [
92
92
  '@babylonjs/core/Engines/Extensions/engine.query.js'
93
93
  ]
94
- // node modules path (optional)
95
- nodeModulesPath: '../../node_modules'
96
94
  }]
97
95
  ```
98
96
 
@@ -1,5 +1,6 @@
1
1
  import path from 'path';
2
2
  import fs from 'fs';
3
+ import { createRequire } from 'module';
3
4
  class ParserUtils {
4
5
  /**
5
6
  * It extracts exports and side effects from a file
@@ -60,16 +61,19 @@ class ParserUtils {
60
61
  * @param dir - directory to scan
61
62
  * @param exportsMap - exports map to populate
62
63
  * @param sideEffectsMap - side effects map to populate
64
+ * @param pkgRoot - package root (real path or symlinked path if the consumer is using npm/yarn link)
65
+ * @param pkgName - package name (es. "@babylonjs/core")
63
66
  */
64
- static scanDirectory(dir, exportsMap, sideEffectsMap) {
67
+ static scanDirectory(dir, exportsMap, sideEffectsMap, pkgRoot, pkgName) {
65
68
  const files = fs.readdirSync(dir);
66
69
  files.forEach(file => {
67
70
  const fullPath = path.join(dir, file);
68
71
  if (fs.statSync(fullPath).isDirectory()) {
69
- this.scanDirectory(fullPath, exportsMap, sideEffectsMap);
72
+ this.scanDirectory(fullPath, exportsMap, sideEffectsMap, pkgRoot, pkgName);
70
73
  }
71
74
  else if (file.endsWith('.js') || file.endsWith('.mjs') || file.endsWith('.ts')) {
72
- const importPath = (fullPath.replace(/\.(js|mjs|ts)$/, '')).replace(/\\/g, '/').replace(/^.*?node_modules\//, '');
75
+ const rel = path.relative(pkgRoot, fullPath).replace(/\.(js|mjs|ts)$/, '');
76
+ const importPath = path.posix.join(pkgName, rel.split(path.sep).join('/'));
73
77
  const { exports, sideEffects } = this.getExportsAndSideEffects(fullPath);
74
78
  exports.forEach(exp => {
75
79
  exportsMap[exp] = importPath;
@@ -85,14 +89,20 @@ class ParserUtils {
85
89
  * @param moduleName - module name to analyze
86
90
  * @returns an object containing the discovered exports and side effects maps
87
91
  */
88
- static generateExportsAndSideEffects(moduleName, nodeModulesPath) {
89
- const modulePath = path.join(nodeModulesPath, moduleName);
90
- if (!fs.existsSync(modulePath)) {
91
- console.error(`The module "${moduleName}" doesn't exist.`);
92
+ static generateExportsAndSideEffects(moduleName) {
93
+ const requireFromHere = createRequire(import.meta.url);
94
+ let pkgJsonPath;
95
+ try {
96
+ pkgJsonPath = requireFromHere.resolve(`${moduleName}/package.json`);
92
97
  }
98
+ catch (_a) {
99
+ console.error(`The module "${moduleName}" doesn't exist or cannot be resolved.`);
100
+ return { exports: {}, sideEffects: {} };
101
+ }
102
+ const modulePath = fs.realpathSync(path.dirname(pkgJsonPath));
93
103
  const exportsMap = {};
94
104
  const sideEffectsMap = {};
95
- this.scanDirectory(modulePath, exportsMap, sideEffectsMap);
105
+ this.scanDirectory(modulePath, exportsMap, sideEffectsMap, modulePath, moduleName);
96
106
  return {
97
107
  exports: exportsMap,
98
108
  sideEffects: sideEffectsMap
package/build/index.js CHANGED
@@ -23,11 +23,10 @@ export default declare((api) => {
23
23
  var _a;
24
24
  if (!initialized) {
25
25
  const options = this.opts;
26
- const nodeModulesPath = options.nodeModulesPath || 'node_modules';
27
- const coreExportsAndSideEffects = ParserUtils.generateExportsAndSideEffects('@babylonjs/core', nodeModulesPath);
26
+ const coreExportsAndSideEffects = ParserUtils.generateExportsAndSideEffects('@babylonjs/core');
28
27
  coreExportsMap = coreExportsAndSideEffects.exports;
29
28
  coreSideEffectsMap = coreExportsAndSideEffects.sideEffects;
30
- const guiExportsAndSideEffects = ParserUtils.generateExportsAndSideEffects('@babylonjs/gui', nodeModulesPath);
29
+ const guiExportsAndSideEffects = ParserUtils.generateExportsAndSideEffects('@babylonjs/gui');
31
30
  guiExportsMap = guiExportsAndSideEffects.exports;
32
31
  //guiSideEffectsMap = guiExportsAndSideEffects.sideEffects;
33
32
  // Add explicit side effects (additional side effects declared by user)
@@ -167,7 +166,7 @@ export default declare((api) => {
167
166
  const property = callee.property;
168
167
  if (t.isIdentifier(property)) {
169
168
  const sideEffectPath = coreSideEffectsMap[property.name];
170
- // exclude assets folder where there are .wasm and relative .js files
169
+ // exclude assets folder containing .wasm and relative .js files
171
170
  if (sideEffectPath && !sideEffectPath.startsWith('@babylonjs/core/assets/')) {
172
171
  sideEffects.push(t.importDeclaration([], t.stringLiteral(sideEffectPath)));
173
172
  // callPath.stop();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-reactylon",
3
- "version": "1.0.8",
3
+ "version": "1.1.1",
4
4
  "description": "Babel plugin to enable tree-shaking of Babylon.js classes within a Reactylon application.",
5
5
  "main": "build/index.js",
6
6
  "type": "module",