Skip to content

Commit

Permalink
support absolute paths for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
cjtantay committed Jul 1, 2024
1 parent 9f51a66 commit 3faf07b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
44 changes: 33 additions & 11 deletions lib/blocks/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// native
import debug from 'debug';
import { resolve } from 'path';
import { normalizeUrlPaths } from '../utils.js';
import { isProductionEnv } from '../env.js';

const logger = debug('baker:blocks:script');

Expand All @@ -10,9 +12,13 @@ export function createScriptBlock(pathPrefix, source) {
logger("script block", entry, shouldPreload);

const { manifest } = source;
const { basePath, modern, css, preloads, legacy } = manifest;
const pathSrc = !!basePath
? [basePath, normalizeUrlPaths(pathPrefix)].join('/')
: pathPrefix;

const modernEntry = manifest.modern[entry];
const cssEntry = manifest.css[entry];
const modernEntry = modern[entry];
const cssEntry = css[entry];

const output = [];

Expand All @@ -23,10 +29,10 @@ export function createScriptBlock(pathPrefix, source) {
}

if (shouldPreload) {
manifest.preloads.forEach((preload) => {
preloads.forEach((preload) => {
output.push(
`<link rel="preload" href="${resolve(
pathPrefix,
`<link rel="preload" href="${determineOutput(
pathSrc,
preload
)}" as="script" crossorigin>`
);
Expand All @@ -35,22 +41,22 @@ export function createScriptBlock(pathPrefix, source) {

if (cssEntry) {
output.push(
`<link rel="stylesheet" href="${resolve(pathPrefix, cssEntry)}">`
`<link rel="stylesheet" href="${determineOutput(pathSrc, cssEntry)}">`
);
}

output.push(
`<script type="module" src="${resolve(
pathPrefix,
`<script type="module" src="${determineOutput(
pathSrc,
modernEntry
)}"></script>`
);

if ('legacy' in manifest) {
output.push(
`<script nomodule defer src="${resolve(
pathPrefix,
manifest.legacy[entry]
`<script nomodule defer src="${determineOutput(
pathSrc,
legacy[entry]
)}"></script>`
);
}
Expand All @@ -60,3 +66,19 @@ export function createScriptBlock(pathPrefix, source) {
};
}

/**
*
* @param {string} pathSrc
* @param {string} srcType
* @returns {string}
*/
function determineOutput(pathSrc, srcType) {
if (
isProductionEnv &&
process.env.BAKER_BASE_PATH &&
process.env.BAKER_PATH_PREFIX
) {
return [pathSrc, srcType].join('/');
}
return resolve(pathSrc, srcType);
}
5 changes: 5 additions & 0 deletions lib/engines/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { polyfillsDynamicImport } from '../paths.js';
import * as svelteConfig from '../../svelte.config.js';

import { createRequire } from 'module';
import { normalizeUrlPaths } from '../utils.js';
const require = createRequire(import.meta.url);

export class RollupEngine extends BaseEngine {
Expand Down Expand Up @@ -311,6 +312,10 @@ export class RollupEngine extends BaseEngine {
if (isProductionEnv) {
// if we're in production, build the legacy build too
this.manifest.legacy = {};
this.manifest.basePath =
process.env.BAKER_BASE_PATH &&
process.env.BAKER_PATH_PREFIX &&
normalizeUrlPaths(this.domain);

for (const entrypoint of entrypoints) {
const legacyInput = this.generateLegacyInput(entrypoint);
Expand Down

0 comments on commit 3faf07b

Please sign in to comment.