示例工程 10-amd-requirejs
10-amd-requirejs/02-paths-shim/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>10 · paths 与 shim</title>
</head>
<body>
<p>打开 Console 查看 RequireJS 如何加载非 AMD 的老库。</p>
<script src="/vendor/requirejs/require.js"></script>
<script>
require.config({
baseUrl: './amd',
paths: {
// 逻辑名 legacy → 相对 baseUrl 的 ../legacy.js
legacy: '../legacy'
},
shim: {
// 老库没有 define,用全局变量作为模块值
legacy: { exports: 'LegacyCounter' }
}
});
require(['use-legacy'], function (mod) {
console.log('use-legacy.demo() =>', mod.demo());
console.log('window.LegacyCounter 仍然存在 =>', typeof window.LegacyCounter);
});
</script>
<script type="module">
import { run } from './main.js';
run();
</script>
</body>
</html>