Skip to content

Commit

Permalink
PDCL-10688 - Support custom builds using the npm package (#1087)
Browse files Browse the repository at this point in the history
* Add rollup dependencies and npm scripts for custom build.

* Add rollup dependencies and npm scripts for custom build.

* Add skipwhen for componentCreators and component naming object.

* Conditional build script with arguments.

* Expose components to LibraryInfo.

* Updated customBuild.js file with the changes to generate componentNames.js during runtime

* Refactor componentCreator to identify required and optional components.

* Update componentCreators

* Exclude components for functional tests.

* Remove static splicing from customBuild.

* Fix componentCreators required and optional list.

* Add rollup config to incldue baseCode.

* Custom build test specs.

* Custom build test specs.

* Update custom build tests and update the runner.

* Restore the sandbox.

* Refactor componentCreator and customBuild to create optional component list from skipwhen directive.

* Map test directories for test runner.

* Dynamically determine which components to include in functional tests.

* Test build size with each component removed.

* Test build size with each component removed.

* Fix tests.

---------

Co-authored-by: Serban Stancu <[email protected]>
  • Loading branch information
shammowla and dompuiu authored Apr 26, 2024
1 parent 338a8dd commit 936fc1a
Show file tree
Hide file tree
Showing 14 changed files with 8,952 additions and 4,352 deletions.
2 changes: 1 addition & 1 deletion .sauce/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ suites:
- name: "Edge"
browserName: "microsoftedge"
headless: true
browserVersion: "latest"
browserVersion: "119"
src:
- "test/functional/specs/**/*.js"
platformName: "Windows 11"
Expand Down
5 changes: 4 additions & 1 deletion karma.saucelabs.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ module.exports = config => {
base: "SauceLabs",
browserName: "firefox",
platformName: "Windows 11",
browserVersion: "latest"
browserVersion: "latest",
"sauce:options": {
geckodriverVersion: "0.34.0"
}
},
sl_edgeW3C: {
base: "SauceLabs",
Expand Down
12,915 changes: 8,573 additions & 4,342 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
"test:unit:saucelabs:local": "karma start karma.saucelabs.conf.js --single-run",
"test:unit:coverage": "karma start --single-run --reporters spec,coverage",
"test:functional": "EDGE_BASE_PATH=\"ee-pre-prd\" ALLOY_ENV=\"int\" testcafe chrome",
"test:functional:saucelabs:dev": "NPM_PACKAGE_VERSION=\"2.17.0\" EDGE_BASE_PATH=\"ee-pre-prd\" ALLOY_ENV=\"int\" saucectl run --config ./.sauce/dev.yml",
"test:functional:custom": "node scripts/helpers/runFunctionalTests.js",
"test:functional:saucelabs:dev": "NPM_PACKAGE_VERSION=$(npm pkg get version | tr -d '\"') EDGE_BASE_PATH=\"ee-pre-prd\" ALLOY_ENV=\"int\" saucectl run --config ./.sauce/dev.yml",
"test:functional:watch": "EDGE_BASE_PATH=\"ee-pre-prd\" ALLOY_ENV=\"int\" ./scripts/watchFunctionalTests.js --browsers chrome",
"test:functional:debug": "EDGE_BASE_PATH=\"ee-pre-prd\" ALLOY_ENV=\"int\" testcafe --inspect-brk chrome",
"test:functional:build:int": "rollup -c --environment BASE_CODE_MIN,STANDALONE,NPM_PACKAGE_LOCAL",
"test:functional:build:prod": "rollup -c --environment BASE_CODE_MIN,NPM_PACKAGE_PROD",
"test:scripts": "jasmine --config=scripts/specs/jasmine.json",
"sandbox:build": "rollup -c --environment SANDBOX && cd sandbox && npm run build",
"sandbox:build:custom": "SANDBOX=true npm run build:custom -- --exclude personalization && cd sandbox && npm run build && npm run start",
"dev": "concurrently --names build,sandbox \"rollup -c -w --environment SANDBOX\" \"cd sandbox && export REACT_APP_NONCE=321 && npm start\"",
"build": "npm run format && npm run lint && npm run clean && rollup -c --environment BASE_CODE_MIN,STANDALONE,STANDALONE_MIN && echo \"Base Code:\" && cat distTest/baseCode.min.js",
"build:custom": "npm run clean && rollup -c --environment BASE_CODE_MIN,NPM_PACKAGE_LOCAL && node scripts/helpers/customBuild.js",
"prepare": "husky install && cd sandbox && npm install",
"prepublishOnly": "rimraf libEs5 libEs6 && babel src -d libEs5 --env-name npmEs5 && babel src -d libEs6 --env-name npmEs6",
"checkthattestfilesexist": "./scripts/checkThatTestFilesExist.js",
Expand Down Expand Up @@ -78,9 +81,14 @@
"@babel/plugin-transform-runtime": "^7.16.4",
"@babel/plugin-transform-template-literals": "^7.4.4",
"@babel/preset-env": "^7.4.5",
"@babel/types": "^7.23.0",
"@octokit/rest": "^18.3.5",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"babel-plugin-version": "^0.2.3",
"bundlesize": "^0.18.0",
"chai": "^5.0.0",
"chalk": "^2.4.2",
"concurrently": "^6.5.0",
"date-fns": "^2.23.0",
Expand Down Expand Up @@ -137,7 +145,9 @@
"url-parse": "^1.4.7",
"yargs": "^16.2.0"
},
"resolutions": {
"webdriverio": "^7.19.5"
"overrides": {
"karma-sauce-launcher": {
"webdriverio": "^8.35.1"
}
}
}
1 change: 0 additions & 1 deletion sandbox/.env

This file was deleted.

59 changes: 59 additions & 0 deletions scripts/helpers/conditionalBuildBabelPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

module.exports = excludedModules => {
return {
visitor: {
ImportDeclaration(path) {
let skipWhenComments = [];
if (path.node.leadingComments) {
skipWhenComments = path.node.leadingComments.filter(c => {
return c.value.trim().startsWith("@skipwhen");
});
}

if (skipWhenComments.length > 0) {
const [, webSDKModuleName, value] = skipWhenComments[0].value.match(
"ENV.(.*) === (false|true)"
);

if (excludedModules[webSDKModuleName] === value) {
const variableName = path.node.specifiers[0].local.name;

// Wrap the variable declaration in an IIFE to turn it into an expression
path.replaceWithSourceString(
`(() => { const ${variableName} = () => {}; })()`
);
}
}
},
ExportDefaultDeclaration(path) {
if (path.node.declaration.type === "ArrayExpression") {
path.node.declaration.elements = path.node.declaration.elements.filter(
element => {
if (element.name) {
const variableName = element.name;
const componentName = variableName
.replace("create", "")
.toLowerCase();
return !Object.keys(excludedModules).includes(
`alloy_${componentName}`
);
}
return true;
}
);
}
}
}
};
};
137 changes: 137 additions & 0 deletions scripts/helpers/customBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const fs = require("fs");
const path = require("path");
const { rollup } = require("rollup");
const nodeResolve = require("@rollup/plugin-node-resolve").default;
const commonjs = require("@rollup/plugin-commonjs");
const babel = require("@rollup/plugin-babel").default;
const terser = require("rollup-plugin-terser").terser;
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const conditionalBuildBabelPlugin = require("./conditionalBuildBabelPlugin");

// Path to componentCreators.js
const componentCreatorsPath = path.join(
__dirname,
"../../src/core/componentCreators.js"
);

// Read componentCreators.js
const componentCreatorsContent = fs.readFileSync(componentCreatorsPath, "utf8");

// Extract optional components based on @skipwhen directive
const optionalComponents = componentCreatorsContent
.split("\n")
.filter(line => line.trim().startsWith("/* @skipwhen"))
.map(line => {
const match = line.match(/ENV\.alloy_([a-zA-Z0-9]+) === false/);
if (match) {
const [, componentName] = match;
return componentName.toLowerCase(); // Ensure this matches the expected format for exclusion
}
return null;
})
.filter(Boolean);

console.log("Optional Components:", optionalComponents); // Debugging line

const argv = yargs(hideBin(process.argv))
.scriptName("build:custom")
.usage(`$0 --exclude ${optionalComponents.join(" ")}`)
.option("exclude", {
describe: "the components that you want to be excluded from the build",
choices: optionalComponents,
type: "array"
})
.array("exclude")
.check(() => {
// No need to check for required components as we're using @skipwhen to determine optionality
return true;
}).argv;

if (!argv.exclude) {
console.log(
`No components excluded. To exclude components, try running "npm run build:custom -- --exclude personalization". Your choices are: ${optionalComponents.join(
", "
)}`
);
process.exit(0);
}

const buildConfig = (minify, sandbox) => {
const plugins = [
nodeResolve({
preferBuiltins: false,
mainFields: ["component", "main", "browser"]
}),
commonjs(),
babel({
plugins: [
conditionalBuildBabelPlugin(
(argv.exclude || []).reduce((previousValue, currentValue) => {
previousValue[`alloy_${currentValue}`] = "false";
return previousValue;
}, {})
)
]
})
];
if (minify) {
plugins.push(terser());
}
let filename = `dist/alloy${minify ? ".min" : ""}.js`;
if (sandbox) {
filename = `sandbox/public/alloy${minify ? ".min" : ""}.js`;
}
return {
input: "src/standalone.js",
output: [
{
file: filename,
format: "iife",
intro:
"if (document.documentMode && document.documentMode < 11) {\n" +
" console.warn('The Adobe Experience Cloud Web SDK does not support IE 10 and below.');\n" +
" return;\n" +
"}\n",
sourcemap: false
}
],
plugins
};
};

const getFileSizeInKB = filePath => {
const stats = fs.statSync(filePath);
const fileSizeInBytes = stats.size;
return (fileSizeInBytes / 1024).toFixed(2);
};

const buildWithComponents = async sandbox => {
const prodBuild = buildConfig(false, sandbox);
const minifiedBuild = buildConfig(true, sandbox);

const bundleProd = await rollup(prodBuild);
console.log("✔️ Built alloy.js");
await bundleProd.write(prodBuild.output[0]);
console.log(`✔️ Wrote alloy.js to ${prodBuild.output[0].file}`);
console.log(`📏 Size: ${getFileSizeInKB(prodBuild.output[0].file)} KB`);

const bundleMinified = await rollup(minifiedBuild);

console.log("✔️ Built alloy.min.js");
await bundleMinified.write(minifiedBuild.output[0]);
console.log(`✔️ Wrote alloy.min.js to ${minifiedBuild.output[0].file}`);
console.log(`📏 Size: ${getFileSizeInKB(minifiedBuild.output[0].file)} KB`);
};

buildWithComponents(!!process.env.SANDBOX);
76 changes: 76 additions & 0 deletions scripts/helpers/runFunctionalTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env node

const fs = require("fs");
const glob = require("glob");
const createTestCafe = require("testcafe");

fs.readFile("dist/alloy.js", "utf8", (readFileErr, alloyData) => {
if (readFileErr) {
console.error(`readFile error: ${readFileErr}`);
return;
}

// Extract componentCreators array from alloyData
const componentCreatorsMatch = alloyData.match(
/var componentCreators = \[(.*?)\];/s
);
if (!componentCreatorsMatch) {
console.error("componentCreators array not found in dist/alloy.js");
return;
}
const componentCreators = componentCreatorsMatch[1]
.split(",")
.map(name => name.trim().replace("create", ""));

// Convert component creator function names to component names
const componentNames = componentCreators.map(
creator => creator.charAt(0).toLowerCase() + creator.slice(1) // Ensure first letter is lowercase to match directory names
);

// Define a mapping from component names to their test directory names
const componentNameToTestDirMapping = {
dataCollector: "Data Collector",
activityCollector: "Activity Collector",
identity: "Identity",
audiences: "Audiences",
context: "Context",
privacy: "Privacy",
eventMerge: "EventMerge",
libraryInfo: "LibraryInfo",
machineLearning: "MachineLearning",
decisioningEngine: "DecisioningEngine"
};

// Adjust componentNames using the mapping
const adjustedComponentNames = componentNames.map(
name => componentNameToTestDirMapping[name] || name
);

// Generate a glob pattern to match only the included components' test specs
const includedComponentsPattern = adjustedComponentNames.join("|");
const testSpecsGlobPattern = `test/functional/specs/@(${includedComponentsPattern})/**/*.js`;

glob(testSpecsGlobPattern, (globErr, files) => {
if (globErr) {
console.error(globErr);
process.exit(1);
}

if (files.length === 0) {
console.log("No test files found for the included components.");
return;
}

createTestCafe().then(testcafe => {
const runner = testcafe.createRunner();
runner
.src(files)
.browsers("chrome")
.run()
.then(failedCount => {
console.log(`Tests finished. Failed count: ${failedCount}`);
testcafe.close();
});
});
});
});
4 changes: 3 additions & 1 deletion src/components/LibraryInfo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ const prepareLibraryInfo = ({ config, componentRegistry }) => {
}
resultConfig[key] = value.toString();
});
const components = componentRegistry.getComponentNames();
return {
version: libraryVersion,
configs: resultConfig,
commands: allCommands
commands: allCommands,
components
};
};

Expand Down
Loading

0 comments on commit 936fc1a

Please sign in to comment.