Skip to content

Commit

Permalink
feat: don't create loader for web accessible scripts with no imports (#…
Browse files Browse the repository at this point in the history
…72)

this matches the functionality of content scripts
  • Loading branch information
samrum authored Jan 3, 2023
1 parent fe888e4 commit 8acfaec
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 152 deletions.
12 changes: 5 additions & 7 deletions src/manifestParser/manifestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
45 changes: 7 additions & 38 deletions test/manifest/__snapshots__/fullExtension.v2.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\");
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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\\",
Expand Down
47 changes: 8 additions & 39 deletions test/manifest/__snapshots__/fullExtension.v3.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\");
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -3443,8 +3413,7 @@ img {
\\"<all_urls>\\"
],
\\"resources\\": [
\\"test/manifest/resources/fullExtension/src/lib.js\\",
\\"assets/test/manifest/resources/fullExtension/src/lib.js\\"
\\"test/manifest/resources/fullExtension/src/lib.js\\"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
{
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
{
Expand All @@ -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://*/*\\",
Expand Down

0 comments on commit 8acfaec

Please sign in to comment.