欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

webpack打包单页面如何引用的js

程序员文章站 2022-04-10 10:12:52
目录结构如下图所示: webpack打包代码如下: var webpack = require('webpack'); var fs = require(...

目录结构如下图所示:

webpack打包单页面如何引用的js

webpack打包代码如下:

var webpack = require('webpack');
var fs = require('fs');
var path = require('path');
var fse = require('fs-extra');

const debug = process.env.node_env !== 'production';

function entries(jspath) {
  var dirnames = fs.readdirsync(jspath);

  var entries = {}, entry;

  for (var i = 0; i < dirnames.length; i++) {
    var dirname, basename;
    var jslist = [];
    dirname = dirnames[i];
    var files = fs.readdirsync(jspath + '/' + dirname);
    for (var j = 0; j < files.length; j++) {
      entry = files[j];

      basename = path.basename(entry, '.js');
      jslist.push(path.join(jspath, dirname, entry));
    }
    entries[path.join(dirname, 'app')] = jslist;
  }


  return entries;
}

var option = {
  entry: entries(__dirname + '/js/src'),
  output: {
    path: __dirname + '/dist/',
    filename: '[name].js'
  },
  plugins: [
    new webpack.optimize.uglifyjsplugin({
      compress: {
        warnings: false
      }
    })
  ]
}
var compiler = webpack(option);
compiler.run(function() {
  fse.copy( __dirname + '/page/', __dirname + '/dist/');
  console.log('success');
});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。