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

PDCL-10688 - Support custom builds using the npm package #1087

Merged
merged 22 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6762ad7
Add rollup dependencies and npm scripts for custom build.
shammowla Nov 20, 2023
cad1357
Add rollup dependencies and npm scripts for custom build.
shammowla Nov 20, 2023
46119e4
Add skipwhen for componentCreators and component naming object.
shammowla Nov 20, 2023
2f075a1
Conditional build script with arguments.
shammowla Nov 20, 2023
88fa79a
Expose components to LibraryInfo.
shammowla Nov 20, 2023
88f5838
Updated customBuild.js file with the changes to generate componentNam…
shammowla Nov 29, 2023
eb5f65a
Refactor componentCreator to identify required and optional components.
shammowla Nov 30, 2023
9cf0a7d
Update componentCreators
shammowla Dec 4, 2023
016cf79
Exclude components for functional tests.
shammowla Dec 8, 2023
6c36644
Remove static splicing from customBuild.
shammowla Jan 17, 2024
29292d2
Fix componentCreators required and optional list.
shammowla Jan 17, 2024
f45c636
Add rollup config to incldue baseCode.
shammowla Jan 24, 2024
3b3fd4e
Custom build test specs.
shammowla Jan 24, 2024
0556160
Custom build test specs.
shammowla Jan 24, 2024
06d8b3b
Update custom build tests and update the runner.
shammowla Jan 29, 2024
db3cc4f
Restore the sandbox.
shammowla Jan 29, 2024
89f34b8
Refactor componentCreator and customBuild to create optional componen…
shammowla Jan 31, 2024
dec3cb2
Map test directories for test runner.
shammowla Feb 1, 2024
e20d63d
Dynamically determine which components to include in functional tests.
shammowla Feb 1, 2024
d05a9a6
Test build size with each component removed.
shammowla Feb 1, 2024
ea83cdc
Test build size with each component removed.
shammowla Feb 1, 2024
d1669c1
Fix tests.
dompuiu Apr 25, 2024
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
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,927 changes: 8,579 additions & 4,348 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;
}
);
}
}
}
};
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the goals of this project is to get Adobe Tags to support this build method. To that end whatever thing we make could be used by other Tags extension developers. There is a lot of specific logic inside of the conditionalBuildBabelPlugin code that only applies to our specific use-case. For example, when I see a directive named @skipwhen it makes me think I could put this in front of anything; however, from this code it looks like you could only use it in front of an import and also if you are using the variable in an array.

I did a bit of research about conditionally building things in javascript. One of the established patterns within javascript libraries is to use an if statement that references process.env. This is especially used when referencing process.env.NODE_ENV. i.e.

if (process.env.NODE_ENV === "development") {
  console.log("development mode!");
}

I put together a sample to see if this would work with Rollup. I've also noticed that a lot of rollup systems have plugins or ways to accomplish this. https://github.com/jonsnyder/conditional-compilation-sample

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point @jonsnyder!

@shammowla Please check out Jon's sample and maybe let's schedule a work session to make this PR more reusable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the Babel plugin was from @dompuiu, so it may have something to do with how he envisions Launch support will happen i.e. furance/forge will run Babel but not rollup.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see some customers wanting an allow-list instead of an exclude-list.

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
Loading