I'm writing a new app using (JavaScript) ES6
syntax through babel
transpiler and the preset-es2015
plugins, as well as semantic-ui
for the style.
import * as stylesheet from '../assets/styles/app.scss';
import * as jquery2 from '../dist/scripts/jquery.min';
import * as jquery3 from '../node_modules/jquery/dist/jquery.min';
console.log($('my-app'));
<!DOCTYPE html>
<html lang="fr">
<head>
<body>
<script src="dist/app.js"></script>
</body>
</html>
.
├── app/
│ ├── index.js
├── assets/
├── dist/
│ ├── scripts/
│ │ ├── jquery.min.js
├── index.html
├── node_modules/
│ ├── jquery/
│ │ ├── dist/
│ │ │ ├── jquery.min.js
├── package.json
└── tests/
…
"scripts": {
"build:app": "browserify -e ./app/index.js -o ./dist/app.js",
"copy:jquery": "cpy 'node_modules/jquery/dist/jquery.min.js' ./dist/scripts/",
…
},
"devDependencies": {
"babel-core": "6.3.x",
"babel-preset-es2015": "6.3.x",
"babelify": "7.2.x",
"cpy": "3.4.x",
"npm-run-all": "1.4.x",
"sassify": "0.9.x",
"semantic-ui": "2.1.x",
…
},
"browserify": {
"transform": [
[ "babelify", { "presets": [ "es2015"]}],
[ "sassify", { "auto-inject": true}]
]
}
Using classic <script>
tag to import jquery
works fine, but I'm trying to use the ES6 syntax.
jquery
to satisfy semantic-ui
using ES6 import syntax?node_modules/
directory or my dist/
(where I copy everything)?import {$,jQuery} from 'jquery';
// export for others scripts to use
window.$ = $;
window.jQuery = jQuery;
First, as @nem suggested in comment, the import should be done from node_modules/
:
Well, importing from
dist/
doesn't make sense since that is your distribution folder with production ready app. Building your app should take what's insidenode_modules/
and add it to thedist/
folder, jQuery included.
Next, the glob –* as
– is wrong as I know what object I'm importing (e.g. jQuery
and $
), so a straigforward import statement will work.
Last you need to expose it to other scripts using the window.$ = $
.
Then, I import as both $
and jQuery
to cover all usages, browserify
remove import duplication, so no overhead here! ^o^y