Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic cdn support #301

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions src/helper/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,31 @@ import webpack from 'webpack';
import createBaseWebpackConfig from '../helper/theme.react.config';
import fs from 'fs';
import rimraf from 'rimraf';
import Logger from '../lib/Logger';

export const THEME_ENTRY_FILE = path.join('theme', 'index.js');
export const VUE_THEME_ENTRY_FILE = path.join("..",'theme', 'index.js');
export const DEV_VUE_THEME_ENTRY_FILE = path.join('theme', 'index.js');

export const CDN_ENTRY_FILE = path.join('.fdk', 'cdn_index.js');

export const dynamicCDNScript = ({assetNormalizedBasePath,vueJs }) => {
return `function getCDNurl() {
\n let cdnUrl;
\n try{
\n if(fynd_platform_cdn) {
\n cdnUrl = fynd_platform_cdn
\n } else {
\n throw new Error("undefiend variable")}
\n } catch(error){
\n cdnUrl = '${assetNormalizedBasePath}'}
\n return cdnUrl;
\n }
\n __webpack_public_path__ = getCDNurl() ${vueJs ? `\n
import bundle from '${VUE_THEME_ENTRY_FILE}'
\n export default bundle;
`
: ''}`
}

export function build({
buildFolder,
Expand All @@ -23,13 +46,24 @@ export function build({
'bin',
'vue-cli-service.js',
);
const spinner = new Spinner('Building assets using vue-cli-service');

fs.stat(CDN_ENTRY_FILE, function (err, stat) {
if (err == null) {
//deleting file if exist
fs.unlink(CDN_ENTRY_FILE, function (err) {
if (err) return console.log(err);
Logger.debug(' \n Existing file deleted successfully');
});
}
fs.appendFileSync(CDN_ENTRY_FILE, dynamicCDNScript({ assetNormalizedBasePath:(imageCdnUrl|| assetCdnUrl),vueJs: true }));
});
const spinner = new Spinner('Building assets using vue-cli-service');
return new Promise((resolve, reject) => {
spinner.start();
const isNodeVersionIsGreaterThan18 =
+process.version.split('.')[0].slice(1) >= 18;
let b = exec(
`node ${VUE_CLI_PATH} build --target lib --dest ${buildFolder} --name themeBundle --filename ${assetHash}_themeBundle ${THEME_ENTRY_FILE}`,
`node ${VUE_CLI_PATH} build --target lib --dest ${buildFolder} --name themeBundle --filename ${assetHash}_themeBundle ${CDN_ENTRY_FILE}`,
{
cwd: process.cwd(),
env: {
Expand All @@ -53,6 +87,10 @@ export function build({
b.stdout.pipe(process.stdout);
b.stderr.pipe(process.stderr);
b.on('exit', function (code) {
fs.unlink(CDN_ENTRY_FILE, function (err) {
if (err) return console.log(err);
Logger.debug(' \n Existing file deleted successfully');
});
if (!code) {
spinner.succeed();
return resolve(true);
Expand Down Expand Up @@ -91,7 +129,7 @@ export function devBuild({ buildFolder, imageCdnUrl, isProd }: DevBuild) {

return new Promise((resolve, reject) => {
let b = exec(
`node ${VUE_CLI_PATH} build --target lib --dest ${buildFolder} --name themeBundle ${THEME_ENTRY_FILE}`,
`node ${VUE_CLI_PATH} build --target lib --dest ${buildFolder} --name themeBundle ${DEV_VUE_THEME_ENTRY_FILE}`,
{
cwd: process.cwd(),
env: {
Expand Down Expand Up @@ -159,10 +197,37 @@ export async function devReactBuild({
ctx,
webpackConfigFromTheme,
);
const assetNormalizedBasePath =
assetBasePath[assetBasePath.length - 1] === '/'
? assetBasePath
: assetBasePath + '/';
return new Promise((resolve, reject) => {

if(!runOnLocal) {
fs.stat(CDN_ENTRY_FILE, function (err, stat) {
if (err == null) {
//deleting file if exist
fs.unlink(CDN_ENTRY_FILE, function (err) {
if (err) return console.log(err);
Logger.debug(' \n Existing file deleted successfully');
});
}
fs.appendFileSync(CDN_ENTRY_FILE, dynamicCDNScript({assetNormalizedBasePath, vueJs: false }));

});
}

webpack(baseWebpackConfig, (err, stats) => {
console.log(err);
console.log(stats.toString());

if(!runOnLocal) {
fs.unlink(CDN_ENTRY_FILE, function (err) {
if (err) return console.log(err);
Logger.debug(' \n file deleted successfully');
});
}

if (err || stats.hasErrors()) {
reject();
}
Expand Down
10 changes: 6 additions & 4 deletions src/helper/theme.react.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import webpack, { Configuration } from 'webpack';
import fs from 'fs';
import { mergeWithRules, merge } from 'webpack-merge';
import { getLocalBaseUrl } from './serve.utils';
import { CDN_ENTRY_FILE } from './build';
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');

const context = process.cwd();
// const themeConfigPath = path.join(context, 'webpack.config.js');
// const isWebpackExtendedInTheme = fs.existsSync(themeConfigPath);
Expand Down Expand Up @@ -47,7 +47,9 @@ const baseConfig = (ctx) => {
require.resolve('webpack-hot-middleware/client'),
path.resolve(context, 'theme/index.jsx'),
]
: [path.resolve(context, 'theme/index.jsx')],
: [
path.resolve(context, CDN_ENTRY_FILE),
path.resolve(context, 'theme/index.jsx')],
},
devtool: isLocal ? 'source-map' : false,
optimization: {
Expand Down Expand Up @@ -198,8 +200,8 @@ const baseConfig = (ctx) => {
output: {
path: buildPath,
filename: isLocal
? 'themeBundle.umd.js'
: 'themeBundle.[contenthash].umd.js',
? `themeBundle.umd.js`
: `themeBundle.[contenthash].umd.js`,
publicPath: isLocal ? localBasePath : assetNormalizedBasePath,
chunkFilename: isLocal
? '[name].themeBundle.umd.js'
Expand Down
7 changes: 3 additions & 4 deletions src/lib/Theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import ThemeService from './api/services/theme.service';
import UploadService from './api/services/upload.service';
import ExtensionService from './api/services/extension.service';
import {
THEME_ENTRY_FILE,
DEV_VUE_THEME_ENTRY_FILE,
build,
devBuild,
devReactBuild,
Expand Down Expand Up @@ -766,7 +766,7 @@ export default class Theme {
spaces: 2,
},
);
if (!fs.existsSync(path.join(process.cwd(), THEME_ENTRY_FILE))) {
if (!fs.existsSync(path.join(process.cwd(), DEV_VUE_THEME_ENTRY_FILE))) {
Logger.info('Restructuring folder structure');
let restructureSpinner = new Spinner(
'Restructuring folder structure',
Expand Down Expand Up @@ -839,7 +839,6 @@ export default class Theme {

const imageCdnUrl = await Theme.getImageCdnBaseUrl();
const assetBasePath = await Theme.getAssetCdnBaseUrl();

Logger.info('Building Theme for Production...');
await devReactBuild({
buildFolder: Theme.BUILD_FOLDER,
Expand All @@ -851,7 +850,6 @@ export default class Theme {

Logger.info('Uploading theme assets/images');
await Theme.assetsImageUploader();

Logger.info('Uploading theme assets/fonts');
await Theme.assetsFontsUploader();

Expand Down Expand Up @@ -1585,6 +1583,7 @@ export default class Theme {
);
}


private static assetsImageUploader = async () => {
try {
const cwd = path.resolve(
Expand Down
Loading