@@ -1218,43 +1218,21 @@ export function getFilter(filter) {
12181218 } ;
12191219}
12201220
1221- const GET_SOURCE_FROM_IMPORT_NAME = "___HTML_LOADER_GET_SOURCE_FROM_IMPORT___" ;
1222-
1223- export function getImportCode ( html , loaderContext , imports , options ) {
1221+ export function getImportCode ( html , imports , options ) {
12241222 if ( imports . length === 0 ) {
12251223 return "" ;
12261224 }
12271225
1228- // TODO simplify in the next major release
1229- const getURLRuntime = require . resolve ( "./runtime/getUrl.js" ) ;
1230- const context = loaderContext . context || loaderContext . rootContext ;
1231- const fileURLToHelper =
1232- typeof loaderContext . utils !== "undefined" &&
1233- typeof loaderContext . utils . contextify === "function"
1234- ? loaderContext . utils . contextify ( context , getURLRuntime )
1235- : contextify ( context , getURLRuntime ) ;
1236-
1237- let code = options . esModule
1238- ? `import ${ GET_SOURCE_FROM_IMPORT_NAME } from "${ fileURLToHelper } ";\n`
1239- : `var ${ GET_SOURCE_FROM_IMPORT_NAME } = require("${ fileURLToHelper } ");\n` ;
1226+ let code = "" ;
12401227
12411228 for ( const item of imports ) {
1242- const { format , importName, request } = item ;
1229+ const { importName, request } = item ;
12431230
1244- switch ( format ) {
1245- case "import" :
1246- code += options . esModule
1247- ? `import ${ importName } from ${ JSON . stringify ( request ) } ;\n`
1248- : `var ${ importName } = require(${ JSON . stringify ( request ) } );\n` ;
1249- break ;
1250- case "url" :
1251- default :
1252- code += options . esModule
1253- ? `var ${ importName } = new URL(${ JSON . stringify (
1254- request ,
1255- ) } , import.meta.url);\n`
1256- : `var ${ importName } = require(${ JSON . stringify ( request ) } );\n` ;
1257- }
1231+ code += options . esModule
1232+ ? `var ${ importName } = new URL(${ JSON . stringify (
1233+ request ,
1234+ ) } , import.meta.url);\n`
1235+ : `var ${ importName } = require(${ JSON . stringify ( request ) } );\n` ;
12581236 }
12591237
12601238 return `// Imports\n${ code } ` ;
@@ -1279,44 +1257,55 @@ export function convertToTemplateLiteral(str) {
12791257 return `\`${ escapedString } \`` ;
12801258}
12811259
1282- export function getModuleCode ( html , replacements , options ) {
1260+ const GET_SOURCE_FROM_IMPORT_NAME = "___HTML_LOADER_GET_SOURCE_FROM_IMPORT___" ;
1261+
1262+ export function getModuleCode ( html , replacements , loaderContext , options ) {
12831263 let code = html ;
1284- let replacersCode = "" ;
12851264
12861265 const { isTemplateLiteralSupported } = options ;
12871266
1267+ let needHelperImport = false ;
1268+
12881269 for ( const item of replacements ) {
1289- const { runtime , importName, replacementName, isValueQuoted, hash } = item ;
1270+ const { importName, replacementName, isValueQuoted, hash } = item ;
12901271
1291- if ( typeof runtime === "undefined" || runtime === true ) {
1292- const getUrlOptions = [ ]
1293- . concat ( hash ? [ `hash: ${ JSON . stringify ( hash ) } ` ] : [ ] )
1294- . concat ( isValueQuoted ? [ ] : "maybeNeedQuotes: true" ) ;
1295- const preparedOptions =
1296- getUrlOptions . length > 0 ? `, { ${ getUrlOptions . join ( ", " ) } }` : "" ;
1272+ if ( ! isValueQuoted && ! needHelperImport ) {
1273+ needHelperImport = true ;
1274+ }
12971275
1298- replacersCode += `var ${ replacementName } = ${ GET_SOURCE_FROM_IMPORT_NAME } (${ importName } ${ preparedOptions } );\n` ;
1276+ const name = ! isValueQuoted
1277+ ? `${ GET_SOURCE_FROM_IMPORT_NAME } (${ importName } ${ ! isValueQuoted ? ", true" : "" } )`
1278+ : importName ;
12991279
1300- code = code . replace ( new RegExp ( replacementName , "g" ) , ( ) =>
1301- isTemplateLiteralSupported
1302- ? `\${${ replacementName } }`
1303- : `" + ${ replacementName } + "` ,
1304- ) ;
1305- } else {
1306- code = code . replace ( new RegExp ( replacementName , "g" ) , ( ) =>
1307- isTemplateLiteralSupported
1308- ? `\${${ replacementName } }`
1309- : `" + ${ replacementName } + "` ,
1310- ) ;
1311- }
1280+ code = code . replace ( new RegExp ( replacementName , "g" ) , ( ) =>
1281+ isTemplateLiteralSupported
1282+ ? `\${${ name } }${ typeof hash !== "undefined" ? hash : "" } `
1283+ : `" + ${ name } ${ typeof hash !== "undefined" ? ` + ${ JSON . stringify ( hash ) } ` : "" } + "` ,
1284+ ) ;
13121285 }
13131286
13141287 // Replaces "<script>" or "</script>" to "<" + "script>" or "<" + "/script>".
13151288 code = code . replace ( / < ( \/ ? s c r i p t ) / g, ( _ , s ) =>
13161289 isTemplateLiteralSupported ? `\${"<" + "${ s } "}` : `<" + "${ s } ` ,
13171290 ) ;
13181291
1319- return `// Module\n${ replacersCode } var code = ${ code } ;\n` ;
1292+ code = `// Module\nvar code = ${ code } ;\n` ;
1293+
1294+ if ( needHelperImport ) {
1295+ // TODO simplify in the next major release
1296+ const getURLRuntime = require . resolve ( "./runtime/getUrl.js" ) ;
1297+ const context = loaderContext . context || loaderContext . rootContext ;
1298+ const fileURLToHelper =
1299+ typeof loaderContext . utils !== "undefined" &&
1300+ typeof loaderContext . utils . contextify === "function"
1301+ ? loaderContext . utils . contextify ( context , getURLRuntime )
1302+ : contextify ( context , getURLRuntime ) ;
1303+ code = options . esModule
1304+ ? `import ${ GET_SOURCE_FROM_IMPORT_NAME } from "${ fileURLToHelper } ";\n${ code } `
1305+ : `var ${ GET_SOURCE_FROM_IMPORT_NAME } = require("${ fileURLToHelper } ");\n${ code } ` ;
1306+ }
1307+
1308+ return code ;
13201309}
13211310
13221311export function getExportCode ( html , options ) {
0 commit comments