working with webpack - managing Page based variables - what is best practice?

88 views
Skip to first unread message

Nicolaas Thiemen Francken - Sunny Side Up

unread,
May 29, 2016, 12:04:49 AM5/29/16
to silverstripe-dev
Hi

Aaron, aka uncle cheese, recommended us to work with webpack (https://webpack.github.io/).  We are now using this, but it is a pretty steep learning curve.  

I am curious to get a best practice recommendation for injecting DB variables (set through CMS) into JS... 

I will give you an example of how I used to do that and how I am rewriting this now.


Here is how I used to write JS:


And this is how I would inject custom variables:




ISSUE: This does not work with webpack (and similar set ups) because MyJSFx is not available in the global name space. 


New Method:

Now, I inject variables from PHP like this:


and kick things into action using this piece of code


also note:


This works - but I wonder if it is a good solution.

As a bonus feature ;-) ...  here is a file that I use to block all requirements and save them within the web pack folder:


Again - my question: what is best practice for the code linked to above?  

THANK YOU 

Nicolaas


Stevie Mayhew

unread,
May 29, 2016, 5:16:47 PM5/29/16
to SilverStripe Core Development, n...@sunnysideup.co.nz
We do similar things to inject JS variables from generated code. We steer completely clear of the Requirements::whatever files through and never ever include and JS that way, its all included via webpack.

Either its dumped into the page and cached, or we set up a simple API and call that to get the data as necessary. Another option is to set `data-` attributes on the element which is the root of your JS component. We do this a lot when using react standalone components.

So a component looks like:
`<div id="MyJSComponent" data-firstname="{$FirstName}"><span class="sr-only">You must have JavaScript enabled to use this component.</span></div>`

And the JS will look like:

```
import React from 'react';
import ReactDom from 'react-dom';
import dataset from 'dataset';

const myJSComponent = document.getElementById('MyJSComponent');

class MyComponent extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            FirstName: dataset('firstname', myJSComponent)
        };
    }

    render() {
        return <div className="my-component">{this.state.FirstName}</div>;
    }
}

if (myJSComponent) {
    ReactDom.render(< MyComponent />, myJSComponent);
}
```

I'm sure this could be easily applied to a jQuery situation too :)

Cheers,
Stevie

Uncle Cheese

unread,
May 29, 2016, 6:52:52 PM5/29/16
to SilverStripe Core Development, n...@sunnysideup.co.nz
There should be an option in webpack somewhere to export your config object to the global namespace.

Nicolaas Thiemen Francken - Sunny Side Up

unread,
May 29, 2016, 8:34:03 PM5/29/16
to silverstripe-dev
Thank you Stevie and Aaron for your comments. 

​​It sounds like we are on the right track.  I definitely like setting data in the html itself.​


On 30 May 2016 at 10:52, Uncle Cheese <aaronc...@gmail.com> wrote:
There should be an option in webpack somewhere to export your config object to the global namespace.

​It seems to me we are trying to do it the other way ... getting "global" config into specific modules.​ 


Nicolaas Thiemen Francken

unread,
Apr 5, 2017, 5:50:49 PM4/5/17
to silverstripe-dev
We would like to make a small website in 4.0 with webpack.  What is the best way to get started - has anything changed since our last post?

Install:
composer create-project --keep-vcs --dev silverstripe/installer ./my/website/folder 4.0.x-dev

Modules to add:

Documentation:

It has to be simple enough for a junior front-end developer to make sense of it so that will be a bit of a challenge.

Are there any developments since our last post that will make it easier?

Thank you

Nicolaas




Reply all
Reply to author
Forward
0 new messages