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

Commit

Permalink
fix: Use isProduction when defined and fallback to NODE_ENV/BUILD
Browse files Browse the repository at this point in the history
  • Loading branch information
znck committed Feb 5, 2019
1 parent 911eabc commit 5fb30ce
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
37 changes: 22 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
parseVuePartRequest,
resolveVuePart,
isVuePartRequest,
transformRequireToImport,
transformRequireToImport
} from './utils'
import {
createDefaultCompiler,
Expand All @@ -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')
Expand Down Expand Up @@ -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'}`)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)]
})
}
}

Expand Down Expand Up @@ -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
})
)
)
Expand All @@ -257,7 +264,7 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
const input: any = {
scopeId,
styles,
customBlocks: [],
customBlocks: []
}

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

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

return result
}
},
}
}
}
24 changes: 15 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.