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.com/zurb/foundation-sites/issues/7386
Essentially, when installing foundation-sites via NPM, there are references to a module "foundation" that is not found. The error:
Module not found: Error: Cannot resolve module 'foundation' in c:\Users\Matt\Documents\Projects\test\node_modules\foundation-sites\dist
@ ./~/foundation-sites/dist/foundation.js
Here's the package.json:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"dependencies": {
"foundation-sites": "6.0.5",
"webpack": "~1.12.6",
"webpack-dev-server": "~1.2",
"jquery": "2.1.1"
}
}
And here is webpack.config.js:
var path = require("path");
var webpack = require("webpack");
module.exports = {
entry: {
main: "./app/js/main.js"
},
output: {
path: __dirname,
filename: "bundle.js"
},
module: {
loaders: [
{ test: /\.css$/, loader: "style!css" },
{
test: /\.scss$/,
loaders: ["style", "css", "sass"]
},
{ test: /\.vue$/, loader: 'vue' }
],
resolve: {
modulesDirectories: ['node_modules']
}
},
sassLoader: {
includePaths: [path.resolve(__dirname, "./node_modules/foundation-sites/scss/")]
},
devServer: {
proxy: {
'/api/*': {
target: 'http://localhost:4567',
secure: false
}
}
}
};
I can work around this by including foundation via bower instead, but I want to eliminate bower and use only NPM.
Here is how I am using the hack. I put foundation and jquery in a separate entry point called vendor and loaded them with the script-loader. The only relevant bits are in the vendor entry point.
While @roberto's answer looks great, I wanted to provide a simpler solution (in which it does not require any extra vendor/foundation files).
In your webpack config use this:
And in your index.js:
NOTE #1 (thank you @mtyson)
You need to use the exports loader:
$ npm install --save exports-loader
or$ npm install --save-dev exports-loader
NOTE #2
Since jQuery is not global inside single modules (or for some other reason that is beyond my understanding), there might be problems relying on
data-
attributes for Foundation JS components. If that is the case, you can always use the pure javascript way as documented in Foundation docs.It works fine for webpack if you can tell it to ignore the
define
test for the troublesome code below:The best way to do that is to use the imports-loader and set
define
to false.