我问了问题 Module not found: Error: Can't resolve 'cropbox'
但我没有得到答案,所以我必须专注于更具体的问题并在 StackOverflow 中搜索,
所以我得到三个问题
1 rails 不加载cropbox.js
2 clopbox.js 无法导出cropbox 功能
3 fileUplod.js 无法导入cropbox 功能
这就是上述问题之一的结果
application.html.erb 的一部分:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application' %>
<%= javascript_pack_tag 'cropbox' %>
<%= javascript_pack_tag 'fileUpload'%> #Here is deleted
结果,我故意不编译 fileUpload,rails 可以加载 clopbox.js 所以,我缩小了 2 个问题
有人愿意告诉我这些问题吗?
环境:
导轨 6.0.2 红宝石 2.5.3
应用程序/javascript/packs/fileUpload.js:
import clopbox from './packs/cropbox' **#Here is problem **
uppy.on('upload-success', (file, response) => {
// retrieve uploaded file data
const uploadedFileData = response.body['data']
// set hidden field value to the uploaded file data so that it's submitted
// with the form as the attachment
hiddenInput.value = JSON.stringify(uploadedFileData)
cropbox(imagePreview, response.uploadURL, {
onCrop(detail) {
let fileData = JSON.parse(hiddenInput.value)
fileData['metadata']['crop'] = detail
hiddenInput.value = JSON.stringify(fileData)
}
})
})
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
check_yarn_integrity: false
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:
- .mjs
- .js
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg
development:
<<: *default
compile: true
# Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock,
and
node_modules
check_yarn_integrity: 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
应用程序/javascript/packs/cropbox.js:
import 'cropperjs/dist/cropper.css'
import Cropper from 'cropperjs'
function cropbox(image, url, { onCrop }) {
image.src = url
new Cropper(image, {
aspectRatio: 1,
viewMode: 1,
guides: false,
autoCropArea: 1.0,
background: false,
zoomable: false,
crop: event => onCrop(event.detail)
})
}
export default cropbox
form.html.erb
<%= form_with model: @blog_form , url: user_blogs_path ,local: true do |f|
%>
<div class="field">
<% f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<% f.label :content %>
<%= f.text_area :content %>
</div>
<div class="field ">
<% f.label :user_id %>
<%= f.hidden_field :user_id, value: current_user.id %>
</div>
<div class ="field form-group">
<%= f.fields_for :photos, Photo.new do |photos_fileds| %>
<%= photos_fileds.label :image , class: "form-control" %>
<%= photos_fileds.hidden_field :image, class: "upload-data", value:
photos_fileds.object.cached_image_data %>
<%= photos_fileds.file_field :image , class: "form-control ", id:
"select-
files"%><br/>
<div class="image-preview">
<img id="image" src="<%= photos_fileds.object.image_url(:medium) %>"
height="300" class="rounded" >
</div>
<% end %>
</div>
<%= f.submit "create", class: "btn btn-primary" %>
<% end %>
日志:
ERROR in ./app/javascript/packs/fileUpload.js
Module not found: Error: Can't resolve './packs/cropbox' in
'/home/master/prot/prottype2/app/javascript/packs'
@ ./app/javascript/packs/fileUpload.js 1:0-38
[Webpacker] Everything's up-to-date. Nothing to do
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 39733ms (Views: 39715.1ms | ActiveRecord: 0.6ms | Allocations: 1