-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Update import attribute syntax (#938)
- Loading branch information
1 parent
578fb47
commit 741b29c
Showing
29 changed files
with
223 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* A rollup plugin that allows you to import css files like so: | ||
* ```js | ||
* import sheet from "./path/to/style.css" with { type: "css" } | ||
* document.adoptedStyleSheets = [sheet]; | ||
* ``` | ||
* @returns {import("$rollup").Plugin} | ||
*/ | ||
export function cssImportAttributesPlugin() { | ||
return { | ||
name: "css-import-attributes", | ||
async resolveId(source, importer, options) { | ||
const { attributes } = options; | ||
if ("type" in attributes && attributes.type == "css") { | ||
const resolution = await this.resolve(source, importer, options); | ||
if (!resolution || resolution.external) return resolution; | ||
return { | ||
id: resolution.id, | ||
meta: { | ||
cssImportAttributes: { | ||
isCssType: true, | ||
}, | ||
}, | ||
}; | ||
} | ||
return null; | ||
}, | ||
transform(code, id) { | ||
const moduleInfo = this.getModuleInfo(id); | ||
const meta = moduleInfo?.meta; | ||
if (meta && "cssImportAttributes" in meta && meta.cssImportAttributes.isCssType) { | ||
const newCode = `const sheet = new CSSStyleSheet();sheet.replaceSync(\`${code}\`);export default sheet;`; | ||
return { code: newCode }; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as stdPath from "std/path/mod.ts"; | ||
|
||
/** @type {import("$rollup").PluginImpl} */ | ||
export function resolveWasmUrls() { | ||
return { | ||
name: "resolve-wasm-urls", | ||
async transform(code, id) { | ||
const re = /new\s+URL\s*\(\s*['"`](?<wasmUrl>.+\.wasm)['"`]\s*,\s*import\.meta\.url\s*\)/gid; | ||
for (const match of code.matchAll(re)) { | ||
if (!match.indices) continue; | ||
if (!match.indices.groups) continue; | ||
if (!match.groups) continue; | ||
const startUrlIndex = match.indices.groups.wasmUrl[0] - 1; | ||
const endUrlIndex = match.indices.groups.wasmUrl[1] + 1; | ||
let wasmUrl = match.groups.wasmUrl; | ||
if (!wasmUrl.match(/^.?.?\//)) { | ||
wasmUrl = "./" + wasmUrl; | ||
} | ||
|
||
const resolveResult = await this.resolve(wasmUrl, id, { | ||
attributes: { | ||
type: "webassembly", | ||
}, | ||
}); | ||
if (!resolveResult) continue; | ||
|
||
const parsedPath = stdPath.parse(resolveResult.id); | ||
|
||
const source = await Deno.readFile(resolveResult.id); | ||
const hashBuffer = await crypto.subtle.digest("SHA-256", source); | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); | ||
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); | ||
const hash = hashHex.slice(0, 8); | ||
|
||
const chunkRefId = this.emitFile({ | ||
type: "asset", | ||
fileName: `${parsedPath.name}-${hash}.wasm`, | ||
source, | ||
}); | ||
const newUrl = `import.meta.ROLLUP_FILE_URL_${chunkRefId}`; | ||
|
||
code = code.substring(0, startUrlIndex) + newUrl + code.substring(endUrlIndex); | ||
} | ||
|
||
return { | ||
code, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.