te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Can I use ES Modules library from CDN? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I use ES Modules library from CDN? - Stack Overflow

programmeradmin3浏览0评论

I want to use this library via CDN.

My js code is here.

import { LitElement, html } from "/[email protected]/lit-element.js";

class MyElement extends LitElement {
    render(){
        return html`ABCD`
    }
}

customElements.define("my-element", MyElement)

I get following error.

Uncaught TypeError: Failed to resolve module specifier "lit-html". Relative references must start with either "/", "./", or "../".

Do I have to build using npm ?


Update

Following code works.

import { LitElement, html } from ".js?module"

class MyElement extends LitElement {
    render(){
        return html`ABCD`
    }
}

customElements.define("my-element", MyElement)

I want to use this library via CDN.
https://www.jsdelivr./package/npm/lit-element

My js code is here.

import { LitElement, html } from "https://cdn.jsdelivr/npm/[email protected]/lit-element.js";

class MyElement extends LitElement {
    render(){
        return html`ABCD`
    }
}

customElements.define("my-element", MyElement)

I get following error.

Uncaught TypeError: Failed to resolve module specifier "lit-html". Relative references must start with either "/", "./", or "../".

Do I have to build using npm ?


Update

Following code works.

import { LitElement, html } from "https://unpkg./lit-element/lit-element.js?module"

class MyElement extends LitElement {
    render(){
        return html`ABCD`
    }
}

customElements.define("my-element", MyElement)
Share Improve this question edited Jun 8, 2019 at 14:05 blz asked Jun 8, 2019 at 11:49 blzblz 9377 silver badges24 bronze badges 2
  • 2 I would have thought that adding ?module to the URL would fix it (as here codepen.io/johnthad/pen/vbBojK?editors=1001 with unpkg.) but I see nothing. Might be a CDN issue. – Thad Commented Jun 8, 2019 at 13:25
  • 1 Thank you. It works fine with unpkg.. – blz Commented Jun 8, 2019 at 13:57
Add a ment  | 

2 Answers 2

Reset to default 9

Inside LitHtml and LitElement modules use bare module specifiers internally to import dependencies, and these aren't supported by JS modules yet. LitElement has import { TemplateResult } from 'lit-html', but JS needs that 'lit-html' replaced with whatever path to the actual file (not the abstract name of the package).

If you use the npm packages (which cdn.jsdelivr is serving up here) you have to use a build step (WebPack, Rollup, etc) to resolve all those import statements to either the file paths JS supports or inline all the files together.

The former is what unpkg. ... ?module does, it replaces the bare reference to lit-html with the absolute https://unpkg./lit-html@^1.1.1/lit-html.js?module.

The accepted answer is great; the core issue is your CDN file(s) use a "module specifier" that is the "bare" specifier (i.e. not using "/" or "./" or "../") . I fixed my issue with an importmap, mapping my "bare" module specifier, to a full URL/baseURL specifier, i.e.

<script type="importmap">
   { "imports": { 'bare-specificer': 'https://full-url-specifier' } } 
</script>

DETAIL ON CURRENT APPROACH: importmap

I was using jsm three.js modules like ColladaLoader, but I got this error:

Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../"

This happens because I import this ColladaLoader.js file, which imports many things from 'three' (a "bare module specifier"):

import {
    AmbientLight,
    // ...etc....
    sRGBEncoding
} from 'three';
//--------^ this is the "bare" module specifier problem

I learned the fix from reading the collada loader example from three.js, which adds an importmap to its html. I think importmap is still a draft feature but it works well to map a "bare module specifier" to a full URL /baseURL-- just what we need! (more here and here):

<script type="importmap">
    {
        "imports": {
            "three": "../build/three.module.js"
        }
    }
</script>
<script type="module">
        //...
</script>

In my case I am using the three.js file located here: https://cdnjs.cloudflare./ajax/libs/three.js/r123/three.module.js

Therefore my importmap looks like this:

<script type="importmap">
    {
        "imports": {
            "three": "https://cdnjs.cloudflare./ajax/libs/three.js/r123/three.module.js"
        }
    }
</script>
<script type="module">
        //...
</script>

I expect an importmap can work for you even if you have a "bare" module specifier from somewhere other than three.js; but the importmap approach may only work in certain browsers (i.e. caniuse suggests Edge 89+, Chrome 89+, and Opera 76+ support type="importmap" as of 5/4/2022)

DETAIL ON ALTERNATIVE APPROACH: skypack

THE BELOW ANSWER DEPENDS ON skypack hosting up-to-date modules; i.e. the latest ColladaLoader should use BufferGeometry instead of Geometry which was deprecated with the change in r125, but the latest ColladaLoader was not available on skypack AFAIK

I'm providing this answer to suggest skypack as an alternative to your CDN, which works especially in the context of three.js, (apparently even unpkg might not work; read on)

The solution is discussed in this github issue, which references this migration guide

NPM: ES6 modules in examples/jsm now import using the bare specifier three. This change breaks working with modules in cdns such as https://www.jsdelivr./ and https://unpkg./. Please use https://www.skypack.dev/ instead.

So in my case I replaced this line:

import { ColladaLoader } from 'https://cdn.jsdelivr/gh/mrdoob/three.js/examples/jsm/loaders/ColladaLoader.js';

with this line:

import ColladaLoader from 'https://cdn.skypack.dev/three-collada-loader';

This fixed the error re "Failed to resolve module specifier". But I did face a different issue, but that's three.js specific; i.e. using the deprecated Geometry constructor, because I guess the skypack.dev/three-collada-loader is not the latest code...

发布评论

评论列表(0)

  1. 暂无评论