123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- 'use strict'
- const path = require('path')
- const config = require('../config')
- const ExtractTextPlugin = require('extract-text-webpack-plugin')
- const packageConfig = require('../package.json')
- const HtmlWebpackPlugin = require('html-webpack-plugin');
- const merge = require('webpack-merge');
- const glob = require('glob');
- const pathSrc = path.resolve(__dirname, '../src');
- const devPathSrc = path.resolve(__dirname, '../../../src');
- let devModules = ['all'];
- if (pathSrc.indexOf('node_modules') > -1) {
- devModules = require('../../../cors.js').devModules;
- }
- const getEntries = function (suffix, polyfill) {
-
- const entryHtml = glob.sync(pathSrc + '/views/**/app.html');
- const devEntryHtml = glob.sync(devPathSrc + '/views/**/app.html');
- const entries = {};
- let setEntries = function (filePath, _chunk) {
- _chunk = _chunk.substring(0, _chunk.lastIndexOf('/'));
- let flag = true;
- flag = false;
- for (let item of devModules) {
- if (item && (_chunk.indexOf('views/' + item) >= 0 || item === 'all')) {
- flag = true;
- break;
- }
- }
- if (flag) {
- let arr = _chunk.split('/'),
- larr = [],
- rarr = [];
- console.log('arr: ', arr);
- larr = arr.slice(0, 2);
- _chunk = larr.join('/');
- if (arr.length > 2) {
- rarr = arr.slice(2, arr.length);
- _chunk = _chunk + '/' + rarr.join('-');
- }
- if (suffix) {
- filePath = filePath.replace('.html', suffix);
- }
- if (polyfill) {
- entries[_chunk] = ['babel-polyfill', filePath];
- } else {
- entries[_chunk] = filePath;
- }
- }
- }
- entryHtml.forEach((filePath) => {
-
- let _chunk = filePath.split(pathSrc.replace(/\\/g, '/') + '/')[1];
- setEntries(filePath, _chunk);
- });
- devEntryHtml.forEach((filePath) => {
-
- let _chunk = filePath.split(devPathSrc.replace(/\\/g, '/') + '/')[1];
- setEntries(filePath, _chunk);
- });
- return entries;
- };
- exports.entries = function () {
-
- let _entries = getEntries('.js', true);
- return _entries;
- };
- exports.htmlPlugins = function () {
- let entryHtmls = getEntries('.html');
- let arr = [];
- for (let _chunk in entryHtmls) {
- console.log('loading chunk:', _chunk);
-
- let conf = {
- favicon: pathSrc + '/assets/img/favicon.ico',
- template: entryHtmls[_chunk],
-
- filename: _chunk + '.html',
- chunks: [_chunk],
- inject: true
- };
- if (process.env.NODE_ENV === 'production') {
- console.log('package content: ', _chunk)
- conf = merge(conf, {
- chunks: ['manifest', 'vendor', _chunk],
- minify: {
-
- removeComments: true,
- collapseWhitespace: true
- },
- chunksSortMode: 'dependency'
- })
- }
- arr.push(new HtmlWebpackPlugin(conf));
- }
- console.log('arr:', arr);
- return arr;
- };
- exports.assetsPath = function (_path) {
- const assetsSubDirectory = process.env.NODE_ENV === 'production' ?
- config.build.assetsSubDirectory :
- config.dev.assetsSubDirectory
- return path.posix.join(assetsSubDirectory, _path)
- }
- exports.cssLoaders = function (options) {
- options = options || {}
- const cssLoader = {
- loader: 'css-loader',
- options: {
- sourceMap: options.sourceMap
- }
- }
- const postcssLoader = {
- loader: 'postcss-loader',
- options: {
- sourceMap: options.sourceMap
- }
- }
-
- function generateLoaders(loader, loaderOptions) {
- const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]
- if (loader) {
- loaders.push({
- loader: loader + '-loader',
- options: Object.assign({}, loaderOptions, {
- sourceMap: options.sourceMap
- })
- })
- }
-
-
- if (options.extract) {
- return ExtractTextPlugin.extract({
- use: loaders,
-
-
-
- publicPath: '../../../',
- fallback: 'vue-style-loader'
- })
- } else {
- return ['vue-style-loader'].concat(loaders)
- }
- }
-
- return {
- css: generateLoaders(),
- postcss: generateLoaders(),
- less: generateLoaders('less'),
- sass: generateLoaders('sass', {
- indentedSyntax: true
- }),
- scss: generateLoaders('sass'),
- stylus: generateLoaders('stylus'),
- styl: generateLoaders('stylus')
- }
- }
- exports.styleLoaders = function (options) {
- const output = []
- const loaders = exports.cssLoaders(options)
- for (const extension in loaders) {
- const loader = loaders[extension]
- output.push({
- test: new RegExp('\\.' + extension + '$'),
- use: loader
- })
- }
- return output
- }
- exports.createNotifierCallback = () => {
- const notifier = require('node-notifier')
- return (severity, errors) => {
- if (severity !== 'error') return
- const error = errors[0]
- const filename = error.file && error.file.split('!').pop()
- notifier.notify({
- title: packageConfig.name,
- message: severity + ': ' + error.name,
- subtitle: filename || '',
- icon: path.join(__dirname, 'logo.png')
- })
- }
- }
|