Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 5fb30ce

Browse files
committed
fix: Use isProduction when defined and fallback to NODE_ENV/BUILD
1 parent 911eabc commit 5fb30ce

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"pre:docs": "cp CHANGELOG.md docs/changelog.md",
3838
":docs": "vuepress dev docs/",
3939
"post:docs": "rm docs/CHANGELOG.md",
40-
"lint": "prettier --no-semi --single-quote --write **/*.js **/*.vue !test/target/** !dist/**",
40+
"lint": "prettier --no-semi --single-quote --write **/*.js src/*.ts **/*.vue !test/target/** !dist/**",
4141
"release": "standard-version -a",
4242
"test": "jest"
4343
},

Diff for: src/index.ts

+22-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
parseVuePartRequest,
55
resolveVuePart,
66
isVuePartRequest,
7-
transformRequireToImport,
7+
transformRequireToImport
88
} from './utils'
99
import {
1010
createDefaultCompiler,
@@ -13,15 +13,15 @@ import {
1313
StyleOptions,
1414
TemplateOptions,
1515
StyleCompileResult,
16-
DescriptorCompileResult,
16+
DescriptorCompileResult
1717
} from '@vue/component-compiler'
1818
import { Plugin, RawSourceMap } from 'rollup'
1919
import * as path from 'path'
2020
import { parse, SFCDescriptor, SFCBlock } from '@vue/component-compiler-utils'
2121
import debug from 'debug'
2222
import {
2323
VueTemplateCompiler,
24-
VueTemplateCompilerParseOptions,
24+
VueTemplateCompilerParseOptions
2525
} from '@vue/component-compiler-utils/dist/types'
2626

2727
const templateCompiler = require('vue-template-compiler')
@@ -118,8 +118,11 @@ export interface VuePluginOptions {
118118
*/
119119
export default function vue(opts: VuePluginOptions = {}): Plugin {
120120
const isVue = createVueFilter(opts.include, opts.exclude)
121-
const isProduction = (opts.template && opts.template.isProduction) ||
122-
process.env.NODE_ENV === 'production' || process.env.BUILD === 'production'
121+
const isProduction =
122+
opts.template && typeof opts.template.isProduction === 'boolean'
123+
? opts.template.isProduction
124+
: process.env.NODE_ENV === 'production' ||
125+
process.env.BUILD === 'production'
123126

124127
d('Version ' + version)
125128
d(`Build environment: ${isProduction ? 'production' : 'development'}`)
@@ -128,13 +131,15 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
128131
if (!opts.normalizer)
129132
opts.normalizer = '~' + 'vue-runtime-helpers/dist/normalize-component.js'
130133
if (!opts.styleInjector)
131-
opts.styleInjector = '~' + 'vue-runtime-helpers/dist/inject-style/browser.js'
134+
opts.styleInjector =
135+
'~' + 'vue-runtime-helpers/dist/inject-style/browser.js'
132136
if (!opts.styleInjectorSSR)
133-
opts.styleInjectorSSR = '~' + 'vue-runtime-helpers/dist/inject-style/server.js'
137+
opts.styleInjectorSSR =
138+
'~' + 'vue-runtime-helpers/dist/inject-style/server.js'
134139

135140
createVuePartRequest.defaultLang = {
136141
...createVuePartRequest.defaultLang,
137-
...opts.defaultLang,
142+
...opts.defaultLang
138143
}
139144

140145
const shouldExtractCss = opts.css === false
@@ -162,9 +167,9 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
162167
video: ['src', 'poster'],
163168
source: 'src',
164169
img: 'src',
165-
image: 'xlink:href',
170+
image: 'xlink:href'
166171
},
167-
...opts.template,
172+
...opts.template
168173
} as any
169174
if (opts.template && typeof opts.template.isProduction === 'undefined') {
170175
opts.template.isProduction = isProduction
@@ -194,7 +199,9 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
194199
if (src.startsWith('.')) {
195200
return path.resolve(path.dirname(ref.filename), src as string)
196201
} else {
197-
return require.resolve(src, { paths: [path.dirname(ref.filename)] })
202+
return require.resolve(src, {
203+
paths: [path.dirname(ref.filename)]
204+
})
198205
}
199206
}
200207

@@ -230,7 +237,7 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
230237
compiler: opts.compiler || templateCompiler,
231238
compilerParseOptions: opts.compilerParseOptions,
232239
sourceRoot: opts.sourceRoot,
233-
needMap: true,
240+
needMap: true
234241
})
235242
)
236243
)
@@ -257,7 +264,7 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
257264
const input: any = {
258265
scopeId,
259266
styles,
260-
customBlocks: [],
267+
customBlocks: []
261268
}
262269

263270
if (descriptor.template) {
@@ -297,7 +304,7 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
297304
? JSON.stringify(path.basename(filename))
298305
: JSON.stringify(filename)
299306
}
300-
`,
307+
`
301308
}
302309
: { code: '' }
303310

@@ -352,6 +359,6 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
352359

353360
return result
354361
}
355-
},
362+
}
356363
}
357364
}

Diff for: src/utils.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export interface VuePartRequestCreator {
3030
}
3131

3232
export function createVueFilter(
33-
include: Array<string|RegExp> | string | RegExp = [/\.vue$/i],
34-
exclude: Array<string|RegExp> | string | RegExp = []
33+
include: Array<string | RegExp> | string | RegExp = [/\.vue$/i],
34+
exclude: Array<string | RegExp> | string | RegExp = []
3535
): (file: string) => boolean {
3636
const filter = createFilter(include, exclude)
3737

@@ -128,14 +128,20 @@ export function transformRequireToImport(code: string): string {
128128
const imports: { [key: string]: string } = {}
129129
let strImports = ''
130130

131-
code = code.replace(/require\(("(?:[^"\\]|\\.)+"|'(?:[^'\\]|\\.)+')\)/g, (_, name): any => {
132-
if (!(name in imports)) {
133-
imports[name] = `__$_require_${name.replace(/[^a-z0-9]/g, '_').replace(/_{2,}/g, '_').replace(/^_|_$/g, '')}__`
134-
strImports += 'import ' + imports[name] + ' from ' + name + '\n'
131+
code = code.replace(
132+
/require\(("(?:[^"\\]|\\.)+"|'(?:[^'\\]|\\.)+')\)/g,
133+
(_, name): any => {
134+
if (!(name in imports)) {
135+
imports[name] = `__$_require_${name
136+
.replace(/[^a-z0-9]/g, '_')
137+
.replace(/_{2,}/g, '_')
138+
.replace(/^_|_$/g, '')}__`
139+
strImports += 'import ' + imports[name] + ' from ' + name + '\n'
140+
}
141+
142+
return imports[name]
135143
}
136-
137-
return imports[name]
138-
})
144+
)
139145

140146
return strImports + code
141147
}

0 commit comments

Comments
 (0)