简体   繁体   English

如何导入JavaScript库的源文件?

[英]How can I import the source files of a JavaScript library?

I'm using a library called selection.js . 我正在使用一个名为selection.js的库。 In my app I'm importing from node_modules using: 在我的应用程序中,我使用以下命令从node_modules导入:

import * as Selection from '@simonwep/selection-js'

What I would like to do though, is edit the source of these files. 我想做的是编辑这些文件的源代码。 I copied the contents of the source here and placed it in a folder of my app. 我在这里复制了源的内容,并将其放置在应用程序的文件夹中。

I then attempted: 然后,我尝试:

import * as Selection from './selection-js/selection.js'

But this throws errors (I think due to module.exports). 但这会引发错误(我认为是由于module.exports)。 Do I need to change the export from selection.js to get it work? 我是否需要从selection.js更改导出才能使其正常工作? Or is more involved? 还是参与其中?

EDIT: @JaromandaX pointed out that I need to clone and build the library myself. 编辑:@JaromandaX指出,我需要自己克隆和构建库。 That answers my question. 那回答了我的问题。

New Question: Is there a way to build this project without minification/uglification? 新问题:有没有没有缩小/丑化就构建这个项目的方法? I've tried adding compress: false to the webpack.config.js with little success. 我尝试将compress: false添加到webpack.config.js中 ,但收效甚微。

I managed to get this working by changing the following: 通过更改以下内容,我设法使它起作用:

selection.js selection.js

// module.exports = Selection; <-- module exports is read only

export default Selection; // <-- added this instead

my app 我的应用

// import * as Selection from '@simonwep/selection-js'; // <-- from this

import Selection from './selection-js/selection'; //<-- to this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM