@@ -294,10 +294,18 @@ async function preprocessHTML(html) {
294294 * @return input JS object
295295 */
296296async function preprocessJS ( js ) {
297- var _jsText = js . reduce ( ( accu , cv ) => {
298- return accu + cv . txt ;
299- } , '' ) ;
300- // TODO: import external js file via 'src' attribute
297+
298+ // get content from last script tag
299+ var el = js [ js . length - 1 ] ;
300+ var _jsText = await new Promise ( ( resolve , reject ) => {
301+ if ( el . dom . hasAttribute ( 'src' ) ) {
302+ let path = el . dom . getAttribute ( 'src' ) ;
303+ getContent ( resolveUrl ( path ) , ( content ) => resolve ( content ) ) ;
304+ } else {
305+ resolve ( '' ) ;
306+ }
307+ } ) ;
308+ _jsText += '\n' + el . txt ;
301309
302310 const re = / i m p o r t .+ f r o m [ ' " ` ] ( .* \. v u e ) [ " ' ` ] / g
303311
@@ -354,31 +362,30 @@ async function transpileSFC(sfc_src) {
354362
355363 // add .template to module export
356364 let sfc_code = sfc_obj . js . jsText ;
357- let sfc_var = `sfc_${ genScopeId ( ) . substr ( 7 ) } ` ;
358- sfc_code = sfc_code . replace ( / e x p o r t \W + d e f a u l t / i, `let ${ sfc_var } =` ) ;
365+ sfc_code = sfc_code . replace ( / e x p o r t \W + d e f a u l t / i, `let opts=` ) ;
359366
360367 if ( sfc_obj . css . scopeId ) {
361- sfc_code += `${ sfc_var } ._scopeId = \`${ sfc_obj . css . scopeId } \`;\n` ;
368+ sfc_code += `opts ._scopeId = \`${ sfc_obj . css . scopeId } \`;\n` ;
362369 }
363370
364371 sfc_code += [
365- `${ sfc_var } .template = \`${ sfc_obj . html . templateText } \`;` ,
372+ `opts .template = \`${ sfc_obj . html . templateText } \`;` ,
366373
367374 // inject css tag
368375 `let dom = document.createElement('style');` ,
369376 `dom.innerHTML = \`${ sfc_obj . css . cssText } \`;` ,
370377 `document.body.appendChild(dom);` ,
371378
372379 // bind css dom to Vue instance
373- `let _beforeCreate = ${ sfc_var } .beforeCreate;` ,
374- `${ sfc_var } .beforeCreate = function() {` ,
380+ `let _beforeCreate = opts .beforeCreate;` ,
381+ `opts .beforeCreate = function() {` ,
375382 ` this.$cssDom = dom;` ,
376383 ` if (_beforeCreate) { _beforeCreate(); }` ,
377384 `}` ,
378385
379386 // remove css dom when Vue instance is destroyed
380- `let _destroyed = ${ sfc_var } .destroyed;` ,
381- `${ sfc_var } .destroyed = function() {` ,
387+ `let _destroyed = opts .destroyed;` ,
388+ `opts .destroyed = function() {` ,
382389 ` this.$cssDom = dom;` ,
383390 ` if (this.$cssDom) {` ,
384391 ` this.$cssDom.remove();` ,
@@ -388,7 +395,7 @@ async function transpileSFC(sfc_src) {
388395 `};` ,
389396 ] . join ( '\n' ) ;
390397
391- sfc_code += `export default ${ sfc_var } ;\n` ;
398+ sfc_code += `export default opts ;\n` ;
392399
393400 return [ sfc_code , sfc_obj ] ;
394401}
0 commit comments