]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
build: refactor build
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 28 Jun 2019 14:43:03 +0000 (16:43 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 28 Jun 2019 14:43:03 +0000 (16:43 +0200)
package.json
rollup.config.js
src/entries/iife.ts
src/history/utils.ts
src/index.ts
yarn.lock

index 3a06992fb10e5476c0df34198e3ba1e79de3b164..a8b18557291ac4d7a48c3a042f7eb67e9b202935 100644 (file)
@@ -3,7 +3,7 @@
   "private": true,
   "version": "4.0.0-alpha.0",
   "main": "src/index.ts",
-  "browser": "dist/vue-router.esm.js",
+  "browser": "dist/vue-router.browser.esm.js",
   "unpkg": "dist/vue-router.js",
   "module": "dist/vue-router.esm.js",
   "types": "dist/index.d.ts",
@@ -32,6 +32,7 @@
     "rollup-plugin-alias": "^1.5.2",
     "rollup-plugin-commonjs": "^10.0.1",
     "rollup-plugin-node-resolve": "^5.1.0",
+    "rollup-plugin-replace": "^2.2.0",
     "rollup-plugin-terser": "^5.0.0",
     "rollup-plugin-typescript2": "^0.21.2",
     "ts-jest": "^24.0.2",
index af7c664000d44c3c7250a5e9e69cd7beb0b219cc..1e89557b63889add787862f21b4e9dcb8640512d 100644 (file)
@@ -1,3 +1,4 @@
+import replace from 'rollup-plugin-replace'
 import resolve from 'rollup-plugin-node-resolve'
 import commonjs from 'rollup-plugin-commonjs'
 import ts from 'rollup-plugin-typescript2'
@@ -6,61 +7,102 @@ import { terser } from 'rollup-plugin-terser'
 import pkg from './package.json'
 import path from 'path'
 
-export default [
-  // browser-friendly UMD build
+const banner = `/*!
+  * ${pkg.name} v${pkg.version}
+  * (c) ${new Date().getFullYear()} Eduardo San Martin Morote
+  * @license MIT
+  */`
+
+const exportName = 'VueRouter'
+
+function createEntry(
   {
-    input: 'src/entries/iife.ts',
-    output: {
-      name: 'VueRouter',
-      file: pkg.unpkg,
-      format: 'iife',
-    },
+    format, // Rollup format (iife, umd, cjs, es)
+    external, // Rollup external option
+    input = 'src/index.ts', // entry point
+    env = 'development', // NODE_ENV variable
+    minify = false,
+    isBrowser = false, // produce a browser module version or not
+  } = {
+    input: 'src/index.ts',
+    env: 'development',
+    minify: false,
+    isBrowser: false,
+  }
+) {
+  // force production mode when minifying
+  if (minify) env = 'production'
+
+  const config = {
+    input,
     plugins: [
+      replace({
+        __VERSION__: pkg.version,
+        'process.env.NODE_ENV': `'${env}'`,
+      }),
       alias({
         resolve: ['ts'],
         consola: path.resolve(__dirname, './src/consola.ts'),
       }),
-      resolve(), // so Rollup can find `ms`
-      commonjs(), // so Rollup can convert `ms` to an ES module
-      ts(), // so Rollup can convert TypeScript to JavaScript
     ],
-  },
-
-  // CommonJS and ESM builds
-  {
-    input: 'src/entries/iife.ts',
     output: {
-      name: 'VueRouter',
-      file: 'dist/vue-router.min.js',
-      format: 'iife',
+      banner,
+      file: 'dist/vue-router.other.js',
+      format,
     },
-    plugins: [
-      alias({
-        resolve: ['ts'],
-        consola: path.resolve(__dirname, './src/consola.ts'),
-      }),
-      resolve(), // so Rollup can find `ms`
-      commonjs(), // so Rollup can convert `ms` to an ES module
-      ts(), // so Rollup can convert TypeScript to JavaScript
+  }
+
+  if (format === 'iife') {
+    config.input = 'src/entries/iife.ts'
+    config.output.file = pkg.unpkg
+    config.output.name = exportName
+  } else if (format === 'es') {
+    config.output.file = isBrowser ? pkg.browser : pkg.module
+  } else if (format === 'cjs') {
+    config.output.file = 'dist/vue-router.cjs.js'
+  }
+
+  if (!external) {
+    config.plugins.push(resolve(), commonjs())
+  } else {
+    config.external = external
+  }
+
+  config.plugins.push(
+    ts({
+      // only check once, during the es version with browser (it includes external libs)
+      check: format === 'es' && isBrowser && !minify,
+      tsconfigOverride: {
+        compilerOptions: {
+          // same for d.ts files
+          declaration: format === 'es' && isBrowser && !minify,
+          target: format === 'iife' || format === 'cjs' ? 'es3' : 'esnext',
+        },
+      },
+    })
+  )
+
+  if (minify) {
+    config.plugins.push(
       terser({
-        module: false,
-      }),
-    ],
-  },
+        module: format === 'es',
+        output: {
+          preamble: banner,
+        },
+      })
+    )
+    config.output.file = config.output.file.replace(/\.js$/i, '.min.js')
+  }
 
