Permalink
Browse files

fix: Use isProduction when defined and fallback to NODE_ENV/BUILD

  • Loading branch information...
znck committed Feb 5, 2019
1 parent 911eabc commit 5fb30ce23cbe7904cbaa99f2f3241c9235178dbb
Showing with 38 additions and 25 deletions.
  1. +1 −1 package.json
  2. +22 −15 src/index.ts
  3. +15 −9 src/utils.ts
@@ -37,7 +37,7 @@
"pre:docs": "cp CHANGELOG.md docs/changelog.md",
":docs": "vuepress dev docs/",
"post:docs": "rm docs/CHANGELOG.md",
"lint": "prettier --no-semi --single-quote --write **/*.js **/*.vue !test/target/** !dist/**",
"lint": "prettier --no-semi --single-quote --write **/*.js src/*.ts **/*.vue !test/target/** !dist/**",
"release": "standard-version -a",
"test": "jest"
},
@@ -4,7 +4,7 @@ import {
parseVuePartRequest,
resolveVuePart,
isVuePartRequest,
transformRequireToImport,
transformRequireToImport
} from './utils'
import {
createDefaultCompiler,
@@ -13,15 +13,15 @@ import {
StyleOptions,
TemplateOptions,
StyleCompileResult,
DescriptorCompileResult,
DescriptorCompileResult
} from '@vue/component-compiler'
import { Plugin, RawSourceMap } from 'rollup'
import * as path from 'path'
import { parse, SFCDescriptor, SFCBlock } from '@vue/component-compiler-utils'
import debug from 'debug'
import {
VueTemplateCompiler,
VueTemplateCompilerParseOptions,
VueTemplateCompilerParseOptions
} from '@vue/component-compiler-utils/dist/types'

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

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

createVuePartRequest.defaultLang = {
...createVuePartRequest.defaultLang,
...opts.defaultLang,
...opts.defaultLang
}

const shouldExtractCss = opts.css === false
@@ -162,9 +167,9 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
video: ['src', 'poster'],
source: 'src',
img: 'src',
image: 'xlink:href',
image: 'xlink:href'
},
...opts.template,
...opts.template
} as any
if (opts.template && typeof opts.template.isProduction === 'undefined') {
opts.template.isProduction = isProduction
@@ -194,7 +199,9 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
if (src.startsWith('.')) {
return path.resolve(path.dirname(ref.filename), src as string)
} else {
return require.resolve(src, { paths: [path.dirname(ref.filename)] })
return require.resolve(src, {
paths: [path.dirname(ref.filename)]
})
}
}

@@ -230,7 +237,7 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
compiler: opts.compiler || templateCompiler,
compilerParseOptions: opts.compilerParseOptions,
sourceRoot: opts.sourceRoot,
needMap: true,
needMap: true
})
)
)
@@ -257,7 +264,7 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
const input: any = {
scopeId,
styles,
customBlocks: [],
customBlocks: []
}

if (descriptor.template) {
@@ -297,7 +304,7 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
? JSON.stringify(path.basename(filename))
: JSON.stringify(filename)
}
`,
`
}
: { code: '' }

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

return result
}
},
}
}
}
@@ -30,8 +30,8 @@ export interface VuePartRequestCreator {
}

export function createVueFilter(
include: Array<string|RegExp> | string | RegExp = [/\.vue$/i],
exclude: Array<string|RegExp> | string | RegExp = []
include: Array<string | RegExp> | string | RegExp = [/\.vue$/i],
exclude: Array<string | RegExp> | string | RegExp = []
): (file: string) => boolean {
const filter = createFilter(include, exclude)

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

code = code.replace(/require\(("(?:[^"\\]|\\.)+"|'(?:[^'\\]|\\.)+')\)/g, (_, name): any => {
if (!(name in imports)) {
imports[name] = `__$_require_${name.replace(/[^a-z0-9]/g, '_').replace(/_{2,}/g, '_').replace(/^_|_$/g, '')}__`
strImports += 'import ' + imports[name] + ' from ' + name + '\n'
code = code.replace(
/require\(("(?:[^"\\]|\\.)+"|'(?:[^'\\]|\\.)+')\)/g,
(_, name): any => {
if (!(name in imports)) {
imports[name] = `__$_require_${name
.replace(/[^a-z0-9]/g, '_')
.replace(/_{2,}/g, '_')
.replace(/^_|_$/g, '')}__`
strImports += 'import ' + imports[name] + ' from ' + name + '\n'
}

return imports[name]
}

return imports[name]
})
)

return strImports + code
}

0 comments on commit 5fb30ce

Please sign in to comment.