如何在 Rollup 中导入本地文件系统路径而不是 node_module?
How to import local file system path instead of node_module in Rollup?
出于某种原因,我不得不这样写代码:
import { something } from '/Users/my-user-name/my-working-dir/my-package/src/somefile.ts';
Rollup 看到 /Users
认为这是 node_modules,但不是。
我找不到与此相关的任何汇总插件。
现在,我写了一个 rollup 插件来解决这个问题,但我之前没有写任何插件,我不知道我这样做是对还是错,但输出正是我的想要:
function fixLocalImport() {
return {
name: 'fix-local-import', // this name will show up in warnings and errors
resolveId(source, importer) {
if (source.startsWith('/Users')) {
return source;
}
return null; // other ids should be handled as usually
},
load(id) {
return null; // other ids should be handled as usually
}
};
}
我做错了什么吗?
Rollup 不会自动处理绝对 URL,因为它们根据上下文(网站服务器的根目录、文件系统的根目录或项目的根目录)引用不同的事物。编写插件是这里最好的解决方案,尽管您不需要覆盖 "load" 挂钩。
function fixLocalImport() {
return {
name: 'fix-local-import',
resolveId(source, importer) {
if (source.startsWith('/Users')) {
return source;
}
return null;
},
};
}
出于某种原因,我不得不这样写代码:
import { something } from '/Users/my-user-name/my-working-dir/my-package/src/somefile.ts';
Rollup 看到 /Users
认为这是 node_modules,但不是。
我找不到与此相关的任何汇总插件。
现在,我写了一个 rollup 插件来解决这个问题,但我之前没有写任何插件,我不知道我这样做是对还是错,但输出正是我的想要:
function fixLocalImport() {
return {
name: 'fix-local-import', // this name will show up in warnings and errors
resolveId(source, importer) {
if (source.startsWith('/Users')) {
return source;
}
return null; // other ids should be handled as usually
},
load(id) {
return null; // other ids should be handled as usually
}
};
}
我做错了什么吗?
Rollup 不会自动处理绝对 URL,因为它们根据上下文(网站服务器的根目录、文件系统的根目录或项目的根目录)引用不同的事物。编写插件是这里最好的解决方案,尽管您不需要覆盖 "load" 挂钩。
function fixLocalImport() {
return {
name: 'fix-local-import',
resolveId(source, importer) {
if (source.startsWith('/Users')) {
return source;
}
return null;
},
};
}