-  {
-    input: 'src/index.ts',
-    external: ['path-to-regexp'],
-    plugins: [
-      alias({
-        resolve: ['ts'],
-        consola: path.resolve(__dirname, './src/consola.ts'),
-      }),
-      ts(), // so Rollup can convert TypeScript to JavaScript
-    ],
-    output: [
-      { file: 'dist/vue-router.cjs.js', format: 'cjs' },
-      { file: pkg.module, format: 'es' },
-    ],
-  },
+  return config
+}
+
+export default [
+  // browser-friendly UMD build
+  createEntry({ format: 'iife' }),
+  createEntry({ format: 'iife', minify: true }),
+  createEntry({ format: 'cjs', external: ['path-to-regexp'] }),
+  createEntry({ format: 'es', external: ['path-to-regexp'] }),
+  createEntry({ format: 'es', isBrowser: true }),
+  createEntry({ format: 'es', isBrowser: true, minify: true }),
 ]
index a1ab951cb9f920dd0c9ac929ab0e9814ace98895..eda0360d9a7496bfd7a5a52dddbcf61d0b6e747c 100644 (file)
@@ -1,24 +1,14 @@
-import { Router as BaseRouter, plugin, HTML5History } from '../index'
-import { RouterOptions } from '../router'
-import { VueConstructor } from 'vue'
-
-export default class VueRouter extends BaseRouter {
-  static install = plugin
-  static version = '__VERSION__'
-
-  // TODO: handle mode in a retro compatible way
-  constructor(options: RouterOptions & { mode: 'string' }) {
-    super({
-      history: new HTML5History(),
-      ...options,
-    })
-  }
-}
-
-declare global {
-  interface Window {
-    Vue?: VueConstructor
-  }
-}
-
-if (window.Vue) window.Vue.use(VueRouter)
+import VueRouter from // Router
+// plugin,
+// HTML5History,
+'../index'
+
+// declare module '../index' {
+//   interface BackwardsCompatibleRouter {
+//     Router: BaseRouter
+//     install: typeof plugin
+//     HTML5History: HTML5History
+//   }
+// }
+
+export default VueRouter
index bd31daface1b793eb54665b8c0d9a1283ffbe4d6..70857fcd35a45df29cabaf5da5dc72dcce3b4b9d 100644 (file)
@@ -121,7 +121,10 @@ export function prepareURI(uri: string) {
 }
 
 // use regular decodeURI
-export const decodeURI = global.decodeURI
+// Use a renamed export instead of global.decodeURI
+// to support node and browser at the same time
+const originalDecodeURI = decodeURI
+export { originalDecodeURI as decodeURI }
 
 /**
  * Normalize a History location into an object that looks like
index 8c98689b638f7a9032256f05cf76ac78d337a5d7..1fcb1b989bb6de44eef6b71322ef88f9ba327dd3 100644 (file)
@@ -1,6 +1,6 @@
-import { Router } from './router'
+import { Router, RouterOptions } from './router'
 import { HTML5History } from './history/html5'
-import { PluginFunction } from 'vue'
+import { PluginFunction, VueConstructor } from 'vue'
 import View from './components/View'
 import Link from './components/Link'
 
@@ -63,3 +63,24 @@ const plugin: PluginFunction<void> = Vue => {
 }
 
 export { Router, HTML5History, plugin }
+
+export default class VueRouter extends Router {
+  static install = plugin
+  static version = '__VERSION__'
+
+  // TODO: handle mode in a retro compatible way
+  constructor(options: RouterOptions & { mode: 'string' }) {
+    super({
+      history: new HTML5History(),
+      ...options,
+    })
+  }
+}
+
+declare global {
+  interface Window {
+    Vue?: VueConstructor
+  }
+}
+
+if (window.Vue) window.Vue.use(VueRouter)
index 54901d7b34d6e42a40995689b3900b778fc157d3..31917969a56e3e4169628d23c17155b193658c37 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
@@ -5287,6 +5287,14 @@ rollup-plugin-node-resolve@^5.1.0:
     resolve "^1.11.1"
     rollup-pluginutils "^2.8.1"
 
+rollup-plugin-replace@^2.2.0:
+  version "2.2.0"
+  resolved "https://registry.yarnpkg.com/rollup-plugin-replace/-/rollup-plugin-replace-2.2.0.tgz#f41ae5372e11e7a217cde349c8b5d5fd115e70e3"
+  integrity sha512-/5bxtUPkDHyBJAKketb4NfaeZjL5yLZdeUihSfbF2PQMz+rSTEb8ARKoOl3UBT4m7/X+QOXJo3sLTcq+yMMYTA==
+  dependencies:
+    magic-string "^0.25.2"
+    rollup-pluginutils "^2.6.0"
+
 rollup-plugin-terser@^5.0.0:
   version "5.0.0"
   resolved "https://registry.yarnpkg.com/rollup-plugin-terser/-/rollup-plugin-terser-5.0.0.tgz#ac50fdb703b580447a7e6b1692aeed515a6be8cf"
@@ -5315,7 +5323,7 @@ rollup-pluginutils@2.6.0:
     estree-walker "^0.6.0"
     micromatch "^3.1.10"
 
-rollup-pluginutils@^2.8.1:
+rollup-pluginutils@^2.6.0, rollup-pluginutils@^2.8.1:
   version "2.8.1"
   resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.1.tgz#8fa6dd0697344938ef26c2c09d2488ce9e33ce97"
   integrity sha512-J5oAoysWar6GuZo0s+3bZ6sVZAC0pfqKz68De7ZgDi5z63jOVZn1uJL/+z1jeKHNbGII8kAyHF5q8LnxSX5lQg==