Skip to content

Commit

Permalink
fix: Update import attribute syntax (#938)
Browse files Browse the repository at this point in the history
  • Loading branch information
jespertheend authored Jun 8, 2024
1 parent 578fb47 commit 741b29c
Show file tree
Hide file tree
Showing 29 changed files with 223 additions and 170 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"typescript.tsdk": "./npm_packages/typescript/5.0.2/lib",
"typescript.tsdk": "./npm_packages/typescript/5.4.5/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
"deno.enable": false,
"deno.lint": false,
Expand Down
8 changes: 4 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"dev": "deno run --allow-env --allow-run --allow-read --allow-write --allow-net scripts/dev.js",
"lint": "deno run --allow-env --allow-read --allow-write --allow-sys --allow-run=git scripts/lint.js",
"check": "deno run --allow-env --allow-run --allow-read --allow-write --allow-net scripts/check.js",
"test": "deno run --allow-run --allow-read --allow-write --allow-env --allow-net scripts/test.js",
"build-engine": "deno run --allow-read --allow-write --allow-net --allow-env scripts/buildEngine.js",
"test": "deno run -A scripts/test.js",
"build-engine": "deno run -A scripts/buildEngine.js",
"build-studio": "deno run -A scripts/buildStudio.js",
"build-npm-package": "deno run --allow-read --allow-write scripts/buildNpmPackage.js",
"build-jsr-package": "deno run --allow-read --allow-write scripts/buildJsrPackage.js"
"build-npm-package": "deno run -A scripts/buildNpmPackage.js",
"build-jsr-package": "deno run -A scripts/buildJsrPackage.js"
}
}
2 changes: 1 addition & 1 deletion importmap.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"fake-imports": "https://deno.land/x/fake_imports@v0.10.0/mod.js",
"fake-dom/": "https://deno.land/x/fake_dom@v0.12.0/src/",
"puppeteer": "https://raw.githubusercontent.com/jespertheend/deno-puppeteer/7c5ed2e6e9f43d7fc27d4fa49f17db36be220956/mod.ts",
"rollup": "npm:rollup@2.79.1",
"$rollup": "npm:rollup@4.18.0",
"rollup-plugin-jscc": "npm:rollup-plugin-jscc@2.0.0",
"rollup-plugin-cleanup": "npm:rollup-plugin-cleanup@3.2.1",
"rollup-plugin-commonjs": "npm:@rollup/plugin-commonjs@11.1.0",
Expand Down
4 changes: 2 additions & 2 deletions scripts/buildEngine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from "std/path/mod.ts";
import * as fs from "std/fs/mod.ts";
import { rollup } from "rollup";
import { rollup } from "$rollup";
import cleanup from "rollup-plugin-cleanup";
import jscc from "rollup-plugin-jscc";
import { overrideDefines } from "./shared/overrideDefinesPlugin.js";
Expand Down Expand Up @@ -37,7 +37,7 @@ async function createBundle() {
return bundle;
}

