webpack.config.js
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: '/src/index.js',
output: {
path: path.resolve(__dirname, '..','..', 'public_html', 'dist', 'webpack'),
filename: 'dist.js',
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
}),
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
},
],
},
devtool: 'source-map',
};
index.js
var _ = require('lodash');
var $ = require('jquery');
test.html
Test Lodash
window.onload = function() {
const shuffled = _.shuffle([1, 2, 3, 4, 5]);
console.log('Shuffled array:', [1, 2, 3, 4, 5]);
document.getElementById("parentID").innerHTML+= "shuffled array: " + shuffled + '
';
$(document).ready(function() {}
};
ошибка: Uncaught ReferenceError: $ не определен
Я реализую NPM и webpack, и в настоящее время нам нужно ссылаться на него со страниц HTML, таких как test.html. Я просто хочу таким образом объединить и ссылаться на пакеты из NPM.
Лодэшная часть этого работает правильно.
Подробнее здесь: https://stackoverflow.com/questions/792 ... e-globally