-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: generate globals plugin correctly
- Loading branch information
Showing
4 changed files
with
188 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { ModuleOptions } from './module' | ||
import type { RegistryScript } from '#nuxt-scripts' | ||
|
||
export function templatePlugin(config: Partial<ModuleOptions>, registry: Required<RegistryScript>[]) { | ||
const imports = ['useScript', 'defineNuxtPlugin'] | ||
const inits = [] | ||
// for global scripts, we can initialise them script away | ||
for (const [k, c] of Object.entries(config.registry || {})) { | ||
const importDefinition = registry.find(i => i.import.name === `useScript${k.substring(0, 1).toUpperCase() + k.substring(1)}`) | ||
if (importDefinition) { | ||
// title case | ||
imports.unshift(importDefinition.import.name) | ||
const args = (typeof c !== 'object' ? {} : c) || {} | ||
if (c === 'mock') | ||
args.scriptOptions = { trigger: 'manual', skipValidation: true } | ||
inits.push(` ${importDefinition.import.name}(${JSON.stringify(args)})`) | ||
} | ||
} | ||
const useScriptStatements = (config.globals || []).map(g => typeof g === 'string' | ||
? ` useScript("${g.toString()}")` | ||
: Array.isArray(g) && g.length === 2 | ||
? ` useScript(${JSON.stringify(g[0])}, ${JSON.stringify(g[1])} })` | ||
: ` useScript(${JSON.stringify(g)})`) | ||
return [ | ||
`import { ${imports.join(', ')} } from '#imports'`, | ||
'', | ||
`export default defineNuxtPlugin({`, | ||
` name: "scripts:init",`, | ||
` env: { islands: false },`, | ||
` parallel: true,`, | ||
` setup() {`, | ||
...useScriptStatements, | ||
...inits, | ||
` }`, | ||
`})`, | ||
].join('\n') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { templatePlugin } from '../../src/templates' | ||
|
||
describe('template plugin file', () => { | ||
// global | ||
it('empty global', async () => { | ||
const res = templatePlugin({ | ||
globals: [], | ||
registry: {}, | ||
}, []) | ||
expect(res).toMatchInlineSnapshot(` | ||
"import { useScript, defineNuxtPlugin } from '#imports' | ||
export default defineNuxtPlugin({ | ||
name: "scripts:init", | ||
env: { islands: false }, | ||
parallel: true, | ||
setup() { | ||
} | ||
})" | ||
`) | ||
}) | ||
it('string global', async () => { | ||
const res = templatePlugin({ | ||
globals: [ | ||
'https://js.stripe.com/v3/', | ||
], | ||
}, []) | ||
expect(res).toContain('useScript("https://js.stripe.com/v3/")') | ||
}) | ||
it('object global', async () => { | ||
const res = templatePlugin({ | ||
globals: [ | ||
{ | ||
async: true, | ||
src: 'https://js.stripe.com/v3/', | ||
key: 'stripe', | ||
defer: true, | ||
referrerpolicy: 'no-referrer', | ||
}, | ||
], | ||
}, []) | ||
expect(res).toContain('useScript({"async":true,"src":"https://js.stripe.com/v3/","key":"stripe","defer":true,"referrerpolicy":"no-referrer"})') | ||
}) | ||
it('array global', async () => { | ||
const res = templatePlugin({ | ||
globals: [ | ||
[ | ||
{ | ||
async: true, | ||
src: 'https://js.stripe.com/v3/', | ||
key: 'stripe', | ||
defer: true, | ||
referrerpolicy: 'no-referrer', | ||
}, | ||
{ | ||
trigger: 'onNuxtReady', | ||
mode: 'client', | ||
}, | ||
], | ||
], | ||
}, []) | ||
expect(res).toContain('useScript({"async":true,"src":"https://js.stripe.com/v3/","key":"stripe","defer":true,"referrerpolicy":"no-referrer"}, {"trigger":"onNuxtReady","mode":"client"} })') | ||
}) | ||
it('mixing global', async () => { | ||
const res = templatePlugin({ | ||
globals: [ | ||
'https://js.stripe.com/v3/', | ||
{ | ||
async: true, | ||
src: 'https://js.stripe.com/v3/', | ||
key: 'stripe', | ||
defer: true, | ||
referrerpolicy: 'no-referrer', | ||
}, | ||
[ | ||
'https://js.stripe.com/v3/', | ||
{ | ||
trigger: 'onNuxtReady', | ||
mode: 'client', | ||
}, | ||
], | ||
], | ||
}, []) | ||
expect(res).toMatchInlineSnapshot(` | ||
"import { useScript, defineNuxtPlugin } from '#imports' | ||
export default defineNuxtPlugin({ | ||
name: "scripts:init", | ||
env: { islands: false }, | ||
parallel: true, | ||
setup() { | ||
useScript("https://js.stripe.com/v3/") | ||
useScript({"async":true,"src":"https://js.stripe.com/v3/","key":"stripe","defer":true,"referrerpolicy":"no-referrer"}) | ||
useScript("https://js.stripe.com/v3/", {"trigger":"onNuxtReady","mode":"client"} }) | ||
} | ||
})" | ||
`) | ||
}) | ||
// registry | ||
it('registry object', async () => { | ||
const res = templatePlugin({ | ||
globals: [], | ||
registry: { | ||
stripe: { | ||
id: 'test', | ||
}, | ||
}, | ||
}, [ | ||
{ | ||
import: { | ||
name: 'useScriptStripe', | ||
}, | ||
}, | ||
]) | ||
expect(res).toContain('useScriptStripe({"id":"test"})') | ||
}) | ||
it('registry array', async () => { | ||
const res = templatePlugin({ | ||
globals: [], | ||
registry: { | ||
stripe: [ | ||
{ | ||
id: 'test', | ||
}, | ||
{ | ||
trigger: 'onNuxtReady', | ||
}, | ||
], | ||
}, | ||
}, [ | ||
{ | ||
import: { | ||
name: 'useScriptStripe', | ||
}, | ||
}, | ||
]) | ||
expect(res).toContain('useScriptStripe([{"id":"test"},{"trigger":"onNuxtReady"}])') | ||
}) | ||
}) |