How to set the Webpack project root?
Rasmus Schultz

Rasmus Schultz @mindplay

About: Passionate web-developer since 1998 using various languages, databases and tools. Opinions all my own.

Joined:
Dec 25, 2017

How to set the Webpack project root?

Publish Date: Feb 21 '18
10 4

I've only recently started using WebPack. (with Typescript, which I've been using for a long time, and Preact since recently.)

I'm really happy with it so far - it's working great, both during development, and when building the production assets.

My question here is actually simple: how is a webpack project rooted? It doesn't seem to automatically root itself neither from the locations of the package.json or webpack.config.json files, and I can't find an option to root it.

I have a project with the following structure:

root/
  package.json
  styleguide/
    webpack.config.js
    styleguide.tsx
    tsconfig.json
Enter fullscreen mode Exit fullscreen mode

I have other sub-projects besides styleguide in this project - that one happens to use webpack, others use e.g. tsc or node-sass etc., is why it's structured like this.

My webpack configuration looks like this:

const path = require("path");
const webpack = require("webpack");

module.exports = {
    entry: {
        "styleguide-deps": ["prismjs", "preact", "js-beautify"],
        "styleguide": "./styleguide.tsx"
    },

    output: {
        filename: "[name].js",
        path: path.join(__dirname, "/../assets/demo/"),
        libraryTarget: "amd"
    },

    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: "styleguide-deps",
            minChunks: Infinity,
        })
    ],

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".ts", ".tsx", ".js", ".json"]
    },

    module: {
        rules: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            { test: /\.tsx?$/, loader: "awesome-typescript-loader" },

            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
        ]
    },

    // Don't bundle these modules, but request them at runtime (client-side, via require.js)
    externals: {
        "kodus": "kodus"
    }
};

Enter fullscreen mode Exit fullscreen mode

My package.json has a scripts entry like the following:

    "scripts": {
        "build-styleguide-ts": "cd styleguide && webpack",
    },

Enter fullscreen mode Exit fullscreen mode

And the question I'm asking is, why do I have to cd into styleguide to run webpack?

I've tried e.g. webpack --config styleguide/webpack.config.json from the project root, and I can't get it to work.

I can use absolute paths in most places, but not for entry.styleguide, and it seems ./ works relative to the current directory, rather than to the project root.

My objection is that no script should ever rely on the current directory, as that's just global state, and I generally use absolute paths everywhere, but that hasn't worked for me with webpack.

How can I remove the dependency on the current directory?

Comments 4 total

  • Jess Lee
    Jess LeeFeb 21, 2018
    • Nick Taylor
      Nick TaylorMar 5, 2018

      @mindplay , the reason you need to cd into your styleguide folder is because styleguide has your tsconfig.json and webpack.config.js in there. If you don't want the styleguide to be the root, simply move your tsconfig.json and webpack.config.js to your root folder. If for some reason you can't change that, simply call webpack with an explicit configuration file and point it to ./styleguide/webpack.config.js, e.g.

      "scripts": {
            "build-styleguide-ts": "webpack --config ./styleguide/webpack.config.js",
      },
      

      I have a typescript preact boilerplate if you want to take a peek.

      Hit me up if you're still stuck.

  • Rasmus Schultz
    Rasmus SchultzMar 5, 2018

    I completely missed it, but here it is - it's called context... I must have look for this 20 times, and was expecting something like "rootDir", but - there it is :-)

  • Sean Larkin
    Sean LarkinMar 26, 2018

    webpack.js.org/configuration/context

Add comment