Skip to content

Instantly share code, notes, and snippets.

@mmis1000
Last active June 24, 2022 08:23
Show Gist options
  • Save mmis1000/3a5882636a340df8e67b84099a9364ad to your computer and use it in GitHub Desktop.
Save mmis1000/3a5882636a340df8e67b84099a9364ad to your computer and use it in GitHub Desktop.
Patch to workaround @nuxt/framework#4475
MIT License
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
diff --git a/node_modules/@nuxt/vite-builder/dist/index.mjs b/node_modules/@nuxt/vite-builder/dist/index.mjs
index bbfe012..bcaef55 100644
--- a/node_modules/@nuxt/vite-builder/dist/index.mjs
+++ b/node_modules/@nuxt/vite-builder/dist/index.mjs
@@ -162,6 +162,12 @@ const RelativeAssetPlugin = function() {
};
const VITE_ASSET_RE = /^export default ["'](__VITE_ASSET.*)["']$/;
const DynamicBasePlugin = createUnplugin(function(options = {}) {
+ const queryRE = /\?.*$/s
+ const hashRE = /#.*$/s
+ const cleanUrl = (url) => url.replace(hashRE, '').replace(queryRE, '')
+
+ let conf
+
return {
name: "nuxt:dynamic-base-path",
resolveId(id) {
@@ -174,6 +180,9 @@ const DynamicBasePlugin = createUnplugin(function(options = {}) {
return null;
},
enforce: "post",
+ configResolved (config) {
+ conf = config
+ },
transform(code, id) {
const s = new MagicString(code);
if (options.globalPublicPath && id.includes("paths.mjs") && code.includes("const appConfig = ")) {
@@ -183,8 +192,7 @@ const DynamicBasePlugin = createUnplugin(function(options = {}) {
const assetId = code.match(VITE_ASSET_RE);
if (assetId) {
s.overwrite(0, code.length, [
- "import { buildAssetsURL } from '#build/paths.mjs';",
- `export default buildAssetsURL("${assetId[1]}".replace("/__NUXT_BASE__", ""));`
+ `export default /*#__PURE__*/"${assetId[1]}".replace("/__NUXT_BASE__", "");`
].join("\n"));
}
if (!id.includes("paths.mjs") && code.includes("NUXT_BASE") && !code.includes("import { publicAssetsURL as __publicAssetsURL }")) {
@@ -197,6 +205,57 @@ const DynamicBasePlugin = createUnplugin(function(options = {}) {
s.replace(/from *['"]\/__NUXT_BASE__(\/[^'"]*)['"]/g, 'from "$1"');
const delimiterRE = /(?<!(const base = |from *))(`([^`]*)\/__NUXT_BASE__\/([^`]*)`|'([^\n']*)\/__NUXT_BASE__\/([^\n']*)'|"([^\n"]*)\/__NUXT_BASE__\/([^\n"]*)")/g;
s.replace(delimiterRE, (r) => "`" + r.replace(/\/__NUXT_BASE__\//g, "${__publicAssetsURL()}").slice(1, -1) + "`");
+
+ if (
+ conf.base === '/__NUXT_BASE__/' &&
+ !id.startsWith('virtual:') &&
+ !conf.assetsInclude((cleanUrl(id))) &&
+ code.includes('import')
+ ) {
+ try {
+ const parsed = this.parse(code)
+
+ const bindings = []
+
+ const body = parsed.body
+
+ for (const node of body) {
+ if (node.type === 'ImportDeclaration') {
+ if (conf.assetsInclude((cleanUrl(node.source.value)))) {
+ const name = node.specifiers.find((i) => i.type === 'ImportDefaultSpecifier')
+ if (name) {
+ bindings.push({
+ name: name.local.name,
+ path: node.source.value,
+ start: node.start,
+ end: node.end
+ })
+ }
+ }
+ }
+ }
+
+ if (bindings.length > 0) {
+ const nameSpacePrefix = '_TEMP_RAW_'
+ let nameSpaceId = -1
+ const getNameSpace = () => nameSpaceId === -1 ? nameSpacePrefix : nameSpacePrefix + nameSpaceId + '_'
+ while (code.includes(getNameSpace())) {
+ nameSpaceId++
+ }
+ for (const binding of bindings) {
+ s.overwrite(
+ binding.start,
+ binding.end,
+ `import ${getNameSpace() + binding.name} from ${JSON.stringify(binding.path)};\nconst ${binding.name} = /*#__PURE__*/buildAssetsURL(${getNameSpace() + binding.name});\n`
+ )
+ }
+ if (!code.includes('buildAssetsURL')) {
+ s.prepend('import { buildAssetsURL } from \'#build/paths.mjs\';\n')
+ }
+ }
+ } catch (_err) {}
+ }
+
if (s.hasChanged()) {
return {
code: s.toString(),
diff --git a/node_modules/vue-bundle-renderer/dist/index.mjs b/node_modules/vue-bundle-renderer/dist/index.mjs
index 0636367..56519fd 100644
--- a/node_modules/vue-bundle-renderer/dist/index.mjs
+++ b/node_modules/vue-bundle-renderer/dist/index.mjs
@@ -186,6 +186,7 @@ function getAllDependencies(ids, rendererContext) {
Object.assign(allDeps.styles, deps.styles);
Object.assign(allDeps.preload, deps.preload);
Object.assign(allDeps.prefetch, deps.prefetch);
+ if (!(rendererContext.clientManifest[id]?.isEntry ?? false)) {
for (const dynamicDepId of rendererContext.clientManifest[id]?.dynamicImports || []) {
const dynamicDeps = getModuleDependencies(dynamicDepId, rendererContext);
Object.assign(allDeps.prefetch, dynamicDeps.scripts);
@@ -193,6 +194,7 @@ function getAllDependencies(ids, rendererContext) {
Object.assign(allDeps.prefetch, dynamicDeps.preload);
Object.assign(allDeps.prefetch, dynamicDeps.prefetch);
}
+ }
}
for (const id in allDeps.prefetch) {
if (id in allDeps.preload) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment