diff --git a/README.md b/README.md index 4fd0e26..4ec622e 100644 --- a/README.md +++ b/README.md @@ -316,6 +316,13 @@ Default: `undefined` Function to transform the meta object before using it for serving or building. Can be used to add or modify meta properties. +#### `meta.generateFile` + +Type: `boolean`\ +Default: `true` + +Generating `*.meta.js` for lightweight update checking when self-hosting userscripts, e.g. GitHub Pages + #### `require.provider` Type: `"jsdelivr" | "unpkg"`\ @@ -1049,7 +1056,6 @@ vite-plugin-monkey is quite a mature project and has been well tested by the com - Loading external assets (@resource) - Greasemonkey support (GM.\*) -- Generating `*.meta.js` for lightweight update checking when self-hosting userscripts, e.g. GitHub Pages webpack-monkey is still in early development but has some exclusive features: diff --git a/src/node/MonkeyPlugin.ts b/src/node/MonkeyPlugin.ts index 11a3dc3..324a83f 100644 --- a/src/node/MonkeyPlugin.ts +++ b/src/node/MonkeyPlugin.ts @@ -95,6 +95,7 @@ export interface MonkeyPluginOptions { resolve?: string | string[] | MetaResolver load?: MetaLoader transform?: metaTransformer + generateFile?: boolean } devScript?: { meta?: @@ -599,25 +600,29 @@ export class MonkeyPlugin { } } - // inject meta block - jsContent = - generateMetaBlock(jsContent, { - ...userscript.meta, - require: [ - ...castTruthyArray(userscript.meta.require), - ...(await this.getRequiresFromExternalModules({ - compilation, - chunk, - projectPackageJson: await projectPackageJsonPromise, - })), - ], - }) + - "\n\n" + - jsContent - - const newJsSource = new RawSource(jsContent) - - compilation.updateAsset(jsFile, newJsSource) + // Generate the meta block + const metaBlock = generateMetaBlock(jsContent, { + ...userscript.meta, + require: [ + ...castTruthyArray(userscript.meta.require), + ...(await this.getRequiresFromExternalModules({ + compilation, + chunk, + projectPackageJson: await projectPackageJsonPromise, + })), + ], + }) + + // Generate meta file for userscript (*.meta.js) + if (this.options.meta?.generateFile ?? true) { + const metaFile = jsFile.replace(/\.user\.js$/, ".meta.js") + compilation.emitAsset(metaFile, new RawSource(metaBlock)) + chunk.auxiliaryFiles.add(metaFile) + } + + // Generate userscript file (*.user.js) + jsContent = metaBlock + "\n\n" + jsContent + compilation.updateAsset(jsFile, new RawSource(jsContent)) }), ) }), diff --git a/tests/cases/basic/__snapshots__/basic.test.ts/build-2.txt b/tests/cases/basic/__snapshots__/basic.test.ts/build-2.txt new file mode 100644 index 0000000..6477fa5 --- /dev/null +++ b/tests/cases/basic/__snapshots__/basic.test.ts/build-2.txt @@ -0,0 +1,8 @@ +// ==UserScript== +// @name Hello world +// @grant GM_log +// @match *:/* +// @version 1.0.0 +// @run-at document-end +// @inject-into content +// ==/UserScript== diff --git a/tests/cases/css/__snapshots__/css.test.ts/build-2.txt b/tests/cases/css/__snapshots__/css.test.ts/build-2.txt new file mode 100644 index 0000000..d01bbaa --- /dev/null +++ b/tests/cases/css/__snapshots__/css.test.ts/build-2.txt @@ -0,0 +1,6 @@ +// ==UserScript== +// @name CSS test +// @grant GM_addStyle +// @match *:/* +// @version 1.0.0 +// ==/UserScript== diff --git a/tests/cases/external/__snapshots__/external.test.ts/build-2.txt b/tests/cases/external/__snapshots__/external.test.ts/build-2.txt new file mode 100644 index 0000000..2025d48 --- /dev/null +++ b/tests/cases/external/__snapshots__/external.test.ts/build-2.txt @@ -0,0 +1,12 @@ +// ==UserScript== +// @name External test +// @require https://unpkg.com/ex-named +// @require https://ex-url-named +// @require https://ex-url-global-named +// @require https://ex-url-global +// @require https://ex-url-global2 +// @require https://ex-url-global3 +// @require https://unpkg.com/ex +// @match *:/* +// @version 1.0.0 +// ==/UserScript== diff --git a/tests/cases/hot/__snapshots__/hot.test.ts/build-2.txt b/tests/cases/hot/__snapshots__/hot.test.ts/build-2.txt new file mode 100644 index 0000000..0c657fd --- /dev/null +++ b/tests/cases/hot/__snapshots__/hot.test.ts/build-2.txt @@ -0,0 +1,7 @@ +// ==UserScript== +// @name Hot test +// @grant GM_addStyle +// @grant GM_addElement +// @match *:/* +// @version 1.0.0 +// ==/UserScript== diff --git a/tests/utils/webpack.ts b/tests/utils/webpack.ts index 8f434e4..1f4d0cc 100644 --- a/tests/utils/webpack.ts +++ b/tests/utils/webpack.ts @@ -18,13 +18,18 @@ export async function testBuild(config: webpack.Configuration) { const compiler = webpack(config) const stats = await compilerRun(compiler) - const files = stats.chunks!.flatMap((chunk) => chunk.files) + // Test the user script file (*.user.js) generated by the compilation + const files = stats.chunks!.flatMap((chunk) => chunk.files) expect(files).toHaveLength(1) - const content = fs.readFileSync(`${config.output!.path}/${files[0]}`, "utf-8") - expect(content).toMatchSnapshot() + + // Test the meta file (*.meta.js) generated by the compilation + const auxiliaryFiles = stats.chunks!.flatMap((chunk) => chunk.auxiliaryFiles) + expect(auxiliaryFiles).toHaveLength(1) + const auxiliaryContent = fs.readFileSync(`${config.output!.path}/${auxiliaryFiles[0]}`, "utf-8") + expect(auxiliaryContent).toMatchSnapshot() } export function compilerRun(compiler: webpack.Compiler) {