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

chore(scripts): remove cdn from webpack configs #4873

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
16 changes: 0 additions & 16 deletions tools/scripts-config-react-webpack/config/inject.js

This file was deleted.

148 changes: 13 additions & 135 deletions tools/scripts-config-react-webpack/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ const { DuplicatesPlugin } = require('inspectpack/plugin');
const SentryWebpackPlugin = require('@sentry/webpack-plugin');
const ReactCMFWebpackPlugin = require('@talend/react-cmf-webpack-plugin');

const cdn = require('@talend/scripts-config-cdn');
const utils = require('@talend/scripts-utils');
const LICENSE_BANNER = require('./licence');
const inject = require('./inject');
const icons = require('./icons');
const AppLoader = require('./loader');
const {
Expand All @@ -30,19 +28,11 @@ const {
getFileNameForExtension,
} = require('./webpack.config.common');

const INITIATOR_URL = process.env.INITIATOR_URL || '@@INITIATOR_URL@@';
const cdnMode = !!process.env.INITIATOR_URL;

const DEFAULT_INDEX_TEMPLATE_PATH = 'src/app/index.html';
const BASE_TEMPLATE_PATH = path.join(__dirname, 'index.tpl.html');

const TALEND_LIB_PREFIX = '@talend/';

const BASENAME = process.env.BASENAME || '/';

// set @talend packages in module-to-cdn
cdn.configureTalendModules();

// Check if Typescript is setup
const useTypescript = utils.fs.tsConfig();

Expand All @@ -59,113 +49,33 @@ function getGitRevision() {
return revision;
}

function getTalendVersions() {
const talendLibraries = {};
const packagelockPath = path.join(process.cwd(), 'package-lock.json');
const yarnlockPath = path.join(process.cwd(), 'yarn.lock');
// eslint-disable-next-line
const packageJson = require(path.join(process.cwd(), 'package.json'));

const talendDependencies = Object.keys(packageJson.dependencies).filter(dependency =>
dependency.includes(TALEND_LIB_PREFIX),
);

if (fs.existsSync(yarnlockPath)) {
const data = fs.readFileSync(yarnlockPath, 'utf-8');
const lock = yarnlock.parse(data);

Object.keys(lock.object)
.filter(k => k.startsWith(TALEND_LIB_PREFIX))
.reduce((acc, key) => {
// @talend/[email protected]
const name = `${TALEND_LIB_PREFIX}${key.split('/')[1].split('@')[0]}`;
if (talendDependencies.includes(name)) {
const info = lock.object[key];
acc[name] = info.version;
}
return acc;
}, talendLibraries);
} else if (fs.existsSync(packagelockPath)) {
// eslint-disable-next-line
const packageLock = require(packagelockPath);

Object.keys(packageLock.packages || packageLock.dependencies)
.filter(k => k.includes(TALEND_LIB_PREFIX))
.reduce((acc, key) => {
const name = `${TALEND_LIB_PREFIX}${key.split(TALEND_LIB_PREFIX)[1]}`;
if (talendDependencies.includes(name)) {
acc[name] = (packageLock.packages || packageLock.dependencies)[key].version;
}
return acc;
}, talendLibraries);
}
// eslint-disable-next-line import/no-dynamic-require
const packageJson = require(path.join(process.cwd(), 'package.json'));

let revision = process.env.GIT_COMMIT;
if (!revision) {
try {
revision = childProcess.execSync('git rev-parse HEAD').toString().trim();
} catch (e) {
// eslint-disable-next-line no-console
console.info('Failed to get git revision');
}
}

return {
version: packageJson.version,
talendLibraries: Object.entries(talendLibraries).map(([name, version]) => ({ name, version })),
revision,
};
}

function getVersions() {
const talendLibraries = cdn
.getModulesFromLockFile()
.filter(Boolean)
.map(info => ({ version: info.version, name: info.name }));
// eslint-disable-next-line
const packageJson = require(path.join(process.cwd(), 'package.json'));

return {
version: packageJson.version,
talendLibraries,
revision: getGitRevision(),
};
}

const VERSIONS = getVersions();
// meta for html webpack plugin
const meta = VERSIONS.talendLibraries.reduce(
(acc, lib) => {
acc[lib.name] = lib.version;
return acc;
},
{
'app-version': VERSIONS.version,
'app-revision': VERSIONS.revision,
},
);
const meta = {
'app-version': packageJson.version,
'app-revision': getGitRevision(),
};

function renderMeta() {
return Object.keys(meta)
.map(key => `<meta name="${key}" content="${meta[key]}" />`)
.join('\n');
}

function getCopyConfig(env, userCopyConfig = [], noDynamicCdn) {
function getCopyConfig(env, userCopyConfig = []) {
const config = [...userCopyConfig];
const assetsOverridden = config.some(nextAsset =>
typeof nextAsset === 'object' ? nextAsset.from === 'src/assets' : nextAsset === 'src/assets',
);
if (!assetsOverridden && fs.existsSync(path.join(process.cwd(), 'src/assets'))) {
config.push({ from: 'src/assets' });
}
if (!cdnMode && !noDynamicCdn) {
cdn.getCopyConfig().forEach(c => config.push(c));
}
return config;
}

async function getIndexTemplate(env, mode, indexTemplatePath, useInitiator = true) {
async function getIndexTemplate(env, mode, indexTemplatePath) {
const headPath = path.join(process.cwd(), '.talend', 'head.html');
const headExists = await utils.fs.isFile(headPath);

Expand All @@ -175,33 +85,10 @@ async function getIndexTemplate(env, mode, indexTemplatePath, useInitiator = tru
console.log('custom head.html found');
customHead = await fs.promises.readFile(headPath);
}
/*
* The following header is used to inject.
* Some UMD are not built correctly for browser.
* For example react-is index.js includes a test on process.env.NODE_ENV to require the min version or not.
* Let's bypass this issue by setting a process.env.NODE_ENV
*/
let headScript = '';
if (useInitiator) {
// meta are not injected if inject is false
headScript = `${renderMeta()}<base href="${BASENAME}" />
<script type="text/javascript">
window.basename = '${BASENAME}';
var process = { browser: true, env: { NODE_ENV: '${mode}' } };
var TALEND_CDN_VERSIONS = {
TUI: '@@TALEND_UI_VERSION@@', ${process.env.CUSTOM_VERSIONS || ''}
};
window.TALEND_INITIATOR_URL = '${INITIATOR_URL}';
window.jsFiles = [<%= htmlWebpackPlugin.files.js.map(href => '"'+href+'"').join(',') %>];
window.cssFiles = [<%= htmlWebpackPlugin.files.css.map(href => '"'+href+'"').join(',') %>];
window.Talend = { build: <%= JSON.stringify(htmlWebpackPlugin.files.jsMetadata || [])%>, cssBuild: <%= JSON.stringify(htmlWebpackPlugin.files.cssMetadata || [])%> };
${await inject.getMinified()}
</script>`;
}
const header = `${customHead}
<link rel="icon" type="image/svg+xml" href="<%= htmlWebpackPlugin.options.favicon || htmlWebpackPlugin.options.b64favicon %>">
<style><%= htmlWebpackPlugin.options.appLoaderStyle %></style>
${headScript}
<base href="${BASENAME}" />
</head>`;
// fs.exists is deprecated
const templateExists = await utils.fs.isFile(indexTemplatePath);
Expand Down Expand Up @@ -234,7 +121,6 @@ module.exports = ({ getUserConfig, mode }) => {
const userSassData = getUserConfig('sass', {});
const userCopyConfig = getUserConfig('copy', []);
const cmf = getUserConfig('cmf');
const dcwpConfig = getUserConfig('dynamic-cdn-webpack-plugin');
const sentryConfig = getUserConfig('sentry', {});
const { theme } = userSassData;

Expand All @@ -248,12 +134,7 @@ module.exports = ({ getUserConfig, mode }) => {

meta['app-id'] = userHtmlConfig.appId || theme;

const indexTemplate = await getIndexTemplate(
env,
mode,
indexTemplatePath,
dcwpConfig !== false,
);
const indexTemplate = await getIndexTemplate(env, mode, indexTemplatePath);

const isEnvDevelopment = mode === 'development';
const isEnvProduction = mode === 'production';
Expand Down Expand Up @@ -319,7 +200,6 @@ module.exports = ({ getUserConfig, mode }) => {
isEnvDevelopment && !!env.analyze && new DuplicatesPlugin(),
new webpack.DefinePlugin({
BUILD_TIMESTAMP: Date.now(),
TALEND_APP_INFO: JSON.stringify(getTalendVersions()),
'process.env.ICON_BUNDLE': JSON.stringify(process.env.ICON_BUNDLE),
'process.env.FORM_MOZ': JSON.stringify(process.env.FORM_MOZ),
'process.env.DISABLE_JS_ERROR_NOTIFICATION': JSON.stringify(
Expand All @@ -343,23 +223,21 @@ module.exports = ({ getUserConfig, mode }) => {
// see https://docs.sentry.io/platforms/node/guides/aws-lambda/sourcemaps/uploading/webpack/
org: sentryConfig.org || process.env.SENTRY_ORG || 'talend-0u',
project: sentryConfig.project || process.env.SENTRY_PROJECT,
release: `${meta['app-id']}@${VERSIONS.version}`,
release: `${meta['app-id']}@${packageJson.version}`,
include: sentryConfig.include || ['dist/'],
ignore: sentryConfig.ignore || ['cdn/'],
ignore: sentryConfig.ignore,
}),
new HtmlWebpackPlugin({
filename: './index.html',
appLoader: AppLoader.APP_LOADER,
appLoaderStyle: AppLoader.getLoaderStyle(appLoaderIcon),
...userHtmlConfig,
b64favicon,
inject: dcwpConfig === false,
template: indexTemplate,
meta: { ...meta, ...(userHtmlConfig.meta || {}) },
}),
cdn.getWebpackPlugin(env, dcwpConfig),
new CopyWebpackPlugin({
patterns: getCopyConfig(env, userCopyConfig, dcwpConfig === false),
patterns: getCopyConfig(env, userCopyConfig),
}),
new webpack.BannerPlugin({ banner: LICENSE_BANNER, entryOnly: true }),
cmf && new ReactCMFWebpackPlugin({ watch: isEnvDevelopment }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const CopyWebpackPlugin = require('copy-webpack-plugin');
const { DuplicatesPlugin } = require('inspectpack/plugin');
const CircularDependencyPlugin = require('circular-dependency-plugin');

const cdn = require('@talend/scripts-config-cdn');

const utils = require('@talend/scripts-utils');

const {
Expand All @@ -17,10 +15,7 @@ const {
getAssetsRules,
} = require('./webpack.config.common');

cdn.configureTalendModules();

module.exports = options => {
const dcwpConfig = options.getUserConfig('dynamic-cdn-webpack-plugin');
const cssModulesEnabled = options.getUserConfig(['css', 'modules'], true);
const userCopyConfig = options.getUserConfig('copy', []);
const useTypescript = utils.fs.tsConfig();
Expand Down Expand Up @@ -87,7 +82,6 @@ module.exports = options => {
filename: isEnvProd ? `${name}.min.css` : `${name}.css`,
chunkFilename: isEnvProd ? `${name}.min.css` : `${name}.css`,
}),
cdn.getWebpackPlugin(env, dcwpConfig),
userCopyConfig.length > 0 && new CopyWebpackPlugin({ patterns: userCopyConfig }),
].filter(Boolean),
};
Expand Down
66 changes: 0 additions & 66 deletions tools/scripts-config-react-webpack/src/inject.js

This file was deleted.

Loading