webpack - remove “webpackBootstrap” code

前端 未结 3 1443
-上瘾入骨i
-上瘾入骨i 2020-12-06 10:08

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 webpac

相关标签:
3条回答
  • 2020-12-06 10:08

    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

    0 讨论(0)
  • 2020-12-06 10:14

    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?
    0 讨论(0)
  • 2020-12-06 10:20

    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,
    }
    
    0 讨论(0)
提交回复
热议问题