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

How do I compile it to a single js and css file? #1096

Closed
pupeno opened this issue Dec 31, 2016 · 29 comments
Closed

How do I compile it to a single js and css file? #1096

pupeno opened this issue Dec 31, 2016 · 29 comments

Comments

@pupeno
Copy link

pupeno commented Dec 31, 2016

I can't figure out how to compile react-toolbox to a single js and css files that is easy to add to a project. You don't seem to provide them either.

For example, for material-components, there's npm build:

https://github.com/material-components/material-components-web/blob/master/package.json#L6-L7

https://github.com/material-components/material-components-web/blob/master/webpack.config.js

@javivelasco
Copy link
Member

This is exactly the purpose of react-toolbox-themr! There you can extract the css into a single file but the js is required works like any other library. You can use browserify or anything else. :)

@pupeno
Copy link
Author

pupeno commented Jan 9, 2017

I really like react-toolbox, but even with react-toolbox-themr, it's very hard to repackage it for use in ClojureScript applications.

@javivelasco
Copy link
Member

How can I help with this? Is there any ongoing issue with the themr package?

@pupeno
Copy link
Author

pupeno commented Jan 23, 2017 via email

@pupeno
Copy link
Author

pupeno commented Jan 28, 2017

Part of packaging a library such as this one for cljsjs is to build it with webpack as a single JavaScript 5 file. I'm trying to do it with this file:

const path = require('path');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

var entryName = "react-toolbox.inc";

module.exports = {
    entry: path.join(__dirname, "components", "index.js"),

    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    },

    output: {
        filename: entryName + ".js",
        libraryTarget: "umd",
        library: "ReactToolbox"
    },

    resolve: {
        extensions: ['', '.js', '.jsx', '.scss']
    },

    module: {
        loaders: [
            {
                test: /(\.js|\.jsx)$/,
                exclude: /(node_modules)/,
                loader: "babel",
                query: {
                    presets: ['es2015', 'react', 'stage-0'],
                    plugins: ['add-module-exports']
                }
            },
            {
                test: /(\.scss|\.css)$/,
                loader: ExtractTextPlugin.extract("style", "css?sourceMap&modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss!sass")
            }
        ]
    },

    postcss: [autoprefixer],

    plugins: [
        new ExtractTextPlugin(entryName + '.css', {allChunks: true})
    ]
};

Does anything look immediately wrong here? When I start off with npm install --production it fails with this error:

Hash: cc7895976b8d13be759c
Version: webpack 1.14.0
Time: 663ms
    + 1 hidden modules

ERROR in ./components/index.js
Module build failed: TypeError: Path must be a string. Received { __esModule: true,
  File: [Getter],
  options: [Getter],
  buildExternalHelpers: [Getter],
  template: [Getter],
  resolvePlugin: [Getter],
  resolvePreset: [Getter],
  version: [Getter],
  util:

and it goes on and on and on.

@pupeno
Copy link
Author

pupeno commented Jan 28, 2017

Any ideas what the problem might be? The commands I'm running can be found here:

https://github.com/pupeno/packages/blob/react-toolbox/react-toolbox/build.boot#L46-L49

(sorry, Clojure code, but the npm calls should be clear enough).

@javivelasco
Copy link
Member

@pupeno not sure about what would be the error there. I'm not very familiar with building umd with webpack. For react-toolbox-themr I've wrote an script for this same reason. I didn't even knew if that's easy to do with webpack. Maybe it's helpful for you too. The css part is right there, the JS should be more straightforward and can be done with Gulp probably.

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

I don't have to package it as umd. Most cljsjs packages use var and that's what I'm using right now.

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

My current attempt is here:

https://github.com/pupeno/packages/blob/react-toolbox/react-toolbox/resources/webpack.config.js

it either ends up having its own copy of React or it cannot find classNames.

@javivelasco
Copy link
Member

It's a well known issue. You can't even npm link a react package because it detects two different copies of React. Don't know if it still happens but it's really messy. Can't you achieve with you want to do by extracting separate CSS as shown in the themr package? The JS transform should be the same as for other packages

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

I'm not even sure what I'm looking at to be honest. Before looking at the CSS, I'd like to get the JavaScript part opening. I need compile React Toolbox to a single JavaScript 5 file that doesn't include its own copy of React.

@javivelasco
Copy link
Member

I would need to dig deeper on this but maybe this could be helpful? I guess adding the lib to cdn should be the same process you're looking for?

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

Yes... if there was a .js/.css in a CDN, packaging for use in Clojure would be trivial (I think).

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

Also, I'm using the latest stable release. Not sure how big of a difference it makes. react-toolbox-themr seems to only work with the beta.

@javivelasco
Copy link
Member

javivelasco commented Jan 29, 2017

Exactly. It only works with the beta because we can take advantage of the postcss flexibility. With sass I think it's not even possible. My advice is to check out that build and how it was generated and to switch to the beta. Actually it's a beta because there will be important changes across releases but it's sage to use it.

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

So, I'm looking at working with 2.0.0-beta.6 and react-toolbox-themr.

some people find difficult to integrate the library in their current stack, specially when Webpack is not included or accessible.

Using webpack for repackaging npm libraries for Clojure is not common, but many packages do it, so, it is accessible to me.

Once installed you can define the configuration in the package.json file of your project or through the --config <path/to/json> option

These packages don't tend to have a package.json, so, there seems to be a bit of impedance mismatch to say the least. I'm concerned about the amount of moving pieces already, it makes it very hard to figure out what's wrong when something goes wrong (all the time).

Looking at the example:

"reactToolbox": {
  "include": [
    "BUTTON",
    "DATE_PICKER"
  ],
  "customProperties": {
    "animation-duration": "0.3s",
    "color-accent": "var(--palette-pink-a200)",
    "color-accent-dark": "var(--palette-pink-700)",
    "color-primary-contrast": "var(--color-dark-contrast)",
    "color-accent-contrast": "var(--color-dark-contrast)",
  },
  "output": "assets/react-toolbox"
}

it looks like it's a pick and chose for my project kind of setup. I need to package everything. Do I have to list everything there then?

@javivelasco
Copy link
Member

You can even not include any configuration at all and it will get the defaults. The thing is that if you want to generate a final build, stuff like theming is not going to work because you need to process stylesheets. Consider this package sort of a bundler to generate static styles and theme for react-toolbox so you can use it in whatever stack with no config at all

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

Ok, so, I'm experimenting with it. I just tried running it with no arguments:

~/.boot/cache/tmp/Users/pupeno/Documents/code/sn/packages/react-toolbox/29f/x50qw4/react-toolbox-2.0.0-beta.6$ ./node_modules/.bin/react-toolbox-themr
module.js:472
    throw err;
    ^

Error: Cannot find module '/Users/pupeno/.boot/cache/tmp/Users/pupeno/Documents/code/sn/packages/react-toolbox/29f/x50qw4/react-toolbox-2.0.0-beta.6/node_modules/react-toolbox/lib/identifiers.js'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/pupeno/.boot/cache/tmp/Users/pupeno/Documents/code/sn/packages/react-toolbox/29f/x50qw4/react-toolbox-2.0.0-beta.6/node_modules/react-toolbox-themr/bin/builder.js:42:21)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

