I am working on using Zurb Foundation with WebPack and NPM, without Bower.
The problem I am encountering is the same as this below:
https://github.c
For myself, I used this solution:
I'm using the Laravel framework, so first I added the .webpackConfig (...)
method to the webpack.mix.js
file:
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css')
// By default, files from the directory "/node_modules" are not processed by the Babel loader,
// so in the Webpack configuration,
// an exception was added for loading from the directory "/node_modules/foundation-sites/js".
.webpackConfig({
module: {
rules: [{
// Section "// Babel Compilation." from "/node_modules/laravel-mix/src/builder/webpack-rules.js"
test: /\.jsx?$/,
// Thanks for the help with "exclude": http://qaru.site/questions/97960/import-a-module-from-nodemodules-with-babel-but-failed/624982#624982
exclude(file) {
if (file.startsWith(__dirname + '/node_modules/foundation-sites/js')) {
return false;
}
return file.startsWith(__dirname + '/node_modules');
},
use: [
{
loader: 'babel-loader',
options: Config.babel()
}
]
}]
}
});
Note:
To install Foundation
I used the package https://github.com/laravel-frontend-presets/zurb-foundation. And added the code to load Foundation
into the /resources/assets/js/bootstrap.js
file:
/**
* We'll load jQuery and the Foundation jQuery plugin which provides support
* for JavaScript based Foundation features such as modals and tabs. This
* code may be modified to fit the specific needs of your application.
*/
try {
window.$ = window.jQuery = require('jquery');
require('foundation-sites/dist/js/foundation'); // 'foundation.min' can also be used if you like
// The app plugins for the Foundation
require('./plugins/entries/foundation');
} catch (e) {}
Secondly, I created the file /resources/assets/js/plugins/entries/foundation.js
(The file is included in the code above // The app plugins for the Foundation.
). In which I included my modules (example):
import { CropText } from '../cropText';
Foundation.plugin(CropText, 'CropText');
Third, I created two files for including the Foundation
plugins:
1) /resources/assets/js/plugins/foundation.plugin.js
// By default, files from the directory "/node_modules" are not processed by the Babel loader,
// so in the Webpack configuration,
// an exception was added for loading from the directory "/node_modules/foundation-sites/js".
import { Plugin } from 'foundation-sites/js/foundation.plugin';
export {Plugin};
2) /resources/assets/js/plugins/foundation.util.mediaQuery.js
// By default, files from the directory "/node_modules" are not processed by the Babel loader,
// so in the Webpack configuration,
// an exception was added for loading from the directory "/node_modules/foundation-sites/js".
import { MediaQuery } from 'foundation-sites/js/foundation.util.mediaQuery';
export {MediaQuery};
In the fourth, I created a file for my plugin using the Foundation
plugins template, which include 2 of the above files:
/resources/assets/js/plugins/cropText.js
'use strict';
import $ from 'jquery';
import { MediaQuery } from './foundation.util.mediaQuery';
import { Plugin } from './foundation.plugin';
/**
* CropText plugin.
* @plugin app.cropText
*/
class CropText extends Plugin {
/**
* Creates a new instance of CropText.
* @class
* @name CropText
* @fires CropText#init
* @param {Object} element - jQuery object to add the trigger to.
* @param {Object} options - Overrides to the default plugin settings.
*/
_setup(element, options = {}) {
this.$element = element;
this.options = $.extend(true, {}, CropText.defaults, this.$element.data(), options);
this.className = 'CropText'; // ie9 back compat
this._init();
}
/**
* Initializes the CropText plugin.
* @private
*/
_init() {
MediaQuery._init();
this.cropText();
}
/**
* Crops the text.
*/
cropText() {
var size = +this.options.cropSize;
$(this.$element).each(function(i, value) {
var $valueText = $(value).text(),
$valueHtml = $(value).html();
if($valueText.length > size){
$(value).html('' + $valueText.slice(0, size).trim() + '' + '...').wrapInner('');
var revealId = '#' + $(value).attr('data-open');
if ($(revealId + ' .close-button').attr('data-close') != undefined) {
$(revealId + ' .close-button').before($valueHtml);
} else {
$(revealId).append($valueHtml);
}
new Foundation.Reveal($(revealId), {
'data-overlay' : false
});
} else {
$(value).removeAttr('data-open').removeAttr('tabindex');
}
});
}
}
/**
* Default settings for plugin
*/
CropText.defaults = {
/**
* The size of the cropped text.
* @option
* @type {number}
* @default 255
*/
cropSize: 255
};
export {CropText};
It's all. Next, I just need to include a standard JavaScript file in the HTML code of the page and initialize the Foundation (example):
/resources/views/layouts/app.blade.php
P.S. Sorry for my English ;-), I used Google Translate.