From: Eduardo San Martin Morote Date: Fri, 28 Jun 2019 14:43:03 +0000 (+0200) Subject: build: refactor build X-Git-Tag: v4.0.0-alpha.0~329 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=f3f7555f1ae9d489d0d6d51700e86373e27be218;p=thirdparty%2Fvuejs%2Frouter.git build: refactor build --- diff --git a/package.json b/package.json index 3a06992f..a8b18557 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/rollup.config.js b/rollup.config.js index af7c6640..1e89557b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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 }), ] diff --git a/src/entries/iife.ts b/src/entries/iife.ts index a1ab951c..eda0360d 100644 --- a/src/entries/iife.ts +++ b/src/entries/iife.ts @@ -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 diff --git a/src/history/utils.ts b/src/history/utils.ts index bd31dafa..70857fcd 100644 --- a/src/history/utils.ts +++ b/src/history/utils.ts @@ -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 diff --git a/src/index.ts b/src/index.ts index 8c98689b..1fcb1b98 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 = 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) diff --git a/yarn.lock b/yarn.lock index 54901d7b..31917969 100644 --- 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==