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] Add webpack support #4501

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions Cakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fs = require 'fs'
os = require 'os'
path = require 'path'
_ = require 'underscore'
{ spawn, exec, execSync } = require 'child_process'
Expand Down Expand Up @@ -51,14 +52,8 @@ run = (args, callback) ->
buildParser = ->
helpers.extend global, require 'util'
require 'jison'
parser = require('./lib/coffeescript/grammar').parser.generate()
# Patch Jison’s output, until https://github.com/zaach/jison/pull/339 is accepted,
# to ensure that require('fs') is only called where it exists.
parser = parser.replace "var source = require('fs')", """
var source = '';
var fs = require('fs');
if (typeof fs !== 'undefined' && fs !== null)
source = fs"""
# We don't need `moduleMain`, since the parser is unlikely to be run standalone.
parser = require('./lib/coffeescript/grammar').parser.generate(moduleMain: ->)
fs.writeFileSync 'lib/coffeescript/parser.js', parser

buildExceptParser = (callback) ->
Expand Down Expand Up @@ -135,7 +130,7 @@ task 'build:browser', 'merge the built scripts into a single file for use in a b
var CoffeeScript = function() {
function require(path){ return require[path]; }
#{code}
return require['./coffeescript'];
return require['./browser'];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you explain this? Why browser here?

Copy link
Contributor Author

@akfish akfish Apr 18, 2017

Choose a reason for hiding this comment

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

This is for logic consistency. Since browser.js is already used as the entry for bower and webpack. It could avoid unnecessary confusion to use it for all browser related situations.

}();

if (typeof define === 'function' && define.amd) {
Expand Down Expand Up @@ -368,7 +363,7 @@ task 'bench', 'quick benchmark of compilation time', ->

# Run the CoffeeScript test suite.
runTests = (CoffeeScript) ->
CoffeeScript.register()
CoffeeScript.register() unless global.testingBrowser
startTime = Date.now()

# These are attached to `global` so that they’re accessible from within
Expand Down Expand Up @@ -440,3 +435,17 @@ task 'test:browser', 'run the test suite against the merged browser script', ->
(-> eval source).call result
testResults = runTests result.CoffeeScript
process.exit 1 unless testResults

task 'test:webpack', 'run webpack test', ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rather than this be its own Cake task, can you create a test/integrations.coffee file and put this code there? That’s where the other related tests can go, that test if the coffeescript module as required by other Node projects still works.

The top of the file should have return if global.testingBrowser like you see at the top of test/repl.coffee, as obviously a test that calls spawnNodeProcess requires Node. You can attach spawnNodeProcess to global to make it available in the test file.

args = [
"./node_modules/webpack/bin/webpack.js"
"--entry=./lib/coffeescript/browser.js",
"--output-path=#{os.tmpdir()}",
"--output-filename=coffeescript.js"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can Webpack be configured to output to stdout, rather than writing a file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See https://github.com/webpack/docs/wiki/webpack-for-browserify-users#usage.

webpack doesn't write to stdout. You need to specify a filename. It can't write to stdout because, unlike browserify, it is ready to generate multiple output files.

]

console.log 'webpack', args.slice(1).join(' ')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please avoid console.logs.


spawnNodeProcess args, 'both', (status) ->
console.log "webpack exits with #{status}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the context of the test file, this should be eq status, 0 (assuming you’re checking for a zero exit status).

process.exit(status)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please end all files with a newline.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "coffeescript",
"main": [
"lib/coffeescript/coffeescript.js"
"lib/coffeescript/browser.js"
],
"description": "Unfancy JavaScript",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions lib/coffeescript/browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/coffeescript/cake.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/coffeescript/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 128 additions & 5 deletions lib/coffeescript/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 1 addition & 11 deletions lib/coffeescript/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/coffeescript/register.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/coffeescript/repl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"directories": {
"lib": "./lib/coffeescript"
},
"main": "./lib/coffeescript/coffeescript",
"main": "./lib/coffeescript/index",
"browser": "./lib/coffeescript/browser",
"bin": {
"coffee": "./bin/coffee",
"cake": "./bin/cake"
Expand Down Expand Up @@ -46,7 +47,8 @@
"highlight.js": "~9.11.0",
"jison": ">=0.4.17",
"markdown-it": "^8.3.1",
"underscore": "~1.8.3"
"underscore": "~1.8.3",
"webpack": "^2.5.1"
},
"dependencies": {}
}
11 changes: 7 additions & 4 deletions src/browser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# `text/coffeescript` script tags, source maps via data-URLs, and so on.

CoffeeScript = require './coffeescript'
CoffeeScript.require = require
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for catching this.

compile = CoffeeScript.compile

# Use standard JavaScript `eval` to eval code.
Expand All @@ -18,11 +17,15 @@ CoffeeScript.run = (code, options = {}) ->
options.shiftLine = on
Function(compile code, options)()

# If we're not in a browser environment, we're finished with the public API.
# Export this more limited `CoffeeScript` than what is exported by
# `index.coffee`, which is intended for a Node environment.
module.exports = CoffeeScript

# If we’re not in a browser environment, we’re finished with the public API.
return unless window?

# Include source maps where possible. If we've got a base64 encoder, a
# JSON serializer, and tools for escaping unicode characters, we're good to go.
# Include source maps where possible. If weve got a base64 encoder, a
# JSON serializer, and tools for escaping unicode characters, were good to go.
# Ported from https://developer.mozilla.org/en-US/docs/DOM/window.btoa
if btoa? and JSON?
compile = (code, options = {}) ->
Expand Down
2 changes: 1 addition & 1 deletion src/cake.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fs = require 'fs'
path = require 'path'
helpers = require './helpers'
optparse = require './optparse'
CoffeeScript = require './coffeescript'
CoffeeScript = require './'

# Register .coffee extension
CoffeeScript.register()
Expand Down
Loading