Skip to content

Commit 3fbc8eb

Browse files
committed
fix: Use rollup's resolveId instead of require.resolve to resolve internal modules
1 parent 7530dfa commit 3fbc8eb

File tree

3 files changed

+67
-30
lines changed

3 files changed

+67
-30
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"hash-sum": "^1.0.2",
5252
"querystring": "^0.2.0",
5353
"rollup-pluginutils": "^2.0.1",
54+
"source-map": "0.7.3",
5455
"vue-runtime-helpers": "^0.2.0"
5556
},
5657
"devDependencies": {

src/index.ts

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,34 @@ import {
44
parseVuePartRequest,
55
resolveVuePart,
66
isVuePartRequest,
7-
transformRequireToImport
7+
transformRequireToImport,
88
} from './utils'
99
import {
1010
createDefaultCompiler,
1111
assemble,
1212
ScriptOptions,
1313
StyleOptions,
1414
TemplateOptions,
15-
StyleCompileResult
15+
StyleCompileResult,
1616
} from '@vue/component-compiler'
1717
import { Plugin } from 'rollup'
1818
import * as path from 'path'
1919
import { parse, SFCDescriptor, SFCBlock } from '@vue/component-compiler-utils'
2020
import debug from 'debug'
21-
import { VueTemplateCompiler, VueTemplateCompilerParseOptions } from '@vue/component-compiler-utils/dist/types'
21+
import {
22+
VueTemplateCompiler,
23+
VueTemplateCompilerParseOptions,
24+
} from '@vue/component-compiler-utils/dist/types'
2225

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

30+
const d = debug('rollup-plugin-vue')
31+
const dR = debug('rollup-plugin-vue:resolve')
32+
const dL = debug('rollup-plugin-vue:load')
33+
const dT = debug('rollup-plugin-vue:transform')
34+
2835
export interface VuePluginOptions {
2936
/**
3037
* Include files or directories.
@@ -115,13 +122,16 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
115122
d(`Build environment: ${isProduction ? 'production' : 'development'}`)
116123
d(`Build target: ${process.env.VUE_ENV || 'browser'}`)
117124

118-
if (!opts.normalizer) opts.normalizer = '~' + require.resolve('vue-runtime-helpers/normalize-component.js')
119-
if (!opts.styleInjector) opts.styleInjector = '~' + require.resolve('vue-runtime-helpers/inject-style/browser.js')
120-
if (!opts.styleInjectorSSR) opts.styleInjectorSSR = '~' + require.resolve('vue-runtime-helpers/inject-style/server.js')
125+
if (!opts.normalizer)
126+
opts.normalizer = '~' + 'vue-runtime-helpers/normalize-component.js'
127+
if (!opts.styleInjector)
128+
opts.styleInjector = '~' + 'vue-runtime-helpers/inject-style/browser.js'
129+
if (!opts.styleInjectorSSR)
130+
opts.styleInjectorSSR = '~' + 'vue-runtime-helpers/inject-style/server.js'
121131

122132
createVuePartRequest.defaultLang = {
123133
...createVuePartRequest.defaultLang,
124-
...opts.defaultLang
134+
...opts.defaultLang,
125135
}
126136

127137
const shouldExtractCss = opts.css === false
@@ -144,9 +154,9 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
144154
video: ['src', 'poster'],
145155
source: 'src',
146156
img: 'src',
147-
image: 'xlink:href'
157+
image: 'xlink:href',
148158
},
149-
...opts.template
159+
...opts.template,
150160
} as any
151161
if (opts.template && typeof opts.template.isProduction === 'undefined') {
152162
opts.template.isProduction = isProduction
@@ -160,6 +170,12 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
160170
name: 'VuePlugin',
161171

162172
resolveId(id, importer) {
173+
const request = id
174+
if (id.startsWith('vue-runtime-helpers/')) {
175+
id = require.resolve(id)
176+
dR(`form: ${request} \nto: ${id}\n`)
177+
return id
178+
}
163179
if (!isVuePartRequest(id)) return
164180
id = path.resolve(path.dirname(importer), id)
165181
const ref = parseVuePartRequest(id)
@@ -174,6 +190,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
174190
}
175191
}
176192

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

186203
const element = resolveVuePart(descriptors, request)
187-
const code = 'code' in element
188-
? ((element as any).code as string) // .code is set when extract styles is used. { css: false }
189-
: element.content
204+
const code =
205+
'code' in element
206+
? ((element as any).code as string) // .code is set when extract styles is used. { css: false }
207+
: element.content
190208
const map = element.map as any
191209

210+
dL(`id: ${id}\ncode: \n${code}\nmap: ${JSON.stringify(map, null, 2)}\n\n`)
211+
192212
return { code, map }
193213
},
194214

195215
async transform(source: string, filename: string) {
196216
if (isVue(filename)) {
197-
const descriptor: SFCDescriptor = JSON.parse(JSON.stringify(parse({
198-
filename,
199-
source,
200-
compiler: opts.compiler || templateCompiler,
201-
compilerParseOptions: opts.compilerParseOptions,
202-
sourceRoot: opts.sourceRoot,
203-
needMap: true
204-
})))
217+
const descriptor: SFCDescriptor = JSON.parse(
218+
JSON.stringify(
219+
parse({
220+
filename,
221+
source,
222+
compiler: opts.compiler || templateCompiler,
223+
compilerParseOptions: opts.compilerParseOptions,
224+
sourceRoot: opts.sourceRoot,
225+
needMap: true,
226+
})
227+
)
228+
)
205229

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

213237
const styles = await Promise.all(
214238
descriptor.styles.map(async style => {
215-
const compiled = await compiler.compileStyleAsync(filename, scopeId, style)
239+
const compiled = await compiler.compileStyleAsync(
240+
filename,
241+
scopeId,
242+
style
243+
)
216244
if (compiled.errors.length > 0) throw Error(compiled.errors[0])
217245
return compiled
218246
})
@@ -221,7 +249,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
221249
const input: any = {
222250
scopeId,
223251
styles,
224-
customBlocks: []
252+
customBlocks: [],
225253
}
226254

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

233-
input.template.code = transformRequireToImport(
234-
input.template.code
235-
)
261+
input.template.code = transformRequireToImport(input.template.code)
236262

237263
if (input.template.errors && input.template.errors.length) {
238264
input.template.errors.map((error: Error) => this.error(error))
@@ -257,9 +283,10 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
257283
'script'
258284
)}'
259285
export default script
260-
`
286+
`,
287+
map: { mappings: '' },
261288
}
262-
: { code: '' }
289+
: { code: '', map: { mappings: '' } }
263290

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

278305
if (style.module || descriptor.styles[index].scoped) {
279-
return { ...style, code: '' }
306+
return { ...style, code: '', map: { mappings: '' } }
280307
}
281308
})
282309
.filter(Boolean)
283310
}
284311

312+
input.script.code = input.script.code.replace(/^\s+/mg, '')
313+
285314
const result = assemble(compiler, filename, input, opts)
286315

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

330+
dT(`id: ${filename}\ncode:\n${result.code}\n\nmap:\n${JSON.stringify(result.map, null, 2)}\n`)
331+
301332
return result
302333
}
303-
}
334+
},
304335
}
305336
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8072,6 +8072,11 @@ source-map@0.6.*, source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
80728072
version "0.6.1"
80738073
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
80748074

8075+
source-map@0.7.3:
8076+
version "0.7.3"
8077+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
8078+
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
8079+
80758080
source-map@^0.4.2, source-map@^0.4.4:
80768081
version "0.4.4"
80778082
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.4.4.tgz#eba4f5da9c0dc999de68032d8b4f76173652036b"

0 commit comments

Comments
 (0)