Skip to content

Commit

Permalink
Html fix (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
montalvomiguelo authored Mar 11, 2023
1 parent 1c63fd1 commit afda74e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/vite-shopify-example/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/assets
/snippets/vite-tag.liquid
/snippets/vite.liquid
12 changes: 6 additions & 6 deletions examples/vite-shopify-example/layout/theme.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<meta name="description" content="">

{%- liquid
render 'vite-tag' with 'theme.scss', preload_stylesheet: true
render 'vite-tag' with 'theme.ts'
render 'vite-tag' with '@/foo.ts'
render 'vite-tag' with '~/bar.ts'
render 'vite-tag' with '/resources/js/baz.js'
render 'vite-tag' with '@modules/qux.ts'
render 'vite' with 'theme.scss', preload_stylesheet: true
render 'vite' with 'theme.ts'
render 'vite' with '@/foo.ts'
render 'vite' with '~/bar.ts'
render 'vite' with '/resources/js/baz.js'
render 'vite' with '@modules/qux.ts'
-%}

{{ content_for_header }}
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-shopify-example/sections/cart-drawer.liquid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{%- render 'vite-tag' with '@@/cart-drawer.js' -%}
{%- render 'vite' with '@@/cart-drawer.js' -%}
2 changes: 1 addition & 1 deletion examples/vite-shopify-example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineConfig({
plugins: [
basicSsl(),
shopify({
snippetFile: 'vite-tag.liquid',
snippetFile: 'vite.liquid',
additionalEntrypoints: [
'frontend/foo.ts', // relative to sourceCodeDir
'frontend/bar.ts',
Expand Down
10 changes: 6 additions & 4 deletions packages/vite-plugin-shopify/src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function shopifyHTML (options: Required<VitePluginShopifyOptions>
let viteDevServerUrl: DevServerUrl

const viteTagSnippetPath = path.resolve(options.themeRoot, `snippets/${options.snippetFile}`)
const viteTagSnippetName = options.snippetFile.replace(/\.[^.]+$/, '')

return {
name: 'vite-plugin-shopify-html',
Expand All @@ -39,7 +40,7 @@ export default function shopifyHTML (options: Required<VitePluginShopifyOptions>

debug({ address, viteDevServerUrl })

const viteTagSnippetContent = viteTagDisclaimer + viteTagEntryPath(config.resolve.alias, options.entrypointsDir) + viteTagSnippetDev(viteDevServerUrl, options.entrypointsDir)
const viteTagSnippetContent = viteTagDisclaimer + viteTagEntryPath(config.resolve.alias, options.entrypointsDir, viteTagSnippetName) + viteTagSnippetDev(viteDevServerUrl, options.entrypointsDir)

// Write vite-tag snippet for development server
fs.writeFileSync(viteTagSnippetPath, viteTagSnippetContent)
Expand Down Expand Up @@ -126,7 +127,7 @@ export default function shopifyHTML (options: Required<VitePluginShopifyOptions>
}
})

const viteTagSnippetContent = viteTagDisclaimer + viteTagEntryPath(config.resolve.alias, options.entrypointsDir) + assetTags.join('\n') + '\n{% endif %}\n'
const viteTagSnippetContent = viteTagDisclaimer + viteTagEntryPath(config.resolve.alias, options.entrypointsDir, viteTagSnippetName) + assetTags.join('\n') + '\n{% endif %}\n'

// Write vite-tag snippet for production build
fs.writeFileSync(viteTagSnippetPath, viteTagSnippetContent)
Expand All @@ -139,7 +140,8 @@ const viteTagDisclaimer = '{% comment %}\n IMPORTANT: This snippet is automatic
// Generate liquid variable with resolved path by replacing aliases
const viteTagEntryPath = (
resolveAlias: Array<{ find: string | RegExp, replacement: string }>,
entrypointsDir: string
entrypointsDir: string,
snippetName: string
): string => {
const replacements: Array<[string, string]> = []

Expand All @@ -149,7 +151,7 @@ const viteTagEntryPath = (
}
})

return `{% assign path = vite-tag | ${replacements.map(([from, to]) => `replace: '${from}/', '${to}/'`).join(' | ')} %}\n`
return `{% assign path = ${snippetName} | ${replacements.map(([from, to]) => `replace: '${from}/', '${to}/'`).join(' | ')} %}\n`
}

// Generate conditional statement for entry tag
Expand Down

0 comments on commit afda74e

Please sign in to comment.