I guess it's assuming it's in a project that requires the npm package react-toolbox, so, I tried to specify the path:

~/.boot/cache/tmp/Users/pupeno/Documents/code/sn/packages/react-toolbox/29f/x50qw4/react-toolbox-2.0.0-beta.6$ ./node_modules/.bin/react-toolbox-themr -p components/
/Users/pupeno/.boot/cache/tmp/Users/pupeno/Documents/code/sn/packages/react-toolbox/29f/x50qw4/react-toolbox-2.0.0-beta.6/components/identifiers.js:1
(function (exports, require, module, __filename, __dirname) { export const APP_BAR = 'RTAppBar';
                                                              ^^^^^^
SyntaxError: Unexpected token export
    at Object.exports.runInThisContext (vm.js:78:16)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/pupeno/.boot/cache/tmp/Users/pupeno/Documents/code/sn/packages/react-toolbox/29f/x50qw4/react-toolbox-2.0.0-beta.6/node_modules/react-toolbox-themr/bin/builder.js:42:21)
    at Module._compile (module.js:571:32)

and now I'm not sure what's up there.

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

BTW, thanks a lot for the help, it's really appreciated. :)

@javivelasco
Copy link
Member

No problem! I'd try to generate a project just to extract styles to make sure it will work. It should specifying your own path too, but I think I didn't test it hardly. Following the video in the package should work. If you are willing to generate a package that install both react toolbox and themr and generates a build maybe it's a different business. Feel free to ping me tomorrow via Discord if it's still troublesome

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

That video starts by creating an app, while what I'm doing has no app. I understand the bit I care about is running ./node_modules/.bin/react-toolbox-themr but I'm stumbling on an error with that.

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

https://discordapp.com/ ?

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

@javivelasco how do I find you there?

@javivelasco
Copy link
Member

javivelasco commented Jan 29, 2017

I'm @Javi there. Or through #react-toolbox channel

@pupeno
Copy link
Author

pupeno commented Jan 29, 2017

When I try to add you it says I need your four digit tag.

@simonlmartin
Copy link

@pupeno - did you ever resolve this and create a webpack config which generates one js file and one css file. Thanks.

@pupeno
Copy link
Author

pupeno commented Jul 19, 2017

@simonlmartin I just noticed your message as I was trying to upgrade the ClojureScript package to beta 12. I did manage to make it work with this webpack:

https://github.com/cljsjs/packages/blob/master/react-toolbox/resources/webpack.config.js

but since then, the layout changed entirely and now there are these different packages and I'm again lost as to how to compile react-toolbox.

@pupeno
Copy link
Author

pupeno commented Jul 19, 2017

Here I started trying to make it work with the new layout:

https://github.com/pupeno/packages/blob/c1461aaf126101f718b6016f2078017722fc994e/react-toolbox/resources/webpack.config.js

At this point I'm getting this error:

Hash: 0e6c2ccdad947fc39b1f
Version: webpack 2.7.0
Time: 91ms
               Asset     Size  Chunks             Chunk Names
react-toolbox.inc.js  27.7 kB       0  [emitted]  main
    + 1 hidden modules

ERROR in ./packages/react-toolbox/src/components/index.js
Module parse failed: /Users/pupeno/.boot/cache/tmp/Users/pupeno/Documents/code/clojure/packages/react-toolbox/5uf/x50qw4/react-toolbox-react-toolbox-2.0.0-beta.12/packages/react-toolbox/src/components/index.js Unexpected token (24:7)
You may need an appropriate loader to handle this file type.
| export { default as ProgressBar } from './progress_bar';
| export * from './radio';
| export Ripple from './ripple';
| export { default as Slider } from './slider';
| export { default as Snackbar } from './snackbar';

@pupeno
Copy link
Author

pupeno commented Jul 19, 2017

This is the line

export Ripple from './ripple';

causing the issue.

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

No branches or pull requests

3 participants