Skip to content

Instantly share code, notes, and snippets.

@zhoukekestar
Created October 30, 2016 12:34
Show Gist options
  • Save zhoukekestar/97e1b3c8ad129d8740668b5b44f97d18 to your computer and use it in GitHub Desktop.
Save zhoukekestar/97e1b3c8ad129d8740668b5b44f97d18 to your computer and use it in GitHub Desktop.
Export Single File from Vue
// ------------------------
// npm install rollup rollup-plugin-flow-no-whitespace rollup-plugin-buble rollup-plugin-alias he de-indent
// -------------------------
// Edit rollup-plugin-alias.js
// if (!/js$/.test(updatedId)) {
// console.log(updatedId + ' ----> ' + updatedId + '.js');
// updatedId += '.js';
// }
// -------------------------
const rollup = require( 'rollup')
, path = require('path')
, flow = require('rollup-plugin-flow-no-whitespace')
, buble = require('rollup-plugin-buble')
, alias = require('rollup-plugin-alias')
, baseAlias = require('./alias')
var config = {
entry: path.resolve(__dirname, '../src/compiler/parser/index.js'),
format: 'cjs',
dest: path.resolve(__dirname, '../bundle.js'),
plugins: [
flow(),
buble(),
alias(Object.assign({}, baseAlias)), // for nodejs
// alias(Object.assign({}, baseAlias, {he: './entity-decoder' })), // for browser
],
external: ['he', 'de-indent'] // for nodejs
};
console.log('start...')
rollup.rollup(config).then( function ( bundle ) {
console.log('bundle.js created.')
bundle.write({
format: config.format,
dest: config.dest
});
}).catch(function(err) {
console.log(err.stack)
})
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var path = _interopDefault(require('path'));
var fs = _interopDefault(require('fs'));
// Helper functions
var noop = function () {
return null;
};
var startsWith = function (needle, haystack) {
return !haystack.indexOf(needle);
};
var endsWith = function (needle, haystack) {
return haystack.slice(-needle.length) === needle;
};
var isFilePath = function (id) {
return (/^\.?\//.test(id)
);
};
var exists = function (uri) {
try {
return fs.statSync(uri).isFile();
} catch (e) {
return false;
}
};
function alias() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var hasResolve = Array.isArray(options.resolve);
var resolve = hasResolve ? options.resolve : ['.js'];
var aliasKeys = hasResolve ? Object.keys(options).filter(function (k) {
return k !== 'resolve';
}) : Object.keys(options);
// No aliases?
if (!aliasKeys.length) {
return {
resolveId: noop
};
}
return {
resolveId: function (importee, importer) {
// First match is supposed to be the correct one
var toReplace = aliasKeys.find(function (key) {
return startsWith(key, importee);
});
if (!toReplace) {
return null;
}
var entry = options[toReplace];
var updatedId = importee.replace(toReplace, entry);
if (isFilePath(updatedId)) {
var _ret = function () {
var directory = path.dirname(importer);
// Resolve file names
var filePath = path.resolve(directory, updatedId);
var match = resolve.map(function (ext) {
return '' + filePath + ext;
}).find(exists);
if (match) {
return {
v: match
};
}
// To keep the previous behaviour we simply return the file path
// with extension
if (endsWith('.js', filePath)) {
return {
v: filePath
};
}
return {
v: filePath + '.js'
};
}();
if (typeof _ret === "object") return _ret.v;
}
// --------------------------- add code ----------------------------
// This file: node_modules/rollup-plugin-alias/dist/rollup-plugin-alias.js
if (!/js$/.test(updatedId)) {
console.log(updatedId + ' ----> ' + updatedId + '.js');
updatedId += '.js';
}
// --------------------------- add code ----------------------------
return updatedId;
}
};
}
module.exports = alias;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment