123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 'use strict';
- const path = require('path');
- const defaultSettings = require('./src/settings.js');
- const SpritesmithPlugin = require('webpack-spritesmith');
- function resolve(dir) {
- return path.join(__dirname, dir)
- }
- const name = defaultSettings.title; // page title
- // If your port is set to 80,
- // use administrator privileges to execute the command line.
- // For example, Mac: sudo npm run
- // You can change the port by the following method:
- // port = 9518 npm run dev OR npm run dev --port = 9518
- const port = process.env.port || process.env.npm_config_port || 9518; // dev port
- // All configuration item explanations can be find in https://cli.vuejs.org/config/
- module.exports = {
- /**
- * You will need to set publicPath if you plan to deploy your site under a sub path,
- * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
- * then publicPath should be set to "/bar/".
- * In most cases please use '/' !!!
- * Detail: https://cli.vuejs.org/config/#publicpath
- */
- // publicPath: '/',
- publicPath: process.env.NODE_ENV === 'production'
- ? '/admin' // [通过 ip:port/admin 访问,并注意配置route 的base] ??history模式下打包有问题, 只能使用'/' 不能是'./'否则静态资源会报错,要直接点访问只能使用hash模式('./')?? https://cli.vuejs.org/zh/config/#publicpath
- : '/',
- outputDir: 'dist',
- assetsDir: 'static',
- lintOnSave: false,
- productionSourceMap: false,
- devServer: {
- port: port,
- open: true,
- overlay: {
- warnings: false,
- errors: true
- },
- proxy: {
- '/api': {
- // prod
- target: 'http://127.0.0.1:8088',
- changeOrigin: true,
- pathRewrite: {}
- },
- '/ws': {
- target: 'http://127.0.0.1:8088',
- ws: true,
- changeOrigin: true,
- pathRewrite: {
- '^/ws' : ''
- }
- },
- '/dfs': {
- target: 'http://127.0.0.1:80',
- changeOrigin: true,
- pathRewrite: {}
- },
- },
- // before: require('./mock/mock-server.js')
- },
- transpileDependencies: [/node_modules[/\\\\](element-ui|vuex|vue-baidu-map|)[/\\\\]/], // 默认情况下 babel-loader 会忽略所有 node_modules 中的文件。如果你想要通过 Babel 显式转译一个依赖,可以在 transpileDependencies 选项中列出来。
- configureWebpack: config => {
- /*
- 细节坑。文档里面写着 需要resolve,引入图片生成的位置,
- 不加这行会报错。因为github,Readme里面有这句话
- resolve contains location of where generated image is
- (要把生成的地址resolve到modules里面。不写就错)
- 一定要加,血的教训啊
- */
- config.resolve.modules = ['node_modules', './src/assets/images'];
- // 定义一个插件数组。用来覆盖,在里面使用我们的主角
- const Plugins = [
- new SpritesmithPlugin({
- /*
- 目标小图标,这里就是你需要整合的小图片的老巢。
- 现在是一个个的散兵,把他们位置找到,合成一个
- */
- src: {
- cwd: path.resolve(__dirname, './src/icons/png'),
- glob: '*.png'
- },
- // 输出雪碧图文件及样式文件,这个是打包后,自动生成的雪碧图和样式,自己配置想生成去哪里就去哪里
- target: {
- image: path.resolve(__dirname, './src/assets/images/sprite.png'),
- css: [
- [path.resolve(__dirname, './src/assets/css/sprite.scss'), {
- // 引用自己的模板
- format: 'function_based_template'
- }]
- ]
- },
- // 自定义模板入口,我们需要基本的修改webapck生成的样式,上面的大函数就是我们修改的模板
- customTemplates: {
- 'function_based_template': templateFunction
- },
- // 样式文件中调用雪碧图地址写法(Readme这么写的)
- apiOptions: {
- cssImageRef: '~sprite.png'
- },
- // 让合成的每个图片有一定的距离,否则就会紧挨着,不好使用
- spritesmithOptions: {
- padding: 20
- }
- })
- ];
- // config里面,覆盖掉以前的,要不然不好使
- config.plugins = [...config.plugins, ...Plugins];
- // provide the app's title in webpack's name field, so that
- // it can be accessed in index.html to inject the correct title.
- return {
- name: name,
- resolve: {
- alias: {
- '@': resolve('src')
- }
- }
- }
- },
- chainWebpack(config) {
- config.plugins.delete('preload'); // TODO: need test
- config.plugins.delete('prefetch'); // TODO: need test
- // set svg-sprite-loader
- config.module
- .rule('svg')
- .exclude.add(resolve('src/icons'))
- .end();
- config.module
- .rule('icons')
- .test(/\.svg$/)
- .include.add(resolve('src/icons'))
- .end()
- .use('svg-sprite-loader')
- .loader('svg-sprite-loader')
- .options({
- symbolId: 'icon-[name]'
- })
- .end();
- // set preserveWhitespace
- config.module
- .rule('vue')
- .use('vue-loader')
- .loader('vue-loader')
- .tap(options => {
- options.compilerOptions.preserveWhitespace = true;
- return options
- })
- .end();
- config
- // https://webpack.js.org/configuration/devtool/#development
- .when(process.env.NODE_ENV === 'development',
- config => config.devtool('cheap-source-map')
- );
- config
- .when(process.env.NODE_ENV !== 'development',
- config => {
- config
- .plugin('ScriptExtHtmlWebpackPlugin')
- .after('html')
- .use('script-ext-html-webpack-plugin', [{
- // `runtime` must same as runtimeChunk name. default is `runtime`
- inline: /runtime\..*\.js$/
- }])
- .end();
- config
- .optimization.splitChunks({
- chunks: 'all',
- cacheGroups: {
- libs: {
- name: 'chunk-libs',
- test: /[\\/]node_modules[\\/]/,
- priority: 10,
- chunks: 'initial' // only package third parties that are initially dependent
- },
- elementUI: {
- name: 'chunk-elementUI', // split elementUI into a single package
- priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
- test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
- },
- commons: {
- name: 'chunk-commons',
- test: resolve('src/components'), // can customize your rules
- minChunks: 3, // minimum common number
- priority: 5,
- reuseExistingChunk: true
- }
- }
- });
- config.optimization.runtimeChunk('single')
- }
- );
- },
- css: {
- extract: false, //false表示开发环境,true表示生成环境
- sourceMap: false,
- loaderOptions: {
- postcss: {
- plugins: [
- require('postcss-px-to-viewport')({
- unitToConvert: 'px', // 需要转换的单位,默认为"px"
- viewportWidth: 1920, // 视窗的宽度,对应pc设计稿的宽度,一般是1920
- viewportHeight: 1080, // 视窗的高度,对应的是我们设计稿的高度,我做的是大屏监控,高度就是1080
- unitPrecision: 3, // 单位转换后保留的精度
- propList: [ // 能转化为vw的属性列表
- '*'
- ],
- viewportUnit: 'vw', // 希望使用的视口单位
- fontViewportUnit: 'vw', // 字体使用的视口单位
- selectorBlackList: ['div', 'li', '.a', '.b', '.c', '.d', '.e', '.f', '.g',
- '.h', '.i', '.j', '.k', '.l', '.m', '.n', '.o', '.p', '.q', '.r', '.s', '.t', '.u', '.v', '.w', '.x', '.y', '.z'], // 需要忽略的CSS选择器,不会转为视口单位,使用原有的px等单位。
- minPixelValue: 1, // 设置最小的转换数值,如果为1的话,只有大于1的值会被转换
- mediaQuery: false, // 媒体查询里的单位是否需要转换单位
- replace: true, // 是否直接更换属性值,而不添加备用属性
- exclude: /(\/|\\)(node_modules)(\/|\\)/, // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
- })
- ]
- }
- }
- }
- };
- /* 这里是我们自己修改的模板样式,webpack,会自动生成一个sprite.css的样式,有时候生成的不满意,
- 我们可以在这里修改,可以自己打印一下 data里面的参数,看着就会大概明白(先看下面的配置,最后看这个模板)
- */
- var templateFunction = function (data) {
- var shared = '.easy-icon { background-image: url(I);background-size: Wpx Hpx;display: inline-block;}'.replace('I', data.sprites[0].image).replace('W', data.spritesheet.width)
- .replace('H', data.spritesheet.height);
- var perSprite = data.sprites.map(function (sprite) {
- return '.easy-icon-N { width: Wpx; height: Hpx; background-position: Xpx Ypx; }'
- .replace('N', sprite.name)
- .replace('W', sprite.width)
- .replace('H', sprite.height)
- .replace('X', sprite.offset_x)
- .replace('Y', sprite.offset_y)
- }).join('\n');
- return shared + '\n' + perSprite
- };
|