-
Notifications
You must be signed in to change notification settings - Fork 23
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
Twing 2.2.0 not working in browser #314
Comments
Hi @m-hall, Can you give me more details on how you include Twing into the browser? Are you bundling it yourself or are you using the CDN version? I just conducted a test with Twing@2.2.0 CDN build and your code run perfectly - of course by using |
Now that I think about it, you are probably bundling Twing yourself and crypto support is missing from your bundler configuration. CDN's version of Twing if bundled with Browserify which comes with a crypto library for the Browser. Which explains why it works. |
After looking at the source code, it seems crypto is only used to create sha256 hashes. It's an overkill to use node.js whole crypto lib just for that. |
@m-hall Can you give Twing@2.2.1 a try? |
Nice, you work fast! I'll check it shortly :) You were right, I was not using the CDN version. I am loading it through node.js to work with a webpack/typescript combination. |
It makes sense. It puts into light the weakness of our browser tests that can't detect that kind of issues. All the tests were successful because browserify is used to create the test bundle and browserify comes with a lot of polyfills that handle things that are not handled out of the box by webpack. I'll have to find a way to make browser test suite more reliable. |
It appears to now be returning undefined with the same code I originally mentioned :( |
What error message do you receive? When I setup a project with the following entry point: const {TwingLoaderArray, TwingEnvironment} = require('twing');
const templateLoader = new TwingLoaderArray({
'template': '<p>{{ content }}</p>'
});
const twing = new TwingEnvironment(templateLoader);
console.log(twing.render('template', { content: 'it works'})); And execute webpack-cli to bundle that entry point:
I obtain a viable bundle that unfortunately throw the following error once executed by the browser:
This is an issue that was supposed to be fixed months ago (see #205) but that is still present with webpack (see webpack-contrib/uglifyjs-webpack-plugin#269). When I fix this issue (EDIT: by disabling webpack minifying) and execute the webpack command again, the bundle works perfectly in the browser and log |
note: with babel-minify you can switch of mangleing to get it working
|
Thanks for the workaround. I will have this fixed for good next week. |
just for reference, we now use terser instead of babel-minify as babel-minify has issues with source-maps and it's author seems to have moved to terser: webpack-contrib/babel-minify-webpack-plugin#68
|
Browser: Chrome 72 tested
Version: 2.2.0
Steps:
Load twing into browser
run the following code
See that you get this error in the console:
ERROR TypeError: crypto.createHash is not a function
On this line https://github.com/ericmorand/twing/blob/7ab824a6394d8e4036ad9717fe414ecb1fb33c18/src/lib/environment.ts#L366
Summary:
I believe what's happening here, is that the crypto class that is normally provided by node doesn't exist when in the browser, so require is returning an empty object in it's place.
The text was updated successfully, but these errors were encountered: