一聚教程网:一个值得你收藏的教程网站

热门教程

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

时间:2022-06-29 01:30:45 编辑:袖梨 来源:一聚教程网

目录结构如下图所示:

webpack打包代码如下:

 

 代码如下复制代码

varwebpack = require('webpack');

varfs = require('fs');

varpath = require('path');

varfse = require('fs-extra');

 

const debug = process.env.NODE_ENV !=='production'

 

functionentries(jsPath) {

  vardirnames = fs.readdirSync(jsPath);

 

  varentries = {}, entry;

 

  for(vari = 0; i < dirnames.length; i++) {

    vardirname, basename;

    varjsList = [];

    dirname = dirnames[i];

    varfiles = fs.readdirSync(jsPath +'/'+ dirname);

    for(varj = 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;

  }

 

 

  returnentries;

}

 

varoption = {

  entry: entries(__dirname +'/js/src'),

  output: {

    path: __dirname +'/dist/',

    filename:'[name].js'

  },

  plugins: [

    newwebpack.optimize.UglifyJsPlugin({

      compress: {

        warnings:false

      }

    })

  ]

}

varcompiler = webpack(option);

compiler.run(function() {

  fse.copy( __dirname +'/page/', __dirname +'/dist/');

  console.log('success');

});

 

热门栏目