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

Node module #706

Merged
merged 22 commits into from
Aug 30, 2017
Merged

Node module #706

merged 22 commits into from
Aug 30, 2017

Conversation

javan
Copy link
Contributor

@javan javan commented Aug 28, 2017

Moves the bulk of our webpack config into a node module instead of copying it all to apps on webpacker:install. There are only four files copied to config/webpack/ now:

1 config/webpack/environment.js for setting up base configuration:

const { environment } = require('webpacker')
module.exports = environment

2-4 config/webpack/{development,production,test}.js, which build off environment.js and are identical to start:

const environment = require('./environment')
module.exports = environment.toWebpackConfig()

TODO

Before merging

  • Relax dependency versions?
  • Update docs

After merging

  • Publish to npm
    • $ npm version major
    • $ npm publish --access public

Fixes #649
Closes #264

@gauravtiwari
Copy link
Member

@javan Just briefly looked at your changes and it's looking really good 👍 💯

Happy to help with docs if you like or perhaps we can do it in separate PR i.e. move all README contents into docs/ folder or something. I can do that in parallel.

For node_modules, seems like someone already got https://www.npmjs.com/package/webpacker registered 😞

@javan
Copy link
Contributor Author

javan commented Aug 29, 2017

@gauravtiwari, I added new documentation and touched up some of the existing bits in 34142b1. Let me know if I missed anything critical.

@gauravtiwari
Copy link
Member

@javan Sure thing, reviewing now. The API looks really nice though 💯

We haven't published the package to npm yet right?

@javan
Copy link
Contributor Author

javan commented Aug 29, 2017

Right. Not published yet. Will eventually be published as a scoped package: @rails/webpacker.

Side note: We should gut the README as soon as this is merged.

@gauravtiwari
Copy link
Member

@javan Cool 👍

Yepp, will do. Thinking we create a docs/ folder and create .md for each topic. What do you think?

@javan
Copy link
Contributor Author

javan commented Aug 29, 2017

We can discuss more in #645, but basically I think we should remove everything that's webpack-not-webpacker related.

@javan
Copy link
Contributor Author

javan commented Aug 29, 2017

@gauravtiwari, can you please try this out in a real app?

You can do:

# Gemfile
gem 'webpacker', github: 'rails/webpacker', branch: 'node-module'
// package.json
"dependencies": {
  "webpacker": "rails/webpacker#node-module"
}

and then rails webpacker:install

@gauravtiwari
Copy link
Member

gauravtiwari commented Aug 29, 2017

@javan Yepp doing that now.

BTW, is code like this intentional i.e. if block and rest of the code, no space?

 const { extensions } = config
  if (!extensions.length) {
    throw new Error('You must configure at least one extension to compile in webpacker.yml')
  }
  return extensions.length === 1 ? `**/${extensions[0]}` : `**/*{${extensions.join(',')}}`

I corrected a few and if you don't mind I can push :)

@javan
Copy link
Contributor Author

javan commented Aug 29, 2017

Hmm, not seeing that locally or on github

function getExtensionsGlob() {
const { extensions } = config
if (!extensions.length) {
throw new Error('You must configure at least one extension to compile in webpacker.yml')
}
return extensions.length === 1 ? `**/${extensions[0]}` : `**/*{${extensions.join(',')}}`
}

@gauravtiwari
Copy link
Member

Oops sorry I meant lb before/after if block.

@javan
Copy link
Contributor Author

javan commented Aug 29, 2017

I prefer the spacing as-is since it's a relatively short block of code. And the linter isn't complaining. 😬

@javan javan changed the title WIP: Node module Node module Aug 29, 2017
package/index.js Outdated
const { existsSync } = require('fs')

function createEnvironment() {
const path = `./environments/${process.env.NODE_ENV}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javan Missing file extension here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed. I removed them all throughout the codebase. Just like Ruby, Node's require doesn't need an extension.

Copy link
Member

@gauravtiwari gauravtiwari Aug 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but existsSync won't work, it requires a path

const path = resolve(__dirname, `./environments/${process.env.NODE_ENV}.js`)

https://nodejs.org/api/fs.html#fs_fs_existssync_path

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const constructor = existsSync(path) ? require(path) : Environment

Will always evaluate to false and return default config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh, right you are. Great catch! Fixed in df21fae

"description": "Webpacker makes it easy to use the JavaScript preprocessor and bundler [Webpack](https://webpack.github.io) to manage application-like JavaScript in Rails. It coexists with the asset pipeline, as the purpose is only to use Webpack for app-like JavaScript, not images, css, or even JavaScript Sprinkles (that all continues to live in app/assets).",
"main": "index.js",
"main": "package/index.js",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add files we are distributing from NPM so the package doesn't include ruby files.

  "files": [
    "package"
  ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 e0ba006

@gauravtiwari
Copy link
Member

Great, will take a final look now and we can merge

README.md Outdated
test: /.(ts|tsx)$/,
loader: 'ts-loader'
})
```
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this step because we're adding all loaders by default now, but didn't notice the tsx extension part. Maybe we should just add it to the loader?

module.exports = {
test: /\.ts$/,
loader: 'ts-loader'
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javan Oh yes, totally forgot. Yes, makes sense 👍

README.md Outdated
}
// Update an option directly
const vueLoader = environment.loaders.get('vue')
vueLoader.test = /\.vue(\.erb)?$/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this the default test for the vue loader?

test: /\.vue$/,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we can do that but I guess then we would need to update all loaders to support .erb (if we want that out of the box)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already do for ~half of the loaders. Might as well for all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍 We also received this through many PR's and issues so makes sense 💯

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Want to make that change (and also add tsx to the typescript loader)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on it 👍

@gauravtiwari
Copy link
Member

All done 👍 added an small change to dev server to add overlay for errors - this will basically show a screen with error instead of blank screen.

README.md Outdated
@@ -90,7 +90,7 @@ in which case you may not even need the asset pipeline. This is mostly relevant

## Features

* [Webpack 2](https://webpack.js.org/)
* [Webpack 2 and 3](https://webpack.js.org/)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe drop the 2? We technically don't support it now:

"webpack": "^3.5.5",

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah, although the config we have will work with both versions but makes sense to just say 3.

Also, we should add that module concatenation plugin? https://github.com/webpack/webpack/tree/master/examples/scope-hoisting#webpackconfigjs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will push an update now 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config may work, but you won't be able to install v2 if you have webpacker as a dependency in package.json

@javan javan merged commit 50aaf77 into master Aug 30, 2017
@javan javan deleted the node-module branch August 30, 2017 13:30
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

Successfully merging this pull request may close these issues.

3 participants