Skip to content

Commit

Permalink
first implementation of library and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
getify committed Mar 25, 2024
1 parent 351f3e1 commit 8daa269
Show file tree
Hide file tree
Showing 7 changed files with 755 additions and 22 deletions.
2 changes: 1 addition & 1 deletion BUNDLERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The plugins for vite and webpack are included in the `bundler-plugins/` director

ESM library module that's suitable for bundling and `import`ing into your web app.

**Note:** not `dist/auto/qrds.js`, which is only intended [for web application projects WITHOUT a bundler](NON-BUNDLERS.md)
**Note:** this is *not* the same as `dist/auto/qrds.js`, which is only intended [for web application projects WITHOUT a bundler](NON-BUNDLERS.md)

* `dist/bundlers/qrds-external-bundle.js`

Expand Down
4 changes: 2 additions & 2 deletions NON-BUNDLERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Then copy over all `dist/auto/*` contents, as-is:

* `dist/auto/qrds.js`

**Note:** not `dist/bundlers/qrds.js`, which is only intended [for web application projects WITH a bundler](BUNDLERS.md)
**Note:** this is *not* the same as `dist/bundlers/qrds.js`, which is only intended [for web application projects WITH a bundler](BUNDLERS.md)

* `dist/auto/external.js`

This is an *auto-loader* that dynamically loads the rest of the `external/*` dependencies via `<script>`-element injection into the DOM. `dist/auto/qrds.js` imports and activates this loader automatically.
This is an *auto-loader* that dynamically loads some of the `external/*` dependencies via `<script>`-element injection into the DOM (and others with `import`). `dist/auto/qrds.js` imports and activates this loader automatically.

* `dist/auto/external/*` (preserve the whole `external/` sub-directory):
- `qrcode.js`
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"description": "Browser-only utils for sharing/synchronizing data using 'animated' QR codes",
"version": "0.0.0-a",
"exports": {
".": "./dist/bundlers/qrds.js"
".": "./dist/bundlers/qrds.mjs",
"./bundlers/vite": "./bundler-plugins/vite.mjs",
"./bundlers/webpack": "./bundler-plugins/webpack.mjs"
},
"browser": {
"qr-data-sync": "./dist/bundlers/qrds.js"
"qr-data-sync": "./dist/bundlers/qrds.mjs"
},
"scripts": {
"build:all": "node scripts/build-all.js",
Expand Down
24 changes: 15 additions & 9 deletions scripts/build-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const DIST_AUTO_EXTERNAL_QRSCANNER = path.join(DIST_AUTO_EXTERNAL_DIR,path.basen
const DIST_AUTO_EXTERNAL_QRSCANNER_WORKER = path.join(DIST_AUTO_EXTERNAL_DIR,path.basename(QRSCANNER_WORKER_SRC));

const DIST_BUNDLERS_DIR = path.join(DIST_DIR,"bundlers");
const DIST_BUNDLERS_QRDS_FILE = path.join(DIST_BUNDLERS_DIR,path.basename(QRDS_SRC).replace(/\.js$/,".mjs"));
const DIST_BUNDLERS_QRDS_EXTERNAL_BUNDLE_FILE = path.join(DIST_BUNDLERS_DIR,"qrds-external-bundle.js");


Expand Down Expand Up @@ -73,14 +74,15 @@ async function main() {
[ QRDS_SRC, ],
SRC_DIR,
DIST_BUNDLERS_DIR,
(contents,filename) => prepareFileContents(
(contents,outputPath,filename = path.basename(outputPath)) => prepareFileContents(
// alter (remove) "external.js" dependencies-import
// since bundlers handle dependencies differently
contents.replace(
/import[^\r\n]*".\/external.js";?/,
"import QRScanner from \"qr-scanner\""
),
`bundlers/${filename}`
outputPath.replace(/\.js$/,".mjs"),
`bundlers/${filename.replace(/\.js$/,".mjs")}`
),
/*skipPatterns=*/[ "**/*.txt", "**/*.json", "**/external" ]
);
Expand Down Expand Up @@ -124,18 +126,22 @@ async function main() {

// ****************************

async function prepareFileContents(contents,filename) {
async function prepareFileContents(contents,outputPath,filename = path.basename(outputPath)) {
// JS file (to minify)?
if (/\.[mc]?js$/i.test(filename)) {
contents = await minifyJS(contents);
}

// add copyright header
return `${
mainCopyrightHeader.replace(/#FILENAME#/g,filename)
}\n${
contents
}`;
return {
contents: `${
mainCopyrightHeader.replace(/#FILENAME#/g,filename)
}\n${
contents
}`,

outputPath,
};
}
}

Expand All @@ -156,7 +162,7 @@ async function buildFiles(files,fromBasePath,toDir,processFileContents,skipPatte
}

let contents = await fsp.readFile(fromPath,{ encoding: "utf8", });
contents = await processFileContents(contents,path.basename(relativePath));
({ contents, outputPath, } = await processFileContents(contents,outputPath));

await fsp.writeFile(outputPath,contents,{ encoding: "utf8", });
}
Expand Down
Loading

0 comments on commit 8daa269

Please sign in to comment.