survivejs / webpack-book

From apprentice to master (CC BY-NC-ND)
https://survivejs.com/webpack/
2.42k stars 319 forks source link

cannot load external moduls locate in subfolder #81

Closed Trispa closed 8 years ago

Trispa commented 8 years ago

hi since 3 days i try to resolve a probleme about webpack configuration. my code was with requirejs an now i use webpack. i used this link for support : https://gist.github.com/xjamundx/b1c800e9282e16a6a18e. in fact, i have a probleme to load my modules : in my webpack-config.js file i have some things like that :

var webpack = require("webpack");

module.exports = {
    entry: {
        app: ["./public/login.ts",
            "./scripts/collections.js",
            "./cb/bo/index.js",
            "./public/components/HSplitLayout.js"
        ],

        lib: ["./scripts/sax.js",
            "./scripts/fontIcons.ts",
            "./scripts/collections.js",

        ],

        test:["./test/index.js"]
    },
    output: {
        path: './builds',
        filename: '[name].js',
        publicPath: './builds/',
        libraryTarget: "var"
    },
    resolve :{
        alias:{
            "sax": "./scripts/sax.js",
            "fontIcon": "./scripts/fontIcons.js",
            "underscore" : "./scripts/underscore.js",
            "collections" : "./scripts/collections.js"
        },

    },
    module: {
        loaders: [
            {test: /\.html/, loader: 'html'},
            {test: /\.ts/, loader: 'ts-loader'},
            {test: /underscore/, loader:'exports?_' , exclude: /node_modules/,},
            {test: /collections/, loader:"exports?collections", exclude: /node_modules/,}
        ]
    },

    plugins: [
      new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "windows.$": "jquery",
            "windows.jQuery": "jquery"
        }),

    ],
    resolveLoader: {
        modulesDirectories: ['scripts','node_modules']
    },

};

in my index file ia have :

<script src="/builds/lib.js"></script>
<script src="/builds/app.js"></script>

but i stil have error : Uncaught ReferenceError: collections is not defined

can someone helps me please ?

bebraw commented 8 years ago

It's hard to say anything concrete based on that error alone. Are you referring to the file containing collections through an import statement? If you expect it to be global, look into shimming modules.

By the way, Stack Overflow might work better for generic issues like this. It would be good to restrict this tracker to book related problems.

Trispa commented 8 years ago

thanks @bebraw for help. I rephrase my question, I have a scripts folder that contains my modules that i would like to load with webpack. it's seem webpack knows only modules located in node-modules folder. So what can i tell webpack to load my modules which located in my scriptsfolder. i tried to configure pakage.json to add scripts folder in node-modules with npm like automaticly but it's not work.

bebraw commented 8 years ago

So what can i tell webpack to load my modules which located in my scriptsfolder.

You could try resolve.modulesDirectories. Add the scripts directory to the array and it should look up modules from there as well as you might expect.

Trispa commented 8 years ago

I tried that but i have stil error : collections is not defined this error refere to here (near new whene creting an insance og collections :

var EventBusHandler = (function () {
        function EventBusHandler() {
            this.eventBusHandlerState = vertx.EventBus.CLOSED;
            this.queue = new collections.Queue();
bebraw commented 8 years ago

It's hard to tell anything based on that alone. As long as you have an import statement for collections within that file, it should definitely work.

Trispa commented 8 years ago

I don't use import statement but exports loader you can see again my webpack.config.js content in my first comment.

bebraw commented 8 years ago

I don't use import statement but exports loader you can see again my webpack.config.js content in my first comment.

But as per documentation exports-loader just writes a exports["file"] = file kind of line. It won't make the code available to your other module.

Is there some particular reason why you cannot import directly? Maybe you want to use expose-loader instead? The book discusses one way to split.

As this isn't book related, I'm closing this for now. But feel free to continue on those paths.