@dev-to/react-plugin 0.2.0 → 0.3.0
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/dist/debugTools.d.ts.map +1 -1
- package/dist/index.js +103 -24
- package/dist/loaderUmdWrapper.d.ts +1 -0
- package/dist/loaderUmdWrapper.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/debugTools.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"debugTools.d.ts","sourceRoot":"","sources":["../src/debugTools.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"debugTools.d.ts","sourceRoot":"","sources":["../src/debugTools.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,MAAM,CAAA;AAEzC,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAA;AAG/H,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,cAAc,CAAA;IACxB,KAAK,EAAE,WAAW,CAAA;IAClB,KAAK,EAAE,iBAAiB,CAAA;IACxB,cAAc,EAAE,0BAA0B,CAAA;IAC1C,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,CAAC,EAAE,OAAO,CAAA;CACf;AAqFD,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,aAAa,EAAE,GAAG,EAAE,iBAAiB,EAAE,KAAK,EAAE,iBAAiB,QAwUxG"}
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import node_path from "node:path";
|
|
|
4
4
|
import picocolors from "picocolors";
|
|
5
5
|
import { mergeConfig } from "vite";
|
|
6
6
|
import { exec } from "node:child_process";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
7
8
|
import node_os from "node:os";
|
|
8
9
|
import typescript from "typescript";
|
|
9
10
|
const PLUGIN_NAME = DEV_TO_REACT_NAMESPACE;
|
|
@@ -814,7 +815,7 @@ function generateLibBuildNextConfig(params) {
|
|
|
814
815
|
};
|
|
815
816
|
}
|
|
816
817
|
function createLoaderUmdWrapper(options) {
|
|
817
|
-
const { componentName, origin, contractEndpoint = constants_STABLE_CONTRACT_PATH } = options;
|
|
818
|
+
const { componentName, origin, contractEndpoint = constants_STABLE_CONTRACT_PATH, reactLoaderUrl = 'https://cdn.jsdelivr.net/npm/@dev-to/react-loader@latest/dist/index.umd.js' } = options;
|
|
818
819
|
const globalName = toSafeUmdName(componentName);
|
|
819
820
|
const code = `/**
|
|
820
821
|
* UMD Loader Wrapper for component: ${componentName}
|
|
@@ -834,14 +835,20 @@ function createLoaderUmdWrapper(options) {
|
|
|
834
835
|
* 3. Load this wrapper:
|
|
835
836
|
* <script src="${origin}/__dev_to_react__/loader/${componentName}.js"></script>
|
|
836
837
|
*
|
|
837
|
-
* 4. Render the component:
|
|
838
|
+
* 4. Render the component (returns a Promise):
|
|
838
839
|
* <div id="app"></div>
|
|
839
840
|
* <script>
|
|
840
841
|
* window.${globalName}.render(
|
|
841
842
|
* document.getElementById('app'),
|
|
842
|
-
* {
|
|
843
|
-
* )
|
|
843
|
+
* { prop1: 'value1', prop2: 'value2' }
|
|
844
|
+
* ).then(function(root) {
|
|
845
|
+
* console.log('Component rendered successfully');
|
|
846
|
+
* }).catch(function(error) {
|
|
847
|
+
* console.error('Failed to render component:', error);
|
|
848
|
+
* });
|
|
844
849
|
* </script>
|
|
850
|
+
*
|
|
851
|
+
* Note: ReactLoader will be automatically loaded from CDN if not already available.
|
|
845
852
|
*/
|
|
846
853
|
(function (root, factory) {
|
|
847
854
|
if (typeof exports === 'object' && typeof module !== 'undefined') {
|
|
@@ -858,15 +865,58 @@ function createLoaderUmdWrapper(options) {
|
|
|
858
865
|
})(this, function (exports, React, ReactDOM, ReactLoaderModule) {
|
|
859
866
|
'use strict';
|
|
860
867
|
|
|
861
|
-
|
|
862
|
-
var
|
|
868
|
+
var ReactLoader = null;
|
|
869
|
+
var loadingPromise = null;
|
|
870
|
+
|
|
871
|
+
// Helper function to load a script dynamically
|
|
872
|
+
function loadScript(src) {
|
|
873
|
+
return new Promise(function(resolve, reject) {
|
|
874
|
+
var script = document.createElement('script');
|
|
875
|
+
script.src = src;
|
|
876
|
+
script.onload = resolve;
|
|
877
|
+
script.onerror = reject;
|
|
878
|
+
document.head.appendChild(script);
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// Helper function to ensure ReactLoader is loaded
|
|
883
|
+
function ensureReactLoaderLoaded() {
|
|
884
|
+
if (ReactLoader) {
|
|
885
|
+
return Promise.resolve();
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
if (!loadingPromise) {
|
|
889
|
+
loadingPromise = (function() {
|
|
890
|
+
// First, try to get ReactLoader from the global scope
|
|
891
|
+
if (typeof window !== 'undefined' && window.DevToReactLoader && window.DevToReactLoader.ReactLoader) {
|
|
892
|
+
ReactLoader = window.DevToReactLoader.ReactLoader;
|
|
893
|
+
return Promise.resolve();
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// If not available, load it from URL
|
|
897
|
+
console.log('${constants_PLUGIN_LOG_PREFIX} Loading @dev-to/react-loader...');
|
|
898
|
+
return loadScript(${JSON.stringify(reactLoaderUrl)})
|
|
899
|
+
.then(function() {
|
|
900
|
+
if (typeof window !== 'undefined' && window.DevToReactLoader && window.DevToReactLoader.ReactLoader) {
|
|
901
|
+
ReactLoader = window.DevToReactLoader.ReactLoader;
|
|
902
|
+
console.log('${constants_PLUGIN_LOG_PREFIX} ReactLoader loaded successfully');
|
|
903
|
+
} else {
|
|
904
|
+
throw new Error('${constants_PLUGIN_LOG_PREFIX} ReactLoader not found after loading');
|
|
905
|
+
}
|
|
906
|
+
})
|
|
907
|
+
.catch(function(error) {
|
|
908
|
+
console.error('${constants_PLUGIN_LOG_PREFIX} Failed to load ReactLoader:', error);
|
|
909
|
+
throw error;
|
|
910
|
+
});
|
|
911
|
+
})();
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
return loadingPromise;
|
|
915
|
+
}
|
|
863
916
|
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
'Please load @dev-to/react-loader before this script: ' +
|
|
868
|
-
'<script src="https://cdn.jsdelivr.net/npm/@dev-to/react-loader@latest/dist/index.umd.js"></script>'
|
|
869
|
-
);
|
|
917
|
+
// Try to get ReactLoader from the module if available
|
|
918
|
+
if (ReactLoaderModule && ReactLoaderModule.ReactLoader) {
|
|
919
|
+
ReactLoader = ReactLoaderModule.ReactLoader;
|
|
870
920
|
}
|
|
871
921
|
|
|
872
922
|
// Component configuration
|
|
@@ -892,19 +942,26 @@ function createLoaderUmdWrapper(options) {
|
|
|
892
942
|
throw new Error('${constants_PLUGIN_LOG_PREFIX} ReactDOM is not loaded');
|
|
893
943
|
}
|
|
894
944
|
|
|
895
|
-
//
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
componentProps: componentProps || {}
|
|
901
|
-
};
|
|
945
|
+
// Ensure ReactLoader is available before rendering
|
|
946
|
+
return ensureReactLoaderLoaded().then(function() {
|
|
947
|
+
if (!ReactLoader) {
|
|
948
|
+
throw new Error('${constants_PLUGIN_LOG_PREFIX} ReactLoader initialization failed');
|
|
949
|
+
}
|
|
902
950
|
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
951
|
+
// Create ReactLoader component props
|
|
952
|
+
var loaderProps = {
|
|
953
|
+
origin: config.origin,
|
|
954
|
+
name: config.name,
|
|
955
|
+
contractEndpoint: config.contractEndpoint,
|
|
956
|
+
componentProps: componentProps || {}
|
|
957
|
+
};
|
|
906
958
|
|
|
907
|
-
|
|
959
|
+
// Render using ReactLoader
|
|
960
|
+
var root = ReactDOM.createRoot(targetElement);
|
|
961
|
+
root.render(React.createElement(ReactLoader, loaderProps));
|
|
962
|
+
|
|
963
|
+
return root;
|
|
964
|
+
});
|
|
908
965
|
}
|
|
909
966
|
|
|
910
967
|
// Export the API
|
|
@@ -1113,6 +1170,27 @@ function installDebugTools(server, ctx, state) {
|
|
|
1113
1170
|
}, null, 2));
|
|
1114
1171
|
return;
|
|
1115
1172
|
}
|
|
1173
|
+
if (pathname === `${STABLE_BASE_PATH}/react-loader.js`) try {
|
|
1174
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
1175
|
+
const __dirname = node_path.dirname(__filename);
|
|
1176
|
+
const reactLoaderUmdPath = node_path.resolve(__dirname, '../../react-loader/dist/index.umd.js');
|
|
1177
|
+
if (node_fs.existsSync(reactLoaderUmdPath)) {
|
|
1178
|
+
const umdCode = node_fs.readFileSync(reactLoaderUmdPath, 'utf-8');
|
|
1179
|
+
res.statusCode = 200;
|
|
1180
|
+
res.setHeader('Content-Type', "application/javascript; charset=utf-8");
|
|
1181
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
1182
|
+
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
|
|
1183
|
+
res.end(umdCode);
|
|
1184
|
+
return;
|
|
1185
|
+
}
|
|
1186
|
+
res.statusCode = 404;
|
|
1187
|
+
res.end(`react-loader UMD not found at ${reactLoaderUmdPath}. Run 'pnpm build' in react-loader package.`);
|
|
1188
|
+
return;
|
|
1189
|
+
} catch (error) {
|
|
1190
|
+
res.statusCode = 500;
|
|
1191
|
+
res.end(`Error loading react-loader UMD: ${error}`);
|
|
1192
|
+
return;
|
|
1193
|
+
}
|
|
1116
1194
|
if (pathname.startsWith(STABLE_LOADER_BASE_PATH)) {
|
|
1117
1195
|
const loaderPathPattern = new RegExp(`^${STABLE_LOADER_BASE_PATH}/([^/]+)\\.js$`);
|
|
1118
1196
|
const match = pathname.match(loaderPathPattern);
|
|
@@ -1127,7 +1205,8 @@ function installDebugTools(server, ctx, state) {
|
|
|
1127
1205
|
const code = createLoaderUmdWrapper({
|
|
1128
1206
|
componentName,
|
|
1129
1207
|
origin,
|
|
1130
|
-
contractEndpoint: constants_STABLE_CONTRACT_PATH
|
|
1208
|
+
contractEndpoint: constants_STABLE_CONTRACT_PATH,
|
|
1209
|
+
reactLoaderUrl: `${origin}${STABLE_BASE_PATH}/react-loader.js`
|
|
1131
1210
|
});
|
|
1132
1211
|
res.statusCode = 200;
|
|
1133
1212
|
res.setHeader('Content-Type', "application/javascript; charset=utf-8");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loaderUmdWrapper.d.ts","sourceRoot":"","sources":["../src/loaderUmdWrapper.ts"],"names":[],"mappings":"AAGA,UAAU,6BAA6B;IACrC,aAAa,EAAE,MAAM,CAAA;IACrB,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,CAAC,EAAE,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"loaderUmdWrapper.d.ts","sourceRoot":"","sources":["../src/loaderUmdWrapper.ts"],"names":[],"mappings":"AAGA,UAAU,6BAA6B;IACrC,aAAa,EAAE,MAAM,CAAA;IACrB,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,6BAA6B,GAAG,MAAM,CAwKrF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dev-to/react-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"picocolors": "^1.1.0",
|
|
33
33
|
"typescript": "^5.4.5",
|
|
34
|
-
"@dev-to/react-shared": "0.1.
|
|
34
|
+
"@dev-to/react-shared": "0.1.2"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "rslib build",
|