Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating *.meta.js during production build #6

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"`\
Expand Down Expand Up @@ -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:

Expand Down
43 changes: 24 additions & 19 deletions src/node/MonkeyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface MonkeyPluginOptions {
resolve?: string | string[] | MetaResolver
load?: MetaLoader
transform?: metaTransformer
generateFile?: boolean
}
devScript?: {
meta?:
Expand Down Expand Up @@ -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))
}),
)
}),
Expand Down
8 changes: 8 additions & 0 deletions tests/cases/basic/__snapshots__/basic.test.ts/build-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// ==UserScript==
// @name Hello world
// @grant GM_log
// @match *:/*
// @version 1.0.0
// @run-at document-end
// @inject-into content
// ==/UserScript==
6 changes: 6 additions & 0 deletions tests/cases/css/__snapshots__/css.test.ts/build-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ==UserScript==
// @name CSS test
// @grant GM_addStyle
// @match *:/*
// @version 1.0.0
// ==/UserScript==
12 changes: 12 additions & 0 deletions tests/cases/external/__snapshots__/external.test.ts/build-2.txt
Original file line number Diff line number Diff line change
@@ -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==
7 changes: 7 additions & 0 deletions tests/cases/hot/__snapshots__/hot.test.ts/build-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// ==UserScript==
// @name Hot test
// @grant GM_addStyle
// @grant GM_addElement
// @match *:/*
// @version 1.0.0
// ==/UserScript==
11 changes: 8 additions & 3 deletions tests/utils/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading