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

Commit

Permalink
fix: Use rollup's resolveId instead of require.resolve to resolve int…
Browse files Browse the repository at this point in the history
…ernal modules
  • Loading branch information
znck committed Jan 14, 2019
1 parent 7530dfa commit 3fbc8eb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -51,6 +51,7 @@
"hash-sum": "^1.0.2", "hash-sum": "^1.0.2",
"querystring": "^0.2.0", "querystring": "^0.2.0",
"rollup-pluginutils": "^2.0.1", "rollup-pluginutils": "^2.0.1",
"source-map": "0.7.3",
"vue-runtime-helpers": "^0.2.0" "vue-runtime-helpers": "^0.2.0"
}, },
"devDependencies": { "devDependencies": {
Expand Down
91 changes: 61 additions & 30 deletions src/index.ts
Expand Up @@ -4,27 +4,34 @@ import {
parseVuePartRequest, parseVuePartRequest,
resolveVuePart, resolveVuePart,
isVuePartRequest, isVuePartRequest,
transformRequireToImport transformRequireToImport,
} from './utils' } from './utils'
import { import {
createDefaultCompiler, createDefaultCompiler,
assemble, assemble,
ScriptOptions, ScriptOptions,
StyleOptions, StyleOptions,
TemplateOptions, TemplateOptions,
StyleCompileResult StyleCompileResult,
} from '@vue/component-compiler' } from '@vue/component-compiler'
import { Plugin } from 'rollup' import { Plugin } from 'rollup'
import * as path from 'path' import * as path from 'path'
import { parse, SFCDescriptor, SFCBlock } from '@vue/component-compiler-utils' import { parse, SFCDescriptor, SFCBlock } from '@vue/component-compiler-utils'
import debug from 'debug' import debug from 'debug'
import { VueTemplateCompiler, VueTemplateCompilerParseOptions } from '@vue/component-compiler-utils/dist/types' import {
VueTemplateCompiler,
VueTemplateCompilerParseOptions,
} from '@vue/component-compiler-utils/dist/types'


const templateCompiler = require('vue-template-compiler') const templateCompiler = require('vue-template-compiler')
const hash = require('hash-sum') const hash = require('hash-sum')
const d = debug('rollup-plugin-vue')
const { version } = require('../package.json') const { version } = require('../package.json')


const d = debug('rollup-plugin-vue')
const dR = debug('rollup-plugin-vue:resolve')
const dL = debug('rollup-plugin-vue:load')
const dT = debug('rollup-plugin-vue:transform')

export interface VuePluginOptions { export interface VuePluginOptions {
/** /**
* Include files or directories. * Include files or directories.
Expand Down Expand Up @@ -115,13 +122,16 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
d(`Build environment: ${isProduction ? 'production' : 'development'}`) d(`Build environment: ${isProduction ? 'production' : 'development'}`)
d(`Build target: ${process.env.VUE_ENV || 'browser'}`) d(`Build target: ${process.env.VUE_ENV || 'browser'}`)


if (!opts.normalizer) opts.normalizer = '~' + require.resolve('vue-runtime-helpers/normalize-component.js') if (!opts.normalizer)
if (!opts.styleInjector) opts.styleInjector = '~' + require.resolve('vue-runtime-helpers/inject-style/browser.js') opts.normalizer = '~' + 'vue-runtime-helpers/normalize-component.js'
if (!opts.styleInjectorSSR) opts.styleInjectorSSR = '~' + require.resolve('vue-runtime-helpers/inject-style/server.js') if (!opts.styleInjector)
opts.styleInjector = '~' + 'vue-runtime-helpers/inject-style/browser.js'
if (!opts.styleInjectorSSR)
opts.styleInjectorSSR = '~' + 'vue-runtime-helpers/inject-style/server.js'


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


const shouldExtractCss = opts.css === false const shouldExtractCss = opts.css === false
Expand All @@ -144,9 +154,9 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
video: ['src', 'poster'], video: ['src', 'poster'],
source: 'src', source: 'src',
img: 'src', img: 'src',
image: 'xlink:href' image: 'xlink:href',
}, },
...opts.template ...opts.template,
} as any } as any
if (opts.template && typeof opts.template.isProduction === 'undefined') { if (opts.template && typeof opts.template.isProduction === 'undefined') {
opts.template.isProduction = isProduction opts.template.isProduction = isProduction
Expand All @@ -160,6 +170,12 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
name: 'VuePlugin', name: 'VuePlugin',


resolveId(id, importer) { resolveId(id, importer) {
const request = id
if (id.startsWith('vue-runtime-helpers/')) {
id = require.resolve(id)
dR(`form: ${request} \nto: ${id}\n`)

This comment has been minimized.

Copy link
@mgdodge

mgdodge Jan 14, 2019

form: => from:

This comment has been minimized.

Copy link
@znck

znck Jan 14, 2019

Author Member

Thanks.

return id
}
if (!isVuePartRequest(id)) return if (!isVuePartRequest(id)) return
id = path.resolve(path.dirname(importer), id) id = path.resolve(path.dirname(importer), id)
const ref = parseVuePartRequest(id) const ref = parseVuePartRequest(id)
Expand All @@ -174,6 +190,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
} }
} }


dR(`from: ${request} \nto: ${id}\n`)
return id return id
} }
}, },
Expand All @@ -184,24 +201,31 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
if (!request) return if (!request) return


const element = resolveVuePart(descriptors, request) const element = resolveVuePart(descriptors, request)
const code = 'code' in element const code =
? ((element as any).code as string) // .code is set when extract styles is used. { css: false } 'code' in element
: element.content ? ((element as any).code as string) // .code is set when extract styles is used. { css: false }
: element.content
const map = element.map as any const map = element.map as any


dL(`id: ${id}\ncode: \n${code}\nmap: ${JSON.stringify(map, null, 2)}\n\n`)

return { code, map } return { code, map }
}, },


async transform(source: string, filename: string) { async transform(source: string, filename: string) {
if (isVue(filename)) { if (isVue(filename)) {
const descriptor: SFCDescriptor = JSON.parse(JSON.stringify(parse({ const descriptor: SFCDescriptor = JSON.parse(
filename, JSON.stringify(
source, parse({
compiler: opts.compiler || templateCompiler, filename,
compilerParseOptions: opts.compilerParseOptions, source,
sourceRoot: opts.sourceRoot, compiler: opts.compiler || templateCompiler,
needMap: true compilerParseOptions: opts.compilerParseOptions,
}))) sourceRoot: opts.sourceRoot,
needMap: true,
})
)
)


const scopeId = const scopeId =
'data-v-' + 'data-v-' +
Expand All @@ -212,7 +236,11 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {


const styles = await Promise.all( const styles = await Promise.all(
descriptor.styles.map(async style => { descriptor.styles.map(async style => {
const compiled = await compiler.compileStyleAsync(filename, scopeId, style) const compiled = await compiler.compileStyleAsync(
filename,
scopeId,
style
)
if (compiled.errors.length > 0) throw Error(compiled.errors[0]) if (compiled.errors.length > 0) throw Error(compiled.errors[0])
return compiled return compiled
}) })
Expand All @@ -221,7 +249,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
const input: any = { const input: any = {
scopeId, scopeId,
styles, styles,
customBlocks: [] customBlocks: [],
} }


if (descriptor.template) { if (descriptor.template) {
Expand All @@ -230,9 +258,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
descriptor.template descriptor.template
) )


input.template.code = transformRequireToImport( input.template.code = transformRequireToImport(input.template.code)
input.template.code
)


if (input.template.errors && input.template.errors.length) { if (input.template.errors && input.template.errors.length) {
input.template.errors.map((error: Error) => this.error(error)) input.template.errors.map((error: Error) => this.error(error))
Expand All @@ -257,9 +283,10 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
'script' 'script'
)}' )}'
export default script export default script
` `,
map: { mappings: '' },
} }
: { code: '' } : { code: '', map: { mappings: '' } }


if (shouldExtractCss) { if (shouldExtractCss) {
input.styles = input.styles input.styles = input.styles
Expand All @@ -276,12 +303,14 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
)}'` )}'`


if (style.module || descriptor.styles[index].scoped) { if (style.module || descriptor.styles[index].scoped) {
return { ...style, code: '' } return { ...style, code: '', map: { mappings: '' } }
} }
}) })
.filter(Boolean) .filter(Boolean)
} }


input.script.code = input.script.code.replace(/^\s+/mg, '')

const result = assemble(compiler, filename, input, opts) const result = assemble(compiler, filename, input, opts)


descriptor.customBlocks.forEach((block, index) => { descriptor.customBlocks.forEach((block, index) => {
Expand All @@ -298,8 +327,10 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
)}'` )}'`
}) })


dT(`id: ${filename}\ncode:\n${result.code}\n\nmap:\n${JSON.stringify(result.map, null, 2)}\n`)

return result return result
} }
} },
} }
} }
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -8072,6 +8072,11 @@ source-map@0.6.*, source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
version "0.6.1" version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"


source-map@0.7.3:
version "0.7.3"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==

source-map@^0.4.2, source-map@^0.4.4: source-map@^0.4.2, source-map@^0.4.4:
version "0.4.4" version "0.4.4"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.4.4.tgz#eba4f5da9c0dc999de68032d8b4f76173652036b" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.4.4.tgz#eba4f5da9c0dc999de68032d8b4f76173652036b"
Expand Down

0 comments on commit 3fbc8eb

Please sign in to comment.