diff --git a/index.js b/index.js index 391d105..60b96a4 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ "use strict"; const { readFileSync } = require("fs"); -const { join } = require("path"); +const { join, resolve, relative, isAbsolute } = require("path"); const ejs = require("ejs"); const MagicString = require("magic-string"); const json5 = require("json5"); @@ -58,6 +58,12 @@ const workerRegexpForOutput = /new\s+Worker\(new\s+URL\((?:'.*?'|".*?"),\s*modul let longWarningAlreadyShown = false; +// Logic from rollup src/utils/relativeId.ts +function relativeId(id) { + if (!isAbsolute(id)) return id; + return relative(resolve(), id); +} + module.exports = function(opts = {}) { opts = Object.assign({}, defaultOpts, opts); @@ -78,19 +84,29 @@ module.exports = function(opts = {}) { if (!id.startsWith(urlLoaderPrefix)) return; const path = id.slice(urlLoaderPrefix.length); - const resolved = await this.resolve(path, importer); + const resolved = await this.resolve(path, importer, {skipSelf: true}); if (!resolved) throw Error(`Cannot find module '${path}' from '${importer}'`); - const newId = resolved.id; - - return urlLoaderPrefix + newId; + const resolvedPathRel = relativeId(resolved.id); + return { + ...resolved, + id: urlLoaderPrefix + resolvedPathRel, + meta: { + ...resolved.meta, + omt: { realId: resolved.id, importer }, + }, + }; }, load(id) { - if (!id.startsWith(urlLoaderPrefix)) return; - - const realId = id.slice(urlLoaderPrefix.length); - const chunkRef = this.emitFile({ id: realId, type: "chunk" }); + const {meta} = this.getModuleInfo(id); + if (!meta.omt) return; + + const chunkRef = this.emitFile({ + type: "chunk", + id: meta.omt.realId, + importer: meta.omt.importer, + }); return `export default import.meta.ROLLUP_FILE_URL_${chunkRef};`; }, diff --git a/package.json b/package.json index a348f95..1ab086f 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "karma-safaritechpreview-launcher": "2.0.2", "mocha": "6.1.4", "prettier": "1.18.2", - "rollup": "2.2.0" + "rollup": "2.29.0" }, "repository": { "type": "git",