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

[CS2] Module should be require-able in non-Node environments like Webpack and Browserify #4546

Merged
merged 19 commits into from
May 14, 2017

Conversation

akfish
Copy link
Contributor

@akfish akfish commented May 11, 2017

Resend #4501

akfish and others added 16 commits April 14, 2017 21:18
* Move Node.js-only code from src/coffee-script.coffee to src/index.coffee
* Use lib/coffee-script/index.js as npm package's "main" script
* Export CoffeeScript from src/browser.coffee
* Set package.json's "browser" field to lib/coffee-script/browser.js (used by webpack as entry point)
* Use lib/coffee-script/browser.js as bower package's "main" script
# Conflicts:
#	Cakefile
#	bower.json
#	package.json
#	src/browser.coffee
#	src/cake.coffee
#	src/coffeescript.coffee
#	src/command.coffee
#	src/index.coffee
#	src/register.coffee
#	src/repl.coffee
… for the lack of stub when running the browser tests in Node
# Conflicts:
#	docs/v2/browser-compiler/coffeescript.js
#	lib/coffeescript/coffeescript.js
@akfish akfish changed the base branch from master to 2 May 11, 2017 11:16
@GeoffreyBooth GeoffreyBooth self-assigned this May 11, 2017
@GeoffreyBooth GeoffreyBooth changed the title Add webpack support [WIP][CS2] Add webpack support May 11, 2017
Cakefile Outdated
task 'build:webpack', 'build webpack test bundle', ->
args = [
"./node_modules/webpack/bin/webpack.js"
"--entry=./test/webpack/entry.js",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this entry, and not browser.js as above?

Cakefile Outdated

task 'test:webpack', 'run test suite against the webpack test bundle', ->
loader = """
var __exports__ = {};
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this variable? Is it distinct from CommonJS exports?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Webpack will scope known globals. It is not possible to access them directly. This, along with the custom entry file and the loader script in test:webpack task, allows the bundled CoffeeScript to escape thus it can be required in node environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There might be a better way to do this. I'll get back to you later.

@akfish
Copy link
Contributor Author

akfish commented May 12, 2017

Turns out there's an webpack option for that and no additional configuration files nor plugins required. That makes more sense. Don't know what I was thinking back then.
I changed the entry point to ./ instead of explicitly specifying one. This let webpack to use browser field in package.json.
I also removed the build:webpack task and have the build run in test:webpack task.

@GeoffreyBooth
Copy link
Collaborator

@lydell or @jashkenas did you have any further notes? I’m leaving the test:integrations task as a place for future tests like loading test.html via Selenium Chromedriver.

Somehow cake test:browser fails both here and on 2, so something we did recently broke the browser build; but it’s unrelated to this PR.

@jashkenas
Copy link
Owner

Looks great!

@GeoffreyBooth GeoffreyBooth merged commit dc38399 into jashkenas:2 May 14, 2017
@GeoffreyBooth GeoffreyBooth changed the title [WIP][CS2] Add webpack support [CS2] Module should be require-able in non-Node environments like Webpack and Browserify May 14, 2017
GeoffreyBooth pushed a commit that referenced this pull request May 14, 2017
…pack and Browserify (#4546)

* Add webpack support

* Move Node.js-only code from src/coffee-script.coffee to src/index.coffee
* Use lib/coffee-script/index.js as npm package's "main" script
* Export CoffeeScript from src/browser.coffee
* Set package.json's "browser" field to lib/coffee-script/browser.js (used by webpack as entry point)
* Use lib/coffee-script/browser.js as bower package's "main" script

* Use NOP moduleMain when generating parser with Jison

* Remove legacy debug code from browser.coffee

* Improve comments, style

* Fix path

* Remove stub that was only to avoid breaking browser tests; compensate for the lack of stub when running the browser tests in Node

* Update output

* Add test:webpack task to Cakefile

* Update output files

* Run browser tests against webpack build

* Fix newline at end of file

* Export webpack test bundle as CommonJS module

* Remove build:webpack task

* Save webpack build to tmpdir; suppress build output unless it fails
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