From 27496d7af5b55f218e74145a2d4dc659e017d92c Mon Sep 17 00:00:00 2001
From: Jesse Jurman Parameters
--output
and
- --minified
.
+ Aside from the required components, the command has one optional flag, --output
.
--output
(or -o
) can be used to set the file name and directory of the resulting javascript.
If this flag is missing, the command will place the file in the current directory, named based on the component files
passed in.
- --minified
(or -m
) can be used to import the minified Tram-Lite code as part of your export.
- This should reduce the total size of the exported components.
-
Similar to the example above, we start with a component definition in an x-button.html
.
diff --git a/package-lock.json b/package-lock.json
index 437e0e6..d6ae858 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "tram-lite",
- "version": "5.2.0",
+ "version": "5.3.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "tram-lite",
- "version": "5.2.0",
+ "version": "5.3.0",
"license": "MIT",
"devDependencies": {
"cypress": "^12.14.0",
diff --git a/package.json b/package.json
index c524f52..30ed195 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "tram-lite",
- "version": "5.2.0",
+ "version": "5.3.0",
"description": "💡 HTML library for building and enhancing web-components",
"homepage": "https://tram-one.io/tram-lite/",
"repository": "https://github.com/Tram-One/tram-lite",
diff --git a/src/scripts/export-script.js b/src/scripts/export-script.js
index 9ae81e9..4ec03c9 100644
--- a/src/scripts/export-script.js
+++ b/src/scripts/export-script.js
@@ -7,9 +7,6 @@
const fs = require('fs');
const path = require('path');
-// check to see if we should use the minified flag for external dependencies
-const useMinified = process.argv.includes('-m') || process.argv.includes('--minified');
-
// check to see if there is a predefined output filename (otherwise we will try to generate one)
const outputFlagIndex = process.argv.findIndex((arg) => arg === '-o' || arg === '--output');
const customOutputFile = outputFlagIndex !== -1 ? process.argv[outputFlagIndex + 1] : null;
@@ -26,11 +23,40 @@ if (filePaths.length === 0) {
console.log('processing', filePaths, 'for export');
const componentDefinitions = filePaths.map((filePath) => fs.readFileSync(filePath, 'utf8'));
-const tramLiteExportDependenciesPath = useMinified
- ? path.join(__dirname, './export-dependencies.min.js')
- : path.join(__dirname, './export-dependencies.js');
+// load the core Tram-Lite library and classes (which are always needed)
+const coreFiles = [
+ './export/TramLite.min.js',
+ './export/ComponentDefinition.min.js',
+ './export/ImportComponent.min.js',
+].map((filePath) => fs.readFileSync(path.join(__dirname, filePath)).toString());
+
+// load all shadow root processors
+const processorFiles = fs
+ .readdirSync(path.join(__dirname, './export/processors'))
+ .map((file) => `./export/processors/${file}`)
+ .map((filePath) => fs.readFileSync(path.join(__dirname, filePath)).toString());
+
+// determine if we need these processors for these components
+// (note, this is a very weak regex check, and not using proper CSS or static analysis)
+const processorFilesToInclude = processorFiles.filter((processorContent) => {
+ // the following regex matches on calls to appendShadowRootProcessor
+ // it specifically captures the first parameter (a CSS selector), and
+ // parses out the parameter as a whole, and then the text inside `[...]`
+ const matches = processorContent.match(/TramLite\.appendShadowRootProcessor\(\"([^\[\,]*(\[?(.+)\])?[^\]\,]*)\"/);
+
+ if (!matches) {
+ console.log(processorContent);
+ }
+
+ // get the last most match (the most specific)
+ const keySelector = [matches[1], matches[2], matches[3]].filter((selector) => selector != undefined).at(-1);
+
+ // see if any of our component definitions match with this selector
+ return componentDefinitions.some((template) => template.match(keySelector));
+});
+
+const tramLiteExportDepenedencies = [...coreFiles, ...processorFilesToInclude].join('');
-const tramLiteExportDepenedencies = fs.readFileSync(tramLiteExportDependenciesPath).toString();
const templateAndLoadCode = componentDefinitions
.map((componentCode) => {
// update the component code, in case it also uses ``, we'll need to escape them