diff --git a/src/__tests__/fixtures/basic-template-plugin-custom-runtime-id/__snapshots__/browser--test_gpFj.js b/src/__tests__/fixtures/basic-template-plugin-custom-runtime-id/__snapshots__/browser--test_gpFj.js index e81ea79..4cc8ce0 100644 --- a/src/__tests__/fixtures/basic-template-plugin-custom-runtime-id/__snapshots__/browser--test_gpFj.js +++ b/src/__tests__/fixtures/basic-template-plugin-custom-runtime-id/__snapshots__/browser--test_gpFj.js @@ -19,10 +19,7 @@ /***/ (function(module, exports, __webpack_require__) { - if (window.$mwp) { - __webpack_require__.p = $mwp; - } - + if (window.$mwp) __webpack_require__.p = $mwp; __webpack_require__(/*! ./test.marko?dependencies */ "./src/__tests__/fixtures/basic-template-plugin-custom-runtime-id/test.marko?dependencies"); const { init } = __webpack_require__(/*! marko/components */ "marko/components"); diff --git a/src/__tests__/fixtures/basic-template-plugin-custom-runtime-id/__snapshots__/server--main.js b/src/__tests__/fixtures/basic-template-plugin-custom-runtime-id/__snapshots__/server--main.js index 2704a31..a3e0afc 100644 --- a/src/__tests__/fixtures/basic-template-plugin-custom-runtime-id/__snapshots__/server--main.js +++ b/src/__tests__/fixtures/basic-template-plugin-custom-runtime-id/__snapshots__/server--main.js @@ -96,7 +96,7 @@ function renderAssets() { this.end = this.___end; if (assets) { - this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); + __webpack_require__.p && this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); if (assets.js) { const setNonce = nonce && `.setAttribute("nonce", ${JSON.stringify(nonce)})`; diff --git a/src/__tests__/fixtures/basic-template-plugin/__snapshots__/browser--test_uYWJ.js b/src/__tests__/fixtures/basic-template-plugin/__snapshots__/browser--test_uYWJ.js index 1dabe28..527aaf4 100644 --- a/src/__tests__/fixtures/basic-template-plugin/__snapshots__/browser--test_uYWJ.js +++ b/src/__tests__/fixtures/basic-template-plugin/__snapshots__/browser--test_uYWJ.js @@ -19,10 +19,7 @@ /***/ (function(module, exports, __webpack_require__) { - if (window.$mwp) { - __webpack_require__.p = $mwp; - } - + if (window.$mwp) __webpack_require__.p = $mwp; __webpack_require__(/*! ./test.marko?dependencies */ "./src/__tests__/fixtures/basic-template-plugin/test.marko?dependencies"); window.$initComponents && $initComponents(); diff --git a/src/__tests__/fixtures/basic-template-plugin/__snapshots__/server--main.js b/src/__tests__/fixtures/basic-template-plugin/__snapshots__/server--main.js index e11c0f0..abe32a4 100644 --- a/src/__tests__/fixtures/basic-template-plugin/__snapshots__/server--main.js +++ b/src/__tests__/fixtures/basic-template-plugin/__snapshots__/server--main.js @@ -96,7 +96,7 @@ function renderAssets() { this.end = this.___end; if (assets) { - this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); + __webpack_require__.p && this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); if (assets.js) { const setNonce = nonce && `.setAttribute("nonce", ${JSON.stringify(nonce)})`; diff --git a/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/browser--bar_aSxt.js b/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/browser--bar_aSxt.js index 41ce402..64bedbe 100644 --- a/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/browser--bar_aSxt.js +++ b/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/browser--bar_aSxt.js @@ -19,10 +19,7 @@ __webpack_require__(/*! ./components/shared.marko?dependencies */ "./src/__tests /***/ (function(module, exports, __webpack_require__) { - if (window.$mwp) { - __webpack_require__.p = $mwp; - } - + if (window.$mwp) __webpack_require__.p = $mwp; __webpack_require__(/*! ./bar.marko?dependencies */ "./src/__tests__/fixtures/multiple-entries-plugin/bar.marko?dependencies"); window.$initComponents && $initComponents(); diff --git a/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/browser--foo_3XPO.js b/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/browser--foo_3XPO.js index 7d3fca6..1c70011 100644 --- a/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/browser--foo_3XPO.js +++ b/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/browser--foo_3XPO.js @@ -19,10 +19,7 @@ __webpack_require__(/*! ./components/shared.marko?dependencies */ "./src/__tests /***/ (function(module, exports, __webpack_require__) { - if (window.$mwp) { - __webpack_require__.p = $mwp; - } - + if (window.$mwp) __webpack_require__.p = $mwp; __webpack_require__(/*! ./foo.marko?dependencies */ "./src/__tests__/fixtures/multiple-entries-plugin/foo.marko?dependencies"); window.$initComponents && $initComponents(); diff --git a/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/server--main.js b/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/server--main.js index ae3275e..7292306 100644 --- a/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/server--main.js +++ b/src/__tests__/fixtures/multiple-entries-plugin/__snapshots__/server--main.js @@ -85,7 +85,7 @@ function renderAssets() { this.end = this.___end; if (assets) { - this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); + __webpack_require__.p && this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); if (assets.js) { const setNonce = nonce && `.setAttribute("nonce", ${JSON.stringify(nonce)})`; @@ -257,7 +257,7 @@ function renderAssets() { this.end = this.___end; if (assets) { - this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); + __webpack_require__.p && this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); if (assets.js) { const setNonce = nonce && `.setAttribute("nonce", ${JSON.stringify(nonce)})`; diff --git a/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-A--test_YDNP.A.js b/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-A--test_YDNP.A.js index 6fb8333..00fd4dc 100644 --- a/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-A--test_YDNP.A.js +++ b/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-A--test_YDNP.A.js @@ -103,10 +103,7 @@ __webpack_require__(/*! ./components/nested/index.marko?dependencies */ "./src/_ /***/ (function(module, exports, __webpack_require__) { - if (window.$mwp) { - __webpack_require__.p = $mwp; - } - + if (window.$mwp) __webpack_require__.p = $mwp; __webpack_require__(/*! ./test.marko?dependencies */ "./src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/test.marko?dependencies"); window.$initComponents && $initComponents(); diff --git a/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-B--test_YDNP.B.js b/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-B--test_YDNP.B.js index 094e51f..04dc1b9 100644 --- a/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-B--test_YDNP.B.js +++ b/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-B--test_YDNP.B.js @@ -103,10 +103,7 @@ __webpack_require__(/*! ./components/nested/index.marko?dependencies */ "./src/_ /***/ (function(module, exports, __webpack_require__) { - if (window.$mwp) { - __webpack_require__.p = $mwp; - } - + if (window.$mwp) __webpack_require__.p = $mwp; __webpack_require__(/*! ./test.marko?dependencies */ "./src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/test.marko?dependencies"); window.$initComponents && $initComponents(); diff --git a/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-C--test_YDNP.C.js b/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-C--test_YDNP.C.js index 026333b..3df862c 100644 --- a/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-C--test_YDNP.C.js +++ b/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/browser-C--test_YDNP.C.js @@ -103,10 +103,7 @@ __webpack_require__(/*! ./components/nested/index.marko?dependencies */ "./src/_ /***/ (function(module, exports, __webpack_require__) { - if (window.$mwp) { - __webpack_require__.p = $mwp; - } - + if (window.$mwp) __webpack_require__.p = $mwp; __webpack_require__(/*! ./test.marko?dependencies */ "./src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/test.marko?dependencies"); window.$initComponents && $initComponents(); diff --git a/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/server--main.js b/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/server--main.js index 44c0f90..d657929 100644 --- a/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/server--main.js +++ b/src/__tests__/fixtures/with-class-component-plugin-dynamic-bundle/__snapshots__/server--main.js @@ -158,7 +158,7 @@ function renderAssets() { this.end = this.___end; if (assets) { - this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); + __webpack_require__.p && this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); if (assets.js) { const setNonce = nonce && `.setAttribute("nonce", ${JSON.stringify(nonce)})`; diff --git a/src/__tests__/fixtures/with-class-component-plugin/__snapshots__/browser--test_nzzJ.js b/src/__tests__/fixtures/with-class-component-plugin/__snapshots__/browser--test_nzzJ.js index 4ffa4fb..70dd3ec 100644 --- a/src/__tests__/fixtures/with-class-component-plugin/__snapshots__/browser--test_nzzJ.js +++ b/src/__tests__/fixtures/with-class-component-plugin/__snapshots__/browser--test_nzzJ.js @@ -101,10 +101,7 @@ __webpack_require__(/*! ./components/nested/index.marko?dependencies */ "./src/_ /***/ (function(module, exports, __webpack_require__) { - if (window.$mwp) { - __webpack_require__.p = $mwp; - } - + if (window.$mwp) __webpack_require__.p = $mwp; __webpack_require__(/*! ./test.marko?dependencies */ "./src/__tests__/fixtures/with-class-component-plugin/test.marko?dependencies"); window.$initComponents && $initComponents(); diff --git a/src/__tests__/fixtures/with-class-component-plugin/__snapshots__/server--main.js b/src/__tests__/fixtures/with-class-component-plugin/__snapshots__/server--main.js index 14c3d1d..3fc613d 100644 --- a/src/__tests__/fixtures/with-class-component-plugin/__snapshots__/server--main.js +++ b/src/__tests__/fixtures/with-class-component-plugin/__snapshots__/server--main.js @@ -151,7 +151,7 @@ function renderAssets() { this.end = this.___end; if (assets) { - this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); + __webpack_require__.p && this.script(`$mwp=${JSON.stringify(__webpack_require__.p)}`); if (assets.js) { const setNonce = nonce && `.setAttribute("nonce", ${JSON.stringify(nonce)})`; diff --git a/src/__tests__/fixtures/with-public-path/__snapshots__/browser--test_CDVG.js b/src/__tests__/fixtures/with-public-path/__snapshots__/browser--test_CDVG.js new file mode 100644 index 0000000..617418c --- /dev/null +++ b/src/__tests__/fixtures/with-public-path/__snapshots__/browser--test_CDVG.js @@ -0,0 +1,30 @@ +/******/ ({ + +/***/ "./src/__tests__/fixtures/with-public-path/test.marko?dependencies": +/*!*************************************************************************!*\ + !*** ./src/__tests__/fixtures/with-public-path/test.marko?dependencies ***! + \*************************************************************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + + + +/***/ }), + +/***/ "./src/__tests__/fixtures/with-public-path/test.marko?hydrate": +/*!********************************************************************!*\ + !*** ./src/__tests__/fixtures/with-public-path/test.marko?hydrate ***! + \********************************************************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + + + + __webpack_require__(/*! ./test.marko?dependencies */ "./src/__tests__/fixtures/with-public-path/test.marko?dependencies"); + window.$initComponents && $initComponents(); + + + +/***/ }) + +/******/ }); \ No newline at end of file diff --git a/src/__tests__/fixtures/with-public-path/__snapshots__/server--main.js b/src/__tests__/fixtures/with-public-path/__snapshots__/server--main.js new file mode 100644 index 0000000..f3143ca --- /dev/null +++ b/src/__tests__/fixtures/with-public-path/__snapshots__/server--main.js @@ -0,0 +1,235 @@ +/******/ ({ + +/***/ "./__MARKO_WEBPACK__MANIFEST.js": +/*!**************************************!*\ + !*** ./__MARKO_WEBPACK__MANIFEST.js ***! + \**************************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +module.exports = { + getAssets(entry) { + return this.build[entry]; + }, + build: {"test_CDVG":{"js":["test_CDVG.js"]}} +} + +/***/ }), + +/***/ "./src/__tests__/fixtures/with-public-path/server.js": +/*!***********************************************************!*\ + !*** ./src/__tests__/fixtures/with-public-path/server.js ***! + \***********************************************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +const http = __webpack_require__(/*! http */ "http"); +const test = __webpack_require__(/*! ./test.marko */ "./src/__tests__/fixtures/with-public-path/test.marko?assets"); + +http + .createServer((req, res) => { + test.render({}, res); + }) + .listen(0); + + +/***/ }), + +/***/ "./src/__tests__/fixtures/with-public-path/test.marko": +/*!************************************************************!*\ + !*** ./src/__tests__/fixtures/with-public-path/test.marko ***! + \************************************************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; +/* WEBPACK VAR INJECTION */(function(__filename) { + +var marko_template = module.exports = __webpack_require__(/*! marko/dist/html */ "marko/dist/html").t(__filename), + marko_componentType = "/@marko/webpack-tests$x.x.x/fixtures/with-public-path/test.marko", + marko_renderer = __webpack_require__(/*! marko/dist/runtime/components/renderer */ "marko/dist/runtime/components/renderer"); + +function render(input, out, __component, component, state) { + var data = input; + + out.w("

Hello World

"); +} + +marko_template._ = marko_renderer(render, { + d_: true, + e_: marko_componentType + }); + +marko_template.meta = { + id: "/@marko/webpack-tests$x.x.x/fixtures/with-public-path/test.marko" + }; + +/* WEBPACK VAR INJECTION */}.call(this, "/index.js")) + +/***/ }), + +/***/ "./src/__tests__/fixtures/with-public-path/test.marko?assets": +/*!*******************************************************************!*\ + !*** ./src/__tests__/fixtures/with-public-path/test.marko?assets ***! + \*******************************************************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; +/* WEBPACK VAR INJECTION */(function(__filename) { + +var marko_template = module.exports = __webpack_require__(/*! marko/dist/html */ "marko/dist/html").t(__filename), + marko_componentType = "/@marko/webpack-tests$x.x.x/fixtures/with-public-path/test.marko", + marko_renderer = __webpack_require__(/*! marko/dist/runtime/components/renderer */ "marko/dist/runtime/components/renderer"), + template = __webpack_require__(/*! ./test.marko */ "./src/__tests__/fixtures/with-public-path/test.marko"), + module_manifest = __webpack_require__(/*! ./../../../../__MARKO_WEBPACK__MANIFEST.js */ "./__MARKO_WEBPACK__MANIFEST.js"), + manifest = module_manifest.default || module_manifest, + marko_dynamicTag = __webpack_require__(/*! marko/dist/runtime/helpers/dynamic-tag */ "marko/dist/runtime/helpers/dynamic-tag"), + marko_loadTag = __webpack_require__(/*! marko/dist/runtime/helpers/load-tag */ "marko/dist/runtime/helpers/load-tag"), + init_components_tag = marko_loadTag(__webpack_require__(/*! marko/dist/core-tags/components/init-components-tag */ "marko/dist/core-tags/components/init-components-tag")); + +function renderAssets() { + const assets = this.___assets; + const nonce = this.global.cspNonce; + this.___renderAssets = this.___assets = undefined; + this.flush = this.___flush; + this.end = this.___end; + + if (assets) { + + + if (assets.js) { + const setNonce = nonce && `.setAttribute("nonce", ${JSON.stringify(nonce)})`; + this.script( + `(function(b,h){var e=[],c=0;h.forEach(function(d,f){var a=b.createElement("link");a.relList&&a.relList.supports&&a.relList.supports("preload")?(a.href=d,a.rel="preload",a.as="script",a.addEventListener("load",function(){e[f]=d;if(c===f)for(var a;a=e[c];c++){var g=b.createElement("script");g.src=a;${setNonce ? `g${setNonce};` : ""}b.head.appendChild(g)}}),b.head.appendChild(a)):(a=b.createElement("script"),a.src=d,a.defer=!0,${setNonce ? `a${setNonce},` : ""}b.head.appendChild(a))})})(document,${ + JSON.stringify(assets.js.map(js => __webpack_require__.p+js)) + })` + ); + } + + if (assets.css) { + assets.css.forEach(css => { + this.write( + `` + ); + }); + } + } +}; + +function outFlushOverride() { + this.___renderAssets(); + this.flush(); +}; + +function outEndOverride(data, encoding, callback) { + this.___renderAssets(); + this.end(data, encoding, callback); +}; + +function render(input, out, __component, component, state) { + var data = input; + + out.___flush = out.flush; + + out.___end = out.end; + + out.___renderAssets = renderAssets; + + out.___assets = manifest.getAssets("test_CDVG", out.global.buildName); + + out.flush = outFlushOverride; + + out.end = outEndOverride; + + marko_dynamicTag(out, template, function() { + return input; + }, null, null, null, __component, "0"); + + init_components_tag({}, out); +} + +marko_template._ = marko_renderer(render, { + d_: true, + e_: marko_componentType + }); + +marko_template.meta = { + id: "/@marko/webpack-tests$x.x.x/fixtures/with-public-path/test.marko", + tags: [ + "./test.marko", + "marko/dist/core-tags/components/init-components-tag" + ] + }; + +/* WEBPACK VAR INJECTION */}.call(this, "/index.js")) + +/***/ }), + +/***/ "http": +/*!***********************!*\ + !*** external "http" ***! + \***********************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +module.exports = http; + +/***/ }), + +/***/ "marko/dist/core-tags/components/init-components-tag": +/*!**********************************************************************!*\ + !*** external "marko/dist/core-tags/components/init-components-tag" ***! + \**********************************************************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +module.exports = marko/dist/core-tags/components/init-components-tag; + +/***/ }), + +/***/ "marko/dist/html": +/*!**********************************!*\ + !*** external "marko/dist/html" ***! + \**********************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +module.exports = marko/dist/html; + +/***/ }), + +/***/ "marko/dist/runtime/components/renderer": +/*!*********************************************************!*\ + !*** external "marko/dist/runtime/components/renderer" ***! + \*********************************************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +module.exports = marko/dist/runtime/components/renderer; + +/***/ }), + +/***/ "marko/dist/runtime/helpers/dynamic-tag": +/*!*********************************************************!*\ + !*** external "marko/dist/runtime/helpers/dynamic-tag" ***! + \*********************************************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +module.exports = marko/dist/runtime/helpers/dynamic-tag; + +/***/ }), + +/***/ "marko/dist/runtime/helpers/load-tag": +/*!******************************************************!*\ + !*** external "marko/dist/runtime/helpers/load-tag" ***! + \******************************************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +module.exports = marko/dist/runtime/helpers/load-tag; + +/***/ }) + +/******/ }); \ No newline at end of file diff --git a/src/__tests__/fixtures/with-public-path/server.js b/src/__tests__/fixtures/with-public-path/server.js new file mode 100644 index 0000000..6dc9ebc --- /dev/null +++ b/src/__tests__/fixtures/with-public-path/server.js @@ -0,0 +1,8 @@ +const http = require("http"); +const test = require("./test.marko"); + +http + .createServer((req, res) => { + test.render({}, res); + }) + .listen(0); diff --git a/src/__tests__/fixtures/with-public-path/test.marko b/src/__tests__/fixtures/with-public-path/test.marko new file mode 100644 index 0000000..f3e333e --- /dev/null +++ b/src/__tests__/fixtures/with-public-path/test.marko @@ -0,0 +1 @@ +

Hello World

diff --git a/src/__tests__/fixtures/with-public-path/webpack.config.ts b/src/__tests__/fixtures/with-public-path/webpack.config.ts new file mode 100644 index 0000000..91136de --- /dev/null +++ b/src/__tests__/fixtures/with-public-path/webpack.config.ts @@ -0,0 +1,43 @@ +import * as path from "path"; +import * as webpack from "webpack"; +import MarkoPlugin from "../../../plugin"; + +const markoPlugin = new MarkoPlugin(); +const publicPath = "/assets"; + +export default [ + { + name: "server", + target: "async-node", + entry: path.join(__dirname, "server.js"), + output: { publicPath }, + module: { + rules: [ + { + test: /\.marko$/, + loader: "@marko/webpack/loader" + } + ] + }, + plugins: [ + new webpack.DefinePlugin({ + "process.env.BUNDLE": true + }), + markoPlugin.server + ] + }, + { + name: "browser", + target: "web", + output: { publicPath }, + module: { + rules: [ + { + test: /\.marko$/, + loader: "@marko/webpack/loader" + } + ] + }, + plugins: [markoPlugin.browser] + } +]; diff --git a/src/loader/get-asset-code.ts b/src/loader/get-asset-code.ts index 2eaa3d3..5e729e4 100644 --- a/src/loader/get-asset-code.ts +++ b/src/loader/get-asset-code.ts @@ -2,7 +2,11 @@ import * as path from "path"; import moduleName from "../shared/module-name"; import { VIRTUAL_SERVER_MANIFEST_PATH } from "../shared/virtual"; -export default (resourcePath: string, runtimeId: string | undefined): string => ` +export default ( + resourcePath: string, + runtimeId: string | undefined, + publicPath: string | undefined +): string => ` import template from ${JSON.stringify(`./${path.basename(resourcePath)}`)}; import manifest from ${JSON.stringify( `./${path.relative(path.dirname(resourcePath), VIRTUAL_SERVER_MANIFEST_PATH)}` @@ -16,7 +20,11 @@ static function renderAssets() { this.end = this.___end; if (assets) { - this.script(\`$mwp=\${JSON.stringify(__webpack_public_path__)}\`); + ${ + publicPath === undefined + ? "__webpack_public_path__ && this.script(`$mwp=${JSON.stringify(__webpack_public_path__)}`);" + : "" + } if (assets.js) { const setNonce = nonce && \`.setAttribute("nonce", \${JSON.stringify(nonce)})\`; @@ -47,7 +55,7 @@ static function outEndOverride(data, encoding, callback) { this.end(data, encoding, callback); } -${runtimeId === undefined ? "" : `$ out.global.runtimeId = ${runtimeId};` } +${runtimeId === undefined ? "" : `$ out.global.runtimeId = ${runtimeId};`} $ out.___flush = out.flush; $ out.___end = out.end; $ out.___renderAssets = renderAssets; diff --git a/src/loader/index.ts b/src/loader/index.ts index afbf787..cdb8330 100644 --- a/src/loader/index.ts +++ b/src/loader/index.ts @@ -1,7 +1,7 @@ "use strict"; import * as path from "path"; -import { Compiler } from 'webpack'; +import { Compiler } from "webpack"; import * as loaderUtils from "loader-utils"; import ConcatMap from "concat-with-sourcemaps"; import getAssetCode from "./get-asset-code"; @@ -69,6 +69,7 @@ export default function(source: string): string { const target = normalizeTarget( (queryOptions && queryOptions.target) || this.target ); + const publicPath = compiler.options.output.publicPath; const runtimeId = pluginOptions && pluginOptions.runtimeId; // eslint-disable-next-line @typescript-eslint/no-var-requires const markoCompiler = require((queryOptions && queryOptions.compiler) || @@ -113,7 +114,7 @@ export default function(source: string): string { if (assets) { return markoCompiler.compile( - getAssetCode(this.resourcePath, runtimeId), + getAssetCode(this.resourcePath, runtimeId, publicPath), this.resourcePath, { writeToDisk: false, @@ -124,17 +125,19 @@ export default function(source: string): string { ); } else if (hydrate) { return ` - if (window.$mwp) { - __webpack_public_path__ = $mwp; + ${ + publicPath === undefined + ? " if (window.$mwp) __webpack_public_path__ = $mwp;" + : "" } - ${loadStr(`./${path.basename(this.resourcePath)}?dependencies`)} - ${runtimeId - ? ` + ${ + runtimeId + ? ` ${loadStr("marko/components", "{ init }")} init(${runtimeId}); ` - : "window.$initComponents && $initComponents();" + : "window.$initComponents && $initComponents();" } `;