From 8acfaecdf653a491938d361dfe07d3da5ac6bb94 Mon Sep 17 00:00:00 2001 From: Ruben Medina Date: Mon, 2 Jan 2023 20:07:41 -0800 Subject: [PATCH] feat: don't create loader for web accessible scripts with no imports (#72) this matches the functionality of content scripts --- src/manifestParser/manifestParser.ts | 12 ++--- src/utils/loader.ts | 6 +++ .../fullExtension.v2.test.ts.snap | 45 +++--------------- .../fullExtension.v3.test.ts.snap | 47 ++++--------------- ...cessibleScriptWithNoImport.v2.test.ts.snap | 37 ++------------- ...cessibleScriptWithNoImport.v3.test.ts.snap | 37 ++------------- 6 files changed, 32 insertions(+), 152 deletions(-) diff --git a/src/manifestParser/manifestParser.ts b/src/manifestParser/manifestParser.ts index 3235e0b..feeb484 100644 --- a/src/manifestParser/manifestParser.ts +++ b/src/manifestParser/manifestParser.ts @@ -222,13 +222,11 @@ export default abstract class ManifestParser< chunkInfo ); - if (scriptLoaderFile.source) { - result.emitFiles.push({ - type: "asset", - fileName: scriptLoaderFile.fileName, - source: scriptLoaderFile.source, - }); - } + result.emitFiles.push({ + type: "asset", + fileName: scriptLoaderFile.fileName, + source: scriptLoaderFile.source ?? chunkInfo.code, + }); const metadata = this.getMetadataforChunk( chunkInfo.fileName, diff --git a/src/utils/loader.ts b/src/utils/loader.ts index eade580..a4f3ed4 100644 --- a/src/utils/loader.ts +++ b/src/utils/loader.ts @@ -61,5 +61,11 @@ export function getWebAccessibleScriptLoaderForOutputChunk( contentScriptFileName: string, chunk: OutputChunk ): { fileName: string; source?: string } { + if (!chunk.imports.length && !chunk.dynamicImports.length) { + return { + fileName: chunk.fileName, + }; + } + return getScriptLoaderFile(contentScriptFileName, chunk.fileName); } diff --git a/test/manifest/__snapshots__/fullExtension.v2.test.ts.snap b/test/manifest/__snapshots__/fullExtension.v2.test.ts.snap index f204154..7bb2b20 100644 --- a/test/manifest/__snapshots__/fullExtension.v2.test.ts.snap +++ b/test/manifest/__snapshots__/fullExtension.v2.test.ts.snap @@ -156,37 +156,6 @@ console.log(chrome.runtime.getURL(\\"src/lib.js\\"));", }, }, }, - { - "code": "console.log(\\"lib.js\\"); -", - "dynamicImports": [], - "exports": [], - "facadeModuleId": "/mocked-repo-dir/test/manifest/resources/fullExtension/src/lib.js", - "fileName": "assets/test/manifest/resources/fullExtension/src/lib.js", - "implicitlyLoadedBefore": [], - "importedBindings": {}, - "imports": [], - "isDynamicEntry": false, - "isEntry": true, - "isImplicitEntry": false, - "map": null, - "modules": { - "/mocked-repo-dir/test/manifest/resources/fullExtension/src/lib.js": { - "code": "console.log(\\"lib.js\\");", - "originalLength": 23, - "removedExports": [], - "renderedExports": [], - "renderedLength": 22, - }, - }, - "name": "test/manifest/resources/fullExtension/src/lib", - "referencedFiles": [], - "type": "chunk", - "viteMetadata": { - "importedAssets": Set {}, - "importedCss": Set {}, - }, - }, { "code": "browser.runtime.onInstalled.addListener(() => { console.log(\\"Extension installed\\"); @@ -3324,6 +3293,13 @@ export { }, "type": "asset", }, + { + "fileName": "assets/test/manifest/resources/fullExtension/src/lib.js", + "name": undefined, + "source": "console.log(\\"lib.js\\"); +", + "type": "asset", + }, { "fileName": "assets/index.css", "name": "test/manifest/resources/fullExtension/src/entries/options/index.css", @@ -3393,12 +3369,6 @@ img { ", "type": "asset", }, - { - "fileName": "test/manifest/resources/fullExtension/src/lib.js", - "name": undefined, - "source": "(async()=>{await import(chrome.runtime.getURL(\\"assets/test/manifest/resources/fullExtension/src/lib.js\\"))})();", - "type": "asset", - }, { "fileName": "test/manifest/resources/fullExtension/src/entries/contentScript/primary/main.js", "name": undefined, @@ -3428,7 +3398,6 @@ img { ], \\"web_accessible_resources\\": [ \\"test/manifest/resources/fullExtension/src/lib.js\\", - \\"assets/test/manifest/resources/fullExtension/src/lib.js\\", \\"assets/test/manifest/resources/fullExtension/src/entries/contentScript/primary/main.js\\", \\"assets/logo.js\\", \\"assets/logo.svg\\", diff --git a/test/manifest/__snapshots__/fullExtension.v3.test.ts.snap b/test/manifest/__snapshots__/fullExtension.v3.test.ts.snap index 2513772..0b750e2 100644 --- a/test/manifest/__snapshots__/fullExtension.v3.test.ts.snap +++ b/test/manifest/__snapshots__/fullExtension.v3.test.ts.snap @@ -156,37 +156,6 @@ console.log(chrome.runtime.getURL(\\"src/lib.js\\"));", }, }, }, - { - "code": "console.log(\\"lib.js\\"); -", - "dynamicImports": [], - "exports": [], - "facadeModuleId": "/mocked-repo-dir/test/manifest/resources/fullExtension/src/lib.js", - "fileName": "assets/test/manifest/resources/fullExtension/src/lib.js", - "implicitlyLoadedBefore": [], - "importedBindings": {}, - "imports": [], - "isDynamicEntry": false, - "isEntry": true, - "isImplicitEntry": false, - "map": null, - "modules": { - "/mocked-repo-dir/test/manifest/resources/fullExtension/src/lib.js": { - "code": "console.log(\\"lib.js\\");", - "originalLength": 23, - "removedExports": [], - "renderedExports": [], - "renderedLength": 22, - }, - }, - "name": "test/manifest/resources/fullExtension/src/lib", - "referencedFiles": [], - "type": "chunk", - "viteMetadata": { - "importedAssets": Set {}, - "importedCss": Set {}, - }, - }, { "code": "browser.runtime.onInstalled.addListener(() => { console.log(\\"Extension installed\\"); @@ -3317,6 +3286,13 @@ export { }, "type": "asset", }, + { + "fileName": "assets/test/manifest/resources/fullExtension/src/lib.js", + "name": undefined, + "source": "console.log(\\"lib.js\\"); +", + "type": "asset", + }, { "fileName": "assets/index.css", "name": "test/manifest/resources/fullExtension/src/entries/options/index.css", @@ -3379,12 +3355,6 @@ img { ", "type": "asset", }, - { - "fileName": "test/manifest/resources/fullExtension/src/lib.js", - "name": undefined, - "source": "(async()=>{await import(chrome.runtime.getURL(\\"assets/test/manifest/resources/fullExtension/src/lib.js\\"))})();", - "type": "asset", - }, { "fileName": "test/manifest/resources/fullExtension/src/entries/contentScript/primary/main.js", "name": undefined, @@ -3443,8 +3413,7 @@ img { \\"\\" ], \\"resources\\": [ - \\"test/manifest/resources/fullExtension/src/lib.js\\", - \\"assets/test/manifest/resources/fullExtension/src/lib.js\\" + \\"test/manifest/resources/fullExtension/src/lib.js\\" ] }, { diff --git a/test/manifest/__snapshots__/webAccessibleScriptWithNoImport.v2.test.ts.snap b/test/manifest/__snapshots__/webAccessibleScriptWithNoImport.v2.test.ts.snap index 4cadfac..34147a4 100644 --- a/test/manifest/__snapshots__/webAccessibleScriptWithNoImport.v2.test.ts.snap +++ b/test/manifest/__snapshots__/webAccessibleScriptWithNoImport.v2.test.ts.snap @@ -3,40 +3,10 @@ exports[`webAccessibleScriptWithNoImport - Manifest V2 1`] = ` [ { - "code": "console.log(\\"webAccessibleScript\\"); -", - "dynamicImports": [], - "exports": [], - "facadeModuleId": "/mocked-repo-dir/test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js", "fileName": "assets/test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js", - "implicitlyLoadedBefore": [], - "importedBindings": {}, - "imports": [], - "isDynamicEntry": false, - "isEntry": true, - "isImplicitEntry": false, - "map": null, - "modules": { - "/mocked-repo-dir/test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js": { - "code": "console.log(\\"webAccessibleScript\\");", - "originalLength": 36, - "removedExports": [], - "renderedExports": [], - "renderedLength": 35, - }, - }, - "name": "test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript", - "referencedFiles": [], - "type": "chunk", - "viteMetadata": { - "importedAssets": Set {}, - "importedCss": Set {}, - }, - }, - { - "fileName": "test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js", "name": undefined, - "source": "(async()=>{await import(chrome.runtime.getURL(\\"assets/test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js\\"))})();", + "source": "console.log(\\"webAccessibleScript\\"); +", "type": "asset", }, { @@ -47,8 +17,7 @@ exports[`webAccessibleScriptWithNoImport - Manifest V2 1`] = ` \\"name\\": \\"Manifest Name\\", \\"manifest_version\\": 2, \\"web_accessible_resources\\": [ - \\"test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js\\", - \\"assets/test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js\\" + \\"test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js\\" ] }", "type": "asset", diff --git a/test/manifest/__snapshots__/webAccessibleScriptWithNoImport.v3.test.ts.snap b/test/manifest/__snapshots__/webAccessibleScriptWithNoImport.v3.test.ts.snap index a8eb802..03ec886 100644 --- a/test/manifest/__snapshots__/webAccessibleScriptWithNoImport.v3.test.ts.snap +++ b/test/manifest/__snapshots__/webAccessibleScriptWithNoImport.v3.test.ts.snap @@ -3,40 +3,10 @@ exports[`webAccessibleScriptWithNoImport - Manifest V3 1`] = ` [ { - "code": "console.log(\\"webAccessibleScript\\"); -", - "dynamicImports": [], - "exports": [], - "facadeModuleId": "/mocked-repo-dir/test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js", "fileName": "assets/test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js", - "implicitlyLoadedBefore": [], - "importedBindings": {}, - "imports": [], - "isDynamicEntry": false, - "isEntry": true, - "isImplicitEntry": false, - "map": null, - "modules": { - "/mocked-repo-dir/test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js": { - "code": "console.log(\\"webAccessibleScript\\");", - "originalLength": 36, - "removedExports": [], - "renderedExports": [], - "renderedLength": 35, - }, - }, - "name": "test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript", - "referencedFiles": [], - "type": "chunk", - "viteMetadata": { - "importedAssets": Set {}, - "importedCss": Set {}, - }, - }, - { - "fileName": "test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js", "name": undefined, - "source": "(async()=>{await import(chrome.runtime.getURL(\\"assets/test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js\\"))})();", + "source": "console.log(\\"webAccessibleScript\\"); +", "type": "asset", }, { @@ -49,8 +19,7 @@ exports[`webAccessibleScriptWithNoImport - Manifest V3 1`] = ` \\"web_accessible_resources\\": [ { \\"resources\\": [ - \\"test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js\\", - \\"assets/test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js\\" + \\"test/manifest/resources/webAccessibleScriptWithNoImport/webAccessibleScript.js\\" ], \\"matches\\": [ \\"https://*/*\\",