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

Twing 2.2.0 not working in browser #314

Closed
m-hall opened this issue Feb 13, 2019 · 11 comments
Closed

Twing 2.2.0 not working in browser #314

m-hall opened this issue Feb 13, 2019 · 11 comments
Labels
Milestone

Comments

@m-hall
Copy link

m-hall commented Feb 13, 2019

Browser: Chrome 72 tested
Version: 2.2.0

Steps:
Load twing into browser
run the following code

const templateLoader = new TwingLoaderArray({
    'template': '<p>{{ content }}</p>'
});
const twing = new TwingEnvironment(templateLoader);

console.log(twing.render('template', { content: 'it works'}));

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.

@ericmorand
Copy link
Member

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 Twing.TwingLoaderArray and Twing.TwingEnvironment.

@ericmorand
Copy link
Member

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.

@ericmorand
Copy link
Member

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.

ericmorand added a commit that referenced this issue Feb 13, 2019
@ericmorand ericmorand added this to the 2.2.1 milestone Feb 13, 2019
ericmorand added a commit that referenced this issue Feb 13, 2019
@ericmorand ericmorand added the bug label Feb 13, 2019
@ericmorand
Copy link
Member

@m-hall Can you give Twing@2.2.1 a try?

@m-hall
Copy link
Author

m-hall commented Feb 13, 2019

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.

@ericmorand
Copy link
Member

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.

@m-hall
Copy link
Author

m-hall commented Feb 13, 2019

It appears to now be returning undefined with the same code I originally mentioned :(

@ericmorand
Copy link
Member

ericmorand commented Feb 13, 2019

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:

webpack index.js

I obtain a viable bundle that unfortunately throw the following error once executed by the browser:

Uncaught Error: Unable to register extension "" as it is already registered.

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 <p>it works</p> in the browser console.

@corneliusweiss
Copy link

note: with babel-minify you can switch of mangleing to get it working

const MinifyPlugin = require("babel-minify-webpack-plugin");
...

mode: 'production',
optimization:{
    minimize: false, // disables uglify -> we use babel minify
},
plugins: [
    new MinifyPlugin({
        keepClassName: true,
    })
],

@ericmorand
Copy link
Member

Thanks for the workaround. I will have this fixed for good next week.

@corneliusweiss
Copy link

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

const TerserPlugin = require('terser-webpack-plugin')

module.exports = merge(common, {
    devtool: 'source-map',
    mode: 'production',
    optimization:{
        minimizer: [new TerserPlugin({
            sourceMap: true,
            terserOptions: {
                keep_classnames: true, // twing problem @see https://github.com/ericmorand/twing/issues/314
            },
        })],
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants