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

2.9.0 breaks webpack import #353

Closed
wokung opened this issue Dec 13, 2018 · 7 comments
Closed

2.9.0 breaks webpack import #353

wokung opened this issue Dec 13, 2018 · 7 comments

Comments

@wokung
Copy link

wokung commented Dec 13, 2018

Issue description: Importing tinyslider with webpack causes Issue: Object(...) is not a function

Demo link/slider setting:

Tiny-slider version: 2.9.0
Browser name && version: all browser
OS name && version:

I import it like in the docs:
import { tns } from 'tiny-slider'
let slider= tns({
container: '.slider',
items: 1,
slideBy: 'page',
autoplay: false
})

Webpack creates his own scope:
var slider = Object(tiny_slider__WEBPACK_IMPORTED_MODULE_1__["tns"])({
container: '.category-slider',
items: 1,
slideBy: 'page',
autoplay: false
}); // bind function to event

and then I get:

vue.runtime.esm.js:626 [Vue warn]: Error in mounted hook: "TypeError: Object(...) is not a function"
.....

vue.runtime.esm.js:1858 TypeError: Object(...) is not a function
at initSlider (Slider.vue?./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options:504)
at VueComponent.mounted (Slider.vue?./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options:592)
at callHook (vue.runtime.esm.js:3071)
at Object.insert (vue.runtime.esm.js:4267)
at invokeInsertHook (vue.runtime.esm.js:6090)
at VueComponent.patch [as patch] (vue.runtime.esm.js:6315)
at VueComponent.Vue._update (vue.runtime.esm.js:2800)
at VueComponent.updateComponent (vue.runtime.esm.js:2924)
at Watcher.get (vue.runtime.esm.js:3305)
at Watcher.run (vue.runtime.esm.js:3391)

@wokung
Copy link
Author

wokung commented Dec 13, 2018

Ok, I found the problem, but no solution yet.

in 2.8.8 import { tns } from 'tiny-slider' imports it from the /src/ folder.

/* harmony import / var tiny_slider_src_tiny_slider__WEBPACK_IMPORTED_MODULE_1__ = webpack_require(/! tiny-slider/src/tiny-slider */ "./node_modules/tiny-slider/src/tiny-slider.js");

in 2.9.0 import { tns } from 'tiny-slider' imports it from the /dist/ folder.

/* harmony import / var tiny_slider_src_tiny_slider__WEBPACK_IMPORTED_MODULE_1__ = webpack_require(/! tiny-slider/src/tiny-slider */ "./node_modules/tiny-slider/dist/tiny-slider.js");

When I import 2.9.0 directly with

import { tns } from 'tiny-slider/src/tiny-slider'

it works.

Is the project build correctly?

Where did you tell him to point to /dist/tiny-slider when importing the module?

@wokung
Copy link
Author

wokung commented Dec 13, 2018

Ok, I found the commit, where the author changed it:

8e10b48

However, I think the solution would be to not point to /dist/, but then we need the same (babel) compiler, to make sure not to break anything.

The /dist code only makes sense if you import the module globally

@ganlanyuan
Copy link
Owner

Oh, yeah.
I did this in reference to #338

@ivan-kleshnin
Copy link

ivan-kleshnin commented Jan 29, 2019

I don't think you should distribute dist in this form at all.

Most people expect to consume CommonJS modules so I'd build ES modules to CommonJS modules.
The simplest build pipeline can look like this:

# package.json
"scripts": {
  "prebuild": "rm -rf lib && mkdir -p lib",
  "build": "babel src -d lib",
  "build-watch": "babel --watch src -d lib",
  "prepare": "npm run build",
},
"main": "lib/index.js", // your dist/index.js

I believe you don't even need Browserify in this pipeline (what does it add?).

Also I release stuff with $ np <minor|major|patch> – it has a lot of sanity checks.

Those who stuck with jQuery mindset will consume CDN anyway. They won't import/require so main field shouldn't point to a file exposing old-school globals i.m.o.

Pls. correct me if I'm wrong, I also often find myself struggling with this library/distribution topic.

@baoanhng
Copy link

baoanhng commented Jan 25, 2020

I'm using webpack with laravel mix and still struggling to import correct tns instance before finding this issue.

There @wokung solution helped me.

@marcvangend
Copy link

The workaround to use import directly from src (import { tns } from 'tiny-slider/src/tiny-slider') works for me when simply running the app.

Unfortunately, when I run a Jest test on the component which contains this import I get an error message:

 FAIL  src/routes/Home.test.js
  ● Test suite failed to run

    /Users/user/project/node_modules/tiny-slider/src/tiny-slider.js:23
    import { raf } from './helpers/raf.js';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      2 | // Tiny Slider must be explicitly imported from its /src/ folder, see also
      3 | // https://github.com/ganlanyuan/tiny-slider/issues/353#issuecomment-447000852.
    > 4 | import { tns } from 'tiny-slider/src/tiny-slider';
        | ^
      5 | import 'tiny-slider/dist/tiny-slider.css';
      6 | import styled from 'styled-components';
      7 | import { ReactComponent as ArrowPrevIcon } from '../assets/images/icon-arrow-prev.svg';

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (src/components/HorizontalSlider.js:4:1)

I learned from https://stackoverflow.com/a/58212338/241546 that this error is caused by importing from src instead of dist. This probably means that @ivan-kleshnin is correct and the code in dist is supposed (or at least expected) to take a different form.

reubano added a commit to reubano/tiny-slider that referenced this issue Aug 23, 2021
reubano added a commit to reubano/tiny-slider that referenced this issue Aug 23, 2021
reubano added a commit to reubano/tiny-slider that referenced this issue Aug 23, 2021
ganlanyuan added a commit that referenced this issue Oct 26, 2021
Create CommonJS module builds (fixes #353)
@johnstonian
Copy link

@wokung 's solution worked for me, with one slight tweak. Had to add '.js' in the path:
import { tns } from 'node_modules/tiny-slider/src/tiny-slider.js';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants