I’m trying to import three.js from CDN to WordPress but console returns: example.js:2 Uncaught Error: Cannot find module 'three'
. I’m also using Webpack.
example.js:
import * as THREE from 'three';
[…]
app.js: import "./example.js";
functions.php:
//wp enqueues
function example_scripts() {
wp_enqueue_script( 'three-js', 'https://cdn.skypack.dev/[email protected]', array(), null );
}
add_action( 'wp_enqueue_scripts', 'example_scripts' );
//three-js
function set_scripts_type_attribute( $tag, $handle, $src ) {
if ( 'three-js' === $handle ) {
$tag = '<script type="module" src="'. $src .'"></script>';
}
return $tag;
}
add_filter( 'script_loader_tag', 'set_scripts_type_attribute', 10, 3 );
part of webpack config:
module.exports = {
externals: {
THREE: 'three',
},
};
it appears in html : <script type="module" src="https://cdn.skypack.dev/[email protected]"></script>