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

using coffee-script with webpack #4276

Closed
Radivarig opened this issue Jun 30, 2016 · 18 comments
Closed

using coffee-script with webpack #4276

Radivarig opened this issue Jun 30, 2016 · 18 comments
Labels

Comments

@Radivarig
Copy link

When I require with webpack var CoffeeScript = require ('coffee-script') I get the following output

WARNING in ./~/coffee-script/lib/coffee-script/coffee-script.js
require.extensions is not supported by webpack. Use a loader instead.

WARNING in ./~/coffee-script/lib/coffee-script/coffee-script.js
require.extensions is not supported by webpack. Use a loader instead.

WARNING in ./~/coffee-script/lib/coffee-script/coffee-script.js
require.extensions is not supported by webpack. Use a loader instead.

WARNING in ./~/coffee-script/lib/coffee-script/coffee-script.js
Critical dependencies:
175:42-49 require function is used in a way in which dependencies cannot be statically extracted
179:26-33 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/coffee-script/lib/coffee-script/coffee-script.js 175:42-49 179:26-33

WARNING in ./~/coffee-script/lib/coffee-script/register.js
require.extensions is not supported by webpack. Use a loader instead.

WARNING in ./~/coffee-script/lib/coffee-script/register.js
require.extensions is not supported by webpack. Use a loader instead.

WARNING in ./~/coffee-script/lib/coffee-script/browser.js
Critical dependencies:
8:25-32 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/coffee-script/lib/coffee-script/browser.js 8:25-32

