- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 Rails 5 应用程序迁移到 Rails 6 并将 coffeescript 切换为使用 webpack 加载。我创建了一个目录 app/javascripts/coffee 并放置了一个测试 greetings.coffee,其中仅包含
console.log('Hello World')
在 application.js 中,我尝试了几种要求目录内容的方法,但除了一种(显式要求)之外,所有方法都会产生运行时错误,提示找不到模块“咖啡”。
.*\.coffee$:12 Uncaught Error: Cannot find module './.coffee'
我可以编译并更新控制台的唯一方法是明确要求该文件。这意味着必须在 application.js 中列出所有现有的 coffeescript 文件,这似乎不对。
以下是我尝试过的各种 require 排列组合 (application.js)。
require.context('coffee', false, /\.coffee$/)
require('coffee/' + name + '.coffee')
require('coffee/greetings.coffee')
目录结构:
/app/javascript/packs/application.js
/app/javascript/coffee/greetings.coffee
我的理解是,我可以将所有 coffeescript 文件从 app/assets/javascripts 移动到 app/javascript/coffee,webpack 将编译这些文件。也许事实并非如此。
有人可以解释一下我做错了什么以及如何使用 webpack 将 Rails 5 应用程序中的遗留 coffeescript 引入 Rails 6 版本吗?
(我也安装了 coffee webpack 扩展,示例 hello_coffee.coffee pack 可以正常工作)。
我已经检查了 public/packs/js 下生成的文件,它们包含来自 greetings.coffee 源文件的代码,因此肯定会编译该文件。但是当网页加载时没有任何内容记录到控制台,因此它没有执行。为什么不呢?
更新:
我将其提炼为 3 种使用 require 的方式,以及 3 个结果:
require('coffee/greetings')
这有效并在 js 控制台中生成一条消息。
require.context('coffee', true, /\.coffee$/)
这可以编译,但是当网页加载时,控制台不会输出任何内容。
require('coffee/' + name + '.coffee')
这会编译但会在控制台中产生此错误
Uncaught Error: Cannot find module './.coffee'
at webpackContextResolve (.*\.coffee$:12)
at webpackContext (.*\.coffee$:7)
at Object../app/javascript/packs/application.js (application.js:19)
at __webpack_require__ (bootstrap:19)
at Object.0 (log.js:56)
at __webpack_require__ (bootstrap:19)
at bootstrap:83
at bootstrap:83
结论是我必须手动将每个单独的 coffeescript 文件包含在 coffee 目录中,但这感觉不对。
谢谢
编辑。根据要求配置/webpacker.yml
# Note: You must restart bin/webpack-dev-server for changes to take effect
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
cache_path: tmp/cache/webpacker
webpack_compile_output: true
# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
resolved_paths: []
# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false
# Extract and emit a css file
extract_css: false
static_assets_extensions:
- .jpg
- .jpeg
- .png
- .gif
- .tiff
- .ico
- .svg
- .eot
- .otf
- .ttf
- .woff
- .woff2
extensions:
- .coffee
- .mjs
- .js
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg
development:
<<: *default
compile: true
# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
public: localhost:3035
hmr: false
# Inline should be set to true if using HMR
inline: true
overlay: true
compress: true
disable_host_check: true
use_local_ip: false
quiet: false
pretty: false
headers:
'Access-Control-Allow-Origin': '*'
watch_options:
ignored: '**/node_modules/**'
test:
<<: *default
compile: true
# Compile test packs to a separate directory
public_output_path: packs-test
production:
<<: *default
# Production depends on precompilation of packs prior to booting for performance.
compile: false
# Extract and emit a css file
extract_css: true
# Cache manifest.json for performance
cache_manifest: true
最佳答案
到目前为止,我认为对此有两个可能的答案:
require('coffee/greetings.coffee')
function importAll (r) {
r.keys().forEach(r);
}
importAll(require.context('coffee', true, /\.coffee$/));
在 application.js 中
require('coffee')
看起来 require.context 实际上并没有使文件可用,而是返回一个对象,该对象具有一个属性,该属性是所有可用文件的数组 - 然后必须单独要求(?)它们。
我确定这不是完全正确的答案,但它为我的问题提供了一个可行的解决方案 - 目前 - 我欢迎任何更正确的答案。
希望这会及时帮助其他人。
关于javascript - Rails 6 和 webpack : compile all coffeescript from one directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62231646/
我有以下 Coffeescript: $ -> $('#new_event').submit -> $.post( $(this).attr('acti
Coffeescript docs包含以下简介 String Interpolation, Block Strings, and Block Comments Ruby-style string in
如何获得类似于 HTML 的 target='_blank' 的行为在 CoffeeScript 里面? 到目前为止,我已经尝试过: window.location = "/site/#{pk}/go
array = [1,2,3,4] for num in array //do something num的值是多少在函数的其余部分?是否num得到范围到循环? 最佳答案 不,num不会被限制
Coffeescript 使用存在运算符来确定变量何时存在,并且在 coffeescript documentation 中它表明 something?将编译为 something !== undef
我一直在阅读一些关于 coffeescript 的继承模型的内容,我感觉自己正处于一场我真的不理解的意识形态辩论的边缘。所以,我会非常高兴地发现我只是以错误的方式做事。 基本上我正在做的是编写一组小部
这个问题在这里已经有了答案: How to iterate over the keys and values in an object in CoffeeScript? (4 个回答) 8年前关闭。
让我们定义这个简单的代码: class Foo @foo = 'blah' console.log(@foo) class Bar extends Foo constructor: ()
除了这些示例之外,我正在努力寻找任何好的 CoffeeScript 和模式匹配示例: {x, y} = sprite css = {opacity, fontFamily} 我在 Erlang 中使用
我想做一个 if 语句来检查一个对象是否是一个空对象。 空对象是指如果我执行 console.log(object) 它会打印出 {}。 我该怎么做呢? 最佳答案 myObject = {} if O
在 JS 中创建文字数组时: [{ name: 'david', value: 'blue' }, { name: 'harold', value: 'orange' }] 我能看到在 Coffees
我的问题类似于发布的 here .本质上我想读一个配置file.json看起来像这样: { "message": "Error in #{baseName} at #{directory}" }
如果我有一个类,则将多个参数传递给: class Foo constructor: (parameters) -> @bar = parameters.bar @moo = paramet
coffeescript中是否有 namespace 的内在支持? 适当的命名空间似乎确实可以帮助Coffeescript有所帮助,尽管我似乎无法找到任何迹象表明存在对此的支持。 最佳答案 既可以在自
我有一个具有一些jquery事件侦听器的coffeescript类。我想使用粗箭头=>以避免引用该类,但是我仍然需要引用通常与this一起使用的元素。如何同时使用两者? class PostForm
我要转换 console.log({ a: 'a' }, { b: 'b' }); 进入 CoffeeScript。我发现的唯一方法是 console.log a: 'a', b:
我真的很喜欢这个: var value = maxValue > minValue ? minValue : maxValue; Coffeescript 中是否有同样简洁的东西? 最佳答案 valu
我想在coffeescript中编写一个静态帮助器类。这可能吗? 类别: class Box2DUtility constructor: () -> drawWorld: (world, co
super 简单的coffeescript问题 circles = [] for coordinate, i in coordinates circles[i] = new MakeCircl
我在看this great video由 Jeremy 在 CoffeeScript 上发表。他解释说,CoffeeScript 的理想之一是让“一切都是表达式”。 CoffeeScript 离这个理
我是一名优秀的程序员,十分优秀!