Advertisement
Guest User

Untitled

a guest
Mar 9th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const path = require('path');
  2. const fs = require('fs-extra');
  3. const webpack = require('webpack');
  4. const config = require('./config');
  5. const helper = require('./helper');
  6. const vueLoaderConfig = require('./vue-loader.conf');
  7. const vueWebTemp = helper.rootNode(config.templateDir);
  8. const hasPluginInstalled = fs.existsSync(helper.rootNode(config.pluginFilePath));
  9. const isWin = /^win/.test(process.platform);
  10. const weexEntry = {
  11.   'index': helper.root('entry.js')
  12. }
  13.  
  14. const getEntryFileContent = (source, routerpath) => {
  15.   let dependence = `import Vue from 'vue'\n`;
  16.   dependence += `import weex from 'weex-vue-render'\n`;
  17.   let relativePluginPath = helper.rootNode(config.pluginFilePath);
  18.   let entryContents = fs.readFileSync(source).toString();
  19.   let contents = '';
  20.   entryContents = dependence + entryContents;
  21.   entryContents = entryContents.replace(/\/\* weex initialized/, match => `weex.init(Vue)\n${match}`);
  22.   if (isWin) {
  23.     relativePluginPath = relativePluginPath.replace(/\\/g, '\\\\');
  24.   }
  25.   if (hasPluginInstalled) {
  26.     contents += `\n// If detact plugins/plugin.js is exist, import and the plugin.js\n`;
  27.     contents += `import plugins from '${relativePluginPath}';\n`;
  28.     contents += `plugins.forEach(function (plugin) {\n\tweex.install(plugin)\n});\n\n`;
  29.     entryContents = entryContents.replace(/\.\/router/, routerpath);
  30.     entryContents = entryContents.replace(/weex\.init/, match => `${contents}${match}`);
  31.   }
  32.   return entryContents;
  33. }
  34.  
  35. const getRouterFileContent = (source) => {
  36.   const dependence = `import Vue from 'vue'\n`;
  37.   let routerContents = fs.readFileSync(source).toString();
  38.   routerContents = dependence + routerContents;
  39.   return routerContents;
  40. }
  41.  
  42. const getEntryFile = () => {
  43.   const entryFile = path.join(vueWebTemp, config.entryFilePath)
  44.   const routerFile = path.join(vueWebTemp, config.routerFilePath)
  45.   fs.outputFileSync(entryFile, getEntryFileContent(helper.root(config.entryFilePath), routerFile));
  46.   fs.outputFileSync(routerFile, getRouterFileContent(helper.root(config.routerFilePath)));
  47.   return {
  48.     index: entryFile
  49.   }
  50. }
  51.  
  52. // The entry file for web needs to add some library. such as vue, weex-vue-render
  53. // 1. src/entry.js
  54. // import Vue from 'vue';
  55. // import weex from 'weex-vue-render';
  56. // weex.init(Vue);
  57. // 2. src/router/index.js
  58. // import Vue from 'vue'
  59. const webEntry = getEntryFile();
  60.  
  61.  
  62.  
  63. /**
  64.  * Plugins for webpack configuration.
  65.  */
  66. const plugins = [
  67.   /*
  68.    * Plugin: BannerPlugin
  69.    * Description: Adds a banner to the top of each generated chunk.
  70.    * See: https://webpack.js.org/plugins/banner-plugin/
  71.    */
  72.   new webpack.BannerPlugin({
  73.     banner: '// { "framework": "Vue"} \n',
  74.     raw: true,
  75.     exclude: 'Vue'
  76.   })
  77. ];
  78.  
  79. // Config for compile jsbundle for web.
  80. const webConfig = {
  81.   entry: Object.assign(webEntry, {
  82.     'vendor': [path.resolve('node_modules/phantom-limb/index.js')]
  83.   }),
  84.   output: {
  85.     path: helper.rootNode('./dist'),
  86.     filename: '[name].web.js'
  87.   },
  88.   /**
  89.    * Options affecting the resolving of modules.
  90.    * See http://webpack.github.io/docs/configuration.html#resolve
  91.    */
  92.   resolve: {
  93.     extensions: ['.js', '.vue', '.json'],
  94.     alias: {
  95.       '@': helper.resolve('src')
  96.     }
  97.   },
  98.   /*
  99.    * Options affecting the resolving of modules.
  100.    *
  101.    * See: http://webpack.github.io/docs/configuration.html#module
  102.    */
  103.   module: {
  104.     // webpack 2.0
  105.     rules: [
  106.       {
  107.         test: /\.js$/,
  108.         use: [{
  109.           loader: 'babel-loader'
  110.         }],
  111.         exclude: /node_modules(?!(\/|\\).*(weex).*)/
  112.       },
  113.       {
  114.         test: /(\.vue(\?[^?]+)?$)|(\.css$)/,
  115.         use: [{
  116.           loader: 'weex-loader',
  117.           options: Object.assign(vueLoaderConfig({useVue: true, usePostCSS: false}), {
  118.             /**
  119.              * important! should use postTransformNode to add $processStyle for
  120.              * inline style prefixing.
  121.              */
  122.             optimizeSSR: false,
  123.             postcss: [
  124.               // to convert weex exclusive styles.
  125.               require('postcss-plugin-weex')(),
  126.               require('autoprefixer')({
  127.                 browsers: ['> 0.1%', 'ios >= 8', 'not ie < 12']
  128.               }),
  129.               require('postcss-plugin-px2rem')({
  130.                 // base on 750px standard.
  131.                 rootValue: 75,
  132.                 // to leave 1px alone.
  133.                 minPixelValue: 1.01
  134.               })
  135.             ],
  136.             compilerModules: [
  137.               {
  138.                 postTransformNode: el => {
  139.                   // to convert vnode for weex components.
  140.                   require('weex-vue-precompiler')()(el)
  141.                 }
  142.               }
  143.             ]
  144.            
  145.           })
  146.         }]
  147.       }
  148.     ]
  149.   },
  150.   /*
  151.    * Add additional plugins to the compiler.
  152.    *
  153.    * See: http://webpack.github.io/docs/configuration.html#plugins
  154.    */
  155.   plugins: plugins
  156. };
  157. // Config for compile jsbundle for native.
  158. const weexConfig = {
  159.   entry: weexEntry,
  160.   output: {
  161.     path: path.join(__dirname, '../dist'),
  162.     filename: '[name].js'
  163.   },
  164.   /**
  165.    * Options affecting the resolving of modules.
  166.    * See http://webpack.github.io/docs/configuration.html#resolve
  167.    */
  168.   resolve: {
  169.     extensions: ['.js', '.vue', '.json'],
  170.     alias: {
  171.       '@': helper.resolve('src')
  172.     }
  173.   },
  174.   /*
  175.    * Options affecting the resolving of modules.
  176.    *
  177.    * See: http://webpack.github.io/docs/configuration.html#module
  178.    */
  179.   module: {
  180.     rules: [
  181.       {
  182.         test: /\.js$/,
  183.         use: [{
  184.           loader: 'babel-loader'
  185.         }]
  186.       },
  187.       {
  188.         test: /\.vue(\?[^?]+)?$/,
  189.         use: [{
  190.           loader: 'weex-loader',
  191.           options: vueLoaderConfig({useVue: false})
  192.         }]
  193.       }
  194.     ]
  195.   },
  196.   /*
  197.    * Add additional plugins to the compiler.
  198.    *
  199.    * See: http://webpack.github.io/docs/configuration.html#plugins
  200.    */
  201.   plugins: plugins,
  202.   /*
  203.   * Include polyfills or mocks for various node stuff
  204.   * Description: Node configuration
  205.   *
  206.   * See: https://webpack.github.io/docs/configuration.html#node
  207.   */
  208.   node: config.nodeConfiguration
  209. };
  210.  
  211. module.exports = [webConfig, weexConfig];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement