Skip to content

Commit

Permalink
feat: 1. absolute path
Browse files Browse the repository at this point in the history
2.build entryFileNames index
3.add headless script name buyerPortal
  • Loading branch information
kris-liu-smile authored and CarlLiu2023 committed Sep 8, 2023
1 parent 5c72e02 commit 589a5e5
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
81 changes: 81 additions & 0 deletions apps/storefront/src/buyerPortal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// const str = '<script>
// window.b3CheckoutConfig = {
// routes: {
// dashboard: '/account.php?action=order_status'
// }
// }
// window.B3 = {
// setting: {
// 'store_hash': `{{settings.store_hash}}`,
// 'b2b_url': 'https://staging-v2.bundleb2b.net',
// 'captcha_setkey': '6LdGN_sgAAAAAGYFg1lmVoakQ8QXxbhWqZ1GpYaJ',
// },
// 'dom.checkoutRegisterParentElement': '#checkout-app',
// 'dom.registerElement': '[href^="/login.php"], #checkout-customer-login',
// 'dom.openB3Checkout': 'checkout-customer-continue',
// 'before_login_goto_page': '/account.php?action=order_status',
// 'checkout_super_clear_session': 'true',
// 'dom.navUserLoginElement': '.navUser-item.navUser-item--account',

// }
// </script>

// <script crossorigin src="https://cdn.bundleb2b.net/b2b/staging/storefront/polyfills-legacy.158d896b.js"></script>
// <script crossorigin src="https://cdn.bundleb2b.net/b2b/staging/storefront/index-legacy.40470c56.js"></script>
// '

function init() {
const insertScript = (scriptString: string) => {
// const scriptElement = document.createElement('script');
// scriptElement.innerHTML = scriptString;

// const {body} = document;

// body.appendChild(scriptElement);

document.body.innerHTML += scriptString
}
async function getScriptContent(originurl: string) {
console.log(originurl)
const xxx = `<script>console.log('test123')</script>
<script>console.log('test456')</script>
`

insertScript(xxx)
// const queryParams = new URLSearchParams({
// url: originurl,
// });

// const url = `https://api.example.com/data?${queryParams}`;
// fetch(url).then(response => {
// if (!response.ok) {
// throw new Error('Network response was not ok');
// }
// return response.json();
// })
// .then(data => {
// console.log(data);

// insertScript(data)
// })
// .catch(error => {
// console.error('There was a problem with the fetch operation:', error);
// });
}

async function analyzeScript() {
try {
const { origin } = window.location

await getScriptContent(origin)
} catch (error) {
console.error('Interface error')
}
}

analyzeScript()
}

init()

export {}
44 changes: 44 additions & 0 deletions apps/storefront/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import path from 'path' // eslint-disable-line
import { visualizer } from 'rollup-plugin-visualizer'
import { defineConfig, loadEnv } from 'vite'

interface AssetsAbsolutePathProps {
[key: string]: string
}

const assetsAbsolutePath: AssetsAbsolutePathProps = {
staging: 'https://cdn.bundleb2b.net/b2b/staging/storefront/assets/',
production: 'https://cdn.bundleb2b.net/b2b/production/storefront/assets/',
}

export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd())
return {
Expand All @@ -15,6 +24,23 @@ export default defineConfig(({ mode }) => {
}),
react(),
],
experimental: {
renderBuiltUrl(
filename: string,
{
type,
}: {
type: 'public' | 'asset'
}
) {
if (type === 'asset') {
const name = filename.split('assets/')[1]
return `${assetsAbsolutePath[mode]}${name}`
}

return undefined
},
},
server: {
port: 3001,
proxy: {
Expand All @@ -38,6 +64,24 @@ export default defineConfig(({ mode }) => {
build: {
minify: true,
rollupOptions: {
input: {
index: 'src/main.ts',
buyerPortal: 'src/buyerPortal.ts',
},
output: {
entryFileNames(info) {
const { name } = info
return name.includes('buyerPortal')
? '[name].js'
: '[name].[hash].js'
},
},
onwarn(warning, warn) {
if (warning.code === 'MODULE_LEVEL_DIRECTIVE') {
return
}
warn(warning)
},
manualChunks: {
intl: ['react-intl'],
redux: ['react-redux'],
Expand Down

0 comments on commit 589a5e5

Please sign in to comment.