mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
fix(builder): add lodash inside templates (#4368)
This commit is contained in:
parent
0db1f26e64
commit
27e79be285
@ -429,16 +429,9 @@ export default class Builder {
|
|||||||
resolve: r
|
resolve: r
|
||||||
})
|
})
|
||||||
|
|
||||||
// Interpret and move template files to .nuxt/
|
// Prepare template options
|
||||||
await Promise.all(
|
let lodash = null
|
||||||
templatesFiles.map(async ({ src, dst, options, custom }) => {
|
const templateOptions = {
|
||||||
// Add template to watchers
|
|
||||||
this.options.build.watch.push(src)
|
|
||||||
// Render template to dst
|
|
||||||
const fileContent = await fsExtra.readFile(src, 'utf8')
|
|
||||||
let content
|
|
||||||
try {
|
|
||||||
const templateFunction = template(fileContent, {
|
|
||||||
imports: {
|
imports: {
|
||||||
serialize,
|
serialize,
|
||||||
devalue,
|
devalue,
|
||||||
@ -448,10 +441,31 @@ export default class Builder {
|
|||||||
wChunk,
|
wChunk,
|
||||||
resolvePath: this.nuxt.resolver.resolvePath,
|
resolvePath: this.nuxt.resolver.resolvePath,
|
||||||
resolveAlias: this.nuxt.resolver.resolveAlias,
|
resolveAlias: this.nuxt.resolver.resolveAlias,
|
||||||
relativeToBuild: this.relativeToBuild
|
relativeToBuild: this.relativeToBuild,
|
||||||
|
// Legacy support: https://github.com/nuxt/nuxt.js/issues/4350
|
||||||
|
_: new Proxy({}, {
|
||||||
|
get(target, prop) {
|
||||||
|
if (!lodash) {
|
||||||
|
consola.warn('Avoid using _ inside templates')
|
||||||
|
lodash = require('lodash')
|
||||||
|
}
|
||||||
|
return lodash[prop]
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
interpolate: /<%=([\s\S]+?)%>/g
|
interpolate: /<%=([\s\S]+?)%>/g
|
||||||
})
|
}
|
||||||
|
|
||||||
|
// Interpret and move template files to .nuxt/
|
||||||
|
await Promise.all(
|
||||||
|
templatesFiles.map(async ({ src, dst, options, custom }) => {
|
||||||
|
// Add template to watchers
|
||||||
|
this.options.build.watch.push(src)
|
||||||
|
// Render template to dst
|
||||||
|
const fileContent = await fsExtra.readFile(src, 'utf8')
|
||||||
|
let content
|
||||||
|
try {
|
||||||
|
const templateFunction = template(fileContent, templateOptions)
|
||||||
content = stripWhitespace(
|
content = stripWhitespace(
|
||||||
templateFunction(
|
templateFunction(
|
||||||
Object.assign({}, templateVars, {
|
Object.assign({}, templateVars, {
|
||||||
|
@ -9,3 +9,6 @@ function $reverseStr(str) {
|
|||||||
Vue.prototype.$reverseStr = $reverseStr
|
Vue.prototype.$reverseStr = $reverseStr
|
||||||
|
|
||||||
export default undefined
|
export default undefined
|
||||||
|
|
||||||
|
// Legacy support: https://github.com/nuxt/nuxt.js/issues/4350
|
||||||
|
// <%= _.toUpper('foo') %>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { normalize } from 'path'
|
import { normalize } from 'path'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import { loadFixture, getPort, Nuxt, rp } from '../utils'
|
import { loadFixture, getPort, Nuxt, Builder, rp } from '../utils'
|
||||||
|
|
||||||
let port
|
let port
|
||||||
const url = route => 'http://localhost:' + port + route
|
const url = route => 'http://localhost:' + port + route
|
||||||
@ -69,6 +69,12 @@ describe('module', () => {
|
|||||||
expect(consola.warn).toHaveBeenCalledWith('addVendor has been deprecated due to webpack4 optimization')
|
expect(consola.warn).toHaveBeenCalledWith('addVendor has been deprecated due to webpack4 optimization')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('Lodash - deprecated', async () => {
|
||||||
|
const builder = new Builder(nuxt)
|
||||||
|
await builder.generateRoutesAndFiles()
|
||||||
|
expect(consola.warn).toHaveBeenCalledWith('Avoid using _ inside templates')
|
||||||
|
})
|
||||||
|
|
||||||
// Close server and ask nuxt to stop listening to file changes
|
// Close server and ask nuxt to stop listening to file changes
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await nuxt.close()
|
await nuxt.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user