/** @type {import("rollup").OutputOptions} */
/** @type {import("$rollup").OutputOptions} */
const outputOptions = {
dir: "dist/",
format: "esm",
Expand Down
16 changes: 7 additions & 9 deletions scripts/buildStudio.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { rollup } from "rollup";
import { rollup } from "$rollup";
import { copy, ensureDir, walk } from "std/fs/mod.ts";
import * as path from "std/path/mod.ts";
import { minify } from "terser";
import { setCwd } from "chdir-anywhere";
import { importAssertionsPlugin } from "https://esm.sh/rollup-plugin-import-assert@2.1.0?pin=v87";
import { importAssertions } from "https://esm.sh/acorn-import-assertions@1.8.0?pin=v87";
import postcss from "https://deno.land/x/postcss@8.4.13/mod.js";
import postcssUrl from "npm:postcss-url@10.1.3";
import resolveUrlObjects from "npm:rollup-plugin-resolve-url-objects@0.0.4";
import { dev } from "./dev.js";
import { buildEngineSource } from "./buildEngine.js";
import { toHashString } from "std/crypto/mod.ts";
import { overrideDefines } from "./shared/overrideDefinesPlugin.js";
import { cssImportAttributesPlugin } from "./shared/rollup-css-import-attribute.js";
import { resolveWasmUrls } from "./shared/rollup-plugin-wasm-url.js";

await dev({
needsDependencies: true,
Expand Down Expand Up @@ -67,7 +67,7 @@ async function setHtmlAttribute(filePath, tagComment, attributeValue, attribute
function rebaseCssUrl({
outputPath,
}) {
/** @type {import("rollup").Plugin} */
/** @type {import("$rollup").Plugin} */
const plugin = {
name: "rebaseCssUrl",
async load(id) {
Expand Down Expand Up @@ -143,12 +143,12 @@ const bundle = await rollup({
plugins: [
overrideDefines("/studio/src/studioDefines.js", studioDefines),
resolveUrlObjects(),
resolveWasmUrls(),
rebaseCssUrl({
outputPath,
}),
importAssertionsPlugin(),
cssImportAttributesPlugin(),
],
acornInjectPlugins: [importAssertions],
onwarn: (message) => {
if (message.code == "CIRCULAR_DEPENDENCY") return;
console.error(message.message);
Expand All @@ -166,9 +166,7 @@ const entryPointPaths = new Map();
/** @type {string[]} */
const createdChunkFiles = [];
for (const chunkOrAsset of output) {
if (chunkOrAsset.type != "chunk") {
throw new Error("Assertion failed, unexpected type: " + chunkOrAsset.type);
}
if (chunkOrAsset.type != "chunk") continue;
if (chunkOrAsset.facadeModuleId) {
entryPointPaths.set(chunkOrAsset.facadeModuleId, chunkOrAsset.fileName);
}
Expand Down
3 changes: 2 additions & 1 deletion scripts/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ await dev({
needsTypes: true,
needsTypesSync: true,
needsDependencies: true,
needsDevDependencies: true,
});

const command = new Deno.Command(Deno.execPath(), {
args: ["run", "--allow-env", "--allow-read", "npm:typescript@5.0.2/tsc", "--noEmit", "-p", "./jsconfig.json"],
args: ["run", "--allow-env", "--allow-read", "npm:typescript@5.4.5/tsc", "--noEmit", "-p", "./jsconfig.json"],
stdout: "inherit",
stderr: "inherit",
});
Expand Down
49 changes: 19 additions & 30 deletions scripts/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,38 +104,27 @@ export async function dev({
}

if (needsDependencies || needsDevDependencies) {
const { dev } = await import("https://deno.land/x/dev@v0.2.0/mod.js");
const { downloadNpmPackage, esmify, addTsNocheck } = await import("https://deno.land/x/dev@v0.4.0/mod.js");

await dev({
actions: [
// required for during development, can be skipped with ci
{
type: "downloadNpmPackage",
package: "typescript@5.0.2",
ignore: !needsDevDependencies,
},
if (needsDevDependencies) {
await downloadNpmPackage({
package: "typescript@5.4.5",
});
}

// studio dependencies
{
type: "downloadNpmPackage",
package: "rollup@2.60.0",
},
{
type: "downloadNpmPackage",
package: "rollup-plugin-resolve-url-objects@0.0.4",
downloadDependencies: true,
},
{
type: "esmify",
entryPointPath: "npm_packages/rollup/2.60.0/dist/rollup.browser.js",
outputPath: "studio/deps/rollup.browser.js",
},
{
type: "esmify",
entryPointPath: "npm_packages/rollup-plugin-resolve-url-objects/0.0.4/main.js",
outputPath: "studio/deps/rollup-plugin-resolve-url-objects.js",
},
],
await downloadNpmPackage({
package: "@rollup/browser@4.18.0",
});
await addTsNocheck({
path: "npm_packages/@rollup/browser/4.18.0",
});
await downloadNpmPackage({
package: "rollup-plugin-resolve-url-objects@0.0.4",
downloadDependencies: true,
});
await esmify({
entryPointPath: "npm_packages/rollup-plugin-resolve-url-objects/0.0.4/main.js",
outFile: "studio/deps/rollup-plugin-resolve-url-objects.js",
});
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/shared/overrideDefinesPlugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @param {string} definesFilePath
* @param {Object<string, unknown>} defines
* @returns {import("rollup").Plugin}
* @returns {import("$rollup").Plugin}
*/
export function overrideDefines(definesFilePath, defines) {
return {
Expand Down
37 changes: 37 additions & 0 deletions scripts/shared/rollup-css-import-attribute.js
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 };
}
},
};
}
50 changes: 50 additions & 0 deletions scripts/shared/rollup-plugin-wasm-url.js
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,
};
},
};
}
2 changes: 1 addition & 1 deletion scripts/shared/rollupTerserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const nameCache = {};
/**
* A rollup plugin for minifying builds.
* @param {import("terser").MinifyOptions} minifyOptions
* @returns {import("rollup").Plugin}
* @returns {import("$rollup").Plugin}
*/
export function rollupTerserPlugin(minifyOptions = {}) {
return {
Expand Down
4 changes: 2 additions & 2 deletions scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { setCwd } from "chdir-anywhere";
import { dev } from "./dev.js";
import { parseArgs } from "../test/shared/testArgs.js";
import { buildEngine } from "./buildEngine.js";
import { rollup } from "rollup";
import { rollup } from "$rollup";
import { rollupTerserPlugin } from "./shared/rollupTerserPlugin.js";

setCwd();
Expand Down Expand Up @@ -106,7 +106,7 @@ if (needsMinifiedTests) {
await buildEngine(engineDir);

/**
* @returns {import("rollup").Plugin}
* @returns {import("$rollup").Plugin}
*/
function rollupRedirectBuildPlugin() {
return {
Expand Down
56 changes: 7 additions & 49 deletions studio/src/misc/thirdPartyLicenses/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,52 +54,9 @@ License: MIT
By: Rich Harris
Repository: rollup/plugins
---------------------------------------
## acorn
License: MIT
By: Marijn Haverbeke, Ingvar Stepanyan, Adrian Heine
Repository: https://github.com/acornjs/acorn.git
> MIT License
>
> Copyright (C) 2012-2022 by various contributors (see AUTHORS)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
---------------------------------------
## acorn-import-assertions
License: MIT
By: Sven Sauleau
Repository: https://github.com/xtuc/acorn-import-assertions
---------------------------------------
## acorn-walk
License: MIT
By: Marijn Haverbeke, Ingvar Stepanyan, Adrian Heine
Repository: https://github.com/acornjs/acorn.git
> MIT License
> The MIT License (MIT)
>
> Copyright (C) 2012-2020 by various contributors (see AUTHORS)
> Copyright (c) 2019 RollupJS Plugin Contributors (https://github.com/rollup/plugins/graphs/contributors)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -151,7 +108,8 @@ Repository: sindresorhus/binary-extensions
> MIT License
>
> Copyright (c) 2019 Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com), Paul Miller (https://paulmillr.com)
> Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
> Copyright (c) Paul Miller (https://paulmillr.com)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
>
Expand All @@ -168,7 +126,7 @@ Repository: micromatch/braces
> The MIT License (MIT)
>
> Copyright (c) 2014-2018, Jon Schlinkert.
> Copyright (c) 2014-present, Jon Schlinkert.
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -451,7 +409,7 @@ Repository: git+https://github.com/Rich-Harris/is-reference.git
## locate-character
License: MIT
By: Rich Harris
Repository: Rich-Harris/locate-character
Repository: git+https://gitlab.com/Rich-Harris/locate-character.git
---------------------------------------
Expand Down Expand Up @@ -615,7 +573,7 @@ Repository: https://github.com/tapjs/signal-exit.git
> The ISC License
>
> Copyright (c) 2015, Contributors
> Copyright (c) 2015-2023 Benjamin Coe, Isaac Z. Schlueter, and Contributors
>
> Permission to use, copy, modify, and/or distribute this software
> for any purpose with or without fee is hereby granted, provided
Expand Down
Loading

0 comments on commit 741b29c

Please sign in to comment.