26

I am using WebPack to concat js files and output to a dist folder. All this seems to work, however my problem is that I want to concat all the js files without extra webpack boostrap code

/******/ (function(modules) { // (/******/ (function(modules) { // webpackBootstrap)......

Is there anyway to prevent webpack from adding that code rather just take the plain js files and concat them (like gulp-concat).

user7886649
  • 351
  • 1
  • 3
  • 6
  • 1
    I'm not sure about the answer to your question since webpack adds code to make dependencies work. If you're ONLY concatting files webpack might be more than you need – Andy Ray Apr 19 '17 at 01:48
  • 9
    Webpack should have an option to omit it :( – JCarlosR Jun 30 '17 at 13:53

4 Answers4

4

Assuming you are using Webpack 4, drop the runtimeChunk to the config file so Webpack will generate a runtime .js file which contains only the webpackBootstrap part, leaving your output file clean:

optimization: {
    runtimeChunk: true,
}
Ton Nguyen
  • 153
  • 2
  • 4
2

It's possible using webpack-merge-and-include-globally.

webpack.config.js

const path = require('path');
const MergeIntoSingleFilePlugin = require('webpack-merge-and-include-globally');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: '[name]',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new MergeIntoSingleFilePlugin({
      "bundle.js": [
        path.resolve(__dirname, 'src/util.js'),
        path.resolve(__dirname, 'src/index.js')
      ],
      "bundle.css": [
        path.resolve(__dirname, 'src/css/main.css'),
        path.resolve(__dirname, 'src/css/local.css')
      ]
    })
  ]
};

https://code.luasoftware.com/tutorials/webpack/merge-multiple-javascript-into-single-file-for-global-scope/#webpack-merge-and-include-globally

Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
Desmond Lua
  • 6,142
  • 4
  • 37
  • 50
  • I think the options need to be specified under a `file: {}` key (e.g. `new MergeIntoSingleFilePlugin({ file: { ... })` – abhchand Apr 08 '21 at 18:52
2

you should just change the library type of output in webpack.config.js file to var like this:

    output: {
      library: {
      name: "[name]",
      type: "var" 
           },
     libraryTarget: "umd",
     path: path.resolve(__dirname, "dist"),
},
Mehdi bayat
  • 199
  • 2
  • 13
0

I have the same problem. This webpack extra code was breaking my global namespace and my global variables were not working when using then inside my <script> tags.

I tried Desmond Lua answer but it failed with bunch of errors. To fix, I stopped using webpack and started using gulp, gulp-concat, gulp-typescript:

packages.json

{
  "scripts": {
    "gulp": "gulp main"
  },
  "dependencies": {
    "@types/gulp": "^4.0.6",
    "@types/gulp-concat",
    "@types/gulp-typescript",
    "gulp": "^4.0.2",
    "gulp-concat": "^2.6.1",
    "gulp-resolve-dependencies": "^3.0.1",
    "gulp-typescript": "^6.0.0-alpha.1",
    "typescript": "^3.7.3"
  }
}

src/someimport.ts

class SomeClass {
    delay: number;
}

src/main.ts

/// <reference path="./someimport.ts" />

someclass = new SomeClass();
someclass.delay = 1;

This main gulp task (on gulpfile.js) targets only the src/main.js file, resolving all its /// <reference path=... include references. These includes are know as Triple-Slash Directives and they are used only for transpilers tools to combine files. In our case, they are used explicitly by .pipe(resolveDependencies({ and by typescript itself when checking the file for missing types, variables, etc.

  1. https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html
  2. When do I need a triple slash reference?

Refer to https://github.com/ivogabe/gulp-typescript#api-overview if you would like to customize the var tsProject = ts.createProject call and not use a tsconfig.json file or override its parameters.

gulpfile.js

var gulp = require("gulp");
var concat = require('gulp-concat');
var resolveDependencies = require('gulp-resolve-dependencies');

var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");

gulp.task("main", function() {
  return gulp
    .src(["src/main.ts"])
    .pipe(resolveDependencies({
      pattern: /^\s*\/\/\/\s*<\s*reference\s*path\s*=\s*(?:"|')([^'"\n]+)/gm
    }))
    .on('error', function(err) {
        console.log(err.message);
    })
    .pipe(tsProject())
    .pipe(concat('main.js'))
    .pipe(gulp.dest("build/"));
});

If you wold like to target all your type script project files instead of only src/main.ts, you can replace this:

  return gulp
    .src(["src/main.ts"])
    .pipe(resolveDependencies({
    ...
// -->
  return tsProject
    .src()
    .pipe(resolveDependencies({
    ...

If you do not want to use typescript, you can use this simplified gulpfile.js and remove all typescript includes from package.json:

gulpfile.js

var gulp = require("gulp");
var concat = require('gulp-concat');
var resolveDependencies = require('gulp-resolve-dependencies');

gulp.task("main", function() {
  return gulp
    .src(["src/main.js"])
    .pipe(resolveDependencies({
      pattern: /^\s*\/\/\/\s*<\s*reference\s*path\s*=\s*(?:"|')([^'"\n]+)/gm
    }))
    .on('error', function(err) {
        console.log(err.message);
    })
    .pipe(concat('main.js'))
    .pipe(gulp.dest("build/"));
});

packages.json

{
  "scripts": {
    "gulp": "gulp main"
  },
  "dependencies": {
    "gulp": "^4.0.2",
    "gulp-concat": "^2.6.1",
    "gulp-resolve-dependencies": "^3.0.1"
  }
}

Then, after running the command npm run gulp, the file build/main.js is created with the following as its contents:

build/main.js

class SomeClass {
}
/// <reference path="./someimport.ts" />
someclass = new SomeClass();
someclass.delay = 1;

Which allows me to include it in the browser with the script tag, after serving the build directory files:

<html>
    <head>
        <script src="main.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(someclass.delay);
        </script>
    </body>
</html>

Related questions:

  1. https://www.typescriptlang.org/docs/handbook/gulp.html
  2. Can I use the typescript without requireJS?
  3. Gulp simple concatenation of main file that requires another JS file
  4. Client on node: Uncaught ReferenceError: require is not defined
  5. How can typescript browser node modules be compiled with gulp?
  6. Concatenate files using babel
  7. How to require CommonJS modules in the browser?
  8. Is there an alternative to Browserify?
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144