ERROR in ./~/coffee-script/bin/coffee
Module parse failed: /path_to_repo/node_modules/coffee-script/bin/coffee Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected character '#' (1:0)
    at Parser.pp.raise (/path_to_repo/node_modules/webpack/node_modules/acorn/dist/acorn.js:923:13)
    at Parser.pp.getTokenFromCode (/path_to_repo/node_modules/webpack/node_modules/acorn/dist/acorn.js:2831:8)
    at Parser.pp.readToken (/path_to_repo/node_modules/webpack/node_modules/acorn/dist/acorn.js:2526:15)
    at Parser.pp.nextToken (/path_to_repo/node_modules/webpack/node_modules/acorn/dist/acorn.js:2518:71)
    at Parser.parse (/path_to_repo/node_modules/webpack/node_modules/acorn/dist/acorn.js:1631:10)
    at Object.parse (/path_to_repo/node_modules/webpack/node_modules/acorn/dist/acorn.js:885:44)
    at Parser.parse (/path_to_repo/node_modules/webpack/lib/Parser.js:902:15)
    at DependenciesBlock.<anonymous> (/path_to_repo/node_modules/webpack/lib/NormalModule.js:104:16)
    at DependenciesBlock.onModuleBuild (/path_to_repo/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
    at nextLoader (/path_to_repo/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
 @ ./~/coffee-script/lib/coffee-script/register.js 52:13-48

ERROR in ./~/coffee-script/lib/coffee-script/coffee-script.js
Module not found: Error: Cannot resolve module 'module' in /path_to_repo/node_modules/coffee-script/lib/coffee-script
 @ ./~/coffee-script/lib/coffee-script/coffee-script.js 129:23-40 169:17-34

ERROR in ./~/coffee-script/lib/coffee-script/register.js
Module not found: Error: Cannot resolve module 'child_process' in /path_to_repo/node_modules/coffee-script/lib/coffee-script
 @ ./~/coffee-script/lib/coffee-script/register.js 7:18-42

ERROR in ./~/coffee-script/lib/coffee-script/register.js
Module not found: Error: Cannot resolve module 'module' in /path_to_repo/node_modules/coffee-script/lib/coffee-script
 @ ./~/coffee-script/lib/coffee-script/register.js 25:13-30

ERROR in ./~/coffee-script/lib/coffee-script/command.js
Module not found: Error: Cannot resolve module 'child_process' in /path_to_repo/node_modules/coffee-script/lib/coffee-script
 @ ./~/coffee-script/lib/coffee-script/command.js 16:8-32

ERROR in ./~/coffee-script/lib/coffee-script/grammar.js
Module not found: Error: Cannot resolve module 'jison' in /path_to_repo/node_modules/coffee-script/lib/coffee-script
 @ ./~/coffee-script/lib/coffee-script/grammar.js 5:11-27

ERROR in ./~/coffee-script/lib/coffee-script/repl.js
Module not found: Error: Cannot resolve module 'repl' in /path_to_repo/node_modules/coffee-script/lib/coffee-script
 @ ./~/coffee-script/lib/coffee-script/repl.js 11:13-28
webpack: bundle is now VALID.

if I include a fatter 160kb local file of coffer-script-1.9.3-min.js everything works but I'd like to avoid adding library build to source if possible.

Any ideas? Thanks

@vendethiel
Copy link
Collaborator

I'd say the error message is pretty self-explanatory: don't require coffeescript directly, use a loader.

Googling "webpack coffeescript loader" gave me https://github.com/webpack/coffee-loader as the first result.

Also, please try to refrain using github issues as a help forum – it's better to use StackOverflow.

@Radivarig
Copy link
Author

I am trying to use CoffeScript.compile("coffeescript string code") in browser so loader won't work, that is why I posted here.

@timhuff
Copy link

timhuff commented Apr 5, 2017

Future traveler: I'm struggling with the same issue. Seems to not be an unreasonable thing to want (for example, the coffeescript.org website). I was wanting to do this for the purpose of writing an Electron app. My solution was to use IPC to communicate between the browser and the renderer. The browser is a node environment, so I compile it there and then send it back.

@Radivarig
Copy link
Author

@timhuff using the built library file works for me so I added it as npm package called coffee-script_compiler to avoid adding it to my source, you can try it npm i coffee-script_compiler --save then just require it with webpack and use in browser.

@timhuff
Copy link

timhuff commented Apr 5, 2017

@Radivarig You're a life-saver, man. That's so much nicer. Thanks.

@GeoffreyBooth
Copy link
Collaborator

You want require('coffee-script/register').

@akfish
Copy link
Contributor

akfish commented Apr 13, 2017

I built a custom fork for my project with compiler API exposed for webpack.

Just npm install --save github:akfish/coffeescript#webpack and

import { compile } from 'coffee-script'

@GeoffreyBooth
Copy link
Collaborator

@akfish how does your fork compare with https://github.com/webpack-contrib/coffee-loader? As in, why would someone use your fork rather than coffee-loader?

@timhuff
Copy link

timhuff commented Apr 13, 2017

@GeoffreyBooth The issue is not compiling coffeescript with webpack. It's using the coffee-script package as a compiler inside a webpack application. In my case, I was making a coffeescript-based REPL of sorts (similar to what's on the top of the coffeescript website).

@akfish
Copy link
Contributor

akfish commented Apr 13, 2017

@GeoffreyBooth coffee-loader works at compile time, not runtime.
If someone is trying to make a tool that takes user input and compiles on-the-fly in browser, he'll need some way to pack the coffee compiler into his build.

@GeoffreyBooth
Copy link
Collaborator

Can you post a minimal example app of what you’re trying to accomplish? It sounds like you want to require something that contains a compile method?

@timhuff
Copy link

timhuff commented Apr 14, 2017

@GeoffreyBooth I don't think anyone's still attempting to do this. @Radivarig's tool solved it for me. @akfish was simply contributing his work for future visitors.

@timhuff
Copy link

timhuff commented Apr 14, 2017

@GeoffreyBooth But to answer your question, yea, the idea is to write something along the lines of:

coffee = require 'coffee-script'
output = coffee.compile userInput

And then package that up inside webpack

@akfish
Copy link
Contributor

akfish commented Apr 14, 2017

import { compile } from 'coffee-script'

// User input coffee source
let src = editor.text;

// Compile it in browser
let compiledCode = compile(src, opts)

// Run compiled code or whatever
doStuffWith(compiledCode)

Or imagining tools like codepen, jsfiddle, jsperf and etc if they want to add coffee support.

In the good old days, one can simply include the compiled coffeescript bundle with <script> tag.
But that's not how we do things in this new "Great JavaScript Fatigue™" era, where every projects, no matter big or small, should be packed equally with webpack.

Basically that's how this issue arises.

@akfish
Copy link
Contributor

akfish commented Apr 14, 2017

That and what @timhuff said.

@connec
Copy link
Collaborator

connec commented Apr 14, 2017

Does webpack not support bundling of NPM packages through import? I don't understand why the following won't work with vanilla CS' compile function:

// some file in a webpack bundle
import { compile } from 'coffeescript'

let compiledCode = compile(src, opts)

@akfish
Copy link
Contributor

akfish commented Apr 14, 2017

@connec Coffee-script's main script contains references to nodejs-only modules. Hence it cannot be packed. The alternative is to set a browser field in package.json pointing to a browser-specific entry script. I'll send a PR later.

@connec
Copy link
Collaborator

connec commented Apr 14, 2017

@akfish I see! That makes sense. Perhaps any file-handling logic should be moved into cli.coffee, since people directly using the main script (via require 'coffeescript') are also likely to be working with strings.

@akfish akfish mentioned this issue Apr 14, 2017
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants