Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple modules from 1 package

I'd like to import 2 modules from the package string-template. string-template consists of the 2 files index.js & compile.js which both export a module.

I tried to accomplish this using the following approach:

1) Add both modules to the vendor-bundle dependencies in aurelia.json:

{
    // ...
    "bundles": [
        {
            // app-bundle
        },{
            // vendor-bundle
            // ...
            "dependencies": {
                // ...
                "string-template", // Points per default to index.js which works just fine
                {
                    // Use some custom name and point it explicitly to the second module. Doesn't work...
                    "name": "string-template-compile",
                    "path": "../node_modules/string-template",
                    "main": "compile"
                }
            }
        }
    ]
}

2) Include them where needed (e.g. in foo/bar/code.ts):

import * as Format from 'string-template';
import * as FormatCompile from 'string-template-compile';

Problems

If I only include string-template within aurelia.json & my source code, everything is working as expected but as soon as I add the second module with the custom name string-template-compile the following happens:

  • Typescript tells me TS2307:Cannot find module 'string-template-compile'. where I try to import the module (foo/bar/code.ts)
  • The CLI process started with au run --watch stops execution after the output Tracing foo/bar/code... without any additional error.

I've run into the same issue when trying to only import specific modules from crypto-js to only use crypto-js/aes & crypto-js/sha256 instead of including the whole package (described as "modular include" in the NPM usage section).


Edit: I've also created an issue in the CLI repo since it doesn't seem like I'm actually getting an answer here...

like image 328
suamikim Avatar asked Feb 05 '26 05:02

suamikim


1 Answers

you can use require, this works (tested with esnext project not ts):

//aurelia.json
{
    "name": "string-template",
    "path": "../node_modules/string-template",
    "main": "index"
}

// js
const format = require('string-template');
const compile = require('string-template/compile');

Update: as for require-js each module have one or more dependencies:

  • sha256 module needs only: core
  • aes module needs: core, enc-base64, md5, evpkdf, cipher-core
  • enc-base64 needs: core.
  • md5 needs: core.
  • evpkdf needs: sha1, hmac.
  • cipher-core needs: core, evpkdf.

so for the two modules sha256 and aes you need to load 8 files:

// aurelia.json
{
    "name": "crypto-js",
    "path": "../node_modules/crypto-js",
    "main": "core",
    "resources": [
        "enc-base64.js",
        "md5.js",
        "hmac.js",
        "sha1.js",
        "evpkdf.js",
        "cipher-core.js",
        "sha256.js"
    ]
}

// js
import core from 'crypto-js';
import 'crypto-js/sha256';
import 'crypto-js/aes';

// usage
let ciphertext = core.AES.encrypt('my message', 'secret key 123');
let bytes  = core.AES.decrypt(ciphertext.toString(), 'secret key 123');
let plaintext = bytes.toString(core.enc.Utf8);

console.log(plaintext);
like image 159
Rabah Avatar answered Feb 07 '26 23:02

Rabah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!