Skip to content

Commit

Permalink
Generating *.meta.js during production build
Browse files Browse the repository at this point in the history
  • Loading branch information
Giglium committed Oct 28, 2024
1 parent e181535 commit d3c3007
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 22 deletions.
40 changes: 21 additions & 19 deletions src/node/MonkeyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,25 +599,27 @@ 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)
const metaFile = jsFile.replace("user", "meta")
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

0 comments on commit d3c3007

Please sign in to comment.